-
Notifications
You must be signed in to change notification settings - Fork 113
Added request body buffer regeneration #43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
74bfc12
4ce90bb
ce8ddd8
f9c3a30
ef5cb46
458fb3a
79836a0
873f235
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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,20 +65,34 @@ 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 | ||
|
||
return setmetatable({ | ||
sock = sock, | ||
size = chunk_size or CHUNK_SIZE, | ||
line_size = max_line_size or MAX_LINE_SIZE, | ||
restore_body_buffer = restore_body_buffer, | ||
read2boundary = read2boundary, | ||
read_line = read_line, | ||
boundary = boundary, | ||
|
@@ -99,15 +116,30 @@ local function discard_line(self) | |
|
||
local line, err = read_line(self.line_size) | ||
if not line then | ||
if self.restore_body_buffer then | ||
ngx_finish_body() | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. new line after this. |
||
|
||
return nil, err | ||
end | ||
|
||
if self.restore_body_buffer then | ||
ngx_append_body(line .. "\r\n") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is fine, but you could also have: ngx_append_body(line)
ngx_append_body("\r\n") There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see any point in not concatenating the string here, the same goes for the rest of the remarks regarding concatenation. |
||
end | ||
|
||
local dummy, err = read_line(1) | ||
if dummy then | ||
if self.restore_body_buffer then | ||
ngx_finish_body() | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. new line after this. |
||
|
||
return nil, "line too long: " .. line .. dummy .. "..." | ||
end | ||
|
||
if err then | ||
if self.restore_body_buffer then | ||
ngx_finish_body() | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. new line after this. |
||
return nil, err | ||
end | ||
|
||
|
@@ -122,6 +154,10 @@ local function discard_rest(self) | |
while true do | ||
local dummy, err = sock:receive(size) | ||
if err and err ~= 'closed' then | ||
if self.restore_body_buffer then | ||
ngx_finish_body() | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. new line after this. |
||
|
||
return nil, err | ||
end | ||
|
||
|
@@ -137,6 +173,10 @@ local function read_body_part(self) | |
|
||
local chunk, err = read2boundary(self.size) | ||
if err then | ||
if self.restore_body_buffer then | ||
ngx_finish_body() | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. new line after this. |
||
|
||
return nil, nil, err | ||
end | ||
|
||
|
@@ -145,6 +185,10 @@ local function read_body_part(self) | |
|
||
local data = sock:receive(2) | ||
if data == "--" then | ||
if self.restore_body_buffer then | ||
ngx_append_body("\r\n--" .. self.boundary .. "--\r\n") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here: ngx_append_body("\r\n--")
ngx_append_body(self.boundary)
ngx_append_body(ngx_append_body("--\r\n")) Not a big deal. |
||
end | ||
|
||
local ok, err = discard_rest(self) | ||
if not ok then | ||
return nil, nil, err | ||
|
@@ -161,10 +205,18 @@ local function read_body_part(self) | |
end | ||
end | ||
|
||
if self.restore_body_buffer then | ||
ngx_append_body("\r\n--" .. self.boundary .. "\r\n") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This coud be cached before the while loop: local boundary_delimiter = "\r\n--" .. self.boundary .. "\r\n" And then here: ngx_append_body(boundary_delimiter) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no while loop surrounding this line |
||
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 | ||
|
||
|
@@ -174,15 +226,31 @@ local function read_header(self) | |
|
||
local line, err = read_line(self.line_size) | ||
if err then | ||
if self.restore_body_buffer then | ||
ngx_finish_body() | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. new line after this. |
||
|
||
return nil, nil, err | ||
end | ||
|
||
if self.restore_body_buffer then | ||
ngx_append_body(line .. "\r\n") | ||
end | ||
|
||
local dummy, err = read_line(1) | ||
if dummy then | ||
if self.restore_body_buffer then | ||
ngx_finish_body() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we also need to restore that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The dummy is there to detect if the line we are trying to read exceeded the size of self.line_size. When this happens the request can't be processed further. This means that the remainder of the request will not be restored anyway. So there is no point is appending the one byte. |
||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. new line after this. |
||
|
||
return nil, nil, "line too long: " .. line .. dummy .. "..." | ||
end | ||
|
||
if err then | ||
if self.restore_body_buffer then | ||
ngx_finish_body() | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. new line after this. |
||
|
||
return nil, nil, err | ||
end | ||
|
||
|
@@ -203,7 +271,11 @@ local function read_header(self) | |
end | ||
|
||
|
||
local function eof() | ||
local function eof(self) | ||
if self.restore_body_buffer then | ||
ngx_finish_body() | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. new line after this. |
||
|
||
return "eof", nil | ||
end | ||
|
||
|
@@ -232,7 +304,15 @@ local function read_preamble(self) | |
while true do | ||
local preamble = read2boundary(size) | ||
if not preamble then | ||
if self.restore_body_buffer then | ||
ngx_append_body("--" .. self.boundary) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto here: ngx_append_body("--")
ngx_append_body(self.boundary) Yes, bikeshedding :-). |
||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. new line after this. |
||
|
||
break | ||
|
||
else if self.restore_body_buffer then | ||
ngx_append_body(preamble) | ||
end | ||
end | ||
|
||
-- discard the preamble data chunk | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The previous read, on line 72, was successful, but we didn't write the request body back, is this intended? Maybe there are many similar cases in code, but no easy way to solve? E.g. on errors we don't write the request body back.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The sock:receiveuntil returns an iterator Lua function and doesn't actually read data from the buffer so there is noting to restore.