Skip to content

Commit b904856

Browse files
committed
Add. easy/multi handle support user values.
```Lua f = io.open(...) e = curl.easy():setdata(f) ... -- when easy done e:getdata():close() ```
1 parent eb21f4f commit b904856

5 files changed

Lines changed: 96 additions & 7 deletions

File tree

src/lceasy.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ static int lcurl_easy_cleanup(lua_State *L){
100100
p->lists[i] = LUA_NOREF;
101101
}
102102

103+
lua_settop(L, 1);
104+
lua_pushnil(L);
105+
lua_rawset(L, LCURL_USERVALUES);
106+
103107
return 0;
104108
}
105109

@@ -941,6 +945,22 @@ static int lcurl_easy_pause(lua_State *L){
941945
return 1;
942946
}
943947

948+
static int lcurl_easy_setdata(lua_State *L){
949+
lcurl_easy_t *p = lcurl_geteasy(L);
950+
lua_settop(L, 2);
951+
lua_pushvalue(L, 1);
952+
lua_insert(L, 2);
953+
lua_rawset(L, LCURL_USERVALUES);
954+
return 1;
955+
}
956+
957+
static int lcurl_easy_getdata(lua_State *L){
958+
lcurl_easy_t *p = lcurl_geteasy(L);
959+
lua_settop(L, 1);
960+
lua_rawget(L, LCURL_USERVALUES);
961+
return 1;
962+
}
963+
944964
//}
945965

