Skip to content

Commit a93965c

Browse files
Soojin Namagentzh
authored andcommitted
feature: new() now accepts an optional 2nd arg to configure the max line size.
1 parent 2a3e572 commit a93965c

File tree

2 files changed

+64
-4
lines changed

2 files changed

+64
-4
lines changed

lib/resty/upload.lua

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ local ngx_var = ngx.var
1313
local _M = { _VERSION = '0.09' }
1414

1515

16+
local CHUNK_SIZE = 4096
1617
local MAX_LINE_SIZE = 512
1718

1819
local STATE_BEGIN = 1
@@ -45,7 +46,7 @@ local function get_boundary()
4546
end
4647

4748

48-
function _M.new(self, chunk_size)
49+
function _M.new(self, chunk_size, max_line_size)
4950
local boundary = get_boundary()
5051

5152
-- print("boundary: ", boundary)
@@ -73,7 +74,8 @@ function _M.new(self, chunk_size)
7374

7475
return setmetatable({
7576
sock = sock,
76-
size = chunk_size or 4096,
77+
size = chunk_size or CHUNK_SIZE,
78+
line_size = max_line_size or MAX_LINE_SIZE,
7779
read2boundary = read2boundary,
7880
read_line = read_line,
7981
boundary = boundary,
@@ -95,7 +97,7 @@ end
9597
local function discard_line(self)
9698
local read_line = self.read_line
9799

98-
local line, err = read_line(MAX_LINE_SIZE)
100+
local line, err = read_line(self.line_size)
99101
if not line then
100102
return nil, err
101103
end
@@ -170,7 +172,7 @@ end
170172
local function read_header(self)
171173
local read_line = self.read_line
172174

173-
local line, err = read_line(MAX_LINE_SIZE)
175+
local line, err = read_line(self.line_size)
174176
if err then
175177
return nil, nil, err
176178
end

t/sanity.t

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,3 +590,61 @@ read: ["eof"]
590590
--- no_error_log
591591
[error]
592592

593+
594+
595+
=== TEST 10: long in-part header line
596+
--- http_config eval: $::HttpConfig
597+
--- config
598+
location /t {
599+
content_by_lua '
600+
local upload = require "resty.upload"
601+
local cjson = require "cjson"
602+
603+
local form = upload:new(5, 1024) -- max_line_size = 1024
604+
605+
form:set_timeout(1000) -- 1 sec
606+
607+
while true do
608+
local typ, res, err = form:read()
609+
if not typ then
610+
ngx.say("failed to read: ", err)
611+
return
612+
end
613+
614+
ngx.say("read: ", cjson.encode({typ, res}))
615+
616+
if typ == "eof" then
617+
break
618+
end
619+
end
620+
621+
local typ, res, err = form:read()
622+
ngx.say("read: ", cjson.encode({typ, res}))
623+
';
624+
}
625+
--- more_headers
626+
Content-Type: multipart/form-data; boundary=---------------------------820127721219505131303151179
627+
--- request eval
628+
qq{POST /t\n-----------------------------820127721219505131303151179\r
629+
Content-Disposition: form-data; name="file1"; filename="a.txt"\r
630+
Content-Type: text/plain\r
631+
} . ("Hello, world" x 64) . qq{\r\n-----------------------------820127721219505131303151179\r
632+
Content-Disposition: form-data; name="test"\r
633+
\r
634+
value\r
635+
\r\n-----------------------------820127721219505131303151179--\r
636+
}
637+
--- response_body
638+
read: ["header",["Content-Disposition","form-data; name=\"file1\"; filename=\"a.txt\"","Content-Disposition: form-data; name=\"file1\"; filename=\"a.txt\""]]
639+
read: ["header",["Content-Type","text\/plain","Content-Type: text\/plain"]]
640+
read: ["header","Hello, worldHello, worldHello, worldHello, worldHello, worldHello, worldHello, worldHello, worldHello, worldHello, worldHello, worldHello, worldHello, worldHello, worldHello, worldHello, worldHello, worldHello, worldHello, worldHello, worldHello, worldHello, worldHello, worldHello, worldHello, worldHello, worldHello, worldHello, worldHello, worldHello, worldHello, worldHello, worldHello, worldHello, worldHello, worldHello, worldHello, worldHello, worldHello, worldHello, worldHello, worldHello, worldHello, worldHello, worldHello, worldHello, worldHello, worldHello, worldHello, worldHello, worldHello, worldHello, worldHello, worldHello, worldHello, worldHello, worldHello, worldHello, worldHello, worldHello, worldHello, worldHello, worldHello, worldHello, world"]
641+
read: ["header","-----------------------------820127721219505131303151179"]
642+
read: ["header",["Content-Disposition","form-data; name=\"test\"","Content-Disposition: form-data; name=\"test\""]]
643+
read: ["body","value"]
644+
read: ["body","\r\n"]
645+
read: ["part_end"]
646+
read: ["eof"]
647+
read: ["eof"]
648+
--- no_error_log
649+
[error]
650+

0 commit comments

Comments
 (0)