Skip to content

Commit bfc5daf

Browse files
committed
matched path
1 parent b9ff593 commit bfc5daf

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

lib/resty/radixtree.lua

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -578,9 +578,6 @@ local function match_route_opts(route, opts, ...)
578578
local uris = route.uris
579579
for i = 1, #uris, 2 do
580580
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
584581
matched = true
585582
break
586583
end
@@ -629,10 +626,14 @@ end
629626

630627

631628
local function _match_from_routes(routes, path, opts, ...)
629+
local opts_matched_exists = (opts.matched ~= nil)
632630
for _, route in ipairs(routes) do
633631
if route.path_op == "=" then
634632
if route.path == path then
635633
if match_route_opts(route, opts, ...) then
634+
if opts_matched_exists then
635+
opts.matched._path = path
636+
end
636637
return route
637638
end
638639
end
@@ -643,6 +644,9 @@ local function _match_from_routes(routes, path, opts, ...)
643644
-- log_info("matched route: ", require("cjson").encode(route))
644645
-- log_info("matched path: ", path)
645646
if compare_gin(path, route.path_org, opts) then
647+
if opts_matched_exists then
648+
opts.matched._path = route.path_org
649+
end
646650
return route
647651
end
648652
end
@@ -660,9 +664,13 @@ local function match_route(self, path, opts, ...)
660664
end
661665

662666
local routes = self.hash_path[path]
667+
local opts_matched_exists = (opts.matched ~= nil)
663668
if routes then
664669
for _, route in ipairs(routes) do
665670
if match_route_opts(route, opts, ...) then
671+
if opts_matched_exists then
672+
opts.matched._path = path
673+
end
666674
return route
667675
end
668676
end

t/parameter.t

Lines changed: 7 additions & 8 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
@@ -180,7 +180,6 @@ matched: {}
180180
local rx = radix.new({
181181
{
182182
paths = {"/aa/*", "/bb/cc/*", "/dd/ee/index.html"},
183-
uris = {"/aa/*", "/bb/cc/*", "/dd/ee/index.html"},
184183
methods = {"GET", "POST", "PUT"},
185184
hosts = {"foo.com", "*.bar.com"},
186185
metadata = "metadata /asf",
@@ -200,4 +199,4 @@ GET /t
200199
[error]
201200
--- response_body
202201
match meta: metadata /asf
203-
matched: {"_uri":"\/bb\/cc\/",":ext":"xx","_method":"GET","_host":"foo.com"}
202+
matched: {"_path":"\/bb\/cc\/*",":ext":"xx","_method":"GET","_host":"foo.com"}

0 commit comments

Comments
 (0)