946966
static const struct luaL_Reg lcurl_easy_methods[] = {
@@ -982,6 +1002,9 @@ static const struct luaL_Reg lcurl_easy_methods[] = {
9821002
{ "close", lcurl_easy_cleanup },
9831003
{ "__gc", lcurl_easy_cleanup },
9841004

1005+
{ "setdata", lcurl_easy_setdata },
1006+
{ "getdata", lcurl_easy_getdata },
1007+
9851008
{NULL,NULL}
9861009
};
9871010

src/lcmulti.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ static int lcurl_multi_cleanup(lua_State *L){
7878
luaL_unref(L, LCURL_LUA_REGISTRY, p->sc.ud_ref);
7979
p->tm.cb_ref = p->tm.ud_ref = LUA_NOREF;
8080
p->sc.cb_ref = p->sc.ud_ref = LUA_NOREF;
81+
82+
lua_settop(L, 1);
83+
lua_pushnil(L);
84+
lua_rawset(L, LCURL_USERVALUES);
85+
8186
return 0;
8287
}
8388

@@ -441,6 +446,22 @@ static int lcurl_multi_setopt(lua_State *L){
441446
return lcurl_fail_ex(L, p->err_mode, LCURL_ERROR_MULTI, CURLM_UNKNOWN_OPTION);
442447
}
443448

449+
static int lcurl_multi_setdata(lua_State *L){
450+
lcurl_multi_t *p = lcurl_getmulti(L);
451+
lua_settop(L, 2);
452+
lua_pushvalue(L, 1);
453+
lua_insert(L, 2);
454+
lua_rawset(L, LCURL_USERVALUES);
455+
return 1;
456+
}
457+
458+
static int lcurl_multi_getdata(lua_State *L){
459+
lcurl_multi_t *p = lcurl_getmulti(L);
460+
lua_settop(L, 1);
461+
lua_rawget(L, LCURL_USERVALUES);
462+
return 1;
463+
}
464+
444465
//}
445466

446467
static const struct luaL_Reg lcurl_multi_methods[] = {
@@ -459,6 +480,9 @@ static const struct luaL_Reg lcurl_multi_methods[] = {
459480
OPT_ENTRY(socketfunction, SOCKETFUNCTION, TTT, 0)
460481
#undef OPT_ENTRY
461482

483+
{ "setdata", lcurl_multi_setdata },
484+
{ "getdata", lcurl_multi_getdata },
485+
462486
{"close", lcurl_multi_cleanup },
463487
{"__gc", lcurl_multi_cleanup },
464488

src/lcurl.c

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ static const lcurl_const_t lcurl_flags[] = {
180180
static volatile int LCURL_INIT = 0;
181181

182182
static const char* LCURL_REGISTRY = "LCURL Registry";
183+
static const char* LCURL_USERVAL = "LCURL Uservalues";
183184

184185
static int luaopen_lcurl_(lua_State *L, const struct luaL_Reg *func){
185186
if(!LCURL_INIT){
@@ -192,16 +193,24 @@ static int luaopen_lcurl_(lua_State *L, const struct luaL_Reg *func){
192193
lua_pop(L, 1);
193194
lua_newtable(L);
194195
}
196+
197+
lua_rawgetp(L, LUA_REGISTRYINDEX, LCURL_USERVAL);
198+
if(!lua_istable(L, -1)){ /* usevalues */
199+
lua_pop(L, 1);
200+
lcurl_util_new_weak_table(L, "k");
201+
}
202+
195203
lua_newtable(L); /* library */
196204

197-
lua_pushvalue(L, -2); luaL_setfuncs(L, func, 1);
198-
lua_pushvalue(L, -2); lcurl_error_initlib(L, 1);
199-
lua_pushvalue(L, -2); lcurl_hpost_initlib(L, 1);
200-
lua_pushvalue(L, -2); lcurl_easy_initlib (L, 1);
201-
lua_pushvalue(L, -2); lcurl_multi_initlib(L, 1);
202-
lua_pushvalue(L, -2); lcurl_share_initlib(L, 1);
205+
lua_pushvalue(L, -3); lua_pushvalue(L, -3); luaL_setfuncs(L, func, 2);
206+
lua_pushvalue(L, -3); lua_pushvalue(L, -3); lcurl_error_initlib(L, 2);
207+
lua_pushvalue(L, -3); lua_pushvalue(L, -3); lcurl_hpost_initlib(L, 2);
208+
lua_pushvalue(L, -3); lua_pushvalue(L, -3); lcurl_easy_initlib (L, 2);
209+
lua_pushvalue(L, -3); lua_pushvalue(L, -3); lcurl_multi_initlib(L, 2);
210+
lua_pushvalue(L, -3); lua_pushvalue(L, -3); lcurl_share_initlib(L, 2);
203211

204-
lua_pushvalue(L, -2); lua_rawsetp(L, LUA_REGISTRYINDEX, LCURL_REGISTRY);
212+
lua_pushvalue(L, -3); lua_rawsetp(L, LUA_REGISTRYINDEX, LCURL_REGISTRY);
213+
lua_pushvalue(L, -2); lua_rawsetp(L, LUA_REGISTRYINDEX, LCURL_USERVAL);
205214

206215
lua_remove(L, -2); /* registry */
207216

src/lcurl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,6 @@
2323

2424
#define LCURL_LUA_REGISTRY lua_upvalueindex(1)
2525

26+
#define LCURL_USERVALUES lua_upvalueindex(2)
2627

2728
#endif

test/test_easy.lua

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -796,4 +796,36 @@ end
796796

797797
end
798798

799+
local _ENV = TEST_CASE'setopt_user_data' if ENABLE then
800+
801+
local c
802+
803+
function setup()
804+
if c then c:close() end
805+
c = nil
806+
end
807+
808+
function test_data()
809+
c = assert(curl.easy())
810+
assert_nil(c:getdata())
811+
c:setdata("hello")
812+
assert_equal("hello", c:getdata())
813+
end
814+
815+
function test_cleanup()
816+
local ptr do
817+
local t = {}
818+
local e = curl.easy():setdata(t)
819+
ptr = weak_ptr(t)
820+
gc_collect()
821+
822+
assert_equal(t, ptr.value)
823+
end
824+
825+
gc_collect()
826+
assert_nil(ptr.value)
827+
end
828+
829+
end
830+
799831
RUN()

0 commit comments

Comments
 (0)