From 4090ccd77221222349e7e50dc8d25bb71b8ad661 Mon Sep 17 00:00:00 2001 From: Gerrard-YNWA Date: Thu, 15 Apr 2021 16:27:00 +0800 Subject: [PATCH] fix: params match should not failed with special symbol (#96) --- lib/resty/radixtree.lua | 3 ++- t/parameter.t | 44 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/lib/resty/radixtree.lua b/lib/resty/radixtree.lua index df0262e..8c77935 100644 --- a/lib/resty/radixtree.lua +++ b/lib/resty/radixtree.lua @@ -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) diff --git a/t/parameter.t b/t/parameter.t index 675a228..49daf92 100644 --- a/t/parameter.t +++ b/t/parameter.t @@ -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