l_quvi_object_opts_new: Add lua index param
[libquvi.git] / src / lua / quvi / http / metainfo.c
blob25cba89076ba1b67a779aa321346da4c67e50ccf
1 /* libquvi
2 * Copyright (C) 2013 Toni Gundogdu <legatvs@gmail.com>
4 * This file is part of libquvi <http://quvi.sourceforge.net/>.
6 * This program 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 program 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 program. If not, see
18 * <http://www.gnu.org/licenses/>.
21 #include "config.h"
23 #include <lauxlib.h>
24 #include <glib.h>
26 #include "quvi.h"
27 /* -- */
28 #include "_quvi_s.h"
29 #include "_quvi_http_metainfo_s.h"
30 /* -- */
31 #include "lua/def.h"
32 #include "lua/getfield.h"
33 #include "lua/setfield.h"
34 #include "lua/quvi/opts.h"
36 gint l_quvi_http_metainfo(lua_State *l)
38 _quvi_http_metainfo_t qmi;
39 gboolean croak_if_error;
40 const gchar *url;
41 GSList *opts;
42 _quvi_t q;
44 q = (_quvi_t) l_get_reg_userdata(l, USERDATA_QUVI_T);
46 url = luaL_checkstring(l, 1);
47 lua_pop(l, 1);
49 opts = l_quvi_object_opts_new(l, 2);
50 croak_if_error = l_quvi_object_opts_croak_if_error(opts);
51 l_quvi_object_opts_curl(opts, q);
53 qmi = quvi_http_metainfo_new(q, url);
55 lua_newtable(l);
56 l_setfield_n(l, QO_RESPONSE_CODE, q->status.resp_code);
57 l_setfield_n(l, QO_QUVI_CODE, q->status.rc);
58 l_setfield_s(l, QO_ERROR_MESSAGE, (q->status.rc != QUVI_OK)
59 ? q->status.errmsg->str
60 : MS_EMPTY);
62 if (quvi_ok(q) == QUVI_TRUE)
64 l_setfield_s(l, QO_CONTENT_TYPE, qmi->content_type->str);
65 l_setfield_n(l, QO_CONTENT_LENGTH, qmi->length_bytes);
67 else
69 if (croak_if_error == TRUE)
70 luaL_error(l, "%s", q->status.errmsg->str);
73 l_quvi_object_opts_free(opts);
74 quvi_http_metainfo_free(qmi);
76 return (1); /* no. of returned values (one table) */
79 /* vim: set ts=2 sw=2 tw=72 expandtab: */