Use word "library" in the source file headers
[libquvi.git] / src / lua / quvi / http / cookie.c
blobe0f12aa2e3bedfe470b6ed5448a6d72cd53408d8
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 static gint _ret(lua_State *l, const _quvi_t q)
39 lua_newtable(l); /* Return a table of results. */
40 l_setfield_s(l, QO_ERROR_MESSAGE, q->status.errmsg->str, -1);
41 l_setfield_n(l, QO_QUVI_CODE, q->status.rc);
42 return (1); /* no. of returned values (a table) */
45 struct _cookie_opts_s
47 const gchar *s;
48 gint mode;
51 typedef struct _cookie_opts_s *_cookie_opts_t;
53 typedef enum
55 COOKIE_MODE_SESSION = 0x01,
56 COOKIE_MODE_FILE,
57 COOKIE_MODE_LIST,
58 COOKIE_MODE_JAR
59 } CookieMode;
61 static gint _setopt(lua_State *l, const _quvi_t q, const CURLoption copt,
62 const _cookie_opts_t co, const gboolean croak_if_error)
64 CURLcode r;
66 if (co->mode == COOKIE_MODE_SESSION)
67 r = curl_easy_setopt(q->handle.curl, copt, (glong) g_strtod(co->s, NULL));
68 else
69 r = curl_easy_setopt(q->handle.curl, copt, co->s);
71 if (r != CURLE_OK)
73 g_string_printf(q->status.errmsg, "%s", curl_easy_strerror(r));
74 q->status.rc = QUVI_ERROR_CALLBACK;
76 if (croak_if_error == TRUE)
77 luaL_error(l, "%s", q->status.errmsg->str);
79 return (_ret(l, q));
82 static void _chk_cookie_opts(lua_State *l, GSList *opts, _cookie_opts_t co)
84 GSList *p;
86 l_quvi_object_opts_chk_given(l, opts, "cookie");
88 /* required */
90 l_quvi_object_opts_chk_req_s(QUVI_OBJECT_OPTION_HTTP_COOKIE_MODE,
91 HRE_COOKIE_MODE, co->mode, n);
94 gint l_quvi_http_cookie(lua_State *l)
96 struct _cookie_opts_s co;
97 gboolean croak_if_error;
98 CURLoption copt;
99 GSList *opts;
100 _quvi_t q;
102 /* quvi handle */
104 q = (_quvi_t) l_get_reg_userdata(l, USERDATA_QUVI_T);
105 g_assert(q != NULL);
107 if (q->opt.allow_cookies == QUVI_FALSE)
108 return (_ret(l, q)); /* Do nothing if cookies have been disabled. */
110 /* arg1 */
112 memset(&co, 0, sizeof(struct _cookie_opts_s));
113 co.s = luaL_checkstring(l, 1);
114 lua_pop(l, 1);
116 /* options */
118 opts = l_quvi_object_opts_new(l, 2);
119 croak_if_error = l_quvi_object_opts_croak_if_error(l, opts);
121 _chk_cookie_opts(l, opts, &co);
122 l_quvi_object_opts_free(opts);
124 /* mode */
126 switch (co.mode)
128 case COOKIE_MODE_SESSION:
129 copt = CURLOPT_COOKIESESSION;
130 break;
132 case COOKIE_MODE_FILE:
133 copt = CURLOPT_COOKIEFILE;
134 break;
136 case COOKIE_MODE_LIST:
137 copt = CURLOPT_COOKIELIST;
138 break;
140 case COOKIE_MODE_JAR:
141 copt = CURLOPT_COOKIEJAR;
142 break;
144 default:
145 g_string_printf(q->status.errmsg,
146 "[%s] invalid cookie function `0x%02x'",
147 __func__, co.mode);
149 q->status.rc = QUVI_ERROR_CALLBACK;
150 copt = CURLOPT_COOKIESESSION;
152 g_warning("%s", q->status.errmsg->str);
154 return (_setopt(l, q, copt, &co, croak_if_error));
157 /* vim: set ts=2 sw=2 tw=72 expandtab: */