Skip to content

Commit

Permalink
fix pdk.request
Browse files Browse the repository at this point in the history
  • Loading branch information
chronolaw committed Jan 13, 2024
1 parent b032e10 commit 790f2bd
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
33 changes: 30 additions & 3 deletions kong/pdk/request.lua
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,12 @@ local function new(self)
if is_trusted_ip() then
local scheme = _REQUEST.get_header(X_FORWARDED_PROTO)
if scheme then
local p = find(scheme, ",", 1, true)

if p then
scheme = sub(scheme, 1, p - 1)
end

return lower(scheme)
end
end
Expand Down Expand Up @@ -243,7 +249,16 @@ local function new(self)
check_phase(PHASES.request)

if is_trusted_ip() then
local port = tonumber(_REQUEST.get_header(X_FORWARDED_PORT), 10)
local port = _REQUEST.get_header(X_FORWARDED_PORT)
if port then
local p = find(port, ",", 1, true)

if p then
port = sub(port, 1, p - 1)
end
end

local port = tonumber(port or "", 10)
if port and port >= MIN_PORT and port <= MAX_PORT then
return port
end
Expand Down Expand Up @@ -300,6 +315,12 @@ local function new(self)
if is_trusted_ip() then
local path = _REQUEST.get_header(X_FORWARDED_PATH)
if path then
local p = find(path, ",", 1, true)

if p then
path = sub(path, 1, p - 1)
end

return path
end
end
Expand Down Expand Up @@ -343,6 +364,12 @@ local function new(self)
if is_trusted_ip() then
prefix = _REQUEST.get_header(X_FORWARDED_PREFIX)
if prefix then
local p = find(prefix, ",", 1, true)

if p then
prefix = sub(prefix, 1, p - 1)
end

return prefix
end
end
Expand Down Expand Up @@ -597,7 +624,7 @@ local function new(self)
-- The returned value is either a `string`, or can be `nil` if a header with
-- `name` was not found in the request. If a header with the same name is
-- present multiple times in the request, this function returns the value
-- of the first occurrence of this header.
-- of the all occurrences of this header (since nginx 1.23.0).
--
-- Header names in are case-insensitive and are normalized to lowercase, and
-- dashes (`-`) can be written as underscores (`_`); that is, the header
Expand All @@ -617,7 +644,7 @@ local function new(self)
--
-- kong.request.get_header("Host") -- "foo.com"
-- kong.request.get_header("x-custom-header") -- "bla"
-- kong.request.get_header("X-Another") -- "foo bar"
-- kong.request.get_header("X-Another") -- "foo bar, baz"
function _REQUEST.get_header(name)
check_phase(PHASES.request)

Expand Down
4 changes: 2 additions & 2 deletions t/01-pdk/04-request/13-get_header.t
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ run_tests();

__DATA__
=== TEST 1: request.get_header() returns first header when multiple is given with same name
=== TEST 1: request.get_header() returns all headers when multiple is given with same name
--- http_config eval: $t::Util::HttpConfig
--- config
location = /t {
Expand All @@ -26,7 +26,7 @@ GET /t
Accept: application/json
Accept: text/html
--- response_body
accept header value: application/json
accept header value: application/json, text/html
--- no_error_log
[error]
Expand Down

0 comments on commit 790f2bd

Please sign in to comment.