Skip to content

Commit a9874c7

Browse files
committed
Add. Support null in mime module.
1 parent 4a70bcb commit a9874c7

File tree

2 files changed

+101
-4
lines changed

2 files changed

+101
-4
lines changed

src/lcmime.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ int lcurl_mime_set_lua(lua_State *L, lcurl_mime_t *p, lua_State *v){
135135

136136
#define IS_NILORSTR(L, i) (lua_type(L, i) == LUA_TSTRING) || (lua_type(L, i) == LUA_TNIL)
137137
#define IS_TABLE(L, i) lua_type(L, i) == LUA_TTABLE
138-
#define IS_FALSE(L, i) (lua_type(L, i) == LUA_TBOOLEAN) && (!lua_toboolean(L, i))
138+
#define IS_FALSE(L, i) ((lua_type(L, i) == LUA_TBOOLEAN) && (!lua_toboolean(L, i))) || lutil_is_null(L,i)
139139
#define IS_OPTSTR(L, i) (IS_FALSE(L, i)) || (IS_NILORSTR(L, i))
140140

141141
static int lutil_isarray(lua_State *L, int i){
@@ -512,13 +512,13 @@ static int lcurl_mime_part_headers(lua_State *L){
512512
}
513513
else{
514514
list = lcurl_util_to_slist(L, 2);
515-
luaL_argcheck(L, list, 2, "array or nil expected");
515+
luaL_argcheck(L, list || IS_TABLE(L, 2), 2, "array or null expected");
516516
}
517517

518518
ret = curl_mime_headers(p->part, list, 1);
519519

520520
if(ret != CURLE_OK){
521-
curl_slist_free_all(list);
521+
if(list) curl_slist_free_all(list);
522522
return lcurl_fail_ex(L, p->err_mode, LCURL_ERROR_EASY, ret);
523523
}
524524

test/test_mime.lua

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ local weak_ptr, gc_collect, dump_mime_ = utils.import('weak_ptr', 'gc_collect',
1919

2020
local GET_URL = 'http://127.0.0.1:7090/get'
2121

22+
local null = curl.null
23+
2224
local function is_freed(c)
2325
return not not string.find(tostring(c), '%(freed%)')
2426
end
@@ -313,6 +315,15 @@ function test_unset_name()
313315
assert_not_match('Content%-Disposition:.-name="test"', info)
314316
end
315317

318+
function test_unset_name_by_null()
319+
mime:addpart():data('hello', 'test/html', 'test'):name(null)
320+
321+
local info = assert_string(dump_mime(mime))
322+
assert_match('\r\n\r\nhello', info)
323+
assert_match('Content%-Type:%s+test/html', info)
324+
assert_not_match('Content%-Disposition:.-name="test"', info)
325+
end
326+
316327
function test_unset_type()
317328
mime:addpart():data('hello', 'test/html'):type(false)
318329

@@ -321,6 +332,14 @@ function test_unset_type()
321332
assert_not_match('Content%-Type:%s+test/html', info)
322333
end
323334

335+
function test_unset_type_by_null()
336+
mime:addpart():data('hello', 'test/html'):type(null)
337+
338+
local info = assert_string(dump_mime(mime))
339+
assert_match('\r\n\r\nhello', info)
340+
assert_not_match('Content%-Type:%s+test/html', info)
341+
end
342+
324343
function test_unset_headers()
325344
mime:addpart():data('hello', 'test/html',{
326345
'X-Custom-Header: hello'
@@ -331,6 +350,26 @@ function test_unset_headers()
331350
assert_not_match('X%-Custom%-Header:%s*hello', info)
332351
end
333352

353+
function test_unset_headers_by_null()
354+
mime:addpart():data('hello', 'test/html',{
355+
'X-Custom-Header: hello'
356+
}):headers(null)
357+
358+
local info = assert_string(dump_mime(mime))
359+
assert_match('\r\n\r\nhello', info)
360+
assert_not_match('X%-Custom%-Header:%s*hello', info)
361+
end
362+
363+
function test_unset_headers_by_empty_array()
364+
mime:addpart():data('hello', 'test/html',{
365+
'X-Custom-Header: hello'
366+
}):headers({})
367+
368+
local info = assert_string(dump_mime(mime))
369+
assert_match('\r\n\r\nhello', info)
370+
assert_not_match('X%-Custom%-Header:%s*hello', info)
371+
end
372+
334373
function test_unset_data()
335374
mime:addpart():data('hello', 'test/html', 'test'):data(false)
336375

@@ -340,6 +379,15 @@ function test_unset_data()
340379
assert_match('Content%-Disposition:.-name="test"', info)
341380
end
342381

382+
function test_unset_data_by_null()
383+
mime:addpart():data('hello', 'test/html', 'test'):data(null)
384+
385+
local info = assert_string(dump_mime(mime))
386+
assert_not_match('\r\n\r\nhello', info)
387+
assert_match('Content%-Type:%s+test/html', info)
388+
assert_match('Content%-Disposition:.-name="test"', info)
389+
end
390+
343391
function test_unset_data_type_1()
344392
local part = mime:addpart():data('hello', 'test/html', 'test', {
345393
'X-Custom-Header: hello'
@@ -352,6 +400,18 @@ function test_unset_data_type_1()
352400
assert_match('X%-Custom%-Header:%s*hello', info)
353401
end
354402

403+
function test_unset_data_type_1_by_null()
404+
local part = mime:addpart():data('hello', 'test/html', 'test', {
405+
'X-Custom-Header: hello'
406+
}):data('hello', null)
407+
408+
local info = assert_string(dump_mime(mime))
409+
assert_match('\r\n\r\nhello', info)
410+
assert_not_match('Content%-Type:%s+test/html', info)
411+
assert_match('Content%-Disposition:.-name="test"', info)
412+
assert_match('X%-Custom%-Header:%s*hello', info)
413+
end
414+
355415
function test_unset_data_type_2()
356416
local part = mime:addpart():data('hello', 'test/html', 'test', {
357417
'X-Custom-Header: hello'
@@ -376,6 +436,18 @@ function test_unset_data_name_1()
376436
assert_match('X%-Custom%-Header:%s*hello', info)
377437
end
378438

439+
function test_unset_data_name_1_by_null()
440+
local part = mime:addpart():data('hello', 'test/html', 'test', {
441+
'X-Custom-Header: hello'
442+
}):data('hello', nil, null)
443+
444+
local info = assert_string(dump_mime(mime))
445+
assert_match('\r\n\r\nhello', info)
446+
assert_match('Content%-Type:%s+test/html', info)
447+
assert_not_match('Content%-Disposition:.-name="test"', info)
448+
assert_match('X%-Custom%-Header:%s*hello', info)
449+
end
450+
379451
function test_unset_data_name_2()
380452
local part = mime:addpart():data('hello', 'test/html', 'test', {
381453
'X-Custom-Header: hello'
@@ -400,6 +472,18 @@ function test_unset_data_header()
400472
assert_not_match('X%-Custom%-Header:%s*hello', info)
401473
end
402474

475+
function test_unset_data_header_by_null()
476+
local part = mime:addpart():data('hello', 'test/html', 'test', {
477+
'X-Custom-Header: hello'
478+
}):data('hello', nil, nil, nil, null)
479+
480+
local info = assert_string(dump_mime(mime))
481+
assert_match('\r\n\r\nhello', info)
482+
assert_match('Content%-Type:%s+test/html', info)
483+
assert_match('Content%-Disposition:.-name="test"', info)
484+
assert_not_match('X%-Custom%-Header:%s*hello', info)
485+
end
486+
403487
function test_unset_data_filename_1()
404488
local part = mime:addpart():data('hello', 'test/html', 'test', 'test.html', {
405489
'X-Custom-Header: hello'
@@ -409,7 +493,20 @@ function test_unset_data_filename_1()
409493
assert_match('\r\n\r\nhello', info)
410494
assert_match('Content%-Type:%s+test/html', info)
411495
assert_match('Content%-Disposition:.-%sname="test"', info)
412-
assert_not_match('Content%-Disposition:.-%sname="test%.html"', info)
496+
assert_not_match('Content%-Disposition:.-%sfilename="test%.html"', info)
497+
assert_match('X%-Custom%-Header:%s*hello', info)
498+
end
499+
500+
function test_unset_data_filename_1_by_null()
501+
local part = mime:addpart():data('hello', 'test/html', 'test', 'test.html', {
502+
'X-Custom-Header: hello'
503+
}):data('hello', nil, nil, null)
504+
505+
local info = assert_string(dump_mime(mime))
506+
assert_match('\r\n\r\nhello', info)
507+
assert_match('Content%-Type:%s+test/html', info)
508+
assert_match('Content%-Disposition:.-%sname="test"', info)
509+
assert_not_match('Content%-Disposition:.-%sfilename="test%.html"', info)
413510
assert_match('X%-Custom%-Header:%s*hello', info)
414511
end
415512

0 commit comments

Comments
 (0)