Skip to content

Commit

Permalink
Merge pull request #193 from Mashape/fix/multiple-host
Browse files Browse the repository at this point in the history
Supporting multiple Host headers
  • Loading branch information
subnetmarco committed May 1, 2015
2 parents bc9a545 + cb93802 commit 0382513
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 13 deletions.
35 changes: 24 additions & 11 deletions kong/resolver/access.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,31 @@ end

-- Retrieve the API from the Host that has been requested
function _M.execute(conf)
local host = ngx.var.http_host
local hosts = ngx.req.get_headers()["host"] -- Multiple "Host" can have been requested
if type(hosts) == "string" then
hosts = { hosts }
elseif not hosts then
hosts = {}
end

-- Find the API
local api = nil
for _,host in ipairs(hosts) do
api = cache.get_and_set(cache.api_key(host), function()
local apis, err = dao.apis:find_by_keys { public_dns = host }
if err then
ngx.log(ngx.ERR, tostring(err))
utils.show_error(500, tostring(err))
elseif apis and #apis == 1 then
return apis[1]
end
end)
if api then break end
end

local api = cache.get_and_set(cache.api_key(host), function()
local apis, err = dao.apis:find_by_keys { public_dns = host }
if err then
ngx.log(ngx.ERR, tostring(err))
utils.show_error(500, tostring(err))
elseif not apis or #apis == 0 then
utils.not_found("API not found with Host: \""..host.."\"")
end
return apis[1]
end)
if not api then
utils.not_found("API not found with Host: "..table.concat(hosts, ","))
end

-- Setting the backend URL for the proxy_pass directive
ngx.var.backend_url = get_backend_url(api)..ngx.var.request_uri
Expand Down
46 changes: 44 additions & 2 deletions spec/integration/proxy/resolver_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ local spec_helper = require "spec.spec_helpers"
local http_client = require "kong.tools.http_client"
local constants = require "kong.constants"
local cjson = require "cjson"
local socket = require "socket"
local url = require "socket.url"
local stringy = require "stringy"

local STUB_GET_URL = spec_helper.STUB_GET_URL

Expand All @@ -16,14 +19,14 @@ describe("Resolver", function()
spec_helper.stop_kong()
spec_helper.reset_db()
end)

describe("Inexistent API", function()

it("should return Not Found when the API is not in Kong", function()
local response, status, headers = http_client.get(spec_helper.STUB_GET_URL, nil, { host = "foo.com" })
local body = cjson.decode(response)
assert.are.equal(404, status)
assert.are.equal('API not found with Host: "foo.com"', body.message)
assert.are.equal('API not found with Host: foo.com', body.message)
end)

end)
Expand All @@ -50,5 +53,44 @@ describe("Resolver", function()
assert.are.equal(constants.NAME.."/"..constants.VERSION, headers.via)
end)

it("should return Success when the API is in Kong and one Host headers is being sent via plain TCP", function()
local parsed_url = url.parse(STUB_GET_URL)
local host = parsed_url.host
local port = parsed_url.port

local tcp = socket.tcp()
tcp:connect(host, port)
tcp:send("GET "..parsed_url.path.." HTTP/1.0\r\nHost: test4.com\r\n\r\n");
local response = ""
while true do
local s, status, partial = tcp:receive()
response = response..(s or partial)
if status == "closed" then break end
end
tcp:close()

assert.truthy(stringy.startswith(response, "HTTP/1.1 200 OK"))
end)

it("should return Success when the API is in Kong and multiple Host headers are being sent via plain TCP", function()
local parsed_url = url.parse(STUB_GET_URL)
local host = parsed_url.host
local port = parsed_url.port

local tcp = socket.tcp()
tcp:connect(host, port)
tcp:send("GET "..parsed_url.path.." HTTP/1.0\r\nHost: fake.com\r\nHost: test4.com\r\n\r\n");
local response = ""
while true do
local s, status, partial = tcp:receive()
response = response..(s or partial)
if status == "closed" then break end
end
tcp:close()

assert.truthy(stringy.startswith(response, "HTTP/1.1 200 OK"))
end)

end)

end)

0 comments on commit 0382513

Please sign in to comment.