Skip to content

Commit dd87111

Browse files
bug: Trailing slash with argument is not respected (#140)
* Fix linting errors Avoid unused loops, trailing whitespaces and hiding global vars. * Trailing Slash not respected with parameter Show the issue where when an argument is specified it's also matching without the trailing slash * Handle the trailing slash As ngx.re.split will `delete empty trailing ones` we need to double check that there is no required `/` at the end of the path. If there is a extra `/` then add it to the pat regex. Also see https://github.com/openresty/lua-resty-core/blob/50f73790b6bb1d83f52cb3b4ec7e4ab7f649ab32/lib/ngx/re.lua#L273
1 parent 2f6e491 commit dd87111

File tree

2 files changed

+53
-12
lines changed

2 files changed

+53
-12
lines changed

lib/resty/radixtree.lua

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -259,9 +259,9 @@ local function modify_route(self, opts)
259259
log_err("no route array exists or route array is not a table in hash_path.", path, opts.id)
260260
return false
261261
end
262-
262+
263263
update_tab_in_order(route_arr, opts, sort_route)
264-
264+
265265
return true
266266
end
267267

@@ -279,15 +279,15 @@ local function modify_route(self, opts)
279279
end
280280

281281
update_tab_in_order(routes, opts, sort_route)
282-
282+
283283
return true
284284
end
285285

286286

287287
local function remove_route(self, opts)
288288
local path = opts.path
289289
opts = clone_tab(opts)
290-
290+
291291
if not self.disable_path_cache_opt and opts.path_op == '=' then
292292
local route_arr = self.hash_path[path]
293293
if route_arr == nil or type(route_arr) ~= "table" then
@@ -347,7 +347,7 @@ local function remove_route(self, opts)
347347
return
348348
end
349349
end
350-
350+
351351
log_err("removed route does not exist.", opts.id)
352352
return
353353
end
@@ -506,7 +506,7 @@ end
506506

507507

508508
local function pre_update_route(self, path, route, global_opts)
509-
if type(path) ~= "string" then
509+
if type(path) ~= "string" then
510510
error("invalid argument path", 2)
511511
end
512512

@@ -628,9 +628,9 @@ end
628628

629629

630630
local tmp = {}
631-
local lru_pat, err = lrucache.new(1000)
631+
local lru_pat, lru_pat_err = lrucache.new(1000)
632632
if not lru_pat then
633-
error("failed to generate new lru object: " .. err)
633+
error("failed to generate new lru object: " .. lru_pat_err)
634634
end
635635

636636

@@ -666,6 +666,11 @@ local function fetch_pat(path)
666666
end
667667

668668
pat = table.concat(res, [[\/]])
669+
-- As ngx.re.split will remove empty trailing items check if there is a trailing slash and append it
670+
if path:sub(-1) == '/' then
671+
pat = pat .. [[\/]]
672+
end
673+
669674
lru_pat:set(path, {pat, names}, 60 * 60)
670675
return pat, names
671676
end
@@ -891,24 +896,24 @@ function _M.update_route(self, pre_r, r, opts)
891896
end
892897

893898
local common = {}
894-
for k,v in pairs(pre_t) do
899+
for k in pairs(pre_t) do
895900
if t[k] ~= nil then
896901
table.insert(common, k)
897902
end
898903
end
899904

900905
for _, p in ipairs(common) do
901906
pre_update_route(self, p, r, opts)
902-
907+
903908
pre_t[p] = nil
904909
t[p] = nil
905910
end
906911

907-
for k,v in pairs(pre_t) do
912+
for k in pairs(pre_t) do
908913
pre_delete_route(self, k, pre_r, opts)
909914
end
910915

911-
for k,v in pairs(t) do
916+
for k in pairs(t) do
912917
pre_insert_route(self, k, r, opts)
913918
end
914919
end

t/parameter.t

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,3 +392,39 @@ GET /t
392392
--- response_body
393393
match meta: /user/:user
394394
match meta: /user/:user/age/:age
395+
396+
397+
398+
=== TEST 12: /name/:name/ (respect trailing slash)
399+
--- config
400+
location /t {
401+
content_by_lua_block {
402+
local json = require("toolkit.json")
403+
local radix = require("resty.radixtree")
404+
local rx = radix.new({
405+
{
406+
paths = {"/name/:name/"},
407+
metadata = "metadata /name",
408+
},
409+
})
410+
411+
local opts = {matched = {}}
412+
local meta = rx:match("/name/json/", opts)
413+
ngx.say("match meta: ", meta)
414+
ngx.say("matched: ", json.encode(opts.matched))
415+
416+
opts.matched = {}
417+
meta = rx:match("/name/json", opts)
418+
ngx.say("match meta: ", meta)
419+
ngx.say("matched: ", json.encode(opts.matched))
420+
}
421+
}
422+
--- request
423+
GET /t
424+
--- no_error_log
425+
[error]
426+
--- response_body
427+
match meta: metadata /name
428+
matched: {"_path":"/name/:name/","name":"json"}
429+
match meta: nil
430+
matched: []

0 commit comments

Comments
 (0)