Skip to content

Commit 12539b3

Browse files
committed
bugfix: the boundary string could not be parsed if no space was present before the boundary=xxx parameter in the Content-Type request header. thanks chenshu for reporting this issue in github openresty#2.
1 parent 0dfbb6f commit 12539b3

File tree

2 files changed

+132
-2
lines changed

2 files changed

+132
-2
lines changed

lib/resty/upload.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ local function get_boundary()
3838
return nil
3939
end
4040

41-
local m = match(header, ";%s+boundary=\"([^\"]+)\"")
41+
local m = match(header, ";%s*boundary=\"([^\"]+)\"")
4242
if m then
4343
return m
4444
end
4545

46-
return match(header, ";%s+boundary=([^\",;]+)")
46+
return match(header, ";%s*boundary=([^\",;]+)")
4747
end
4848

4949

t/sanity.t

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,3 +397,133 @@ read: ["eof"]
397397
--- no_error_log
398398
[error]
399399

400+
401+
402+
=== TEST 7: github issue #2: cannot parse boundary - no space before parameter (w/o quotes)
403+
--- http_config eval: $::HttpConfig
404+
--- config
405+
location /t {
406+
content_by_lua '
407+
local upload = require "resty.upload"
408+
local cjson = require "cjson"
409+
410+
local form, err = upload:new(5)
411+
if not form then
412+
ngx.say("cannot get form: ", err)
413+
return
414+
end
415+
416+
form:set_timeout(1000) -- 1 sec
417+
418+
while true do
419+
local typ, res, err = form:read()
420+
if not typ then
421+
ngx.say("failed to read: ", err)
422+
return
423+
end
424+
425+
ngx.say("read: ", cjson.encode({typ, res}))
426+
427+
if typ == "eof" then
428+
break
429+
end
430+
end
431+
432+
local typ, res, err = form:read()
433+
ngx.say("read: ", cjson.encode({typ, res}))
434+
';
435+
}
436+
--- more_headers
437+
Content-Type: multipart/form-data;boundary=---------------------------820127721219505131303151179
438+
--- request eval
439+
qq{POST /t\n-----------------------------820127721219505131303151179\r
440+
Content-Disposition: form-data; name="file1"; filename="a.txt"\r
441+
Content-Type: text/plain\r
442+
\r
443+
Hello, world\r\n-----------------------------820127721219505131303151179\r
444+
Content-Disposition: form-data; name="test"\r
445+
\r
446+
value\r
447+
\r\n-----------------------------820127721219505131303151179--\r
448+
}
449+
--- response_body
450+
read: ["header",["Content-Disposition","form-data; name=\"file1\"; filename=\"a.txt\"","Content-Disposition: form-data; name=\"file1\"; filename=\"a.txt\""]]
451+
read: ["header",["Content-Type","text\/plain","Content-Type: text\/plain"]]
452+
read: ["body","Hello"]
453+
read: ["body",", wor"]
454+
read: ["body","ld"]
455+
read: ["part_end"]
456+
read: ["header",["Content-Disposition","form-data; name=\"test\"","Content-Disposition: form-data; name=\"test\""]]
457+
read: ["body","value"]
458+
read: ["body","\r\n"]
459+
read: ["part_end"]
460+
read: ["eof"]
461+
read: ["eof"]
462+
--- no_error_log
463+
[error]
464+
465+
466+
467+
=== TEST 8: github issue #2: cannot parse boundary - no space before parameter (with quotes)
468+
--- http_config eval: $::HttpConfig
469+
--- config
470+
location /t {
471+
content_by_lua '
472+
local upload = require "resty.upload"
473+
local cjson = require "cjson"
474+
475+
local form, err = upload:new(5)
476+
if not form then
477+
ngx.say("cannot get form: ", err)
478+
return
479+
end
480+
481+
form:set_timeout(1000) -- 1 sec
482+
483+
while true do
484+
local typ, res, err = form:read()
485+
if not typ then
486+
ngx.say("failed to read: ", err)
487+
return
488+
end
489+
490+
ngx.say("read: ", cjson.encode({typ, res}))
491+
492+
if typ == "eof" then
493+
break
494+
end
495+
end
496+
497+
local typ, res, err = form:read()
498+
ngx.say("read: ", cjson.encode({typ, res}))
499+
';
500+
}
501+
--- more_headers
502+
Content-Type: multipart/form-data;boundary="---------------------------820127721219505131303151179"
503+
--- request eval
504+
qq{POST /t\n-----------------------------820127721219505131303151179\r
505+
Content-Disposition: form-data; name="file1"; filename="a.txt"\r
506+
Content-Type: text/plain\r
507+
\r
508+
Hello, world\r\n-----------------------------820127721219505131303151179\r
509+
Content-Disposition: form-data; name="test"\r
510+
\r
511+
value\r
512+
\r\n-----------------------------820127721219505131303151179--\r
513+
}
514+
--- response_body
515+
read: ["header",["Content-Disposition","form-data; name=\"file1\"; filename=\"a.txt\"","Content-Disposition: form-data; name=\"file1\"; filename=\"a.txt\""]]
516+
read: ["header",["Content-Type","text\/plain","Content-Type: text\/plain"]]
517+
read: ["body","Hello"]
518+
read: ["body",", wor"]
519+
read: ["body","ld"]
520+
read: ["part_end"]
521+
read: ["header",["Content-Disposition","form-data; name=\"test\"","Content-Disposition: form-data; name=\"test\""]]
522+
read: ["body","value"]
523+
read: ["body","\r\n"]
524+
read: ["part_end"]
525+
read: ["eof"]
526+
read: ["eof"]
527+
--- no_error_log
528+
[error]
529+

0 commit comments

Comments
 (0)