Skip to content

Commit

Permalink
feat(pdk): add negative value to support unlimited body size
Browse files Browse the repository at this point in the history
It is the cherry-pick of #9472.

KAG-4698
  • Loading branch information
Water-Melon committed Jul 29, 2024
1 parent 61e2c76 commit 7d7d4e4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
7 changes: 4 additions & 3 deletions kong/pdk/request.lua
Original file line number Diff line number Diff line change
Expand Up @@ -695,13 +695,14 @@ local function new(self)
-- If the size of the body is greater than the Nginx buffer size (set by
-- `client_body_buffer_size`), this function fails and returns an error
-- message explaining this limitation, unless `max_allowed_file_size`
-- is set and larger than the body size buffered to disk.
-- is set and less than 0 or larger than the body size buffered to disk.
-- Use of `max_allowed_file_size` requires Kong to read data from filesystem
-- and has performance implications.
--
-- @function kong.request.get_raw_body
-- @phases rewrite, access, response, admin_api
-- @max_allowed_file_size[opt] number the max allowed file size to be read from
-- @max_allowed_file_size[opt] number the max allowed file size to be read from,
-- less than zero mean no limit.
-- @treturn string|nil The plain request body or nil if it does not fit into
-- the NGINX temporary buffer.
-- @treturn nil|string An error message.
Expand Down Expand Up @@ -731,7 +732,7 @@ local function new(self)
end

local size = file:seek("end") or 0
if size > max_allowed_file_size then
if max_allowed_file_size >= 0 and size > max_allowed_file_size then
return nil, ("request body file too big: %d > %d"):format(size, max_allowed_file_size)
end

Expand Down
28 changes: 27 additions & 1 deletion t/01-pdk/04-request/15-get_raw_body.t
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,30 @@ body length: 20000
--- response_body
body err: request body file too big: 20000 > 19999
--- no_error_log
[error]
[error]
=== TEST 8: request.get_raw_body() returns correctly if max_allowed_file_size is less than 0
--- http_config eval: $t::Util::HttpConfig
--- config
location = /t {
access_by_lua_block {
local PDK = require "kong.pdk"
local pdk = PDK.new()
local body, err = pdk.request.get_raw_body(-1)
if body then
ngx.say("body length: ", #body)
else
ngx.say("body err: ", err)
end
}
}
--- request eval
"GET /t\r\n" . ("a" x 20000)
--- response_body
body length: 20000
--- no_error_log
[error]

0 comments on commit 7d7d4e4

Please sign in to comment.