Skip to content

Commit 8fc15f5

Browse files
committed
feature: lf_line_break
1 parent 8f4f0f8 commit 8fc15f5

File tree

2 files changed

+94
-5
lines changed

2 files changed

+94
-5
lines changed

lib/resty/upload.lua

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,26 @@ local function get_boundary()
4545
return match(header, ";%s*boundary=([^\",;]+)")
4646
end
4747

48+
local function read_line_construct(sock, lf_line_break)
49+
local line_end = "\r\n"
50+
if lf_line_break then
51+
line_end = "\n"
52+
end
53+
local read_line, err = sock:receiveuntil(line_end)
54+
if lf_line_break and read_line then
55+
return function (arg)
56+
local ret, err_inner = read_line(arg)
57+
if ret and ret:sub(-1,-1) == '\r' then
58+
ret = ret:sub(1,-2)
59+
end
60+
return ret, err_inner
61+
end
62+
else
63+
return read_line, err
64+
end
65+
end
4866

49-
function _M.new(self, chunk_size, max_line_size)
67+
function _M.new(self, chunk_size, max_line_size, lf_line_break)
5068
local boundary = get_boundary()
5169

5270
-- print("boundary: ", boundary)
@@ -67,7 +85,7 @@ function _M.new(self, chunk_size, max_line_size)
6785
return nil, err
6886
end
6987

70-
local read_line, err = sock:receiveuntil("\r\n")
88+
local read_line, err = read_line_construct(sock, lf_line_break)
7189
if not read_line then
7290
return nil, err
7391
end
@@ -79,7 +97,8 @@ function _M.new(self, chunk_size, max_line_size)
7997
read2boundary = read2boundary,
8098
read_line = read_line,
8199
boundary = boundary,
82-
state = STATE_BEGIN
100+
state = STATE_BEGIN,
101+
lf_line_break = lf_line_break
83102
}, mt)
84103
end
85104

@@ -143,7 +162,17 @@ local function read_body_part(self)
143162
if not chunk then
144163
local sock = self.sock
145164

146-
local data = sock:receive(2)
165+
local data, err = sock:receive(1)
166+
167+
-- partial lookahead: -- or CRLF?
168+
if data == "-" or data == "\r" then
169+
local next
170+
next, err = sock:receive(1)
171+
if next then
172+
data = data .. next
173+
end
174+
end
175+
147176
if data == "--" then
148177
local ok, err = discard_rest(self)
149178
if not ok then
@@ -154,7 +183,7 @@ local function read_body_part(self)
154183
return "part_end"
155184
end
156185

157-
if data ~= "\r\n" then
186+
if data ~= "\r\n" and not (data == "\n" and self.lf_line_break) then
158187
local ok, err = discard_line(self)
159188
if not ok then
160189
return nil, nil, err

t/sanity.t

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,3 +648,63 @@ read: ["eof"]
648648
--- no_error_log
649649
[error]
650650

651+
652+
653+
=== TEST 11: LF line break
654+
--- http_config eval: $::HttpConfig
655+
--- config
656+
location /t {
657+
content_by_lua '
658+
local upload = require "resty.upload"
659+
local ljson = require "ljson"
660+
661+
local form = upload:new(5, nil, true)
662+
663+
form:set_timeout(1000) -- 1 sec
664+
665+
while true do
666+
local typ, res, err = form:read()
667+
if not typ then
668+
ngx.say("failed to read: ", err)
669+
return
670+
end
671+
672+
ngx.say("read: ", ljson.encode({typ, res}))
673+
674+
if typ == "eof" then
675+
break
676+
end
677+
end
678+
679+
local typ, res, err = form:read()
680+
ngx.say("read: ", ljson.encode({typ, res}))
681+
';
682+
}
683+
--- more_headers
684+
Content-Type: multipart/form-data; boundary=---------------------------820127721219505131303151179
685+
--- request eval
686+
qq{POST /t\n-----------------------------820127721219505131303151179\r
687+
Content-Disposition: form-data; name="file1"; filename="a.txt"\r
688+
Content-Type: text/plain\r
689+
\r
690+
Hello, world\n-----------------------------820127721219505131303151179
691+
Content-Disposition: form-data; name="test"\r
692+
\r
693+
value\r
694+
\r\n-----------------------------820127721219505131303151179--\r
695+
}
696+
--- response_body
697+
read: ["header",["Content-Disposition","form-data; name=\"file1\"; filename=\"a.txt\"","Content-Disposition: form-data; name=\"file1\"; filename=\"a.txt\""]]
698+
read: ["header",["Content-Type","text/plain","Content-Type: text/plain"]]
699+
read: ["body","Hello"]
700+
read: ["body",", wor"]
701+
read: ["body","ld"]
702+
read: ["part_end"]
703+
read: ["header",["Content-Disposition","form-data; name=\"test\"","Content-Disposition: form-data; name=\"test\""]]
704+
read: ["body","value"]
705+
read: ["body","\r\n"]
706+
read: ["part_end"]
707+
read: ["eof"]
708+
read: ["eof"]
709+
--- no_error_log
710+
[error]

0 commit comments

Comments
 (0)