lua: exec_util_*.c: Revise comment
[libquvi.git] / src / lua / util / exec_util_resolve_redirections.c
blob846653b61d0b17e3f1b2f5743a4f94056c685523
1 /* libquvi
2 * Copyright (C) 2012 Toni Gundogdu <legatvs@gmail.com>
4 * This file is part of libquvi <http://quvi.sourceforge.net/>.
6 * This library is free software: you can redistribute it and/or
7 * modify it under the terms of the GNU Affero General Public
8 * License as published by the Free Software Foundation, either
9 * version 3 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General
17 * Public License along with this library. If not, see
18 * <http://www.gnu.org/licenses/>.
21 #include "config.h"
23 #include <lauxlib.h>
24 #include <lualib.h>
25 #include <glib.h>
27 #include "quvi.h"
28 /* -- */
29 #include "_quvi_s.h"
30 #include "_quvi_media_s.h"
31 /* -- */
32 #include "lua/load_util_script.h"
33 #include "lua/setfield.h"
34 #include "lua/def.h"
36 static const gchar script_fname[]= "resolve_redirections.lua";
37 static const gchar script_func[] = "resolve_redirections";
39 /* Resolve URL redirections with exception rules. */
40 gchar *l_exec_util_resolve_redirections(_quvi_t q, const gchar *url)
42 lua_State *l;
43 gchar *r;
45 q->status.rc = l_load_util_script(q, script_fname, script_func);
47 if (quvi_ok(q) == QUVI_FALSE)
48 return (NULL);
50 l = q->handle.lua;
51 l_setfield_s(l, US_INPUT_URL, url); /* Set as qargs.input_url */
54 * 1=qargs [qargs: set in l_load_util_script]
55 * 1=returns a string
57 if (lua_pcall(l, 1, 1, 0))
59 g_string_assign(q->status.errmsg, lua_tostring(l, -1));
61 /* Keep error code if it was set by a callback: quvi.resolve
62 * calling the network callback responsible for resolving URL
63 * redirections. The error is most likely a network error. */
64 if (q->status.rc != QUVI_ERROR_CALLBACK)
65 q->status.rc = QUVI_ERROR_SCRIPT;
67 return (NULL);
70 r = NULL;
72 if (lua_isstring(l, -1))
74 const gchar *s = lua_tostring(l, -1);
75 if (g_strcmp0(s, url) != 0) /* Ignore, unless it is different. */
76 r = g_strdup(s);
78 else
79 luaL_error(l, "%s: did not return a string", script_func);
81 lua_pop(l, 1);
83 /* quvi.resolve which is called from the script sets q->status.rc . */
84 return (r);
87 /* vim: set ts=2 sw=2 tw=72 expandtab: */