Skip to content

Commit e3b5c92

Browse files
committed
Add. Allow set NULL values explicitly.
1 parent 4ddacb1 commit e3b5c92

File tree

5 files changed

+77
-5
lines changed

5 files changed

+77
-5
lines changed

src/l52util.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,11 @@ int64_t lutil_optint64(lua_State *L, int idx, int64_t v){
156156
void lutil_pushnvalues(lua_State *L, int n){
157157
for(;n;--n) lua_pushvalue(L, -n);
158158
}
159+
160+
int lutil_is_null(lua_State *L, int i){
161+
return lua_islightuserdata(L, i) && 0 == lua_touserdata(L, i);
162+
}
163+
164+
void lutil_push_null(lua_State *L){
165+
lua_pushlightuserdata(L, (void*)0);
166+
}

src/l52util.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,8 @@ int64_t lutil_optint64(lua_State *L, int idx, int64_t v);
8888

8989
void lutil_pushnvalues(lua_State *L, int n);
9090

91+
int lutil_is_null(lua_State *L, int i);
92+
93+
void lutil_push_null(lua_State *L);
94+
9195
#endif

src/lceasy.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -344,16 +344,22 @@ static int lcurl_opt_set_off_(lua_State *L, int opt){
344344

345345
static int lcurl_opt_set_string_(lua_State *L, int opt, int store){
346346
lcurl_easy_t *p = lcurl_geteasy(L);
347-
CURLcode code;
347+
CURLcode code; const char *value;
348348

349-
luaL_argcheck(L, lua_type(L, 2) == LUA_TSTRING, 2, "string expected");
349+
luaL_argcheck(L, lua_type(L, 2) == LUA_TSTRING || lutil_is_null(L, 2), 2, "string expected");
350350

351-
code = curl_easy_setopt(p->curl, opt, lua_tostring(L, 2));
351+
value = lua_tostring(L, 2);
352+
code = curl_easy_setopt(p->curl, opt, value);
352353
if(code != CURLE_OK){
353354
return lcurl_fail_ex(L, p->err_mode, LCURL_ERROR_EASY, code);
354355
}
355356

356-
if(store)lcurl_storage_preserve_iv(L, p->storage, opt, 2);
357+
if(store){
358+
if(value)
359+
lcurl_storage_preserve_iv(L, p->storage, opt, 2);
360+
else
361+
lcurl_storage_remove_i(L, p->storage, opt);
362+
}
357363

358364
lua_settop(L, 1);
359365
return 1;
@@ -365,7 +371,7 @@ static int lcurl_opt_set_slist_(lua_State *L, int opt, int list_no){
365371
CURLcode code;
366372
int ref = p->lists[list_no];
367373

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

370376
if(ref != LUA_NOREF){
371377
struct curl_slist *tmp = lcurl_storage_remove_slist(L, p->storage, ref);

src/lcurl.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,9 @@ static int luaopen_lcurl_(lua_State *L, const struct luaL_Reg *func){
270270

271271
lcurl_util_set_const(L, lcurl_flags);
272272

273+
lutil_push_null(L);
274+
lua_setfield(L, -2, "null");
275+
273276
return 1;
274277
}
275278

test/test_easy.lua

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ local POST_URL = "http://127.0.0.1:7090/post"
3535
local weak_ptr, gc_collect, is_curl_ge, read_file, stream, Stream, dump_request =
3636
utils.import('weak_ptr', 'gc_collect', 'is_curl_ge', 'read_file', 'stream', 'Stream', 'dump_request')
3737

38+
local null = curl.null
39+
3840
local ENABLE = true
3941

4042
local _ENV = TEST_CASE'curl error' if ENABLE then
@@ -1060,4 +1062,53 @@ end
10601062

10611063
end
10621064

1065+
local _ENV = TEST_CASE'set_null' if ENABLE then
1066+
1067+
local c
1068+
1069+
function teardown()
1070+
if c then c:close() end
1071+
c = nil
1072+
end
1073+
1074+
function test_string()
1075+
c = curl.easy()
1076+
c:setopt_accept_encoding('gzip')
1077+
local body, headers = assert_string(dump_request(c))
1078+
assert_match("Accept%-Encoding:%s*gzip", headers)
1079+
1080+
c:setopt_accept_encoding(null)
1081+
body, headers = assert_string(dump_request(c))
1082+
assert_not_match("Accept%-Encoding:%s*gzip", headers)
1083+
end
1084+
1085+
function test_string_via_table()
1086+
c = curl.easy()
1087+
c:setopt_accept_encoding('gzip')
1088+
local body, headers = assert_string(dump_request(c))
1089+
assert_match("Accept%-Encoding:%s*gzip", headers)
1090+
1091+
c:setopt{ accept_encoding = null }
1092+
body, headers = assert_string(dump_request(c))
1093+
assert_not_match("Accept%-Encoding:%s*gzip", headers)
1094+
end
1095+
1096+
function test_slist()
1097+
c = curl.easy()
1098+
c:setopt_httpheader({'X-Custom: value'})
1099+
c:setopt_httpheader(null)
1100+
local body, headers = assert_string(dump_request(c))
1101+
assert_not_match("X%-Custom:%s*value\r\n", headers)
1102+
end
1103+
1104+
function test_slist_via_table()
1105+
c = curl.easy()
1106+
c:setopt_httpheader({'X-Custom: value'})
1107+
c:setopt{httpheader = null}
1108+
local body, headers = assert_string(dump_request(c))
1109+
assert_not_match("X%-Custom:%s*value\r\n", headers)
1110+
end
1111+
1112+
end
1113+
10631114
RUN()

0 commit comments

Comments
 (0)