diff --git a/kong/router/atc.lua b/kong/router/atc.lua index 20d5a18a16b8..a9f89c264b8e 100644 --- a/kong/router/atc.lua +++ b/kong/router/atc.lua @@ -406,6 +406,19 @@ local add_debug_headers = utils.add_debug_headers local get_upstream_uri_v0 = utils.get_upstream_uri_v0 +local function set_upstream_uri(req_uri, match_t) + local matched_route = match_t.route + + local request_prefix = match_t.prefix or "/" + local request_postfix = sanitize_uri_postfix(req_uri:sub(#request_prefix + 1)) + + local upstream_base = match_t.upstream_url_t.path or "/" + + match_t.upstream_uri = get_upstream_uri_v0(matched_route, request_postfix, + req_uri, upstream_base) +end + + function _M:matching(params) local req_uri = params.uri local req_host = params.host @@ -443,12 +456,6 @@ function _M:matching(params) service_hostname_type, service_path = get_service_info(service) local request_prefix = matched_route.strip_path and matched_path or nil - local request_postfix = request_prefix and req_uri:sub(#matched_path + 1) or req_uri:sub(2, -1) - request_postfix = sanitize_uri_postfix(request_postfix) or "" - local upstream_base = service_path or "/" - - local upstream_uri = get_upstream_uri_v0(matched_route, request_postfix, req_uri, - upstream_base) return { route = matched_route, @@ -461,9 +468,9 @@ function _M:matching(params) type = service_hostname_type, host = service_host, port = service_port, + path = service_path, }, upstream_scheme = service_protocol, - upstream_uri = upstream_uri, upstream_host = matched_route.preserve_host and req_host or nil, } end @@ -550,6 +557,9 @@ function _M:exec(ctx) -- found a match + -- update upstream_uri in cache result + set_upstream_uri(req_uri, match_t) + -- debug HTTP request header logic add_debug_headers(var, header, match_t) diff --git a/spec/01-unit/08-router_spec.lua b/spec/01-unit/08-router_spec.lua index ede1a4a7bd5e..d7a52af5541a 100644 --- a/spec/01-unit/08-router_spec.lua +++ b/spec/01-unit/08-router_spec.lua @@ -4943,10 +4943,8 @@ do describe("Router (flavor = " .. flavor .. ")", function() reload_router(flavor) - local use_case, router - - lazy_setup(function() - use_case = { + it("[cache hit should be case sensitive]", function() + local use_case = { { service = service, route = { @@ -4960,10 +4958,8 @@ do }, }, } - end) - it("[cache hit should be case sensitive]", function() - router = assert(new_router(use_case)) + local router = assert(new_router(use_case)) local ctx = {} local _ngx = mock_ngx("GET", "/foo", { test1 = "QUOTE", }) @@ -4993,6 +4989,46 @@ do -- cache miss, case sensitive assert.falsy(ctx.route_match_cached) end) + + it("[cache hit should have correct match_t.upstream_uri]", function() + local host = "example.com" + local use_case = { + { + service = service, + route = { + id = "e8fb37f1-102d-461e-9c51-6608a6bb8101", + hosts = { host }, + preserve_host = true, + }, + }, + } + + local router = assert(new_router(use_case)) + + local ctx = {} + local _ngx = mock_ngx("GET", "/foo", { host = host, }) + router._set_ngx(_ngx) + + -- first match + local match_t = router:exec(ctx) + assert.truthy(match_t) + assert.same(use_case[1].route, match_t.route) + assert.falsy(ctx.route_match_cached) + assert.equal(host, match_t.upstream_host) + assert.same("/foo", match_t.upstream_uri) + + local ctx = {} + local _ngx = mock_ngx("GET", "/bar", { host = host, }) + router._set_ngx(_ngx) + + -- cache hit + local match_t = router:exec(ctx) + assert.truthy(match_t) + assert.same(use_case[1].route, match_t.route) + assert.same(ctx.route_match_cached, "pos") + assert.equal(host, match_t.upstream_host) + assert.same("/bar", match_t.upstream_uri) + end) end) end -- local flavor = "traditional_compatible" diff --git a/spec/02-integration/01-helpers/01-helpers_spec.lua b/spec/02-integration/01-helpers/01-helpers_spec.lua index c4e383ffd236..fa00dbd313aa 100644 --- a/spec/02-integration/01-helpers/01-helpers_spec.lua +++ b/spec/02-integration/01-helpers/01-helpers_spec.lua @@ -26,7 +26,6 @@ for _, strategy in helpers.each_strategy() do bp.routes:insert { hosts = { "mock_upstream" }, protocols = { "http" }, - paths = { "/" }, service = service } diff --git a/spec/02-integration/05-proxy/02-router_spec.lua b/spec/02-integration/05-proxy/02-router_spec.lua index c4988c1de779..d8c1ad223291 100644 --- a/spec/02-integration/05-proxy/02-router_spec.lua +++ b/spec/02-integration/05-proxy/02-router_spec.lua @@ -881,7 +881,6 @@ for _, strategy in helpers.each_strategy() do routes = insert_routes(bp, { { hosts = { "mock_upstream" }, - paths = { "/" }, }, }) end) @@ -1302,7 +1301,6 @@ for _, strategy in helpers.each_strategy() do routes = insert_routes(bp, { { protocols = { "https" }, - paths = { "/" }, snis = { "www.example.org" }, service = { name = "service_behind_www.example.org" diff --git a/spec/02-integration/05-proxy/03-upstream_headers_spec.lua b/spec/02-integration/05-proxy/03-upstream_headers_spec.lua index 7b949a3df1b9..de794afe7ebf 100644 --- a/spec/02-integration/05-proxy/03-upstream_headers_spec.lua +++ b/spec/02-integration/05-proxy/03-upstream_headers_spec.lua @@ -278,7 +278,6 @@ for _, strategy in helpers.each_strategy() do assert(bp.routes:insert { hosts = { "headers-charset.com" }, - paths = { "/" }, service = service, }) diff --git a/spec/02-integration/05-proxy/14-server_tokens_spec.lua b/spec/02-integration/05-proxy/14-server_tokens_spec.lua index 0b1a7f15fbd4..b75ed2db205e 100644 --- a/spec/02-integration/05-proxy/14-server_tokens_spec.lua +++ b/spec/02-integration/05-proxy/14-server_tokens_spec.lua @@ -291,7 +291,6 @@ describe("headers [#" .. strategy .. "]", function() return function() bp.routes:insert { hosts = { "headers-inspect.com" }, - paths = { "/" }, } local service = bp.services:insert({ diff --git a/spec/03-plugins/03-http-log/01-log_spec.lua b/spec/03-plugins/03-http-log/01-log_spec.lua index 1a5edbdc6eea..04c405c14fe0 100644 --- a/spec/03-plugins/03-http-log/01-log_spec.lua +++ b/spec/03-plugins/03-http-log/01-log_spec.lua @@ -37,7 +37,6 @@ for _, strategy in helpers.each_strategy() do local route1 = bp.routes:insert { hosts = { "http_logging.test" }, - paths = { "/" }, service = service1 } @@ -746,7 +745,6 @@ for _, strategy in helpers.each_strategy() do local route = bp.routes:insert { hosts = { "http_queue_logging.test" }, - paths = { "/" }, service = service } diff --git a/spec/03-plugins/07-loggly/01-log_spec.lua b/spec/03-plugins/07-loggly/01-log_spec.lua index 3f5473fac9fc..ef415c5fb1ef 100644 --- a/spec/03-plugins/07-loggly/01-log_spec.lua +++ b/spec/03-plugins/07-loggly/01-log_spec.lua @@ -19,7 +19,6 @@ for _, strategy in helpers.each_strategy() do local route1 = bp.routes:insert { hosts = { "logging.com" }, - paths = { "/" }, } local route2 = bp.routes:insert { diff --git a/spec/03-plugins/13-cors/01-access_spec.lua b/spec/03-plugins/13-cors/01-access_spec.lua index 8f854b5f2cef..7113948c57af 100644 --- a/spec/03-plugins/13-cors/01-access_spec.lua +++ b/spec/03-plugins/13-cors/01-access_spec.lua @@ -237,7 +237,6 @@ for _, strategy in helpers.each_strategy() do local route1 = bp.routes:insert({ hosts = { "cors1.com" }, - paths = { "/" }, }) local route2 = bp.routes:insert({ diff --git a/spec/03-plugins/25-oauth2/04-invalidations_spec.lua b/spec/03-plugins/25-oauth2/04-invalidations_spec.lua index 48120d0d6577..379167a6adec 100644 --- a/spec/03-plugins/25-oauth2/04-invalidations_spec.lua +++ b/spec/03-plugins/25-oauth2/04-invalidations_spec.lua @@ -43,7 +43,6 @@ for _, strategy in helpers.each_strategy() do route = assert(admin_api.routes:insert { hosts = { "oauth2.com" }, protocols = { "http", "https" }, - paths = { "/" }, service = service, }) diff --git a/spec/03-plugins/31-proxy-cache/02-access_spec.lua b/spec/03-plugins/31-proxy-cache/02-access_spec.lua index b0e326eabb67..5716a6ea458e 100644 --- a/spec/03-plugins/31-proxy-cache/02-access_spec.lua +++ b/spec/03-plugins/31-proxy-cache/02-access_spec.lua @@ -31,7 +31,6 @@ do local route1 = assert(bp.routes:insert { hosts = { "route-1.com" }, - paths = { "/" }, }) local route2 = assert(bp.routes:insert { hosts = { "route-2.com" },