Skip to content

Commit 39c84f8

Browse files
authored
Merge pull request #165 from moteus/reset_cleanup
Reset cleanup
2 parents 4939181 + 4dd6d43 commit 39c84f8

File tree

5 files changed

+111
-55
lines changed

5 files changed

+111
-55
lines changed

src/lceasy.c

Lines changed: 34 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,6 @@ int lcurl_easy_create(lua_State *L, int error_mode){
8383
p->multi = NULL;
8484
#if LCURL_CURL_VER_GE(7,56,0)
8585
p->mime = NULL;
86-
#endif
87-
#if LCURL_CURL_VER_GE(7,63,0)
88-
p->url = NULL;
8986
#endif
9087
p->storage = lcurl_storage_init(L);
9188
p->wr.cb_ref = p->wr.ud_ref = LUA_NOREF;
@@ -130,46 +127,18 @@ static int lcurl_easy_to_s(lua_State *L){
130127
return 1;
131128
}
132129

133-
static int lcurl_easy_cleanup(lua_State *L){
134-
lcurl_easy_t *p = lcurl_geteasy(L);
130+
static int lcurl_easy_cleanup_storage(lua_State *L, lcurl_easy_t *p){
135131
int i;
136132

137-
if(p->multi){
138-
LCURL_UNUSED_VAR CURLMcode code = lcurl__multi_remove_handle(L, p->multi, p);
139-
140-
//! @todo what I can do if I can not remove it???
141-
}
142-
143-
if(p->curl){
144-
lua_State *curL;
145-
146-
// In my tests when I cleanup some easy handle.
147-
// timerfunction called only for single multi handle.
148-
// Also may be this function may call `close` callback
149-
// for `curl_mimepart` structure.
150-
curL = p->L; lcurl__easy_assign_lua(L, p, L, 1);
151-
curl_easy_cleanup(p->curl);
152-
#ifndef LCURL_RESET_NULL_LUA
153-
if(curL != NULL)
154-
#endif
155-
lcurl__easy_assign_lua(L, p, curL, 1);
156-
157-
p->curl = NULL;
133+
if(p->storage != LUA_NOREF){
134+
p->storage = lcurl_storage_free(L, p->storage);
158135
}
159136

160137
p->post = NULL;
161138
#if LCURL_CURL_VER_GE(7,56,0)
162139
p->mime = NULL;
163140
#endif
164141

165-
#if LCURL_CURL_VER_GE(7,63,0)
166-
p->url = NULL;
167-
#endif
168-
169-
if(p->storage != LUA_NOREF){
170-
p->storage = lcurl_storage_free(L, p->storage);
171-
}
172-
173142
luaL_unref(L, LCURL_LUA_REGISTRY, p->wr.cb_ref);
174143
luaL_unref(L, LCURL_LUA_REGISTRY, p->wr.ud_ref);
175144
luaL_unref(L, LCURL_LUA_REGISTRY, p->rd.cb_ref);
@@ -221,8 +190,37 @@ static int lcurl_easy_cleanup(lua_State *L){
221190
for(i = 0; i < LCURL_LIST_COUNT; ++i){
222191
p->lists[i] = LUA_NOREF;
223192
}
193+
}
224194

195+
static int lcurl_easy_cleanup(lua_State *L){
196+
lcurl_easy_t *p = lcurl_geteasy(L);
225197
lua_settop(L, 1);
198+
199+
if(p->multi){
200+
LCURL_UNUSED_VAR CURLMcode code = lcurl__multi_remove_handle(L, p->multi, p);
201+
202+
//! @todo what I can do if I can not remove it???
203+
}
204+
205+
if(p->curl){
206+
lua_State *curL;
207+
208+
// In my tests when I cleanup some easy handle.
209+
// timerfunction called only for single multi handle.
210+
// Also may be this function may call `close` callback
211+
// for `curl_mimepart` structure.
212+
curL = p->L; lcurl__easy_assign_lua(L, p, L, 1);
213+
curl_easy_cleanup(p->curl);
214+
#ifndef LCURL_RESET_NULL_LUA
215+
if(curL != NULL)
216+
#endif
217+
lcurl__easy_assign_lua(L, p, curL, 1);
218+
219+
p->curl = NULL;
220+
}
221+
222+
lcurl_easy_cleanup_storage(L, p);
223+
226224
lua_pushnil(L);
227225
lua_rawset(L, LCURL_USERVALUES);
228226

@@ -305,15 +303,8 @@ static int lcurl_easy_reset(lua_State *L){
305303
curl_easy_reset(p->curl);
306304
lua_settop(L, 1);
307305

308-
if(p->storage != LUA_NOREF){
309-
int i;
310-
for (i = 0; i < LCURL_LIST_COUNT; ++i) {
311-
p->lists[i] = LUA_NOREF;
312-
}
313-
lcurl_storage_free(L, p->storage);
314-
p->storage = lcurl_storage_init(L);
315-
lua_settop(L, 1);
316-
}
306+
lcurl_easy_cleanup_storage(L, p);
307+
p->storage = lcurl_storage_init(L);
317308

318309
return 1;
319310
}
@@ -621,8 +612,6 @@ static int lcurl_easy_set_CURLU(lua_State *L) {
621612

622613
lcurl_storage_preserve_iv(L, p->storage, CURLOPT_CURLU, 2);
623614

624-
p->url = url;
625-
626615
lua_settop(L, 1);
627616
return 1;
628617
}
@@ -1030,8 +1019,6 @@ static int lcurl_easy_unset_CURLU(lua_State *L) {
10301019

10311020
lcurl_storage_remove_i(L, p->storage, CURLOPT_CURLU);
10321021

1033-
p->url = NULL;
1034-
10351022
lua_settop(L, 1);
10361023
return 1;
10371024
}

src/lceasy.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,6 @@ typedef struct lcurl_easy_tag{
7575
lcurl_mime_t *mime;
7676
#endif
7777

78-
#if LCURL_CURL_VER_GE(7,63,0)
79-
lcurl_url_t *url;
80-
#endif
81-
8278
CURL *curl;
8379
int storage;
8480
int lists[LCURL_LIST_COUNT];

src/lcutils.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ int lcurl_storage_free(lua_State *L, int storage){
115115
lua_pop(L, 1);
116116
}
117117
}
118-
lua_pop(L, 1);
119118
luaL_unref(L, LCURL_LUA_REGISTRY, storage);
119+
lua_pop(L, 2);
120120
return LUA_NOREF;
121121
}
122122

test/test_easy.lua

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -889,16 +889,16 @@ function test_reset()
889889
do
890890
local form = curl.form()
891891
e = curl.easy{httppost = form}
892-
pfrom = weak_ptr(form)
892+
pform = weak_ptr(form)
893893
end
894894

895895
gc_collect()
896-
assert(pfrom.value)
896+
assert(pform.value)
897897

898898
assert_equal(e, e:reset())
899899

900900
gc_collect()
901-
assert(not pfrom.value)
901+
assert(not pform.value)
902902
end
903903

904904
end
@@ -1027,6 +1027,8 @@ end
10271027

10281028
local _ENV = TEST_CASE'unset_callback_ctx' if ENABLE then
10291029

1030+
local HSTS = curl.version_info().features.HSTS
1031+
10301032
local c
10311033

10321034
function setup()
@@ -1057,6 +1059,19 @@ local function test_cb(name)
10571059

10581060
gc_collect()
10591061
assert_nil(pctx.value)
1062+
1063+
do local ctx = {}
1064+
pctx = weak_ptr(ctx)
1065+
assert(set(c, function() end, ctx))
1066+
end
1067+
1068+
gc_collect()
1069+
assert_table(pctx.value)
1070+
1071+
c:reset()
1072+
1073+
gc_collect()
1074+
assert_nil(pctx.value)
10601075
end
10611076

10621077
function test_read() test_cb('readfunction') end
@@ -1069,6 +1084,11 @@ function test_fnmatch() test_cb('fnmatch_function') end
10691084
function test_chunk_bgn() test_cb('chunk_bgn_function') end
10701085
function test_chunk_end() test_cb('chunk_end_function') end
10711086

1087+
if curl.OPT_HSTSREADFUNCTION and HSTS then
1088+
function test_hstsreadfunction() test_cb('hstsreadfunction') end
1089+
function test_hstswritefunction() test_cb('hstswritefunction') end
1090+
end
1091+
10721092
end
10731093

10741094
local _ENV = TEST_CASE'set_slist' if ENABLE then

test/test_mime.lua

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,27 @@ function test_mimepost_does_not_ref_to_easy()
207207
assert_nil(peasy.value)
208208
end
209209

210+
function test_cleanup_on_easy_reset()
211+
212+
local mime do
213+
mime = weak_ptr(easy:mime())
214+
easy:setopt_mimepost(mime.value)
215+
end
216+
217+
gc_collect()
218+
219+
assert_not_nil(mime.value)
220+
221+
easy:reset()
222+
223+
gc_collect(10)
224+
225+
assert_nil(mime.value)
226+
227+
easy:setopt{url = GET_URL, writefunction = function() end}
228+
easy:perform()
229+
end
230+
210231
end
211232

212233
local _ENV = TEST_CASE'mime basic' if not curl.OPT_MIMEPOST then
@@ -573,6 +594,38 @@ function test_pass_args()
573594
assert_match('Content%-Transfer%-Encoding:%s*base64', info)
574595
end
575596

597+
local function easy_dump_mime(easy, mime, url)
598+
assert(mime:addpart{
599+
data = 'hello';
600+
encoder = 'base64';
601+
name = 'test';
602+
filename = 'test.html';
603+
type = 'test/html';
604+
headers = {
605+
'X-Custom-Header: hello';
606+
}
607+
})
608+
609+
easy:setopt{
610+
mimepost = mime;
611+
}
612+
613+
assert(easy:setopt{
614+
url = GET_URL;
615+
customrequest = "GET";
616+
617+
writefunction = function()end;
618+
})
619+
620+
if not ok then return nil, err end
621+
622+
ok, err = easy:perform()
623+
624+
if not ok then return nil, err end
625+
626+
return table.concat(buffer)
627+
end
628+
576629
end
577630

578631
RUN()

0 commit comments

Comments
 (0)