Skip to content

Commit

Permalink
perf(pdk) faster request.get_header (#8716)
Browse files Browse the repository at this point in the history
* perf(pdk) faster reqeust.get_header
* typo
* cache ngx.var
* more effecient implementation of string process
* no need to lower
* bug fix
* try to archive better performance
* style
* bug fix
  • Loading branch information
StarlightIbuki authored May 18, 2022
1 parent 6a0a579 commit ce5e6a2
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
7 changes: 3 additions & 4 deletions kong/pdk/request.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ local phase_checker = require "kong.pdk.private.phases"


local ngx = ngx
local var = ngx.var
local sub = string.sub
local find = string.find
local lower = string.lower
Expand Down Expand Up @@ -568,10 +569,8 @@ local function new(self)
error("header name must be a string", 2)
end

local header_value = _REQUEST.get_headers()[name]
if type(header_value) == "table" then
return header_value[1]
end
-- Do not localize ngx.re.gsub! It will crash because ngx.re is monkey patched.
local header_value = var["http_" .. ngx.re.gsub(name, "-", "_", "jo")]

return header_value
end
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 @@ -104,7 +104,7 @@ X-Foo-Header: ''
=== TEST 5: request.get_header() returns nil when requested header does not fit in default max_headers
=== TEST 5: request.get_header() have no limit on header numbers
--- http_config eval: $t::Util::HttpConfig
--- config
location = /t {
Expand Down Expand Up @@ -133,7 +133,7 @@ X-Foo-Header: ''
--- request
GET /t
--- response_body
accept header value: nil
accept header value: text/html
--- no_error_log
[error]
Expand Down

0 comments on commit ce5e6a2

Please sign in to comment.