Skip to content

Commit

Permalink
refactor(router) simplify URI stripping logic
Browse files Browse the repository at this point in the history
  • Loading branch information
thibaultcha committed Jun 9, 2017
1 parent 0010bce commit 8ca38dc
Showing 1 changed file with 15 additions and 23 deletions.
38 changes: 15 additions & 23 deletions kong/core/router.lua
Original file line number Diff line number Diff line change
Expand Up @@ -632,8 +632,9 @@ function _M.new(apis)


function self.exec(ngx)
local method = ngx.req.get_method()
local uri = ngx.var.request_uri
local method = ngx.req.get_method()
local request_uri = ngx.var.request_uri
local uri = request_uri


do
Expand All @@ -659,16 +660,12 @@ function _M.new(apis)
return nil
end

local new_uri
local uri_root = uri == "/"
local uri_root = request_uri == "/"

if uri_root or not api_t.strip_uri_regex then
new_uri = uri

else
if not uri_root and api_t.strip_uri_regex then
local _, err
new_uri, _, err = re_sub(uri, api_t.strip_uri_regex, "/$1", "ajo")
if not new_uri then
uri, _, err = re_sub(uri, api_t.strip_uri_regex, "/$1", "ajo")
if not uri then
log(ERR, "could not strip URI: ", err)
return
end
Expand All @@ -677,35 +674,30 @@ function _M.new(apis)

local upstream = api_t.upstream
if upstream.path and upstream.path ~= "/" then
if new_uri ~= "/" then
new_uri = upstream.file .. new_uri
if uri ~= "/" then
uri = upstream.file .. uri

else
if upstream.path ~= upstream.file then
if uri_root or sub(uri, -1) == "/" then
new_uri = upstream.path
if uri_root or sub(request_uri, -1) == "/" then
uri = upstream.path

else
new_uri = upstream.file
uri = upstream.file
end

else
if uri_root or sub(uri, -1) ~= "/" then
new_uri = upstream.file
if uri_root or sub(request_uri, -1) ~= "/" then
uri = upstream.file

else
new_uri = upstream.file .. new_uri
uri = upstream.file .. uri
end
end
end
end


if new_uri ~= uri then
uri = new_uri
end


local host_header

if api_t.preserve_host then
Expand Down

0 comments on commit 8ca38dc

Please sign in to comment.