Skip to content

Commit

Permalink
feat(pdk): support unlimited body size for `kong.request.get_raw_body…
Browse files Browse the repository at this point in the history
…()` (#13431)

This function will be used in EE.

KAG-4698
  • Loading branch information
Water-Melon authored Aug 2, 2024
1 parent 83fce6b commit 4a3dacf
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
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`.
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]

1 comment on commit 4a3dacf

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bazel Build

Docker image available kong/kong:4a3dacf7b0b32d3f12ebce1763b6bf192a8bd7f9
Artifacts available https://github.com/Kong/kong/actions/runs/10210912076

Please sign in to comment.