Skip to content

Commit b723615

Browse files
committed
Add. Ability unset info by passing false
1 parent 5239176 commit b723615

2 files changed

Lines changed: 185 additions & 12 deletions

File tree

src/lcmime.c

Lines changed: 58 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -241,19 +241,40 @@ static void lcurl_mime_part_remove_subparts(lua_State *L, lcurl_mime_part_t *p,
241241

242242
#define IS_NILORSTR(L, i) (lua_type(L, i) == LUA_TSTRING) || (lua_type(L, i) == LUA_TNIL)
243243
#define IS_TABLE(L, i) lua_type(L, i) == LUA_TTABLE
244+
#define IS_FALSE(L, i) (lua_type(L, i) == LUA_TBOOLEAN) && (!lua_toboolean(L, i))
245+
#define IS_OPTSTR(L, i) (IS_FALSE(L, i)) || (IS_NILORSTR(L, i))
244246

245247
static int lcurl_mime_part_assing_ext(lua_State *L, lcurl_mime_part_t *p, int i){
246248
const char *mime_type = NULL, *mime_name = NULL;
247249
int headers = 0;
248250
CURLcode ret;
249251

250252
if(IS_TABLE(L, i)) headers = i;
251-
else if(IS_NILORSTR(L, i)){
252-
mime_type = lua_tostring(L, i);
253+
else if (IS_OPTSTR(L, i)) {
254+
if (IS_FALSE(L, i)) {
255+
ret = curl_mime_type(p->part, NULL);
256+
if(ret != CURLE_OK)
257+
return lcurl_fail_ex(L, p->err_mode, LCURL_ERROR_EASY, ret);
258+
}
259+
else{
260+
mime_type = lua_tostring(L, i);
261+
}
253262
if(IS_TABLE(L, i+1)) headers = i+1;
254-
else if(IS_NILORSTR(L, i+1)){
255-
mime_name = lua_tostring(L, i+1);
263+
else if(IS_OPTSTR(L, i+1)){
264+
if (IS_FALSE(L, i+1)) {
265+
ret = curl_mime_name(p->part, NULL);
266+
if(ret != CURLE_OK)
267+
return lcurl_fail_ex(L, p->err_mode, LCURL_ERROR_EASY, ret);
268+
}
269+
else{
270+
mime_name = lua_tostring(L, i+1);
271+
}
256272
if(IS_TABLE(L, i+2)) headers = i+2;
273+
else if(IS_FALSE(L, i + 2)){
274+
ret = curl_mime_headers(p->part, NULL, 0);
275+
if(ret != CURLE_OK)
276+
return lcurl_fail_ex(L, p->err_mode, LCURL_ERROR_EASY, ret);
277+
}
257278
}
258279
}
259280

@@ -286,12 +307,18 @@ static int lcurl_mime_part_assing_ext(lua_State *L, lcurl_mime_part_t *p, int i)
286307
// part:data(str[, type[, name]][, headers])
287308
static int lcurl_mime_part_data(lua_State *L){
288309
lcurl_mime_part_t *p = lcurl_getmimepart(L);
289-
size_t len; const char *data = luaL_checklstring(L, 2, &len);
310+
size_t len; const char *data;
290311
CURLcode ret;
291312

292-
/*string too long*/
293-
if(len == CURL_ZERO_TERMINATED){
294-
return lcurl_fail_ex(L, p->err_mode, LCURL_ERROR_EASY, CURLE_BAD_FUNCTION_ARGUMENT);
313+
if(IS_FALSE(L, 2)){
314+
data = NULL;
315+
}
316+
else{
317+
data = luaL_checklstring(L, 2, &len);
318+
/*string too long*/
319+
if(len == CURL_ZERO_TERMINATED){
320+
return lcurl_fail_ex(L, p->err_mode, LCURL_ERROR_EASY, CURLE_BAD_FUNCTION_ARGUMENT);
321+
}
295322
}
296323

297324
/* curl_mime_data copies data */
@@ -364,10 +391,16 @@ static int lcurl_mime_part_filedata(lua_State *L){
364391
// part:headers(t)
365392
static int lcurl_mime_part_headers(lua_State *L){
366393
lcurl_mime_part_t *p = lcurl_getmimepart(L);
367-
struct curl_slist *list = lcurl_util_to_slist(L, 2);
394+
struct curl_slist *list;
368395
CURLcode ret;
369396

370-
luaL_argcheck(L, list, 2, "array expected");
397+
if(IS_FALSE(L, 2)){
398+
list = NULL;
399+
}
400+
else{
401+
list = lcurl_util_to_slist(L, 2);
402+
luaL_argcheck(L, list, 2, "array or nil expected");
403+
}
371404

372405
ret = curl_mime_headers(p->part, list, 1);
373406

@@ -383,9 +416,16 @@ static int lcurl_mime_part_headers(lua_State *L){
383416
// part:type(t)
384417
static int lcurl_mime_part_type(lua_State *L){
385418
lcurl_mime_part_t *p = lcurl_getmimepart(L);
386-
const char *mime_type = luaL_checkstring(L, 2);
419+
const char *mime_type;
387420
CURLcode ret;
388421

422+
if(IS_FALSE(L, 2)){
423+
mime_type = NULL;
424+
}
425+
else{
426+
mime_type = luaL_checkstring(L, 2);
427+
}
428+
389429
ret = curl_mime_type(p->part, mime_type);
390430

391431
if(ret != CURLE_OK){
@@ -399,9 +439,15 @@ static int lcurl_mime_part_type(lua_State *L){
399439
// part:name(t)
400440
static int lcurl_mime_part_name(lua_State *L){
401441
lcurl_mime_part_t *p = lcurl_getmimepart(L);
402-
const char *mime_name = luaL_checkstring(L, 2);
442+
const char *mime_name;
403443
CURLcode ret;
404444

445+
if(IS_FALSE(L, 2)){
446+
mime_name = NULL;
447+
}
448+
else{
449+
mime_name = luaL_checkstring(L, 2);
450+
}
405451
ret = curl_mime_name(p->part, mime_name);
406452

407453
if(ret != CURLE_OK){

test/test_mime.lua

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,38 @@ function test_data_name()
229229
assert_match('Content%-Disposition:.-name="test"', info)
230230
end
231231

232+
function test_data_should_not_unset_on_nil()
233+
local part = mime:addpart():data('hello', 'text/html', 'test', {
234+
'X-Custom-Header: hello'
235+
})
236+
local info = assert_string(dump_mime(mime))
237+
assert_match('\r\n\r\nhello', info)
238+
assert_match('Content%-Type:%s+text/html', info)
239+
assert_match('Content%-Disposition:.-name="test"', info)
240+
assert_match('X%-Custom%-Header:%s*hello', info)
241+
242+
part:data('world', nil, 'test2')
243+
info = assert_string(dump_mime(mime))
244+
assert_match('\r\n\r\nworld', info)
245+
assert_match('Content%-Type:%s+text/html', info)
246+
assert_match('Content%-Disposition:.-name="test2"', info)
247+
assert_match('X%-Custom%-Header:%s*hello', info)
248+
249+
part:data('!!!', 'text/xml', nil)
250+
info = assert_string(dump_mime(mime))
251+
assert_match('\r\n\r\n!!!', info)
252+
assert_match('Content%-Type:%s+text/xml', info)
253+
assert_match('Content%-Disposition:.-name="test2"', info)
254+
assert_match('X%-Custom%-Header:%s*hello', info)
255+
256+
part:data('!!!', 'text/xml', nil, nil)
257+
info = assert_string(dump_mime(mime))
258+
assert_match('\r\n\r\n!!!', info)
259+
assert_match('Content%-Type:%s+text/xml', info)
260+
assert_match('Content%-Disposition:.-name="test2"', info)
261+
assert_match('X%-Custom%-Header:%s*hello', info)
262+
end
263+
232264
function test_data_headers()
233265
mime:addpart():data('hello', {
234266
'X-Custom-Header: hello'
@@ -255,6 +287,101 @@ function test_unset_type()
255287
assert_not_match('Content%-Type:%s+text/html', info)
256288
end
257289

290+
function test_unset_headers()
291+
mime:addpart():data('hello', 'text/html',{
292+
'X-Custom-Header: hello'
293+
}):headers(false)
294+
295+
local info = assert_string(dump_mime(mime))
296+
assert_match('\r\n\r\nhello', info)
297+
assert_not_match('X%-Custom%-Header:%s*hello', info)
298+
end
299+
300+
function test_unset_data()
301+
mime:addpart():data('hello', 'text/html', 'test'):data(false)
302+
303+
local info = assert_string(dump_mime(mime))
304+
assert_not_match('\r\n\r\nhello', info)
305+
assert_match('Content%-Type:%s+text/html', info)
306+
assert_match('Content%-Disposition:.-name="test"', info)
307+
end
308+
309+
function test_unset_data_type_1()
310+
local part = mime:addpart():data('hello', 'text/html', 'test', {
311+
'X-Custom-Header: hello'
312+
}):data('hello', false)
313+
314+
local info = assert_string(dump_mime(mime))
315+
assert_match('\r\n\r\nhello', info)
316+
assert_not_match('Content%-Type:%s+text/html', info)
317+
assert_match('Content%-Disposition:.-name="test"', info)
318+
assert_match('X%-Custom%-Header:%s*hello', info)
319+
end
320+
321+
function test_unset_data_type_2()
322+
local part = mime:addpart():data('hello', 'text/html', 'test', {
323+
'X-Custom-Header: hello'
324+
}):data('hello', false, nil, nil)
325+
326+
local info = assert_string(dump_mime(mime))
327+
assert_match('\r\n\r\nhello', info)
328+
assert_not_match('Content%-Type:%s+text/html', info)
329+
assert_match('Content%-Disposition:.-name="test"', info)
330+
assert_match('X%-Custom%-Header:%s*hello', info)
331+
end
332+
333+
function test_unset_data_name_1()
334+
local part = mime:addpart():data('hello', 'text/html', 'test', {
335+
'X-Custom-Header: hello'
336+
}):data('hello', nil, false)
337+
338+
local info = assert_string(dump_mime(mime))
339+
assert_match('\r\n\r\nhello', info)
340+
assert_match('Content%-Type:%s+text/html', info)
341+
assert_not_match('Content%-Disposition:.-name="test"', info)
342+
assert_match('X%-Custom%-Header:%s*hello', info)
343+
end
344+
345+
function test_unset_data_name_2()
346+
local part = mime:addpart():data('hello', 'text/html', 'test', {
347+
'X-Custom-Header: hello'
348+
}):data('hello', nil, false, nil)
349+
350+
local info = assert_string(dump_mime(mime))
351+
assert_match('\r\n\r\nhello', info)
352+
assert_match('Content%-Type:%s+text/html', info)
353+
assert_not_match('Content%-Disposition:.-name="test"', info)
354+
assert_match('X%-Custom%-Header:%s*hello', info)
355+
end
356+
357+
function test_unset_data_header()
358+
local part = mime:addpart():data('hello', 'text/html', 'test', {
359+
'X-Custom-Header: hello'
360+
}):data('hello', nil, nil, false)
361+
362+
local info = assert_string(dump_mime(mime))
363+
assert_match('\r\n\r\nhello', info)
364+
assert_match('Content%-Type:%s+text/html', info)
365+
assert_match('Content%-Disposition:.-name="test"', info)
366+
assert_not_match('X%-Custom%-Header:%s*hello', info)
367+
end
368+
369+
function test_fail_pass_nil_as_first_arg()
370+
local part = mime:addpart()
371+
assert_error(function() part:data() end)
372+
assert_error(function() part:data(nil) end)
373+
374+
assert_error(function() part:name() end)
375+
assert_error(function() part:name(nil) end)
376+
377+
assert_error(function() part:type() end)
378+
assert_error(function() part:type(nil) end)
379+
380+
assert_error(function() part:headers() end)
381+
assert_error(function() part:headers(nil) end)
382+
end
383+
384+
258385
end
259386

260387
RUN()

0 commit comments

Comments
 (0)