Skip to content

Commit

Permalink
Merge pull request #2271 from Mashape/perf/router-host-header-reading
Browse files Browse the repository at this point in the history
perf(router) efficient host header reading
  • Loading branch information
thibaultcha authored Mar 28, 2017
2 parents eb9cc3a + 636af88 commit 98d379d
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 72 deletions.
28 changes: 10 additions & 18 deletions kong/core/router.lua
Original file line number Diff line number Diff line change
Expand Up @@ -446,25 +446,24 @@ function _M.new(apis)
end)


local grab_headers = #wildcard_hosts > 0 or next(indexes.plain_hosts) ~= nil
local grab_host = #wildcard_hosts > 0 or next(indexes.plain_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


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 @@ -574,21 +573,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 @@ -615,11 +611,7 @@ function _M.new(apis)


if api_t.preserve_host then
if not headers then
headers = ngx.req.get_headers()
end

host_header = headers["host"]
host_header = req_host
end


Expand Down
Loading

0 comments on commit 98d379d

Please sign in to comment.