Use word "library" in the source file headers
[libquvi.git] / src / lua / quvi / http / header.c
blobfd6623d28617328f5dd3bf1defcee899e49bc747
1 /* libquvi
2 * Copyright (C) 2013 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 <string.h>
24 #include <lauxlib.h>
25 #include <glib.h>
26 #include <curl/curl.h>
28 #include "quvi.h"
29 /* -- */
30 #include "_quvi_s.h"
31 /* -- */
32 #include "lua/quvi/opts.h"
33 #include "lua/getfield.h"
34 #include "lua/setfield.h"
35 #include "lua/def.h"
37 extern glong c_reset_headers(_quvi_t);
39 gint l_quvi_http_header(lua_State *l)
41 gboolean croak_if_error;
42 const gchar *s;
43 GSList *opts;
44 CURLcode cc;
45 _quvi_t q;
47 /* quvi handle */
49 q = (_quvi_t) l_get_reg_userdata(l, USERDATA_QUVI_T);
50 g_assert(q != NULL);
52 /* arg1 */
54 s = luaL_checkstring(l, 1);
55 lua_pop(l, 1);
57 /* options */
59 opts = l_quvi_object_opts_new(l, 2);
60 croak_if_error = l_quvi_object_opts_croak_if_error(l, opts);
61 l_quvi_object_opts_free(opts);
63 /* apply */
65 if (strlen(s) >0)
67 CURL *c = q->handle.curl;
68 q->http.headers = curl_slist_append(q->http.headers, s);
69 cc = curl_easy_setopt(c, CURLOPT_HTTPHEADER, q->http.headers);
71 else
72 cc = c_reset_headers(q);
74 if (cc != CURLE_OK)
76 g_string_printf(q->status.errmsg, "%s", curl_easy_strerror(cc));
77 q->status.rc = QUVI_ERROR_CALLBACK;
79 if (croak_if_error == TRUE)
80 luaL_error(l, "%s", q->status.errmsg->str);
83 /* Return a table of results. */
85 lua_newtable(l);
86 l_setfield_s(l, QO_ERROR_MESSAGE, q->status.errmsg->str, -1);
87 l_setfield_n(l, QO_QUVI_CODE, q->status.rc);
89 return (1); /* no. of returned values (a table) */
92 /* vim: set ts=2 sw=2 tw=72 expandtab: */