Skip to content

Commit b2c0321

Browse files
authored
fix: host match should ignore case. (#88)
1 parent 7ef9641 commit b2c0321

File tree

2 files changed

+72
-4
lines changed

2 files changed

+72
-4
lines changed

lib/resty/radixtree.lua

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,11 @@ local newproxy = newproxy
4949
local cur_level = ngx.config.subsystem == "http" and
5050
require("ngx.errlog").get_sys_filter_level()
5151
local ngx_var = ngx.var
52-
local re_find = ngx.re.find
5352
local re_match = ngx.re.match
5453
local ngx_re = require("ngx.re")
55-
local ngx_null = ngx.null
5654
local empty_table = {}
5755
local str_find = string.find
56+
local str_lower = string.lower
5857

5958

6059
setmetatable(empty_table, {__newindex = function()
@@ -132,7 +131,7 @@ end
132131
local _M = { _VERSION = 1.7 }
133132

134133
-- expose radix tree api for test
135-
_M._symbols = radix
134+
_M._symbols = radix
136135

137136

138137
local function has_suffix(s, suffix)
@@ -304,13 +303,14 @@ function pre_insert_route(self, path, route, global_opts)
304303
h = h:sub(2)
305304
end
306305

306+
h = str_lower(h)
307307
insert_tab(route_opts.hosts, is_wildcard)
308308
insert_tab(route_opts.hosts, h)
309309
end
310310

311311
elseif type(hosts) == "string" then
312312
local is_wildcard = false
313-
local host = hosts
313+
local host = str_lower(hosts)
314314
if host:sub(1, 1) == '*' then
315315
is_wildcard = true
316316
host = host:sub(2)
@@ -624,6 +624,10 @@ end
624624

625625

626626
local function match_route(self, path, opts, args)
627+
if opts.host then
628+
opts.host = str_lower(opts.host)
629+
end
630+
627631
if opts.matched then
628632
clear_tab(opts.matched)
629633
end

t/host.t

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,3 +190,67 @@ nil
190190
metadata /aa
191191
metadata /aa
192192
metadata /aa
193+
194+
195+
196+
=== TEST 7: hosts contains uppercase
197+
--- config
198+
location /t {
199+
content_by_lua_block {
200+
local radix = require("resty.radixtree")
201+
local rx = radix.new({
202+
{
203+
paths = {"/aa*"},
204+
metadata = "metadata /aa",
205+
hosts = {"foo.cOm"},
206+
}
207+
})
208+
209+
ngx.say(rx:match("/aa/bb", {host = "foo.com"}))
210+
ngx.say(rx:match("/aa/bb", {host = "www.foo.com"}))
211+
212+
local opts = {host = "foo.com", matched = {}}
213+
rx:match("/aa/bb", opts)
214+
ngx.say("matched: ", opts.matched._host)
215+
}
216+
}
217+
--- request
218+
GET /t
219+
--- no_error_log
220+
[error]
221+
--- response_body
222+
metadata /aa
223+
nil
224+
matched: foo.com
225+
226+
227+
228+
=== TEST 8: opt.host contains uppercase
229+
--- config
230+
location /t {
231+
content_by_lua_block {
232+
local radix = require("resty.radixtree")
233+
local rx = radix.new({
234+
{
235+
paths = {"/aa*"},
236+
metadata = "metadata /aa",
237+
hosts = {"foo.com"},
238+
}
239+
})
240+
241+
ngx.say(rx:match("/aa/bb", {host = "foo.com"}))
242+
ngx.say(rx:match("/aa/bb", {host = "www.foo.com"}))
243+
244+
local opts = {host = "foo.cOm", matched = {}}
245+
rx:match("/aa/bb", opts)
246+
ngx.say("matched: ", opts.matched._host)
247+
}
248+
}
249+
--- request
250+
GET /t
251+
--- no_error_log
252+
[error]
253+
--- response_body
254+
metadata /aa
255+
nil
256+
matched: foo.com

0 commit comments

Comments
 (0)