From 74bfc1258d789c8d6ad8cee918824fa0c9351e8c Mon Sep 17 00:00:00 2001 From: dylan <97.dylan@gmail.com> Date: Thu, 29 Mar 2018 12:47:23 +0200 Subject: [PATCH 1/9] Added request body buffer regeneration --- lib/resty/upload.lua | 53 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/lib/resty/upload.lua b/lib/resty/upload.lua index 37da961..e17a363 100644 --- a/lib/resty/upload.lua +++ b/lib/resty/upload.lua @@ -7,6 +7,9 @@ local match = string.match local setmetatable = setmetatable local type = type local ngx_var = ngx.var +local ngx_init_body = ngx.req.init_body +local ngx_finish_body = ngx.req.finish_body +local ngx_append_body = ngx.req.append_body -- local print = print @@ -46,7 +49,7 @@ local function get_boundary() end -function _M.new(self, chunk_size, max_line_size) +function _M.new(self, chunk_size, max_line_size, restore_body_buffer) local boundary = get_boundary() -- print("boundary: ", boundary) @@ -62,13 +65,27 @@ function _M.new(self, chunk_size, max_line_size) return nil, err end + if restore_body_buffer then + ngx_init_body(chunk_size) + end + local read2boundary, err = sock:receiveuntil("--" .. boundary) if not read2boundary then + + if restore_body_buffer then + ngx_finish_body() + end + return nil, err end local read_line, err = sock:receiveuntil("\r\n") if not read_line then + + if restore_body_buffer then + ngx_finish_body() + end + return nil, err end @@ -76,6 +93,7 @@ function _M.new(self, chunk_size, max_line_size) sock = sock, size = chunk_size or CHUNK_SIZE, line_size = max_line_size or MAX_LINE_SIZE, + resore_body = restore_body_buffer or false, read2boundary = read2boundary, read_line = read_line, boundary = boundary, @@ -93,21 +111,37 @@ function _M.set_timeout(self, timeout) return sock:settimeout(timeout) end +local function append_body(self, data_chunk) + if self.resore_body then + ngx_append_body(data_chunk) + end +end + +local function finish_body(self) + if self.resore_body then + ngx_finish_body() + end +end local function discard_line(self) local read_line = self.read_line local line, err = read_line(self.line_size) if not line then + finish_body(self); return nil, err end + append_body(self, line .. "\r\n") + local dummy, err = read_line(1) if dummy then + finish_body(self) return nil, "line too long: " .. line .. dummy .. "..." end if err then + finish_body(self) return nil, err end @@ -122,6 +156,7 @@ local function discard_rest(self) while true do local dummy, err = sock:receive(size) if err and err ~= 'closed' then + finish_body(self) return nil, err end @@ -137,6 +172,7 @@ local function read_body_part(self) local chunk, err = read2boundary(self.size) if err then + finish_body(self) return nil, nil, err end @@ -145,6 +181,8 @@ local function read_body_part(self) local data = sock:receive(2) if data == "--" then + append_body(self, "--" .. self.boundary .. "--") + local ok, err = discard_rest(self) if not ok then return nil, nil, err @@ -161,10 +199,14 @@ local function read_body_part(self) end end + append_body(self, "--" .. self.boundary .. "\r\n") + self.state = STATE_READING_HEADER return "part_end" end + append_body(self, chunk) + return "body", chunk end @@ -174,15 +216,20 @@ local function read_header(self) local line, err = read_line(self.line_size) if err then + finish_body(self) return nil, nil, err end + append_body(self, line .. "\r\n") + local dummy, err = read_line(1) if dummy then + finish_body(self) return nil, nil, "line too long: " .. line .. dummy .. "..." end if err then + finish_body(self) return nil, nil, err end @@ -204,6 +251,7 @@ end local function eof() + finish_body(self) return "eof", nil end @@ -232,7 +280,10 @@ local function read_preamble(self) while true do local preamble = read2boundary(size) if not preamble then + append_body("--" .. self.boundary) break + else + append_body(preamble) end -- discard the preamble data chunk From 4ce90bbda609a35bce7470cb69f80759e26316b3 Mon Sep 17 00:00:00 2001 From: dylan <97.dylan@gmail.com> Date: Thu, 29 Mar 2018 16:18:04 +0200 Subject: [PATCH 2/9] Fixed missing \r\n's and first boundary --- lib/resty/upload.lua | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/resty/upload.lua b/lib/resty/upload.lua index e17a363..bc550ec 100644 --- a/lib/resty/upload.lua +++ b/lib/resty/upload.lua @@ -73,7 +73,7 @@ function _M.new(self, chunk_size, max_line_size, restore_body_buffer) if not read2boundary then if restore_body_buffer then - ngx_finish_body() + ngx_finish_body(self) end return nil, err @@ -83,7 +83,7 @@ function _M.new(self, chunk_size, max_line_size, restore_body_buffer) if not read_line then if restore_body_buffer then - ngx_finish_body() + ngx_finish_body(self) end return nil, err @@ -181,7 +181,7 @@ local function read_body_part(self) local data = sock:receive(2) if data == "--" then - append_body(self, "--" .. self.boundary .. "--") + append_body(self, "\r\n--" .. self.boundary .. "--\r\n") local ok, err = discard_rest(self) if not ok then @@ -199,7 +199,7 @@ local function read_body_part(self) end end - append_body(self, "--" .. self.boundary .. "\r\n") + append_body(self, "\r\n--" .. self.boundary .. "\r\n") self.state = STATE_READING_HEADER return "part_end" @@ -250,7 +250,7 @@ local function read_header(self) end -local function eof() +local function eof(self) finish_body(self) return "eof", nil end @@ -280,10 +280,10 @@ local function read_preamble(self) while true do local preamble = read2boundary(size) if not preamble then - append_body("--" .. self.boundary) + append_body(self, "--" .. self.boundary) break else - append_body(preamble) + append_body(self, preamble) end -- discard the preamble data chunk From ce8ddd86e560a6ac7188f803b5e69ce6994c9e1f Mon Sep 17 00:00:00 2001 From: dylan <97.dylan@gmail.com> Date: Mon, 2 Apr 2018 16:04:48 +0200 Subject: [PATCH 3/9] Fixed typo and removed unnecessary concatenation --- lib/resty/upload.lua | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/lib/resty/upload.lua b/lib/resty/upload.lua index bc550ec..c0bc602 100644 --- a/lib/resty/upload.lua +++ b/lib/resty/upload.lua @@ -93,7 +93,7 @@ function _M.new(self, chunk_size, max_line_size, restore_body_buffer) sock = sock, size = chunk_size or CHUNK_SIZE, line_size = max_line_size or MAX_LINE_SIZE, - resore_body = restore_body_buffer or false, + restore_body = restore_body_buffer or false, read2boundary = read2boundary, read_line = read_line, boundary = boundary, @@ -111,14 +111,16 @@ function _M.set_timeout(self, timeout) return sock:settimeout(timeout) end -local function append_body(self, data_chunk) - if self.resore_body then - ngx_append_body(data_chunk) +local function append_body(self, ...) + if self.restore_body then + for _, v in ipairs{...} do + ngx_append_body(v) + end end end local function finish_body(self) - if self.resore_body then + if self.restore_body then ngx_finish_body() end end @@ -128,11 +130,11 @@ local function discard_line(self) local line, err = read_line(self.line_size) if not line then - finish_body(self); + finish_body(self) return nil, err end - append_body(self, line .. "\r\n") + append_body(self, line, "\r\n") local dummy, err = read_line(1) if dummy then @@ -181,7 +183,7 @@ local function read_body_part(self) local data = sock:receive(2) if data == "--" then - append_body(self, "\r\n--" .. self.boundary .. "--\r\n") + append_body(self, "\r\n--", self.boundary, "--\r\n") local ok, err = discard_rest(self) if not ok then @@ -199,7 +201,7 @@ local function read_body_part(self) end end - append_body(self, "\r\n--" .. self.boundary .. "\r\n") + append_body(self, "\r\n--", self.boundary, "\r\n") self.state = STATE_READING_HEADER return "part_end" @@ -220,7 +222,7 @@ local function read_header(self) return nil, nil, err end - append_body(self, line .. "\r\n") + append_body(self, line, "\r\n") local dummy, err = read_line(1) if dummy then @@ -280,7 +282,7 @@ local function read_preamble(self) while true do local preamble = read2boundary(size) if not preamble then - append_body(self, "--" .. self.boundary) + append_body(self, "--", self.boundary) break else append_body(self, preamble) From f9c3a30da8daf4177970e400529cea5463b67d69 Mon Sep 17 00:00:00 2001 From: dylan <97.dylan@gmail.com> Date: Wed, 4 Apr 2018 22:08:05 +0200 Subject: [PATCH 4/9] Replaced multiple body append by a string buffer and single body append --- lib/resty/upload.lua | 92 ++++++++++++++++++++++++++++++-------------- 1 file changed, 63 insertions(+), 29 deletions(-) diff --git a/lib/resty/upload.lua b/lib/resty/upload.lua index c0bc602..a899811 100644 --- a/lib/resty/upload.lua +++ b/lib/resty/upload.lua @@ -73,7 +73,7 @@ function _M.new(self, chunk_size, max_line_size, restore_body_buffer) if not read2boundary then if restore_body_buffer then - ngx_finish_body(self) + ngx_finish_body() end return nil, err @@ -83,7 +83,7 @@ function _M.new(self, chunk_size, max_line_size, restore_body_buffer) if not read_line then if restore_body_buffer then - ngx_finish_body(self) + ngx_finish_body() end return nil, err @@ -93,7 +93,8 @@ function _M.new(self, chunk_size, max_line_size, restore_body_buffer) sock = sock, size = chunk_size or CHUNK_SIZE, line_size = max_line_size or MAX_LINE_SIZE, - restore_body = restore_body_buffer or false, + restore_body_buffer = restore_body_buffer or false, + body_buffer = {""}, read2boundary = read2boundary, read_line = read_line, boundary = boundary, @@ -111,18 +112,19 @@ function _M.set_timeout(self, timeout) return sock:settimeout(timeout) end -local function append_body(self, ...) - if self.restore_body then - for _, v in ipairs{...} do - ngx_append_body(v) - end +local function append_body_buffer(self, line) + table.insert(self.body_buffer, line) -- push 's' into the the buffer + for i=table.getn(self.body_buffer)-1, 1, -1 do + if string.len(self.body_buffer[i]) > string.len(self.body_buffer[i+1]) then + break + end + self.body_buffer[i] = self.body_buffer[i] .. table.remove(self.body_buffer) end end -local function finish_body(self) - if self.restore_body then - ngx_finish_body() - end +local function restore_body(self) + ngx_append_body(table.concat(self.body_buffer, "")) + ngx_finish_body() end local function discard_line(self) @@ -130,20 +132,28 @@ local function discard_line(self) local line, err = read_line(self.line_size) if not line then - finish_body(self) + if self.restore_body_buffer then + restore_body(self) + end return nil, err end - append_body(self, line, "\r\n") + if self.restore_body_buffer then + append_body_buffer(self, line .. "\r\n") + end local dummy, err = read_line(1) if dummy then - finish_body(self) + if self.restore_body_buffer then + restore_body(self) + end return nil, "line too long: " .. line .. dummy .. "..." end if err then - finish_body(self) + if self.restore_body_buffer then + restore_body(self) + end return nil, err end @@ -158,7 +168,9 @@ local function discard_rest(self) while true do local dummy, err = sock:receive(size) if err and err ~= 'closed' then - finish_body(self) + if self.restore_body_buffer then + restore_body(self) + end return nil, err end @@ -174,7 +186,9 @@ local function read_body_part(self) local chunk, err = read2boundary(self.size) if err then - finish_body(self) + if self.restore_body_buffer then + restore_body(self) + end return nil, nil, err end @@ -183,7 +197,9 @@ local function read_body_part(self) local data = sock:receive(2) if data == "--" then - append_body(self, "\r\n--", self.boundary, "--\r\n") + if self.restore_body_buffer then + append_body_buffer(self, "\r\n--" .. self.boundary .. "--\r\n") + end local ok, err = discard_rest(self) if not ok then @@ -201,13 +217,17 @@ local function read_body_part(self) end end - append_body(self, "\r\n--", self.boundary, "\r\n") - + if self.restore_body_buffer then + append_body_buffer(self, "\r\n--" .. self.boundary .. "\r\n") + end + self.state = STATE_READING_HEADER return "part_end" end - append_body(self, chunk) + if self.restore_body_buffer then + append_body_buffer(self, chunk) + end return "body", chunk end @@ -218,20 +238,28 @@ local function read_header(self) local line, err = read_line(self.line_size) if err then - finish_body(self) + if self.restore_body_buffer then + restore_body(self) + end return nil, nil, err end - append_body(self, line, "\r\n") + if self.restore_body_buffer then + append_body_buffer(self, line .. "\r\n") + end local dummy, err = read_line(1) if dummy then - finish_body(self) + if self.restore_body_buffer then + restore_body(self) + end return nil, nil, "line too long: " .. line .. dummy .. "..." end if err then - finish_body(self) + if self.restore_body_buffer then + restore_body(self) + end return nil, nil, err end @@ -253,7 +281,9 @@ end local function eof(self) - finish_body(self) + if self.restore_body_buffer then + restore_body(self) + end return "eof", nil end @@ -282,10 +312,14 @@ local function read_preamble(self) while true do local preamble = read2boundary(size) if not preamble then - append_body(self, "--", self.boundary) + if self.restore_body_buffer then + append_body_buffer(self, "--" .. self.boundary) + end break else - append_body(self, preamble) + if self.restore_body_buffer then + append_body_buffer(self, preamble) + end end -- discard the preamble data chunk From ef5cb461df6a2f2707a301ce56bf5939ef18fd49 Mon Sep 17 00:00:00 2001 From: dylan <97.dylan@gmail.com> Date: Sat, 7 Apr 2018 16:22:13 +0200 Subject: [PATCH 5/9] Cleaned up unnecessary code --- lib/resty/upload.lua | 47 +++++++++++++++----------------------------- 1 file changed, 16 insertions(+), 31 deletions(-) diff --git a/lib/resty/upload.lua b/lib/resty/upload.lua index a899811..4a760e0 100644 --- a/lib/resty/upload.lua +++ b/lib/resty/upload.lua @@ -112,47 +112,32 @@ function _M.set_timeout(self, timeout) return sock:settimeout(timeout) end -local function append_body_buffer(self, line) - table.insert(self.body_buffer, line) -- push 's' into the the buffer - for i=table.getn(self.body_buffer)-1, 1, -1 do - if string.len(self.body_buffer[i]) > string.len(self.body_buffer[i+1]) then - break - end - self.body_buffer[i] = self.body_buffer[i] .. table.remove(self.body_buffer) - end -end - -local function restore_body(self) - ngx_append_body(table.concat(self.body_buffer, "")) - ngx_finish_body() -end - local function discard_line(self) local read_line = self.read_line local line, err = read_line(self.line_size) if not line then if self.restore_body_buffer then - restore_body(self) + ngx_finish_body() end return nil, err end if self.restore_body_buffer then - append_body_buffer(self, line .. "\r\n") + ngx_append_body(line .. "\r\n") end local dummy, err = read_line(1) if dummy then if self.restore_body_buffer then - restore_body(self) + ngx_finish_body() end return nil, "line too long: " .. line .. dummy .. "..." end if err then if self.restore_body_buffer then - restore_body(self) + ngx_finish_body() end return nil, err end @@ -169,7 +154,7 @@ local function discard_rest(self) local dummy, err = sock:receive(size) if err and err ~= 'closed' then if self.restore_body_buffer then - restore_body(self) + ngx_finish_body() end return nil, err end @@ -187,7 +172,7 @@ local function read_body_part(self) local chunk, err = read2boundary(self.size) if err then if self.restore_body_buffer then - restore_body(self) + ngx_finish_body() end return nil, nil, err end @@ -198,7 +183,7 @@ local function read_body_part(self) local data = sock:receive(2) if data == "--" then if self.restore_body_buffer then - append_body_buffer(self, "\r\n--" .. self.boundary .. "--\r\n") + ngx_append_body("\r\n--" .. self.boundary .. "--\r\n") end local ok, err = discard_rest(self) @@ -218,7 +203,7 @@ local function read_body_part(self) end if self.restore_body_buffer then - append_body_buffer(self, "\r\n--" .. self.boundary .. "\r\n") + ngx_append_body("\r\n--" .. self.boundary .. "\r\n") end self.state = STATE_READING_HEADER @@ -226,7 +211,7 @@ local function read_body_part(self) end if self.restore_body_buffer then - append_body_buffer(self, chunk) + ngx_append_body(chunk) end return "body", chunk @@ -239,26 +224,26 @@ local function read_header(self) local line, err = read_line(self.line_size) if err then if self.restore_body_buffer then - restore_body(self) + ngx_finish_body() end return nil, nil, err end if self.restore_body_buffer then - append_body_buffer(self, line .. "\r\n") + ngx_append_body(line .. "\r\n") end local dummy, err = read_line(1) if dummy then if self.restore_body_buffer then - restore_body(self) + ngx_finish_body() end return nil, nil, "line too long: " .. line .. dummy .. "..." end if err then if self.restore_body_buffer then - restore_body(self) + ngx_finish_body() end return nil, nil, err end @@ -282,7 +267,7 @@ end local function eof(self) if self.restore_body_buffer then - restore_body(self) + ngx_finish_body() end return "eof", nil end @@ -313,12 +298,12 @@ local function read_preamble(self) local preamble = read2boundary(size) if not preamble then if self.restore_body_buffer then - append_body_buffer(self, "--" .. self.boundary) + ngx_append_body("--" .. self.boundary) end break else if self.restore_body_buffer then - append_body_buffer(self, preamble) + ngx_append_body(preamble) end end From 458fb3a8c5bf5ff7a4e5b7944d3881ec3d8f6eff Mon Sep 17 00:00:00 2001 From: dylan <97.dylan@gmail.com> Date: Sat, 7 Apr 2018 16:28:12 +0200 Subject: [PATCH 6/9] Removed body_buffer variable --- lib/resty/upload.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/resty/upload.lua b/lib/resty/upload.lua index 4a760e0..31d16b2 100644 --- a/lib/resty/upload.lua +++ b/lib/resty/upload.lua @@ -94,7 +94,6 @@ function _M.new(self, chunk_size, max_line_size, restore_body_buffer) size = chunk_size or CHUNK_SIZE, line_size = max_line_size or MAX_LINE_SIZE, restore_body_buffer = restore_body_buffer or false, - body_buffer = {""}, read2boundary = read2boundary, read_line = read_line, boundary = boundary, From 79836a085cf186e27dbf4bd3c05b58de72b5f711 Mon Sep 17 00:00:00 2001 From: dylan <97.dylan@gmail.com> Date: Sat, 28 Apr 2018 20:13:41 +0200 Subject: [PATCH 7/9] Made code standards changes --- lib/resty/upload.lua | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/lib/resty/upload.lua b/lib/resty/upload.lua index 31d16b2..c60523d 100644 --- a/lib/resty/upload.lua +++ b/lib/resty/upload.lua @@ -71,7 +71,6 @@ function _M.new(self, chunk_size, max_line_size, restore_body_buffer) local read2boundary, err = sock:receiveuntil("--" .. boundary) if not read2boundary then - if restore_body_buffer then ngx_finish_body() end @@ -93,7 +92,7 @@ function _M.new(self, chunk_size, max_line_size, restore_body_buffer) sock = sock, size = chunk_size or CHUNK_SIZE, line_size = max_line_size or MAX_LINE_SIZE, - restore_body_buffer = restore_body_buffer or false, + restore_body_buffer = restore_body_buffer, read2boundary = read2boundary, read_line = read_line, boundary = boundary, @@ -111,6 +110,7 @@ function _M.set_timeout(self, timeout) return sock:settimeout(timeout) end + local function discard_line(self) local read_line = self.read_line @@ -119,6 +119,7 @@ local function discard_line(self) if self.restore_body_buffer then ngx_finish_body() end + return nil, err end @@ -131,6 +132,7 @@ local function discard_line(self) if self.restore_body_buffer then ngx_finish_body() end + return nil, "line too long: " .. line .. dummy .. "..." end @@ -155,6 +157,7 @@ local function discard_rest(self) if self.restore_body_buffer then ngx_finish_body() end + return nil, err end @@ -173,6 +176,7 @@ local function read_body_part(self) if self.restore_body_buffer then ngx_finish_body() end + return nil, nil, err end @@ -225,6 +229,7 @@ local function read_header(self) if self.restore_body_buffer then ngx_finish_body() end + return nil, nil, err end @@ -237,6 +242,7 @@ local function read_header(self) if self.restore_body_buffer then ngx_finish_body() end + return nil, nil, "line too long: " .. line .. dummy .. "..." end @@ -244,6 +250,7 @@ local function read_header(self) if self.restore_body_buffer then ngx_finish_body() end + return nil, nil, err end @@ -268,6 +275,7 @@ local function eof(self) if self.restore_body_buffer then ngx_finish_body() end + return "eof", nil end @@ -299,9 +307,10 @@ local function read_preamble(self) if self.restore_body_buffer then ngx_append_body("--" .. self.boundary) end + break - else - if self.restore_body_buffer then + + elseif self.restore_body_buffer then ngx_append_body(preamble) end end From 873f235bed3702407894d51dfb297f82a240a1f7 Mon Sep 17 00:00:00 2001 From: dylan <97.dylan@gmail.com> Date: Sat, 28 Apr 2018 20:36:52 +0200 Subject: [PATCH 8/9] Changed elseif to else if --- lib/resty/upload.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/resty/upload.lua b/lib/resty/upload.lua index c60523d..5b67a22 100644 --- a/lib/resty/upload.lua +++ b/lib/resty/upload.lua @@ -310,7 +310,7 @@ local function read_preamble(self) break - elseif self.restore_body_buffer then + else if self.restore_body_buffer then ngx_append_body(preamble) end end From 69dea873aa40f1b1bc709ba1d75607e9373f85de Mon Sep 17 00:00:00 2001 From: Suika <872957+suikabreaker@users.noreply.github.com> Date: Tue, 11 Jan 2022 16:49:25 +0800 Subject: [PATCH 9/9] fix #61: optional lf line break compatibility --- lib/resty/upload.lua | 97 +++++++++++++++++++++++++++++++------------- 1 file changed, 69 insertions(+), 28 deletions(-) diff --git a/lib/resty/upload.lua b/lib/resty/upload.lua index 5b67a22..354a0d2 100644 --- a/lib/resty/upload.lua +++ b/lib/resty/upload.lua @@ -49,7 +49,7 @@ local function get_boundary() end -function _M.new(self, chunk_size, max_line_size, restore_body_buffer) +function _M.new(self, chunk_size, max_line_size, restore_body_buffer, lf_line_break) local boundary = get_boundary() -- print("boundary: ", boundary) @@ -57,7 +57,6 @@ function _M.new(self, chunk_size, max_line_size, restore_body_buffer) if not boundary then return nil, "no boundary defined in Content-Type" end - -- print('boundary: "', boundary, '"') local sock, err = req_socket() @@ -69,7 +68,8 @@ function _M.new(self, chunk_size, max_line_size, restore_body_buffer) ngx_init_body(chunk_size) end - local read2boundary, err = sock:receiveuntil("--" .. boundary) + boundary = "--" .. boundary + local read2boundary, err = sock:receiveuntil(boundary) if not read2boundary then if restore_body_buffer then ngx_finish_body() @@ -78,7 +78,16 @@ function _M.new(self, chunk_size, max_line_size, restore_body_buffer) return nil, err end - local read_line, err = sock:receiveuntil("\r\n") + -- note that it matters when restore_body_buffer + -- because we shuold not change body length + local line_break + if lf_line_break then + line_break = "\n" + else + line_break = "\r\n" + end + + local read_line, err = sock:receiveuntil(line_break) if not read_line then if restore_body_buffer then @@ -96,7 +105,8 @@ function _M.new(self, chunk_size, max_line_size, restore_body_buffer) read2boundary = read2boundary, read_line = read_line, boundary = boundary, - state = STATE_BEGIN + state = STATE_BEGIN, + line_break = line_break }, mt) end @@ -124,7 +134,8 @@ local function discard_line(self) end if self.restore_body_buffer then - ngx_append_body(line .. "\r\n") + ngx_append_body(line) + ngx_append_body(self.line_break) end local dummy, err = read_line(1) @@ -161,6 +172,10 @@ local function discard_rest(self) return nil, err end + if self.restore_body_buffer then + ngx_append_body(dummy) + end + if not dummy then return 1 end @@ -180,15 +195,49 @@ local function read_body_part(self) return nil, nil, err end + -- everything OK we got another body chunk + if chunk then + if self.restore_body_buffer then + ngx_append_body(chunk) + ngx_append_body(self.boundary) + end + + if self.boundary:sub(1,1) == '\n' and chunk:sub(-1,-1) == '\r' then + chunk = chunk:sub(1,-2) + end + + return "body", chunk + end + + -- boundary not found, maybe end of body if not chunk then local sock = self.sock + local data = sock:receive(1) - local data = sock:receive(2) - if data == "--" then + -- neither -- or line_break, something's wrong + if data ~= "-" and data ~= self.line_break:sub(1,1) then if self.restore_body_buffer then - ngx_append_body("\r\n--" .. self.boundary .. "--\r\n") + ngx_append_body(data) end + local ok, err = discard_line(self) + if not ok then + return nil, nil, err + end + end + if data ~= self.line_break then + local next = sock:receive(1) + data = data .. next + end + + if self.restore_body_buffer then + ngx_append_body(data) + end + + if data == self.line_break then + self.state = STATE_READING_HEADER + return "part_end" + elseif data == "--" then local ok, err = discard_rest(self) if not ok then return nil, nil, err @@ -196,28 +245,15 @@ local function read_body_part(self) self.state = STATE_EOF return "part_end" - end - - if data ~= "\r\n" then + else + -- something's wrong local ok, err = discard_line(self) if not ok then return nil, nil, err end end - - if self.restore_body_buffer then - ngx_append_body("\r\n--" .. self.boundary .. "\r\n") - end - - self.state = STATE_READING_HEADER - return "part_end" - end - - if self.restore_body_buffer then - ngx_append_body(chunk) end - return "body", chunk end @@ -234,7 +270,8 @@ local function read_header(self) end if self.restore_body_buffer then - ngx_append_body(line .. "\r\n") + ngx_append_body(line) + ngx_append_body(self.line_break) end local dummy, err = read_line(1) @@ -254,6 +291,9 @@ local function read_header(self) return nil, nil, err end + if self.line_break == "\n" and line:sub(-1,-1) == '\r' then + line = line:sub(1,-2) + end -- print("read line: ", line) if line == "" then @@ -305,9 +345,9 @@ local function read_preamble(self) local preamble = read2boundary(size) if not preamble then if self.restore_body_buffer then - ngx_append_body("--" .. self.boundary) + ngx_append_body(self.boundary) end - + break else if self.restore_body_buffer then @@ -324,7 +364,8 @@ local function read_preamble(self) return nil, nil, err end - local read2boundary, err = sock:receiveuntil("\r\n--" .. self.boundary) + self.boundary = self.line_break .. self.boundary + local read2boundary, err = sock:receiveuntil(self.boundary) if not read2boundary then return nil, nil, err end