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/>.
26 #include <curl/curl.h>
32 #include "lua/quvi/opts.h"
33 #include "lua/getfield.h"
34 #include "lua/setfield.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) */
51 typedef struct _cookie_opts_s
*_cookie_opts_t
;
55 COOKIE_MODE_SESSION
= 0x01,
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
)
66 if (co
->mode
== COOKIE_MODE_SESSION
)
67 r
= curl_easy_setopt(q
->handle
.curl
, copt
, (glong
) g_strtod(co
->s
, NULL
));
69 r
= curl_easy_setopt(q
->handle
.curl
, copt
, co
->s
);
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
);
82 static void _chk_cookie_opts(lua_State
*l
, GSList
*opts
, _cookie_opts_t co
)
86 l_quvi_object_opts_chk_given(l
, opts
, "cookie");
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
;
104 q
= (_quvi_t
) l_get_reg_userdata(l
, USERDATA_QUVI_T
);
107 if (q
->opt
.allow_cookies
== QUVI_FALSE
)
108 return (_ret(l
, q
)); /* Do nothing if cookies have been disabled. */
112 memset(&co
, 0, sizeof(struct _cookie_opts_s
));
113 co
.s
= luaL_checkstring(l
, 1);
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
);
128 case COOKIE_MODE_SESSION
:
129 copt
= CURLOPT_COOKIESESSION
;
132 case COOKIE_MODE_FILE
:
133 copt
= CURLOPT_COOKIEFILE
;
136 case COOKIE_MODE_LIST
:
137 copt
= CURLOPT_COOKIELIST
;
140 case COOKIE_MODE_JAR
:
141 copt
= CURLOPT_COOKIEJAR
;
145 g_string_printf(q
->status
.errmsg
,
146 "[%s] invalid cookie function `0x%02x'",
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: */