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

Supports simple slash in request_path #1227

Merged
merged 1 commit into from
May 24, 2016
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
2 changes: 1 addition & 1 deletion kong/core/resolver.lua
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ function _M.find_api_by_request_path(uri, request_path_arr)
end

for _, item in ipairs(request_path_arr) do
local m, err = ngx.re.match(uri, "^"..item.request_path.."/")
local m, err = ngx.re.match(uri, "^"..(item.request_path == "/" and "/" or item.request_path.."/"))
if err then
ngx.log(ngx.ERR, "[resolver] error matching requested request_path: "..err)
elseif m then
Expand Down
6 changes: 2 additions & 4 deletions kong/dao/schemas/apis.lua
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,7 @@ local function check_request_path(request_path, api_t)
end

if request_path ~= nil and request_path ~= "" then
if request_path == "/" then
return false, "cannot be an empty path: '/'"
elseif sub(request_path, 1, 1) ~= "/" then
if sub(request_path, 1, 1) ~= "/" then
return false, fmt("must be prefixed with slash: '%s'", request_path)
elseif match(request_path, "//+") then
-- Check for empty segments (/status//123)
Expand All @@ -95,7 +93,7 @@ local function check_request_path(request_path, api_t)

-- From now on, the request_path is considered valid.
-- Remove trailing slash
if sub(request_path, -1) == "/" then
if request_path ~= "/" and sub(request_path, -1) == "/" then
api_t.request_path = sub(request_path, 1, -2)
end
end
Expand Down
17 changes: 16 additions & 1 deletion spec/integration/05-proxy/resolver_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ describe("Resolver", function()
{name = "tests-trailing-slash-path4", request_path = "/test-trailing-slash4", strip_request_path = true, upstream_url = "http://www.mockbin.org/"},
{name = "tests-deep-path", request_path = "/hello/world", strip_request_path = true, upstream_url = "http://mockbin.com"},
{name = "tests-deep-path-two", request_path = "/hello/world/wot", strip_request_path = true, upstream_url = "http://httpbin.org"},
{name = "tests-request_path-resolver2", upstream_url = "http://mockbin.com", request_path = "/headers"}
{name = "tests-request_path-resolver2", upstream_url = "http://mockbin.com", request_path = "/headers"},
{name = "tests-root-path", upstream_url = "http://httpbin.org", request_path = "/"},
{name = "tests-root-path2", upstream_url = "http://mockbin.com", request_path = "/noroot", strip_request_path = true},
},
plugin = {
{name = "key-auth", config = {key_names = {"apikey"} }, __api = 2}
Expand Down Expand Up @@ -337,4 +339,17 @@ describe("Resolver", function()
assert.equal("http://mockbin-uri.com/request?foo=abc%7cdef%2c%20world", cjson.decode(response).url)
end)
end)

describe("Priority in request_path resolutions", function()
it("should work for root request_path", function()
local response, status = http_client.get(PROXY_URL.."/get", {})
assert.equal(200, status)
assert.equal("http://httpbin.org/get", cjson.decode(response).url)
end)
it("should work for non root request_path", function()
local response, status = http_client.get(PROXY_URL.."/noroot/request", {})
assert.equal(200, status)
assert.equal("http://mockbin.com/request", cjson.decode(response).url)
end)
end)
end)
6 changes: 3 additions & 3 deletions spec/unit/11-entities_schemas_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -179,14 +179,14 @@ describe("Entities Schemas", function()
assert.equal("must be prefixed with slash: '"..v.."'", errors.request_path)
end
end)
it("should not accept root (no prefix slash)", function()
it("should accept root (prefix slash)", function()
local valid, errors = validate_entity({
name = "mockbin",
request_path = "/",
upstream_url = "http://mockbin.com"
}, api_schema)
assert.False(valid)
assert.equal("cannot be an empty path: '/'", errors.request_path)
assert.falsy(errors)
assert.True(valid)
end)
it("should not accept invalid URI", function()
local invalids = {"//status", "/status//123", "/status/123//"}
Expand Down