Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(router) ensure preserve_host when no hosts #2344

Merged
merged 1 commit into from
Apr 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions kong/core/router.lua
Original file line number Diff line number Diff line change
Expand Up @@ -615,11 +615,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 = ngx.var.http_host
end


Expand Down
22 changes: 21 additions & 1 deletion spec/01-unit/11-router_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,8 @@ describe("Router", function()
_ngx = {
re = ngx.re,
var = {
uri = uri
uri = uri,
http_host = headers["host"] or headers["Host"],
},
req = {
set_uri = function(uri)
Expand Down Expand Up @@ -860,6 +861,25 @@ describe("Router", function()
assert.same(use_case_apis[1], api)
assert.equal("httpbin.org", upstream.host)
end)

it("uses the request's Host header when `grab_header` is disabled", function()
local use_case_apis = {
{
name = "api-1",
upstream_url = "http://httpbin.org",
preserve_host = true,
uris = { "/foo" },
}
}

local router = assert(Router.new(use_case_apis))

local _ngx = mock_ngx("GET", "/foo", { ["host"] = "preserve.com" })

local api, _, host_header = router.exec(_ngx)
assert.same(use_case_apis[1], api)
assert.equal("preserve.com", host_header)
end)
end)

describe("when preserve_host is false", function()
Expand Down
19 changes: 19 additions & 0 deletions spec/02-integration/05-proxy/01-router_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,13 @@ describe("Router", function()
upstream_url = "http://localhost:9999/headers-inspect",
hosts = "discarded.com",
},
{
name = "api-3",
strip_uri = false,
preserve_host = true,
upstream_url = "http://localhost:9999",
uris = "/headers-inspect",
}
}

assert(helpers.start_kong {
Expand Down Expand Up @@ -323,6 +330,18 @@ describe("Router", function()
local json = cjson.decode(body)
assert.equal("preserved.com:123", json.host)
end)

it("forwards request Host even if not matched by [hosts]", function()
local res = assert(client:send {
method = "GET",
path = "/headers-inspect",
headers = { ["Host"] = "preserved.com" },
})

local body = assert.res_status(200, res)
local json = cjson.decode(body)
assert.equal("preserved.com", json.host)
end)
end)
end)

Expand Down