Skip to content
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

feat(pdk): add zero value to support unlimited body size #13431

Merged
merged 4 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelog/unreleased/kong/feat-pdk-unlimited-body-size.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
message: Added `0` to support unlimited body size. When parameter `max_allowed_file_size` is `0`, `get_raw_body` will return the entire body, but the size of this body will still be limited by Nginx's `client_max_body_size`.
ADD-SP marked this conversation as resolved.
Show resolved Hide resolved
type: feature
scope: PDK
10 changes: 6 additions & 4 deletions kong/pdk/request.lua
Original file line number Diff line number Diff line change
Expand Up @@ -695,13 +695,15 @@ 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 equal to 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,
-- 0 means unlimited, but the size of this body will still be limited
-- by Nginx's client_max_body_size.
-- @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 All @@ -721,7 +723,7 @@ local function new(self)
return ""
end

if not max_allowed_file_size then
if not max_allowed_file_size or max_allowed_file_size < 0 then
return nil, "request body did not fit into client body buffer, consider raising 'client_body_buffer_size'"
end

Expand All @@ -731,7 +733,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 equal to 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(0)
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]
Loading