Skip to content

Commit

Permalink
fix: params match should not failed with special symbol (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrard-YNWA authored Apr 15, 2021
1 parent 741715f commit 4090ccd
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/resty/radixtree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,8 @@ local function fetch_pat(path)
local first_byte = item:byte(1, 1)
if first_byte == string.byte(":") then
table.insert(names, res[i]:sub(2))
res[i] = [=[([\w\-_\%]+)]=]
-- See https://www.rfc-editor.org/rfc/rfc1738.txt BNF for specific URL schemes
res[i] = [=[([\w\-_;:@&=!',\%\$\.\+\*\(\)]+)]=]

elseif first_byte == string.byte("*") then
local name = res[i]:sub(2)
Expand Down
44 changes: 44 additions & 0 deletions t/parameter.t
Original file line number Diff line number Diff line change
Expand Up @@ -312,3 +312,47 @@ match meta: nil
matched: []
match meta: metadata /name
matched: {"_path":"/name/:name/id/:id"}
=== TEST 10: /file/:filename (parameter with special symbol)
--- config
location /t {
content_by_lua_block {
local json = require("toolkit.json")
local radix = require("resty.radixtree")
local rx = radix.new({
{
paths = {"/file/:filename"},
metadata = "metadata /file/:filename",
},
})
local opts = {matched = {}}
-- test [";" | ":" | "@" | "&" | "="]
local meta = rx:match("/file/123&45@dd:d=test;", opts)
ngx.say("matched: ", json.encode(opts.matched))
ngx.say("match meta: ", meta)
-- test uchar.unreserved.safe ["$" | "-" | "_" | "." | "+"]
local meta = rx:match("/file/test_a-b+c.lua$", opts)
ngx.say("matched: ", json.encode(opts.matched))
ngx.say("match meta: ", meta)
-- test uchar.unreserved.extra ["!" | "*" | "'" | "(" | ")" | ","]
local meta = rx:match("/file/t!e*s't,(file)", opts)
ngx.say("matched: ", json.encode(opts.matched))
ngx.say("match meta: ", meta)
}
}
--- request
GET /t
--- no_error_log
[error]
--- response_body
matched: {"_path":"/file/:filename","filename":"123&45@dd:d=test;"}
match meta: metadata /file/:filename
matched: {"_path":"/file/:filename","filename":"test_a-b+c.lua$"}
match meta: metadata /file/:filename
matched: {"_path":"/file/:filename","filename":"t!e*s't,(file)"}
match meta: metadata /file/:filename

0 comments on commit 4090ccd

Please sign in to comment.