Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support ip:port in host #139

Merged
merged 10 commits into from
Jan 12, 2024
Merged
7 changes: 7 additions & 0 deletions lib/resty/radixtree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,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
theweakgod marked this conversation as resolved.
Show resolved Hide resolved
if opts.vars.http_host then
theweakgod marked this conversation as resolved.
Show resolved Hide resolved
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
Loading