Skip to content

Commit b8a1542

Browse files
authored
fix(fixtures) listen for ipv6 in mock webserver (Kong#6736)
* fix(fixtures) listen for ipv6 in mock webserver * chore(fixtures) IPv6 address cleanup and HTTP/2 support
1 parent bbc35f5 commit b8a1542

File tree

2 files changed

+42
-9
lines changed

2 files changed

+42
-9
lines changed

spec/fixtures/https_server.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ math.randomseed(os.time())
1818

1919

2020
local tmp_root = os.getenv("TMPDIR") or "/tmp"
21+
local host_regex = [[([a-z0-9\-._~%!$&'()*+,;=]+@)?([a-z0-9\-._~%]+|\[[a-z0-9\-._~%!$&'()*+,;=:]+\])(:?[0-9]+)*]]
2122

2223

2324
local function create_temp_dir(copy_cert_and_key)
@@ -87,7 +88,10 @@ local function count_results(logs_dir)
8788
local status = m[2]
8889
local host = m[3]
8990
if host then
90-
host = string.match(m[3], "[^:]+")
91+
local host_no_port = ngx.re.match(m[3], host_regex)
92+
if host_no_port then
93+
host = host_no_port[1]
94+
end
9195
else
9296
host = "nonamehost"
9397
end

spec/fixtures/mock_webserver_tpl.lua

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@ http {
2828
2929
server {
3030
# if protocol ~= 'https' then
31-
listen ${http_port};
31+
listen 127.0.0.1:${http_port};
32+
listen [::1]:${http_port};
3233
# else
33-
listen ${http_port} ssl;
34+
listen 127.0.0.1:${http_port} ssl http2;
35+
listen [::1]:${http_port} ssl http2;
3436
ssl_certificate ${cert_path}/kong_spec.crt;
3537
ssl_certificate_key ${cert_path}/kong_spec.key;
3638
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
@@ -43,7 +45,12 @@ http {
4345
location = /healthy {
4446
access_by_lua_block {
4547
local host = ngx.req.get_headers()["host"] or "localhost"
46-
host = string.match(host, "[^:]+")
48+
local host_no_port = ngx.re.match(host, [=[([a-z0-9\-._~%!$&'()*+,;=]+@)?([a-z0-9\-._~%]+|\[[a-z0-9\-._~%!$&'()*+,;=:]+\])(:?[0-9]+)*]=])
49+
if host_no_port == nil then
50+
return ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
51+
else
52+
host = host_no_port[2]
53+
end
4754
ngx.shared.server_values:set(host .. "_healthy", true)
4855
ngx.shared.server_values:set(host .. "_timeout", false)
4956
ngx.log(ngx.INFO, "Host ", host, " is now healthy")
@@ -58,7 +65,12 @@ http {
5865
location = /unhealthy {
5966
access_by_lua_block {
6067
local host = ngx.req.get_headers()["host"] or "localhost"
61-
host = string.match(host, "[^:]+")
68+
local host_no_port = ngx.re.match(host, [=[([a-z0-9\-._~%!$&'()*+,;=]+@)?([a-z0-9\-._~%]+|\[[a-z0-9\-._~%!$&'()*+,;=:]+\])(:?[0-9]+)*]=])
69+
if host_no_port == nil then
70+
return ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
71+
else
72+
host = host_no_port[2]
73+
end
6274
ngx.shared.server_values:set(host .. "_healthy", false)
6375
ngx.log(ngx.INFO, "Host ", host, " is now unhealthy")
6476
}
@@ -72,7 +84,12 @@ http {
7284
location = /timeout {
7385
access_by_lua_block {
7486
local host = ngx.req.get_headers()["host"] or "localhost"
75-
host = string.match(host, "[^:]+")
87+
local host_no_port = ngx.re.match(host, [=[([a-z0-9\-._~%!$&'()*+,;=]+@)?([a-z0-9\-._~%]+|\[[a-z0-9\-._~%!$&'()*+,;=:]+\])(:?[0-9]+)*]=])
88+
if host_no_port == nil then
89+
return ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
90+
else
91+
host = host_no_port[2]
92+
end
7693
ngx.shared.server_values:set(host .. "_timeout", true)
7794
ngx.log(ngx.INFO, "Host ", host, " is timeouting now")
7895
}
@@ -86,7 +103,12 @@ http {
86103
location = /status {
87104
access_by_lua_block {
88105
local host = ngx.req.get_headers()["host"] or "localhost"
89-
host = string.match(host, "[^:]+")
106+
local host_no_port = ngx.re.match(host, [=[([a-z0-9\-._~%!$&'()*+,;=]+@)?([a-z0-9\-._~%]+|\[[a-z0-9\-._~%!$&'()*+,;=:]+\])(:?[0-9]+)*]=])
107+
if host_no_port == nil then
108+
return ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
109+
else
110+
host = host_no_port[2]
111+
end
90112
local server_values = ngx.shared.server_values
91113
92114
local status = server_values:get(host .. "_healthy") and
@@ -109,7 +131,13 @@ http {
109131
local cjson = require("cjson")
110132
local server_values = ngx.shared.server_values
111133
local host = ngx.req.get_headers()["host"] or "localhost"
112-
host = string.match(host, "[^:]+")
134+
local host_no_port = ngx.re.match(host, [=[([a-z0-9\-._~%!$&'()*+,;=]+@)?([a-z0-9\-._~%]+|\[[a-z0-9\-._~%!$&'()*+,;=:]+\])(:?[0-9]+)*]=])
135+
ngx.log(ngx.ERR, "host no port: ", require'inspect'(host_no_port))
136+
if host_no_port == nil then
137+
return ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
138+
else
139+
host = host_no_port[2]
140+
end
113141
local status
114142
115143
local status = server_values:get(host .. "_healthy") and
@@ -129,7 +157,8 @@ http {
129157
}
130158
# if check_hostname then
131159
server {
132-
listen ${http_port} default_server;
160+
listen 127.0.0.1:${http_port} default_server;
161+
listen [::1]:${http_port} default_server;
133162
server_name _;
134163
return 400;
135164
}

0 commit comments

Comments
 (0)