Skip to content

Commit 4090ccd

Browse files
authored
fix: params match should not failed with special symbol (#96)
1 parent 741715f commit 4090ccd

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

lib/resty/radixtree.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,8 @@ local function fetch_pat(path)
460460
local first_byte = item:byte(1, 1)
461461
if first_byte == string.byte(":") then
462462
table.insert(names, res[i]:sub(2))
463-
res[i] = [=[([\w\-_\%]+)]=]
463+
-- See https://www.rfc-editor.org/rfc/rfc1738.txt BNF for specific URL schemes
464+
res[i] = [=[([\w\-_;:@&=!',\%\$\.\+\*\(\)]+)]=]
464465

465466
elseif first_byte == string.byte("*") then
466467
local name = res[i]:sub(2)

t/parameter.t

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,3 +312,47 @@ match meta: nil
312312
matched: []
313313
match meta: metadata /name
314314
matched: {"_path":"/name/:name/id/:id"}
315+
316+
317+
318+
=== TEST 10: /file/:filename (parameter with special symbol)
319+
--- config
320+
location /t {
321+
content_by_lua_block {
322+
local json = require("toolkit.json")
323+
local radix = require("resty.radixtree")
324+
local rx = radix.new({
325+
{
326+
paths = {"/file/:filename"},
327+
metadata = "metadata /file/:filename",
328+
},
329+
})
330+
331+
local opts = {matched = {}}
332+
-- test [";" | ":" | "@" | "&" | "="]
333+
local meta = rx:match("/file/123&45@dd:d=test;", opts)
334+
ngx.say("matched: ", json.encode(opts.matched))
335+
ngx.say("match meta: ", meta)
336+
337+
-- test uchar.unreserved.safe ["$" | "-" | "_" | "." | "+"]
338+
local meta = rx:match("/file/test_a-b+c.lua$", opts)
339+
ngx.say("matched: ", json.encode(opts.matched))
340+
ngx.say("match meta: ", meta)
341+
342+
-- test uchar.unreserved.extra ["!" | "*" | "'" | "(" | ")" | ","]
343+
local meta = rx:match("/file/t!e*s't,(file)", opts)
344+
ngx.say("matched: ", json.encode(opts.matched))
345+
ngx.say("match meta: ", meta)
346+
}
347+
}
348+
--- request
349+
GET /t
350+
--- no_error_log
351+
[error]
352+
--- response_body
353+
matched: {"_path":"/file/:filename","filename":"123&45@dd:d=test;"}
354+
match meta: metadata /file/:filename
355+
matched: {"_path":"/file/:filename","filename":"test_a-b+c.lua$"}
356+
match meta: metadata /file/:filename
357+
matched: {"_path":"/file/:filename","filename":"t!e*s't,(file)"}
358+
match meta: metadata /file/:filename

0 commit comments

Comments
 (0)