From 06c59bdd4dab85c679739c340933bf96f655a4d4 Mon Sep 17 00:00:00 2001 From: spacewander Date: Sun, 19 Sep 2021 21:44:22 +0800 Subject: [PATCH] fix(radixtree_host_uri): correct matched path Fix #5077 Signed-off-by: spacewander --- apisix/http/router/radixtree_host_uri.lua | 5 + t/router/radixtree-host-uri3.t | 108 ++++++++++++++++++++++ 2 files changed, 113 insertions(+) diff --git a/apisix/http/router/radixtree_host_uri.lua b/apisix/http/router/radixtree_host_uri.lua index 250dc27af5a2..8ae7a9908aa3 100644 --- a/apisix/http/router/radixtree_host_uri.lua +++ b/apisix/http/router/radixtree_host_uri.lua @@ -80,6 +80,7 @@ local function push_host_router(route, host_routes, only_uri_routes) api_ctx.matched_params = nil api_ctx.matched_route = route api_ctx.curr_req_matched = match_opts.matched + api_ctx.real_curr_req_matched_path = match_opts.matched._path end } @@ -162,6 +163,10 @@ function _M.match(api_ctx) local host_uri = api_ctx.var.host local ok = host_router:dispatch(host_uri:reverse(), match_opts, api_ctx, match_opts) if ok then + if api_ctx.real_curr_req_matched_path then + api_ctx.curr_req_matched._path = api_ctx.real_curr_req_matched_path + api_ctx.real_curr_req_matched_path = nil + end return true end end diff --git a/t/router/radixtree-host-uri3.t b/t/router/radixtree-host-uri3.t index 42c6fafdd0b0..1cb3ee6ee401 100644 --- a/t/router/radixtree-host-uri3.t +++ b/t/router/radixtree-host-uri3.t @@ -136,3 +136,111 @@ hello world 404 404 hello world + + + +=== TEST 2: check matched._path +--- config + location /t { + content_by_lua_block { + local http = require "resty.http" + local uri = "http://127.0.0.1:" .. ngx.var.server_port + .. "/hello" + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/routes/1', + ngx.HTTP_PUT, + [[{ + "hosts": ["foo.com"], + "plugins": { + "serverless-post-function": { + "functions" : ["return function(conf, ctx) + ngx.log(ngx.WARN, 'matched uri: ', ctx.curr_req_matched._path); + end"] + } + }, + "upstream": { + "nodes": { + "127.0.0.1:1980": 1 + }, + "type": "roundrobin" + }, + "uri": "/hello" + }]] + ) + + if code >= 300 then + ngx.status = code + ngx.say(body) + return + end + ngx.say(body) + } + } +--- response_body +passed + + + +=== TEST 3: hit, plain path +--- request +GET /hello +--- more_headers +Host: foo.com +--- grep_error_log eval +qr/matched uri: \/\w+/ +--- grep_error_log_out +matched uri: /hello + + + +=== TEST 4: check matched._path, wildcard +--- config + location /t { + content_by_lua_block { + local http = require "resty.http" + local uri = "http://127.0.0.1:" .. ngx.var.server_port + .. "/hello" + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/routes/1', + ngx.HTTP_PUT, + [[{ + "hosts": ["foo.com"], + "plugins": { + "serverless-post-function": { + "functions" : ["return function(conf, ctx) + ngx.log(ngx.WARN, 'matched uri: ', ctx.curr_req_matched._path); + end"] + } + }, + "upstream": { + "nodes": { + "127.0.0.1:1980": 1 + }, + "type": "roundrobin" + }, + "uri": "/*" + }]] + ) + + if code >= 300 then + ngx.status = code + ngx.say(body) + return + end + ngx.say(body) + } + } +--- response_body +passed + + + +=== TEST 5: hit +--- request +GET /hello +--- more_headers +Host: foo.com +--- grep_error_log eval +qr/matched uri: \/\S+,/ +--- grep_error_log_out +matched uri: /*,