From b2c032159fc221babc7002244aedd62268b68619 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BD=97=E6=B3=BD=E8=BD=A9?= Date: Wed, 20 Jan 2021 02:50:25 -0600 Subject: [PATCH] fix: host match should ignore case. (#88) --- lib/resty/radixtree.lua | 12 +++++--- t/host.t | 64 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 4 deletions(-) diff --git a/lib/resty/radixtree.lua b/lib/resty/radixtree.lua index 5bbe6af4..df0262ef 100644 --- a/lib/resty/radixtree.lua +++ b/lib/resty/radixtree.lua @@ -49,12 +49,11 @@ local newproxy = newproxy local cur_level = ngx.config.subsystem == "http" and require("ngx.errlog").get_sys_filter_level() local ngx_var = ngx.var -local re_find = ngx.re.find local re_match = ngx.re.match local ngx_re = require("ngx.re") -local ngx_null = ngx.null local empty_table = {} local str_find = string.find +local str_lower = string.lower setmetatable(empty_table, {__newindex = function() @@ -132,7 +131,7 @@ end local _M = { _VERSION = 1.7 } -- expose radix tree api for test -_M._symbols = radix +_M._symbols = radix local function has_suffix(s, suffix) @@ -304,13 +303,14 @@ function pre_insert_route(self, path, route, global_opts) h = h:sub(2) end + h = str_lower(h) insert_tab(route_opts.hosts, is_wildcard) insert_tab(route_opts.hosts, h) end elseif type(hosts) == "string" then local is_wildcard = false - local host = hosts + local host = str_lower(hosts) if host:sub(1, 1) == '*' then is_wildcard = true host = host:sub(2) @@ -624,6 +624,10 @@ end local function match_route(self, path, opts, args) + if opts.host then + opts.host = str_lower(opts.host) + end + if opts.matched then clear_tab(opts.matched) end diff --git a/t/host.t b/t/host.t index 3ffc218d..7945e15a 100644 --- a/t/host.t +++ b/t/host.t @@ -190,3 +190,67 @@ nil metadata /aa metadata /aa metadata /aa + + + +=== TEST 7: hosts contains uppercase +--- config + location /t { + content_by_lua_block { + local radix = require("resty.radixtree") + local rx = radix.new({ + { + paths = {"/aa*"}, + metadata = "metadata /aa", + hosts = {"foo.cOm"}, + } + }) + + ngx.say(rx:match("/aa/bb", {host = "foo.com"})) + ngx.say(rx:match("/aa/bb", {host = "www.foo.com"})) + + local opts = {host = "foo.com", matched = {}} + rx:match("/aa/bb", opts) + ngx.say("matched: ", opts.matched._host) + } + } +--- request +GET /t +--- no_error_log +[error] +--- response_body +metadata /aa +nil +matched: foo.com + + + +=== TEST 8: opt.host contains uppercase +--- config + location /t { + content_by_lua_block { + local radix = require("resty.radixtree") + local rx = radix.new({ + { + paths = {"/aa*"}, + metadata = "metadata /aa", + hosts = {"foo.com"}, + } + }) + + ngx.say(rx:match("/aa/bb", {host = "foo.com"})) + ngx.say(rx:match("/aa/bb", {host = "www.foo.com"})) + + local opts = {host = "foo.cOm", matched = {}} + rx:match("/aa/bb", opts) + ngx.say("matched: ", opts.matched._host) + } + } +--- request +GET /t +--- no_error_log +[error] +--- response_body +metadata /aa +nil +matched: foo.com