Skip to content

Commit 592016c

Browse files
authored
feature: record which matches the current request. (#42)
fix #41
2 parents 437a02d + 8981fb3 commit 592016c

File tree

2 files changed

+56
-6
lines changed

2 files changed

+56
-6
lines changed

lib/resty/radixtree.lua

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,10 @@ local function match_route_opts(route, opts, ...)
541541
end
542542
end
543543

544+
if opts.matched ~= nil then
545+
opts.matched._method = method
546+
end
547+
544548
local matcher_ins = route.matcher_ins
545549
if matcher_ins then
546550
local ok, err = matcher_ins:match(opts.remote_addr)
@@ -566,6 +570,9 @@ local function match_route_opts(route, opts, ...)
566570
if reverse_host then
567571
for i = 1, #hosts, 2 do
568572
if match_host(hosts[i], hosts[i + 1], reverse_host) then
573+
if opts.matched ~= nil then
574+
opts.matched._host = hosts[i + 1]:reverse()
575+
end
569576
matched = true
570577
break
571578
end
@@ -621,10 +628,14 @@ end
621628

622629

623630
local function _match_from_routes(routes, path, opts, ...)
631+
local opts_matched_exists = (opts.matched ~= nil)
624632
for _, route in ipairs(routes) do
625633
if route.path_op == "=" then
626634
if route.path == path then
627635
if match_route_opts(route, opts, ...) then
636+
if opts_matched_exists then
637+
opts.matched._path = path
638+
end
628639
return route
629640
end
630641
end
@@ -635,6 +646,9 @@ local function _match_from_routes(routes, path, opts, ...)
635646
-- log_info("matched route: ", require("cjson").encode(route))
636647
-- log_info("matched path: ", path)
637648
if compare_gin(path, route.path_org, opts) then
649+
if opts_matched_exists then
650+
opts.matched._path = route.path_org
651+
end
638652
return route
639653
end
640654
end
@@ -652,9 +666,13 @@ local function match_route(self, path, opts, ...)
652666
end
653667

654668
local routes = self.hash_path[path]
669+
local opts_matched_exists = (opts.matched ~= nil)
655670
if routes then
656671
for _, route in ipairs(routes) do
657672
if match_route_opts(route, opts, ...) then
673+
if opts_matched_exists then
674+
opts.matched._path = path
675+
end
658676
return route
659677
end
660678
end

t/parameter.t

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ GET /t
3636
[error]
3737
--- response_body
3838
match meta: metadata /name
39-
matched: {"name":"json"}
39+
matched: {"_path":"\/name\/*name","name":"json"}
4040
match meta: metadata /name
41-
matched: {"name":""}
41+
matched: {"_path":"\/name\/*name","name":""}
4242
4343
4444
@@ -71,9 +71,9 @@ GET /t
7171
[error]
7272
--- response_body
7373
match meta: metadata /name
74-
matched: {":ext":"json\/foo\/bar"}
74+
matched: {"_path":"\/name\/*",":ext":"json\/foo\/bar"}
7575
match meta: metadata /name
76-
matched: {":ext":""}
76+
matched: {"_path":"\/name\/*",":ext":""}
7777
7878
7979
@@ -107,7 +107,7 @@ GET /t
107107
[error]
108108
--- response_body
109109
match meta: metadata /name
110-
matched: {"id":"1","name":"json"}
110+
matched: {"name":"json","_path":"\/name\/:name\/id\/:id","id":"1"}
111111
match meta: nil
112112
matched: {}
113113
@@ -138,7 +138,7 @@ GET /t
138138
[error]
139139
--- response_body
140140
match meta: metadata /name
141-
matched: {"other":"foo\/bar\/gloo","name":"json","id":"1"}
141+
matched: {"other":"foo\/bar\/gloo","name":"json","_path":"\/name\/:name\/id\/:id\/*other","id":"1"}
142142
143143
144144
@@ -168,3 +168,35 @@ 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+
methods = {"GET", "POST", "PUT"},
184+
hosts = {"foo.com", "*.bar.com"},
185+
metadata = "metadata /asf",
186+
},
187+
})
188+
189+
local opts = {matched = {}, method = "GET", uri = "/bb/cc/xx", host = "foo.com"}
190+
local meta = rx:match("/bb/cc/xx", opts)
191+
ngx.say("match meta: ", meta)
192+
ngx.say("matched: ", json.encode(opts.matched))
193+
194+
}
195+
}
196+
--- request
197+
GET /t
198+
--- no_error_log
199+
[error]
200+
--- response_body
201+
match meta: metadata /asf
202+
matched: {"_path":"\/bb\/cc\/*",":ext":"xx","_method":"GET","_host":"foo.com"}

0 commit comments

Comments
 (0)