Skip to content

Commit

Permalink
feat(router) allow url escaped uris
Browse files Browse the repository at this point in the history
Change the router to use `ngx.var.request_uri` instead of
the normalized `ngx.var.uri`. Makes proxying more transparent.

Fixes #2366
  • Loading branch information
bungle committed Apr 21, 2017
1 parent ff3abad commit 29f81cb
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 20 deletions.
27 changes: 20 additions & 7 deletions kong/core/router.lua
Original file line number Diff line number Diff line change
Expand Up @@ -615,17 +615,23 @@ function _M.new(apis)


function self.exec(ngx)
local method = ngx.req.get_method()
local uri = ngx.var.uri
local uri_root = uri == "/"
local new_uri = uri
local host_header
local req_host
local method = ngx.req.get_method()
local uri = ngx.var.request_uri


do
local s = find(uri, "?", 2, true)
if s then
uri = sub(uri, 1, s - 1)
end
end


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


local req_host

if grab_host then
req_host = ngx.var.http_host
end
Expand All @@ -636,8 +642,13 @@ function _M.new(apis)
return nil
end

local new_uri
local uri_root = uri == "/"

if uri_root or not api_t.strip_uri_regex then
new_uri = uri

if not uri_root and api_t.strip_uri_regex then
else
local err
new_uri, err = re_sub(uri, api_t.strip_uri_regex, "/$1", "ajo")
if not new_uri then
Expand Down Expand Up @@ -678,6 +689,8 @@ function _M.new(apis)
end


local host_header

if api_t.preserve_host then
host_header = req_host or ngx.var.http_host
end
Expand Down
63 changes: 50 additions & 13 deletions spec/01-unit/11-router_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -712,12 +712,12 @@ describe("Router", function()
nop = function() end
}

local function mock_ngx(method, uri, headers)
local function mock_ngx(method, request_uri, headers)
local _ngx
_ngx = {
re = ngx.re,
var = setmetatable({
uri = uri,
request_uri = request_uri,
http_kong_debug = headers.kong_debug
}, {
__index = function(_, key)
Expand All @@ -728,8 +728,8 @@ describe("Router", function()
end
}),
req = {
set_uri = function(uri)
_ngx.var.uri = uri
set_uri = function(request_uri)
_ngx.var.request_uri = request_uri
end,
get_method = function()
return method
Expand Down Expand Up @@ -816,6 +816,43 @@ describe("Router", function()
assert.equal(8443, upstream.port)
end)

it("allows url encoded uris", function()
local use_case_apis = {
{
name = "api-1",
uris = {
"/endel%C3%B8st"
},
},
}

local _ngx = mock_ngx("GET", "/endel%C3%B8st", {})
local router = assert(Router.new(use_case_apis))
local api = router.exec(_ngx)

assert.same(use_case_apis[1], api)
assert.equal("/endel%C3%B8st", _ngx.var.request_uri)
end)

it("strips url encoded uris", function()
local use_case_apis = {
{
name = "api-1",
strip_uri = true,
uris = {
"/endel%C3%B8st"
},
},
}

local _ngx = mock_ngx("GET", "/endel%C3%B8st", {})
local router = assert(Router.new(use_case_apis))
local api = router.exec(_ngx)

assert.same(use_case_apis[1], api)
assert.equal("/", _ngx.var.request_uri)
end)

describe("grab_headers", function()
local use_case_apis = {
{
Expand Down Expand Up @@ -882,23 +919,23 @@ describe("Router", function()

local api = router.exec(_ngx)
assert.same(use_case_apis[1], api)
assert.equal("/hello/world", _ngx.var.uri)
assert.equal("/hello/world", _ngx.var.request_uri)
end)

it("strips if matched URI is plain (not a prefix)", function()
local _ngx = mock_ngx("GET", "/my-api", {})

local api = router.exec(_ngx)
assert.same(use_case_apis[1], api)
assert.equal("/", _ngx.var.uri)
assert.equal("/", _ngx.var.request_uri)
end)

it("doesn't strip if 'strip_uri' is not enabled", function()
local _ngx = mock_ngx("POST", "/my-api/hello/world", {})

local api = router.exec(_ngx)
assert.same(use_case_apis[2], api)
assert.equal("/my-api/hello/world", _ngx.var.uri)
assert.equal("/my-api/hello/world", _ngx.var.request_uri)
end)

it("does not strips root / URI", function()
Expand All @@ -916,33 +953,33 @@ describe("Router", function()

local api = router.exec(_ngx)
assert.same(use_case_apis[1], api)
assert.equal("/my-api/hello/world", _ngx.var.uri)
assert.equal("/my-api/hello/world", _ngx.var.request_uri)
end)

it("can find an API with stripped URI several times in a row", function()
local _ngx = mock_ngx("GET", "/my-api", {})

local api = router.exec(_ngx)
assert.same(use_case_apis[1], api)
assert.equal("/", _ngx.var.uri)
assert.equal("/", _ngx.var.request_uri)

_ngx = mock_ngx("GET", "/my-api", {})
local api2 = router.exec(_ngx)
assert.same(use_case_apis[1], api2)
assert.equal("/", _ngx.var.uri)
assert.equal("/", _ngx.var.request_uri)
end)

it("can proxy an API with stripped URI with different URIs in a row", function()
local _ngx = mock_ngx("GET", "/my-api", {})

local api = router.exec(_ngx)
assert.same(use_case_apis[1], api)
assert.equal("/", _ngx.var.uri)
assert.equal("/", _ngx.var.request_uri)

_ngx = mock_ngx("GET", "/this-api", {})
local api2 = router.exec(_ngx)
assert.same(use_case_apis[1], api2)
assert.equal("/", _ngx.var.uri)
assert.equal("/", _ngx.var.request_uri)
end)
end)

Expand Down Expand Up @@ -1102,7 +1139,7 @@ describe("Router", function()
local api, upstream = router.exec(_ngx)
assert.same(use_case_apis[1], api)
assert.equal(args[1], upstream.path)
assert.equal(args[4], _ngx.var.uri)
assert.equal(args[4], _ngx.var.request_uri)
end)
end
end)
Expand Down

0 comments on commit 29f81cb

Please sign in to comment.