Skip to content

Commit b9ff593

Browse files
committed
feat: record which matches the current request.
1 parent 11f7b4e commit b9ff593

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

lib/resty/radixtree.lua

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,10 @@ local function match_route_opts(route, opts, ...)
529529
end
530530
end
531531

532+
if opts.matched ~= nil then
533+
opts.matched._method = method
534+
end
535+
532536
local matcher_ins = route.matcher_ins
533537
if matcher_ins then
534538
local ok, err = matcher_ins:match(opts.remote_addr)
@@ -554,6 +558,9 @@ local function match_route_opts(route, opts, ...)
554558
if reverse_host then
555559
for i = 1, #hosts, 2 do
556560
if match_host(hosts[i], hosts[i + 1], reverse_host) then
561+
if opts.matched ~= nil then
562+
opts.matched._host = hosts[i + 1]:reverse()
563+
end
557564
matched = true
558565
break
559566
end
@@ -571,6 +578,9 @@ local function match_route_opts(route, opts, ...)
571578
local uris = route.uris
572579
for i = 1, #uris, 2 do
573580
if match_uri(uris[i], uris[i + 1], opts.uri) then
581+
if opts.matched ~= nil then
582+
opts.matched._uri = uris[i + 1]
583+
end
574584
matched = true
575585
break
576586
end

t/parameter.t

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,3 +168,36 @@ GET /t
168168
--- response_body
169169
match meta: nil
170170
matched: {}
171+
172+
173+
174+
=== TEST 6: /name/:name/id/:id
175+
--- config
176+
location /t {
177+
content_by_lua_block {
178+
local json = require("cjson.safe")
179+
local radix = require("resty.radixtree")
180+
local rx = radix.new({
181+
{
182+
paths = {"/aa/*", "/bb/cc/*", "/dd/ee/index.html"},
183+
uris = {"/aa/*", "/bb/cc/*", "/dd/ee/index.html"},
184+
methods = {"GET", "POST", "PUT"},
185+
hosts = {"foo.com", "*.bar.com"},
186+
metadata = "metadata /asf",
187+
},
188+
})
189+
190+
local opts = {matched = {}, method = "GET", uri = "/bb/cc/xx", host = "foo.com"}
191+
local meta = rx:match("/bb/cc/xx", opts)
192+
ngx.say("match meta: ", meta)
193+
ngx.say("matched: ", json.encode(opts.matched))
194+
195+
}
196+
}
197+
--- request
198+
GET /t
199+
--- no_error_log
200+
[error]
201+
--- response_body
202+
match meta: metadata /asf
203+
matched: {"_uri":"\/bb\/cc\/",":ext":"xx","_method":"GET","_host":"foo.com"}

0 commit comments

Comments
 (0)