Skip to content

Commit fa59489

Browse files
authored
fix: correct the regex to match host & port (#92)
2 parents 0a2cf68 + 9d9b620 commit fa59489

File tree

2 files changed

+74
-2
lines changed

2 files changed

+74
-2
lines changed

lib/resty/etcd/v3.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,10 @@ function _M.new(opts)
146146
end
147147

148148
for _, host in ipairs(http_hosts) do
149-
local m, err = re_match(host, [[([^\/]+)\:\/\/([\d.\w]+):(\d+)]], "jo")
149+
local m, err = re_match(host,
150+
[=[([^\/]+):\/\/([\da-zA-Z.-]+|\[[\da-fA-F:]+\]):?(\d+)?$]=], "jo")
150151
if not m then
151-
return nil, "invalid http host: " .. err
152+
return nil, "invalid http host: " .. host .. ", err: " .. (err or "not matched")
152153
end
153154

154155
tab_insert(endpoints, {

t/v3/opt.t

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
use Test::Nginx::Socket::Lua;
2+
3+
log_level('info');
4+
no_long_string();
5+
repeat_each(1);
6+
7+
my $etcd_version = `etcd --version`;
8+
if ($etcd_version =~ /^etcd Version: 2/ || $etcd_version =~ /^etcd Version: 3.1./) {
9+
plan(skip_all => "etcd is too old, skip v3 protocol");
10+
} else {
11+
plan 'no_plan';
12+
}
13+
14+
add_block_preprocessor(sub {
15+
my ($block) = @_;
16+
17+
$block->set_value("request", "GET /t");
18+
$block->set_value("no_error_log", "[error]");
19+
20+
my $http_config = <<_EOC_;
21+
lua_package_path 'lib/?.lua;;';
22+
_EOC_
23+
$block->set_value("http_config", $http_config);
24+
25+
$block;
26+
});
27+
28+
run_tests();
29+
30+
__DATA__
31+
32+
=== TEST 1: http_host
33+
--- config
34+
location /t {
35+
content_by_lua_block {
36+
local cases = {
37+
"http://127.0.0.1:2973",
38+
"http://127.0.0.1",
39+
"https://127.0.0.1:12000",
40+
"http://c.cn:9000",
41+
"http://c-a.cn:9000",
42+
"http://c_a.cn",
43+
"http://[ab::0]:2971",
44+
"http://[ab::0]",
45+
}
46+
for _, case in ipairs(cases) do
47+
local etcd, err = require "resty.etcd" .new({protocol = "v3", api_prefix = "/v3",
48+
http_host = case})
49+
if not etcd then
50+
ngx.say(err)
51+
else
52+
ngx.say(
53+
etcd.endpoints[1].scheme, " ",
54+
etcd.endpoints[1].host, " ",
55+
etcd.endpoints[1].port)
56+
end
57+
end
58+
59+
ngx.say('ok')
60+
}
61+
}
62+
--- response_body
63+
http 127.0.0.1 2973
64+
http 127.0.0.1 2379
65+
https 127.0.0.1 12000
66+
http c.cn 9000
67+
http c-a.cn 9000
68+
invalid http host: http://c_a.cn, err: not matched
69+
http [ab::0] 2971
70+
http [ab::0] 2379
71+
ok

0 commit comments

Comments
 (0)