Skip to content

Commit

Permalink
fix: host match should ignore case. (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
spacewander authored Jan 20, 2021
1 parent 7ef9641 commit b2c0321
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 4 deletions.
12 changes: 8 additions & 4 deletions lib/resty/radixtree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
64 changes: 64 additions & 0 deletions t/host.t
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit b2c0321

Please sign in to comment.