Add. setopt method.

```Lua
e:setopt(curl.OPT_URL, 'http://example.com')
```
This commit is contained in:
Alexey Melnichuk 2014-08-25 17:10:14 +05:00
parent d8e8ecaacd
commit d4b1b76de7
4 changed files with 64 additions and 11 deletions

View File

@ -197,12 +197,6 @@
RelativePath="..\src\lcutils.c"
>
</File>
<Filter
Name="Resource Files"
Filter="rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav"
UniqueIdentifier="{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}"
>
</Filter>
</Filter>
<Filter
Name="Header Files"
@ -242,6 +236,12 @@
>
</File>
</Filter>
<Filter
Name="Resource Files"
Filter="rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav"
UniqueIdentifier="{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}"
>
</Filter>
</Files>
<Globals>
</Globals>

View File

@ -157,7 +157,6 @@ static int lcurl_easy_unescape(lua_State *L){
return 1;
}
//{ OPTIONS
static int lcurl_opt_set_long_(lua_State *L, int opt){
@ -563,13 +562,32 @@ static int lcurl_easy_set_HEADERFUNCTION(lua_State *L){
//}
//}
static int lcurl_easy_setopt(lua_State *L){
lcurl_easy_t *p = lcurl_geteasy(L);
int opt = luaL_checklong(L, 2);
lua_remove(L, 2);
#define OPT_ENTRY(l, N, T, S) case CURLOPT_##N: return lcurl_easy_set_##N(L);
switch(opt){
#include "lcopteasy.h"
OPT_ENTRY(postfields, POSTFIELDS, TTT, 0)
OPT_ENTRY(httppost, HTTPPOST, TTT, 0)
OPT_ENTRY(writefunction, WRITEFUNCTION, TTT, 0)
OPT_ENTRY(readfunction, READFUNCTION, TTT, 0)
OPT_ENTRY(headerfunction, HEADERFUNCTION, TTT, 0)
}
#undef OPT_ENTRY
return lcurl_fail_ex(L, p->err_mode, LCURL_ERROR_EASY, CURLE_UNKNOWN_OPTION);
}
//}
static const struct luaL_Reg lcurl_easy_methods[] = {
#define OPT_ENTRY(L, N, T, S) { "setopt_"#L, lcurl_easy_set_##N },
#include "lcopteasy.h"
OPT_ENTRY(postfields, POSTFIELDS, TTT, 0)
OPT_ENTRY(httppost, HTTPPOST, TTT, 0)
OPT_ENTRY(writefunction, WRITEFUNCTION, TTT, 0)
OPT_ENTRY(readfunction, READFUNCTION, TTT, 0)
@ -580,8 +598,9 @@ static const struct luaL_Reg lcurl_easy_methods[] = {
#include "lcinfoeasy.h"
#undef OPT_ENTRY
{ "escape", lcurl_easy_escape, },
{ "unescape", lcurl_easy_unescape, },
{ "setopt", lcurl_easy_setopt },
{ "escape", lcurl_easy_escape },
{ "unescape", lcurl_easy_unescape },
{ "perform", lcurl_easy_perform },
{ "close", lcurl_easy_cleanup },
{ "__gc", lcurl_easy_cleanup },
@ -589,8 +608,28 @@ static const struct luaL_Reg lcurl_easy_methods[] = {
{NULL,NULL}
};
static const lcurl_const_t lcurl_easy_opt[] = {
#define OPT_ENTRY(L, N, T, S) { "OPT_"#N, CURLOPT_##N },
#include "lcopteasy.h"
OPT_ENTRY(postfields, POSTFIELDS, TTT, 0)
OPT_ENTRY(httppost, HTTPPOST, TTT, 0)
OPT_ENTRY(writefunction, WRITEFUNCTION, TTT, 0)
OPT_ENTRY(readfunction, READFUNCTION, TTT, 0)
OPT_ENTRY(headerfunction, HEADERFUNCTION, TTT, 0)
#undef OPT_ENTRY
#define OPT_ENTRY(L, N, T, S) { "INFO_"#N, CURLINFO_##N },
#include "lcinfoeasy.h"
#undef OPT_ENTRY
{NULL, 0}
};
void lcurl_easy_initlib(lua_State *L, int nup){
if(!lutil_createmetap(L, LCURL_EASY, lcurl_easy_methods, nup))
lua_pop(L, nup);
lua_pop(L, 1);
lcurl_util_set_const(L, lcurl_easy_opt);
}

View File

@ -98,4 +98,11 @@ void lcurl_util_slist_to_table(lua_State *L, struct curl_slist* list){
lcurl_util_slist_set(L, -1, list);
}
void lcurl_util_set_const(lua_State *L, const lcurl_const_t *reg){
lcurl_const_t *p;
for(p = reg; p->name; ++p){
lua_pushstring(L, p->name);
lua_pushnumber(L, p->value);
lua_settable(L, -3);
}
}

View File

@ -3,6 +3,11 @@
#include "lcurl.h"
typedef struct lcurl_const_tag{
const char *name;
int value;
}lcurl_const_t;
int lcurl_storage_init(lua_State *L);
void lcurl_storage_preserve_value(lua_State *L, int storage, int i);
@ -21,4 +26,6 @@ void lcurl_util_slist_set(lua_State *L, int t, struct curl_slist* list);
void lcurl_util_slist_to_table(lua_State *L, struct curl_slist* list);
void lcurl_util_set_const(lua_State *L, const lcurl_const_t *reg);
#endif