Skip to content

Commit

Permalink
refactor(router): clean the logic of get_upstream_uri_v0() (#11278)
Browse files Browse the repository at this point in the history
This comit cleans function `get_upstream_uri_v0`  by using early return.

KAG-2148
  • Loading branch information
chronolaw authored Jul 31, 2023
1 parent cbcddf2 commit 1367fa3
Showing 1 changed file with 36 additions and 41 deletions.
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

1 comment on commit 1367fa3

@khcp-gha-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bazel Build

Docker image available kong/kong:1367fa391d8311e948485807e106b55701b38d78
Artifacts available https://github.com/Kong/kong/actions/runs/5711848563

Please sign in to comment.