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

refactor(router): clean the logic of get_upstream_uri_v0() #11278

Merged
merged 2 commits into from
Jul 31, 2023
Merged
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
77 changes: 36 additions & 41 deletions kong/router/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ end

local function get_upstream_uri_v0(matched_route, request_postfix, req_uri,
upstream_base)
local upstream_uri

local strip_path = matched_route.strip_path or matched_route.strip_uri

Expand All @@ -143,58 +142,54 @@ local function get_upstream_uri_v0(matched_route, request_postfix, req_uri,
if strip_path then
if request_postfix == "" then
if upstream_base == "/" then
upstream_uri = "/"

elseif byte(req_uri, -1) == SLASH then
upstream_uri = upstream_base
return "/"
end

else
upstream_uri = sub(upstream_base, 1, -2)
if byte(req_uri, -1) == SLASH then
return upstream_base
end

elseif byte(request_postfix, 1, 1) == SLASH then
-- double "/", so drop the first
upstream_uri = sub(upstream_base, 1, -2) .. request_postfix
return sub(upstream_base, 1, -2)
end -- if request_postfix

else -- ends with / and strip_path = true, no double slash
upstream_uri = upstream_base .. request_postfix
if byte(request_postfix, 1) == SLASH then
-- double "/", so drop the first
return sub(upstream_base, 1, -2) .. request_postfix
end

else -- ends with / and strip_path = false
-- we retain the incoming path, just prefix it with the upstream
-- path, but skip the initial slash
upstream_uri = upstream_base .. sub(req_uri, 2)
end

else -- does not end with /
-- does not end with / and strip_path = true
if strip_path then
if request_postfix == "" then
if #req_uri > 1 and byte(req_uri, -1) == SLASH then
upstream_uri = upstream_base .. "/"

else
upstream_uri = upstream_base
end

elseif byte(request_postfix, 1, 1) == SLASH then
upstream_uri = upstream_base .. request_postfix

else
upstream_uri = upstream_base .. "/" .. request_postfix
-- ends with / and strip_path = true, no double slash
return upstream_base .. request_postfix
end -- if strip_path

-- ends with / and strip_path = false
-- we retain the incoming path, just prefix it with the upstream
-- path, but skip the initial slash
return upstream_base .. sub(req_uri, 2)
end -- byte(upstream_base, -1) == SLASH

-- does not end with / and strip_path = true
if strip_path then
if request_postfix == "" then
if #req_uri > 1 and byte(req_uri, -1) == SLASH then
return upstream_base .. "/"
end

else -- does not end with / and strip_path = false
if req_uri == "/" then
upstream_uri = upstream_base
return upstream_base
end -- if request_postfix

else
upstream_uri = upstream_base .. req_uri
end
if byte(request_postfix, 1) == SLASH then
return upstream_base .. request_postfix
end

return upstream_base .. "/" .. request_postfix
end -- if strip_path

-- does not end with / and strip_path = false
if req_uri == "/" then
return upstream_base
end

return upstream_uri
return upstream_base .. req_uri
end


Expand Down
Loading