Skip to content

Commit

Permalink
fix: support ip:port in host (#139)
Browse files Browse the repository at this point in the history
  • Loading branch information
theweakgod authored Jan 12, 2024
1 parent dd87111 commit ee3e37b
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/resty/radixtree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,13 @@ local function match_route_opts(route, opts, args)
if host then
local len = #hosts
for i = 1, len, 2 do
if str_find(hosts[i+1], ":", 1, true) then
if opts.vars.http_host then
host = opts.vars.http_host
end
else
host = opts.host
end
if match_host(hosts[i], hosts[i + 1], host) then
if opts_matched_exists then
if hosts[i] then
Expand Down
139 changes: 139 additions & 0 deletions t/add.t
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,142 @@ GET /t?name=json&weight=20
--- response_body
metadata add route succeed.
--- error_code: 200



=== TEST 2: test host and port
--- config
location /t {
content_by_lua_block {
local opts = {vars = {http_host = "127.0.0.1:9080"}, host = "127.0.0.1"}
local radix = require("resty.radixtree")
local rx = radix.new({
{
paths = {"/aa*"},
hosts = {"127.0.0.1:9080"},
handler = function (ctx)
ngx.say("pass")
end
}
})
ngx.say(rx:dispatch("/aa", opts))
}
}
--- request
GET /t
--- no_error_log
[error]
--- response_body
pass
true
=== TEST 3: test domain and port
--- config
location /t {
content_by_lua_block {
local opts = {vars = {http_host = "www.foo.com:9080"}, host = "www.foo.com"}
local radix = require("resty.radixtree")
local rx = radix.new({
{
paths = {"/aa*"},
hosts = "www.foo.com:9080",
handler = function (ctx)
ngx.say("pass")
end
}
})
ngx.say(rx:dispatch("/aa", opts))
}
}
--- request
GET /t
--- no_error_log
[error]
--- response_body
pass
true
=== TEST 4: match failed
--- config
location /t {
content_by_lua_block {
local opts = {vars = {http_host = "127.0.0.1"}, host = "127.0.0.1"}
local radix = require("resty.radixtree")
local rx = radix.new({
{
paths = {"/aa*"},
hosts = "127.0.0.1:9080",
handler = function (ctx)
ngx.say("pass")
end
}
})
ngx.say(rx:dispatch("/aa", opts))
}
}
--- request
GET /t
--- no_error_log
[error]
--- response_body
nil
=== TEST 5: match success
--- config
location /t {
content_by_lua_block {
local opts = {vars = {http_host = "127.0.0.1:9080"}, host = "127.0.0.1"}
local radix = require("resty.radixtree")
local rx = radix.new({
{
paths = {"/aa*"},
hosts = "127.0.0.1",
handler = function (ctx)
ngx.say("pass")
end
}
})
ngx.say(rx:dispatch("/aa", opts))
}
}
--- request
GET /t
--- no_error_log
[error]
--- response_body
pass
true
=== TEST 6: match many host
--- config
location /t {
content_by_lua_block {
local opts = {vars = {http_host = "127.0.0.1:9980"}, host = "127.0.0.1"}
local radix = require("resty.radixtree")
local rx = radix.new({
{
paths = {"/aa*"},
hosts = {"www.foo.com:9080", "127.0.0.1:9991", "www.bar.com:9200", "127.0.0.1"},
handler = function (ctx)
ngx.say("pass")
end
}
})
ngx.say(rx:dispatch("/aa", opts))
}
}
--- request
GET /t
--- no_error_log
[error]
--- response_body
pass
true

0 comments on commit ee3e37b

Please sign in to comment.