Skip to content

Commit af8b08b

Browse files
committed
followup: minor fixes according to pr comments
This includes: * Add set_proxy_options to README TOC. * Make a copy of proxy options gotten from the user. * Rewrite no_proxy matching to use ngx.re.gmatch and friends instead of Lua's built-in gmatch functions. Since the regular expression formats are completely different, the no_proxy matching process had to be modified a bit. * Fix 14-host-header.t on systems with ipv6 support by disabling ipv6 address resolution. * Make 16-http-proxy.t more reliable by ignoring the order of the headers in the CONNECT request (the order is not guaranteed). The new check uses a regular expression to check that the CONNECT line is correct and that the headers include correct Host in some position.
1 parent b0e6fc4 commit af8b08b

File tree

4 files changed

+23
-15
lines changed

4 files changed

+23
-15
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Production ready.
2323
* [new](#new)
2424
* [connect](#connect)
2525
* [connect_proxy](#connect_proxy)
26+
* [set_proxy_options](#set_proxy_options)
2627
* [set_timeout](#set_timeout)
2728
* [set_timeouts](#set_timeouts)
2829
* [ssl_handshake](#ssl_handshake)

lib/resty/http.lua

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ local tbl_concat = table.concat
1515
local tbl_insert = table.insert
1616
local ngx_encode_args = ngx.encode_args
1717
local ngx_re_match = ngx.re.match
18+
local ngx_re_gmatch = ngx.re.gmatch
19+
local ngx_re_sub = ngx.re.sub
1820
local ngx_re_gsub = ngx.re.gsub
1921
local ngx_re_find = ngx.re.find
2022
local ngx_log = ngx.log
@@ -958,7 +960,7 @@ function _M.proxy_response(self, response, chunksize)
958960
end
959961

960962
function _M.set_proxy_options(self, opts)
961-
self.proxy_opts = opts
963+
self.proxy_opts = tbl_copy(opts) -- Take by value
962964
end
963965

964966
function _M.get_proxy_uri(self, scheme, host)
@@ -977,20 +979,27 @@ function _M.get_proxy_uri(self, scheme, host)
977979
local no_proxy_set = {}
978980
-- wget allows domains in no_proxy list to be prefixed by "."
979981
-- e.g. no_proxy=.mit.edu
980-
for host_suffix in self.proxy_opts.no_proxy:gmatch("%.?([^,]+)") do
981-
no_proxy_set[host_suffix] = true
982+
for host_suffix in ngx_re_gmatch(self.proxy_opts.no_proxy, "\\.?([^,]+)") do
983+
no_proxy_set[host_suffix[1]] = true
982984
end
983985

984986
-- From curl docs:
985987
-- matched as either a domain which contains the hostname, or the
986988
-- hostname itself. For example local.com would match local.com,
987-
-- local.com:80, and www.local.com, but not www.notlocal.com.
988-
for pos in host:gmatch("%f[^%z%.]()") do
989-
local host_suffix = host:sub(pos, -1)
990-
if no_proxy_set[host_suffix] then
991-
return nil
992-
end
993-
end
989+
-- local.com:80, and www.local.com, but not www.notlocal.com.
990+
--
991+
-- Therefore, we keep stripping subdomains from the host, compare
992+
-- them to the ones in the no_proxy list and continue until we find
993+
-- a match or until there's only the TLD left
994+
repeat
995+
if no_proxy_set[host] then
996+
return nil
997+
end
998+
999+
-- Strip the next level from the domain and check if that one
1000+
-- is on the list
1001+
host = ngx_re_sub(host, "^[^.]+\\.", "")
1002+
until not ngx_re_find(host, "\\.")
9941003
end
9951004

9961005
if scheme == "http" and self.proxy_opts.http_proxy then

t/14-host-header.t

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ $ENV{TEST_COVERAGE} ||= 0;
1212
our $HttpConfig = qq{
1313
lua_package_path "$pwd/lib/?.lua;/usr/local/share/lua/5.1/?.lua;;";
1414
error_log logs/error.log debug;
15-
resolver 8.8.8.8;
15+
resolver 8.8.8.8 ipv6=off;
1616
1717
init_by_lua_block {
1818
if $ENV{TEST_COVERAGE} == 1 then

t/16-http-proxy.t

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -279,10 +279,8 @@ GET /lua
279279
}
280280
}
281281
--- tcp_listen: 12345
282-
--- tcp_query eval_stdout
283-
# Note: The incoming request contains CRLF line endings and print needs to
284-
# be used here to get the same line breaks to the expected request
285-
print "CONNECT 127.0.0.1:443 HTTP/1.1\r\nUser-Agent: test_ua\r\nHost: 127.0.0.1:443\r\n\r\n"
282+
--- tcp_query eval
283+
qr/CONNECT 127.0.0.1:443 HTTP\/1.1\r\n.*Host: 127.0.0.1:443\r\n.*/s
286284

287285
# The reply cannot be successful or otherwise the client would start
288286
# to do a TLS handshake with the proxied host and that we cannot

0 commit comments

Comments
 (0)