Skip to content

Commit

Permalink
perf(router) read single Host header
Browse files Browse the repository at this point in the history
We now avoid reading all request headers via `ngx.req.get_headers()` and
use the `ngx.var` API instead. In the future, we'll distinguish Host
fetching and full headers fetching when arbitrary headers matching will
be added to the router.
  • Loading branch information
thibaultcha committed Apr 7, 2017
1 parent 739654e commit 78aa900
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 72 deletions.
24 changes: 10 additions & 14 deletions kong/core/router.lua
Original file line number Diff line number Diff line change
Expand Up @@ -455,18 +455,18 @@ function _M.new(apis)
end


local grab_headers = #wildcard_hosts > 0 or next(plain_indexes.hosts) ~= nil
local grab_host = #wildcard_hosts > 0 or next(plain_indexes.hosts) ~= nil


local function find_api(method, uri, headers)
local function find_api(method, uri, host)
if type(method) ~= "string" then
return error("arg #1 method must be a string")
end
if type(uri) ~= "string" then
return error("arg #2 uri must be a string")
end
if type(headers) ~= "table" then
return error("arg #3 headers must be a table")
if host and type(host) ~= "string" then
return error("arg #3 host must be a string")
end


Expand All @@ -475,7 +475,6 @@ function _M.new(apis)

method = upper(method)

local host = headers["host"] or headers["Host"]
if host then
-- strip port number if given
local m, err = re_match(host, "^([^:]+)", "jo")
Expand Down Expand Up @@ -614,21 +613,18 @@ function _M.new(apis)
local uri = ngx.var.uri
local new_uri = uri
local host_header
local headers
local req_host


--print("grab headers: ", grab_headers)
--print("grab host header: ", grab_host)


if grab_headers then
headers = ngx.req.get_headers()

else
headers = empty_t
if grab_host then
req_host = ngx.var.http_host
end


local api_t = find_api(method, uri, headers)
local api_t = find_api(method, uri, req_host)
if not api_t then
return nil
end
Expand All @@ -655,7 +651,7 @@ function _M.new(apis)


if api_t.preserve_host then
host_header = ngx.var.http_host
host_header = req_host or ngx.var.http_host
end


Expand Down
Loading

0 comments on commit 78aa900

Please sign in to comment.