-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
bugfix: read the request body from the temporary file if it was cached. #1863
Changes from 4 commits
75e7457
4da7c9e
e136755
9f8413d
48d52af
4caf565
6f62388
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 |
---|---|---|
|
@@ -117,10 +117,13 @@ local function run() | |
core.response.exit(404) | ||
end | ||
|
||
ngx.req.read_body() | ||
local req_body = ngx.req.get_body_data() | ||
|
||
local req_body = core.request.get_body() | ||
if req_body then | ||
if #req_body > 1024 * 1024 * 1.5 then | ||
core.log.error("maximum request body is 1.5mb, but got ", #req_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. IMHO, we can use MiB to indicate the size is 1024 base. |
||
core.response.exit(400, {error_msg = "request body is too large"}) | ||
end | ||
|
||
local data, err = core.json.decode(req_body) | ||
if not data then | ||
core.log.error("invalid request body: ", req_body, " err: ", err) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,8 +20,13 @@ local tonumber = tonumber | |
local error = error | ||
local type = type | ||
local str_fmt = string.format | ||
local io_open = io.open | ||
local req_read_body = ngx.req.read_body | ||
local req_get_body_data = ngx.req.get_body_data | ||
local req_get_body_file = ngx.req.get_body_file | ||
|
||
local _M = {version = 0.1} | ||
|
||
local _M = {} | ||
|
||
|
||
local function _headers(ctx) | ||
|
@@ -94,7 +99,36 @@ function _M.get_remote_client_port(ctx) | |
ctx = ngx.ctx.api_ctx | ||
end | ||
return tonumber(ctx.var.remote_port) | ||
end | ||
end | ||
|
||
|
||
local function get_file(file_name) | ||
local f = io_open(file_name, 'r') | ||
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. Better to check the second error returned value before using the |
||
if f then | ||
local req_body = f:read("*all") | ||
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 |
||
f:close() | ||
return req_body | ||
end | ||
|
||
return | ||
end | ||
|
||
|
||
function _M.get_body() | ||
req_read_body() | ||
|
||
local req_body = req_get_body_data() | ||
if req_body then | ||
return req_body | ||
end | ||
|
||
local file_name = req_get_body_file() | ||
if file_name then | ||
req_body = get_file(file_name) | ||
end | ||
|
||
return req_body | ||
end | ||
|
||
|
||
return _M |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,7 +47,7 @@ dependencies = { | |
"luafilesystem = 1.7.0-2", | ||
"lua-tinyyaml = 0.1", | ||
"lua-resty-prometheus = 1.1", | ||
"jsonschema = 0.8", | ||
"jsonschema = 0.9", | ||
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. should not modify this line |
||
"lua-resty-ipmatcher = 0.6", | ||
"lua-resty-kafka = 0.07", | ||
"lua-resty-logger-socket = 2.0-0", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -288,7 +288,7 @@ GET /t | |
GET /t | ||
--- error_code: 400 | ||
--- response_body | ||
{"error_msg":"invalid configuration: property \"upstream\" validation failed: property \"checks\" validation failed: property \"active\" validation failed: property \"healthy\" validation failed: property \"http_statuses\" validation failed: expected unique items but items 2 and 1 are equal"} | ||
{"error_msg":"invalid configuration: property \"upstream\" validation failed: property \"checks\" validation failed: property \"active\" validation failed: property \"healthy\" validation failed: property \"http_statuses\" validation failed: expected unique items but items 1 and 2 are equal"} | ||
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. Why did you change this test case? |
||
--- no_error_log | ||
[error] | ||
|
||
|
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.
We need to detect the size (use
stat
, for example) before reading it. It doesn't make sense to do the check if we have already read the very BIG file.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.
agreed.