diff --git a/changelog/unreleased/kong/feat-pdk-unlimited-body-size.yml b/changelog/unreleased/kong/feat-pdk-unlimited-body-size.yml new file mode 100644 index 000000000000..5cb3f291d15f --- /dev/null +++ b/changelog/unreleased/kong/feat-pdk-unlimited-body-size.yml @@ -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 diff --git a/kong/pdk/request.lua b/kong/pdk/request.lua index f588f06c8fb3..fbd55a741944 100644 --- a/kong/pdk/request.lua +++ b/kong/pdk/request.lua @@ -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. @@ -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 @@ -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 diff --git a/t/01-pdk/04-request/15-get_raw_body.t b/t/01-pdk/04-request/15-get_raw_body.t index 490354ac7408..2e47aeb461da 100644 --- a/t/01-pdk/04-request/15-get_raw_body.t +++ b/t/01-pdk/04-request/15-get_raw_body.t @@ -170,4 +170,30 @@ body length: 20000 --- response_body body err: request body file too big: 20000 > 19999 --- no_error_log -[error] \ No newline at end of file +[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]