QuviError l_exec_scan_script_parse(gpointer p, gpointer _qss, const gchar *data) { _quvi_script_t qss; _quvi_scan_t qs; lua_State *l; qss = (_quvi_script_t) _qss; qs = (_quvi_scan_t) p; l = qs->handle.quvi->handle.lua; lua_pushnil(l); if (luaL_dofile(l, qss->fpath->str)) luaL_error(l, "%s", lua_tostring(l, -1)); lua_getglobal(l, script_func); if (!lua_isfunction(l, -1)) { luaL_error(l, "%s: the function `%s' was not found", qss->fpath->str, script_func); } lua_newtable(l); l_set_reg_userdata(l, USERDATA_QUVI_T, (gpointer) qs->handle.quvi); l_setfield_s(l, SS_INPUT_URL, qs->url.input->str, -1); l_setfield_s(l, SS_CONTENT, data, -1); if (lua_pcall(l, 1, 1, 0)) { g_string_assign(qs->handle.quvi->status.errmsg, lua_tostring(l, -1)); return (QUVI_ERROR_SCRIPT); } if (!lua_istable(l, -1)) { static const gchar *_E = "%s: %s: must return a dictionary, this is typically the `qargs'"; luaL_error(l, _E, qss->fpath->str, script_func); } _chk_media_url(l, qs, qss->fpath->str); lua_pop(l, 1); return (QUVI_OK); }
static gint _ret(lua_State *l, const _quvi_t q) { lua_newtable(l); /* Return a table of results. */ l_setfield_s(l, QO_ERROR_MESSAGE, q->status.errmsg->str, -1); l_setfield_n(l, QO_QUVI_CODE, q->status.rc); return (1); /* no. of returned values (a table) */ }
gint l_quvi_http_header(lua_State *l) { gboolean croak_if_error; const gchar *s; GSList *opts; CURLcode cc; _quvi_t q; /* quvi handle */ q = (_quvi_t) l_get_reg_userdata(l, USERDATA_QUVI_T); g_assert(q != NULL); /* arg1 */ s = luaL_checkstring(l, 1); lua_pop(l, 1); /* options */ opts = l_quvi_object_opts_new(l, 2); croak_if_error = l_quvi_object_opts_croak_if_error(l, opts); l_quvi_object_opts_free(opts); /* apply */ if (strlen(s) >0) { CURL *c = q->handle.curl; q->http.headers = curl_slist_append(q->http.headers, s); cc = curl_easy_setopt(c, CURLOPT_HTTPHEADER, q->http.headers); } else cc = c_reset_headers(q); if (cc != CURLE_OK) { g_string_printf(q->status.errmsg, "%s", curl_easy_strerror(cc)); q->status.rc = QUVI_ERROR_CALLBACK; if (croak_if_error == TRUE) luaL_error(l, "%s", q->status.errmsg->str); } /* Return a table of results. */ lua_newtable(l); l_setfield_s(l, QO_ERROR_MESSAGE, q->status.errmsg->str, -1); l_setfield_n(l, QO_QUVI_CODE, q->status.rc); return (1); /* no. of returned values (a table) */ }
/* Resolve URL redirections with exception rules. */ gchar *l_exec_util_resolve_redirections(_quvi_t q, const gchar *url) { lua_State *l; gchar *r; q->status.rc = l_load_util_script(q, script_fname, script_func); if (quvi_ok(q) == QUVI_FALSE) return (NULL); l = q->handle.lua; l_setfield_s(l, US_INPUT_URL, url, -1); /* Set as qargs.input_url */ /* * 1=qargs [qargs: set in l_load_util_script] * 1=returns a string */ if (lua_pcall(l, 1, 1, 0)) { g_string_assign(q->status.errmsg, lua_tostring(l, -1)); /* Keep error code if it was set by a callback: quvi.resolve * calling the network callback responsible for resolving URL * redirections. The error is most likely a network error. */ if (q->status.rc != QUVI_ERROR_CALLBACK) q->status.rc = QUVI_ERROR_SCRIPT; return (NULL); } r = NULL; if (lua_isstring(l, -1)) { const gchar *s = lua_tostring(l, -1); if (g_strcmp0(s, url) != 0) /* Ignore, unless it is different. */ r = g_strdup(s); } else luaL_error(l, "%s: did not return a string", script_func); lua_pop(l, 1); /* quvi.resolve which is called from the script sets q->status.rc . */ return (r); }