Skip to content

Commit

Permalink
fix(radixtree_host_uri): correct matched path
Browse files Browse the repository at this point in the history
Fix apache#5077

Signed-off-by: spacewander <spacewanderlzx@gmail.com>
  • Loading branch information
spacewander committed Sep 19, 2021
1 parent 46cabd6 commit 06c59bd
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 0 deletions.
5 changes: 5 additions & 0 deletions apisix/http/router/radixtree_host_uri.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand Down
108 changes: 108 additions & 0 deletions t/router/radixtree-host-uri3.t
Original file line number Diff line number Diff line change
Expand Up @@ -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: /*,

0 comments on commit 06c59bd

Please sign in to comment.