diff --git a/kong/router/utils.lua b/kong/router/utils.lua index bb6bc064f778..db0d9985e456 100644 --- a/kong/router/utils.lua +++ b/kong/router/utils.lua @@ -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 @@ -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