Skip to content

Commit

Permalink
fix(router) split paths before marshalling routes
Browse files Browse the repository at this point in the history
Paths must be evaluated independently when sorting for route matching.

Fix #5438
  • Loading branch information
locao authored and hishamhm committed Jan 20, 2020
1 parent 53f6895 commit eca08e1
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 9 deletions.
29 changes: 20 additions & 9 deletions kong/router.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1046,21 +1046,32 @@ function _M.new(routes)
local marshalled_routes = {}

for i = 1, #routes do
local route_t, err = marshall_route(routes[i])
if not route_t then
return nil, err
end

if route_t.route.paths ~= nil and #route_t.route.paths > 1 then
local paths = routes[i].route.paths
if paths ~= nil and #paths > 1 then
-- split routes by paths to sort properly
for j = 1, #route_t.route.paths do
for j = 1, #paths do
local route = routes[i]
local index = #marshalled_routes + 1
marshalled_routes[index] = route_t
marshalled_routes[index].route.paths = { route_t.route.paths[j] }
local err

route.route.paths = { paths[j] }
marshalled_routes[index], err = marshall_route(route)
if not marshalled_routes[index] then
return nil, err
end
end

else
marshalled_routes[#marshalled_routes + 1] = route_t
local index = #marshalled_routes + 1
local err

marshalled_routes[index], err = marshall_route(routes[i])
if not marshalled_routes[index] then
return nil, err
end
end

end

-- sort wildcard hosts and uri regexes since those rules
Expand Down
36 changes: 36 additions & 0 deletions spec/02-integration/05-proxy/02-router_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1463,6 +1463,12 @@ for _, strategy in helpers.each_strategy() do
paths = { "/root/fixture/get" },
hosts = { "anotherhost.test" },
},
{
strip_path = true,
methods = { "GET" },
paths = { "/root/fixture/get" },
hosts = { "onemorehost.test" },
},
})
end)

Expand All @@ -1486,6 +1492,36 @@ for _, strategy in helpers.each_strategy() do
assert.equal(routes[2].service.id, res.headers["kong-service-id"])
assert.equal(routes[2].service.name, res.headers["kong-service-name"])
end)

it("prioritizes host over longer URIs", function()
local res = assert(proxy_client:send {
method = "GET",
path = "/root/fixture/get",
headers = {
["kong-debug"] = 1,
["host"] = "anotherhost.test",
}
})

assert.res_status(200, res)

assert.equal(routes[3].id, res.headers["kong-route-id"])
assert.equal(routes[3].service.id, res.headers["kong-service-id"])
assert.equal(routes[3].service.name, res.headers["kong-service-name"])
end)

it("do not match incomplete URIs", function()
local res = assert(proxy_client:send {
method = "GET",
path = "/",
headers = {
["kong-debug"] = 1,
["host"] = "ahost.test",
}
})

assert.res_status(404, res)
end)
end)

describe("[paths] + [hosts]", function()
Expand Down

0 comments on commit eca08e1

Please sign in to comment.