diff --git a/src/lceasy.c b/src/lceasy.c index aaf5640..3a222f1 100644 --- a/src/lceasy.c +++ b/src/lceasy.c @@ -279,7 +279,7 @@ static int lcurl_easy_set_POSTFIELDS(lua_State *L){ #undef LCURL_LST_OPT #undef LCURL_LNG_OPT -static int lcurl_hpost_read_callback(char *buffer, size_t size, size_t nitems, void *arg); +static size_t lcurl_hpost_read_callback(char *buffer, size_t size, size_t nitems, void *arg); static int lcurl_easy_set_HTTPPOST(lua_State *L){ lcurl_easy_t *p = lcurl_geteasy(L); @@ -642,7 +642,7 @@ static int lcurl_easy_set_WRITEFUNCTION(lua_State *L){ //{ Reader -static int lcurl_read_callback(lua_State *L, +static size_t lcurl_read_callback(lua_State *L, lcurl_callback_t *rd, lcurl_read_buffer_t *rbuffer, char *buffer, size_t size, size_t nitems ){ @@ -699,12 +699,12 @@ static int lcurl_read_callback(lua_State *L, return data_size; } -static int lcurl_easy_read_callback(char *buffer, size_t size, size_t nitems, void *arg){ +static size_t lcurl_easy_read_callback(char *buffer, size_t size, size_t nitems, void *arg){ lcurl_easy_t *p = arg; return lcurl_read_callback(p->L, &p->rd, &p->rbuffer, buffer, size, nitems); } -static int lcurl_hpost_read_callback(char *buffer, size_t size, size_t nitems, void *arg){ +static size_t lcurl_hpost_read_callback(char *buffer, size_t size, size_t nitems, void *arg){ lcurl_hpost_stream_t *p = arg; return lcurl_read_callback(p->L, &p->rd, &p->rbuffer, buffer, size, nitems); } @@ -791,9 +791,8 @@ static int lcurl_easy_set_PROGRESSFUNCTION(lua_State *L){ #if LCURL_CURL_VER_GE(7,32,0) if(p->pr.cb_ref != LUA_NOREF){ - CURLcode code; - code = curl_easy_setopt(p->curl, CURLOPT_XFERINFOFUNCTION, lcurl_xferinfo_callback); - code = curl_easy_setopt(p->curl, CURLOPT_XFERINFODATA, p); + curl_easy_setopt(p->curl, CURLOPT_XFERINFOFUNCTION, lcurl_xferinfo_callback); + curl_easy_setopt(p->curl, CURLOPT_XFERINFODATA, p); } #endif @@ -835,7 +834,7 @@ static int lcurl_easy_setopt(lua_State *L){ return lcurl_fail_ex(L, p->err_mode, LCURL_ERROR_EASY, LCURL_E_UNKNOWN_OPTION); } -static int lcurl_easy_unsetsetopt(lua_State *L){ +static int lcurl_easy_unsetopt(lua_State *L){ lcurl_easy_t *p = lcurl_geteasy(L); long opt; @@ -905,6 +904,7 @@ static const struct luaL_Reg lcurl_easy_methods[] = { { "reset", lcurl_easy_reset }, { "setopt", lcurl_easy_setopt }, { "getinfo", lcurl_easy_getinfo }, + { "unsetopt", lcurl_easy_unsetopt }, { "escape", lcurl_easy_escape }, { "unescape", lcurl_easy_unescape }, { "perform", lcurl_easy_perform }, diff --git a/src/lchttppost.c b/src/lchttppost.c index 2525027..8098426 100644 --- a/src/lchttppost.c +++ b/src/lchttppost.c @@ -6,7 +6,60 @@ #define LCURL_HTTPPOST_NAME LCURL_PREFIX" HTTPPost" static const char *LCURL_HTTPPOST = LCURL_HTTPPOST_NAME; -//{ +//{ stream + +static lcurl_hpost_stream_t *lcurl_hpost_stream_add(lua_State *L, lcurl_hpost_t *p){ + lcurl_hpost_stream_t *ptr = p->stream; + lcurl_hpost_stream_t *stream = malloc(sizeof(lcurl_hpost_stream_t)); + if(!stream) return NULL; + + stream->L = L; + stream->rbuffer.ref = LUA_NOREF; + stream->rd.cb_ref = stream->rd.ud_ref = LUA_NOREF; + stream->next = NULL; + if(!p->stream) p->stream = stream; + else{ + while(ptr->next) ptr = ptr->next; + ptr->next = stream; + } + return stream; +} + +static void lcurl_hpost_stream_free(lua_State *L, lcurl_hpost_stream_t *ptr){ + if(ptr){ + luaL_unref(L, LCURL_LUA_REGISTRY, ptr->rbuffer.ref); + luaL_unref(L, LCURL_LUA_REGISTRY, ptr->rd.cb_ref); + luaL_unref(L, LCURL_LUA_REGISTRY, ptr->rd.ud_ref); + free(ptr); + } +} + +static void lcurl_hpost_stream_free_last(lua_State *L, lcurl_hpost_t *p){ + lcurl_hpost_stream_t *ptr = p->stream; + if(!ptr) return; + if(!ptr->next){ + lcurl_hpost_stream_free(L, ptr); + p->stream = 0; + } + + while(ptr->next->next) ptr = ptr->next; + lcurl_hpost_stream_free(L, ptr->next); + ptr->next = NULL; +} + +static void lcurl_hpost_stream_free_all(lua_State *L, lcurl_hpost_t *p){ + lcurl_hpost_stream_t *ptr = p->stream; + while(ptr){ + lcurl_hpost_stream_t *next = ptr->next; + lcurl_hpost_stream_free(L, ptr); + ptr = next; + } + p->stream = 0; +} + +//} + +//{ HTTPPost int lcurl_hpost_create(lua_State *L, int error_mode){ lcurl_hpost_t *p = lutil_newudatap(L, lcurl_hpost_t, LCURL_HTTPPOST); @@ -108,7 +161,7 @@ static int lcurl_hpost_add_file(lua_State *L){ const char *path = luaL_checkstring(L, 3); const char *type = 0, *fname = 0; struct curl_slist *list = NULL; - struct curl_forms forms[3]; + struct curl_forms forms[4]; CURLFORMcode code; int i = 0; @@ -132,8 +185,9 @@ static int lcurl_hpost_add_file(lua_State *L){ } } - if(type){ forms[i].option = CURLFORM_CONTENTTYPE; forms[i++].value = type; } - if(list){ forms[i].option = CURLFORM_CONTENTHEADER; forms[i++].value = (char*)list; } + if(fname){ forms[i].option = CURLFORM_FILENAME; forms[i++].value = fname; } + if(type) { forms[i].option = CURLFORM_CONTENTTYPE; forms[i++].value = type; } + if(list) { forms[i].option = CURLFORM_CONTENTHEADER; forms[i++].value = (char*)list; } forms[i].option = CURLFORM_END; code = curl_formadd(&p->post, &p->last, @@ -154,55 +208,6 @@ static int lcurl_hpost_add_file(lua_State *L){ return 1; } -static lcurl_hpost_stream_t *lcurl_hpost_stream_add(lua_State *L, lcurl_hpost_t *p){ - lcurl_hpost_stream_t *ptr = p->stream; - lcurl_hpost_stream_t *stream = malloc(sizeof(lcurl_hpost_stream_t)); - if(!stream) return NULL; - - stream->L = L; - stream->rbuffer.ref = LUA_NOREF; - stream->rd.cb_ref = stream->rd.ud_ref = LUA_NOREF; - stream->next = NULL; - if(!p->stream) p->stream = stream; - else{ - while(ptr->next) ptr = ptr->next; - ptr->next = stream; - } - return stream; -} - -static void lcurl_hpost_stream_free(lua_State *L, lcurl_hpost_stream_t *ptr){ - if(ptr){ - luaL_unref(L, LCURL_LUA_REGISTRY, ptr->rbuffer.ref); - luaL_unref(L, LCURL_LUA_REGISTRY, ptr->rd.cb_ref); - luaL_unref(L, LCURL_LUA_REGISTRY, ptr->rd.ud_ref); - free(ptr); - } -} - -static void lcurl_hpost_stream_free_last(lua_State *L, lcurl_hpost_t *p){ - lcurl_hpost_stream_t *ptr = p->stream; - if(!ptr) return; - if(!ptr->next){ - lcurl_hpost_stream_free(L, ptr); - p->stream = 0; - } - - while(ptr->next->next) ptr = ptr->next; - lcurl_hpost_stream_free(L, ptr->next); - ptr->next = NULL; -} - -static void lcurl_hpost_stream_free_all(lua_State *L, lcurl_hpost_t *p){ - lcurl_hpost_stream_t *ptr = p->stream; - while(ptr){ - lcurl_hpost_stream_t *next = ptr->next; - lcurl_hpost_stream_free(L, ptr); - ptr = next; - } - p->stream = 0; -} - static int lcurl_hpost_add_stream(lua_State *L){ // add_stream(name, [filename, [type,]] [headers,] size, reader [,context]) lcurl_hpost_t *p = lcurl_gethpost(L); @@ -472,12 +477,6 @@ static int lcurl_hpost_get(lua_State *L){ return 1; } -static int lcurl_hpost_storage(lua_State *L){ - lcurl_hpost_t *p = lcurl_gethpost(L); - lua_rawgeti(L, LCURL_LUA_REGISTRY, p->storage); - return 1; -} - static int lcurl_hpost_free(lua_State *L){ lcurl_hpost_t *p = lcurl_gethpost(L); if(p->post){ @@ -504,7 +503,6 @@ static const struct luaL_Reg lcurl_hpost_methods[] = { {"add_files", lcurl_hpost_add_files }, - {"storage", lcurl_hpost_storage }, {"get", lcurl_hpost_get }, {"free", lcurl_hpost_free }, {"__gc", lcurl_hpost_free }, diff --git a/src/lcmulti.c b/src/lcmulti.c index 8b8a653..1b5f6f6 100644 --- a/src/lcmulti.c +++ b/src/lcmulti.c @@ -15,8 +15,6 @@ #include "lcutils.h" #include "lchttppost.h" -static const char *LCURL_ERROR_TAG = "LCURL_ERROR_TAG"; - #define LCURL_MULTI_NAME LCURL_PREFIX" Multi" static const char *LCURL_MULTI = LCURL_MULTI_NAME; diff --git a/src/lcshare.c b/src/lcshare.c index bf19572..dcccff2 100644 --- a/src/lcshare.c +++ b/src/lcshare.c @@ -4,8 +4,6 @@ #include "lcutils.h" #include "lchttppost.h" -static const char *LCURL_ERROR_TAG = "LCURL_ERROR_TAG"; - #define LCURL_SHARE_NAME LCURL_PREFIX" Share" static const char *LCURL_SHARE = LCURL_SHARE_NAME;