From 18c97805c15c0ecdba99145c0d6f3954b276d357 Mon Sep 17 00:00:00 2001 From: Alexey Melnichuk Date: Thu, 28 Aug 2014 12:40:13 +0500 Subject: [PATCH] Add. `setopt` method to share interface --- doc/lcurl.ldoc | 26 +++++++++++++++++++++ src/lcoptshare.h | 16 +++++++++++++ src/lcshare.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++- src/lcurl.c | 1 + 4 files changed, 102 insertions(+), 1 deletion(-) diff --git a/doc/lcurl.ldoc b/doc/lcurl.ldoc index 6561006..50d9b03 100644 --- a/doc/lcurl.ldoc +++ b/doc/lcurl.ldoc @@ -112,6 +112,32 @@ function free() end end +--- Curl error object +-- @type error +-- +do +--- Get the number value of error. +-- +-- @treturn number number of error (curl.E_XXX constants) +function no ()end + +--- Get the error name. +-- +-- @treturn string error name (e.g. "UNSUPPORTED_PROTOCOL", "BAD_OPTION") +function name ()end + +--- Get the error description. +-- +-- @treturn string error description (e.g. "Login denied") +function msg ()end + +--- Get the full error description. +-- +-- @treturn string string that contain name, message and number of error +function __tostring ()end + +end + --- Easy curl object -- @type easy -- diff --git a/src/lcoptshare.h b/src/lcoptshare.h index 8b13789..17924de 100644 --- a/src/lcoptshare.h +++ b/src/lcoptshare.h @@ -1 +1,17 @@ +#ifndef OPT_ENTRY +# define OPT_ENTRY(a,b,c,d) +#endif +#ifndef FLG_ENTRY +# define FLG_ENTRY(a) +#endif + +OPT_ENTRY(share, SHARE, LNG, 0 ) +OPT_ENTRY(unshare, UNSHARE, LNG, 0 ) + +FLG_ENTRY(LOCK_DATA_COOKIE) +FLG_ENTRY(LOCK_DATA_DNS) +FLG_ENTRY(LOCK_DATA_SSL_SESSION) + +#undef OPT_ENTRY +#undef FLG_ENTRY \ No newline at end of file diff --git a/src/lcshare.c b/src/lcshare.c index f3fd86a..e8a9fef 100644 --- a/src/lcshare.c +++ b/src/lcshare.c @@ -1,5 +1,4 @@ #include "lcurl.h" -#include "lceasy.h" #include "lcshare.h" #include "lcerror.h" #include "lcutils.h" @@ -36,9 +35,62 @@ static int lcurl_share_cleanup(lua_State *L){ return 0; } +//{ OPTIONS + +static int lcurl_opt_set_long_(lua_State *L, int opt){ + lcurl_share_t *p = lcurl_getshare(L); + long val; CURLSHcode code; + + if(lua_isboolean(L, 2)) val = lua_toboolean(L, 2); + else{ + luaL_argcheck(L, lua_type(L, 2) == LUA_TNUMBER, 2, "number or boolean expected"); + val = luaL_checklong(L, 2); + } + + code = curl_share_setopt(p->curl, opt, val); + if(code != CURLSHE_OK){ + return lcurl_fail_ex(L, p->err_mode, LCURL_ERROR_SHARE, code); + } + lua_settop(L, 1); + return 1; +} + +#define LCURL_LNG_OPT(N, S) static int lcurl_share_set_##N(lua_State *L){\ + return lcurl_opt_set_long_(L, CURLSHOPT_##N);\ +} + +#define OPT_ENTRY(L, N, T, S) LCURL_##T##_OPT(N, S) + +#include "lcoptshare.h" + +#undef OPT_ENTRY +#undef LCURL_LNG_OPT + +//} + +static int lcurl_share_setopt(lua_State *L){ + lcurl_share_t *p = lcurl_getshare(L); + int opt = luaL_checklong(L, 2); + lua_remove(L, 2); + +#define OPT_ENTRY(l, N, T, S) case CURLSHOPT_##N: return lcurl_share_set_##N(L); + switch(opt){ + #include "lcoptshare.h" + } +#undef OPT_ENTRY + + return lcurl_fail_ex(L, p->err_mode, LCURL_ERROR_SHARE, CURLSHE_BAD_OPTION); +} + //} static const struct luaL_Reg lcurl_share_methods[] = { + {"setopt", lcurl_share_setopt }, + +#define OPT_ENTRY(L, N, T, S) { "setopt_"#L, lcurl_share_set_##N }, + #include "lcoptshare.h" +#undef OPT_ENTRY + {"close", lcurl_share_cleanup }, {"__gc", lcurl_share_cleanup }, @@ -47,6 +99,12 @@ static const struct luaL_Reg lcurl_share_methods[] = { static const lcurl_const_t lcurl_share_opt[] = { +#define OPT_ENTRY(L, N, T, S) { "OPT_SHARE_"#N, CURLSHOPT_##N }, +#define FLG_ENTRY(N) { #N, CURL_##N }, +# include "lcoptshare.h" +#undef OPT_ENTRY +#undef FLG_ENTRY + {NULL, 0} }; diff --git a/src/lcurl.c b/src/lcurl.c index fd7759a..0f317e6 100644 --- a/src/lcurl.c +++ b/src/lcurl.c @@ -145,6 +145,7 @@ static int luaopen_lcurl_(lua_State *L, const struct luaL_Reg *func){ lua_pushvalue(L, -2); lcurl_hpost_initlib(L, 1); lua_pushvalue(L, -2); lcurl_easy_initlib (L, 1); lua_pushvalue(L, -2); lcurl_multi_initlib(L, 1); + lua_pushvalue(L, -2); lcurl_share_initlib(L, 1); lua_pushvalue(L, -2); lua_rawsetp(L, LUA_REGISTRYINDEX, LCURL_REGISTRY);