Skip to content

Commit e0bef3d

Browse files
authored
Merge pull request #125 from moteus/master
Allow set empty array as slist
2 parents 1fb4bc8 + dbbab9e commit e0bef3d

File tree

3 files changed

+83
-13
lines changed

3 files changed

+83
-13
lines changed

src/lceasy.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ static int lcurl_opt_set_slist_(lua_State *L, int opt, int list_no){
365365
CURLcode code;
366366
int ref = p->lists[list_no];
367367

368-
luaL_argcheck(L, list, 2, "array expected");
368+
luaL_argcheck(L, list || lua_istable(L, 2), 2, "array expected");
369369

370370
if(ref != LUA_NOREF){
371371
struct curl_slist *tmp = lcurl_storage_remove_slist(L, p->storage, ref);
@@ -380,7 +380,10 @@ static int lcurl_opt_set_slist_(lua_State *L, int opt, int list_no){
380380
return lcurl_fail_ex(L, p->err_mode, LCURL_ERROR_EASY, code);
381381
}
382382

383-
p->lists[list_no] = lcurl_storage_preserve_slist(L, p->storage, list);
383+
if (list) {
384+
p->lists[list_no] = lcurl_storage_preserve_slist(L, p->storage, list);
385+
}
386+
384387
lua_settop(L, 1);
385388
return 1;
386389
}

test/test_easy.lua

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ local POST_URL = "http://127.0.0.1:7090/post"
3232
-- print("------------------------------------")
3333
-- print("")
3434

35-
local weak_ptr, gc_collect, is_curl_ge, read_file, stream, Stream =
36-
utils.import('weak_ptr', 'gc_collect', 'is_curl_ge', 'read_file', 'stream', 'Stream')
35+
local weak_ptr, gc_collect, is_curl_ge, read_file, stream, Stream, dump_request =
36+
utils.import('weak_ptr', 'gc_collect', 'is_curl_ge', 'read_file', 'stream', 'Stream', 'dump_request')
3737

3838
local ENABLE = true
3939

@@ -1026,4 +1026,38 @@ function test_chunk_end() test_cb('chunk_end_function') end
10261026

10271027
end
10281028

1029+
local _ENV = TEST_CASE'set_slist' if ENABLE then
1030+
1031+
local c
1032+
1033+
function teardown()
1034+
if c then c:close() end
1035+
c = nil
1036+
end
1037+
1038+
function test_set()
1039+
c = curl.easy()
1040+
c:setopt_httpheader({'X-Custom: value'})
1041+
local body, headers = assert_string(dump_request(c))
1042+
assert_match("X%-Custom:%s*value\r\n", headers)
1043+
end
1044+
1045+
function test_unset()
1046+
c = curl.easy()
1047+
c:setopt_httpheader({'X-Custom: value'})
1048+
c:unsetopt_httpheader()
1049+
local body, headers = assert_string(dump_request(c))
1050+
assert_not_match("X%-Custom:%s*value\r\n", headers)
1051+
end
1052+
1053+
function test_set_empty_array()
1054+
c = curl.easy()
1055+
c:setopt_httpheader({'X-Custom: value'})
1056+
c:setopt_httpheader({})
1057+
local body, headers = assert_string(dump_request(c))
1058+
assert_not_match("X%-Custom:%s*value\r\n", headers)
1059+
end
1060+
1061+
end
1062+
10291063
RUN()

test/utils.lua

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,16 +93,49 @@ local function easy_dump_mime(easy, mime, url)
9393
return table.concat(buffer)
9494
end
9595

96+
local function easy_dump_request(easy, url)
97+
local buffer = {}
98+
local headers = {}
99+
100+
local function dump_mime(type, data)
101+
if type == curl.INFO_DATA_OUT then
102+
buffer[#buffer + 1] = data
103+
end
104+
105+
if type == curl.INFO_HEADER_OUT then
106+
headers[#headers + 1] = data
107+
end
108+
end
109+
110+
local ok, err = easy:setopt{
111+
url = url or "http://127.0.0.1:7090";
112+
customrequest = "GET";
113+
mimepost = mime;
114+
verbose = true;
115+
debugfunction = dump_mime;
116+
writefunction = function()end;
117+
}
118+
119+
if not ok then return nil, err end
120+
121+
ok, err = easy:perform()
122+
123+
if not ok then return nil, err end
124+
125+
return table.concat(buffer), table.concat(headers)
126+
end
127+
96128
local utils = {
97-
weak_ptr = weak_ptr;
98-
gc_collect = gc_collect;
99-
is_curl_ge = is_curl_ge;
100-
is_curl_eq = is_curl_eq;
101-
get_bin_by = get_bin_by;
102-
read_file = read_file;
103-
dump_mime = easy_dump_mime;
104-
stream = stream;
105-
Stream = Stream;
129+
weak_ptr = weak_ptr;
130+
gc_collect = gc_collect;
131+
is_curl_ge = is_curl_ge;
132+
is_curl_eq = is_curl_eq;
133+
get_bin_by = get_bin_by;
134+
read_file = read_file;
135+
dump_mime = easy_dump_mime;
136+
dump_request = easy_dump_request;
137+
stream = stream;
138+
Stream = Stream;
106139
}
107140

108141
utils.import = function(...)

0 commit comments

Comments
 (0)