Skip to content

Commit 21167f5

Browse files
committed
Merge branch 'improve-keepalive' of https://github.com/kleisauke/lua-resty-http
2 parents 769e8b2 + cf957cf commit 21167f5

File tree

2 files changed

+72
-14
lines changed

2 files changed

+72
-14
lines changed

lib/resty/http.lua

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ function _M.send_request(self, params)
594594
if params_headers then
595595
-- We assign one by one so that the metatable can handle case insensitivity
596596
-- for us. You can blame the spec for this inefficiency.
597-
for k,v in pairs(params_headers) do
597+
for k, v in pairs(params_headers) do
598598
headers[k] = v
599599
end
600600
end
@@ -685,8 +685,8 @@ function _M.read_response(self, params)
685685
-- keepalive is true by default. Determine if this is correct or not.
686686
local ok, connection = pcall(str_lower, res_headers["Connection"])
687687
if ok then
688-
if (version == 1.1 and connection == "close") or
689-
(version == 1.0 and connection ~= "keep-alive") then
688+
if (version == 1.1 and str_find(connection, "close", 1, true)) or
689+
(version == 1.0 and not str_find(connection, "keep-alive", 1, true)) then
690690
self.keepalive = false
691691
end
692692
else
@@ -738,7 +738,7 @@ end
738738

739739

740740
function _M.request(self, params)
741-
params = tbl_copy(params) -- Take by value
741+
params = tbl_copy(params) -- Take by value
742742
local res, err = self:send_request(params)
743743
if not res then
744744
return res, err
@@ -749,7 +749,7 @@ end
749749

750750

751751
function _M.request_pipeline(self, requests)
752-
requests = tbl_copy(requests) -- Take by value
752+
requests = tbl_copy(requests) -- Take by value
753753

754754
for _, params in ipairs(requests) do
755755
if params.headers and params.headers["Expect"] == "100-continue" then
@@ -793,7 +793,7 @@ end
793793

794794

795795
function _M.request_uri(self, uri, params)
796-
params = tbl_copy(params or {}) -- Take by value
796+
params = tbl_copy(params or {}) -- Take by value
797797

798798
local parsed_uri, err = self:parse_uri(uri, false)
799799
if not parsed_uri then
@@ -929,18 +929,18 @@ function _M.get_client_body_reader(_, chunksize, sock)
929929
-- Not yet supported by ngx_lua but should just work...
930930
return _chunked_body_reader(sock, chunksize)
931931
else
932-
return nil
932+
return nil
933933
end
934934
end
935935

936936

937937
function _M.proxy_request(self, chunksize)
938-
return self:request{
938+
return self:request({
939939
method = ngx_req_get_method(),
940940
path = ngx_re_gsub(ngx_var.uri, "\\s", "%20", "jo") .. ngx_var.is_args .. (ngx_var.query_string or ""),
941941
body = self:get_client_body_reader(chunksize),
942942
headers = ngx_req_get_headers(),
943-
}
943+
})
944944
end
945945

946946

@@ -953,7 +953,7 @@ function _M.proxy_response(_, response, chunksize)
953953
ngx.status = response.status
954954

955955
-- Filter out hop-by-hop headeres
956-
for k,v in pairs(response.headers) do
956+
for k, v in pairs(response.headers) do
957957
if not HOP_BY_HOP_HEADERS[str_lower(k)] then
958958
ngx_header[k] = v
959959
end
@@ -979,7 +979,7 @@ end
979979

980980

981981
function _M.set_proxy_options(self, opts)
982-
self.proxy_opts = tbl_copy(opts) -- Take by value
982+
self.proxy_opts = tbl_copy(opts) -- Take by value
983983
end
984984

985985

@@ -1003,9 +1003,9 @@ function _M.get_proxy_uri(self, scheme, host)
10031003
no_proxy_set[host_suffix[1]] = true
10041004
end
10051005

1006-
-- From curl docs:
1007-
-- matched as either a domain which contains the hostname, or the
1008-
-- hostname itself. For example local.com would match local.com,
1006+
-- From curl docs:
1007+
-- matched as either a domain which contains the hostname, or the
1008+
-- hostname itself. For example local.com would match local.com,
10091009
-- local.com:80, and www.local.com, but not www.notlocal.com.
10101010
--
10111011
-- Therefore, we keep stripping subdomains from the host, compare

t/07-keepalive.t

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,3 +311,61 @@ keep-alive
311311
--- no_error_log
312312
[error]
313313
[warn]
314+
315+
=== TEST 7: Generic interface, HTTP 1.1, Connection: Upgrade, close. Test we don't try to keepalive, but also that subsequent connections can keepalive.
316+
--- http_config eval: $::HttpConfig
317+
--- config
318+
location = /a {
319+
content_by_lua_block {
320+
local http = require "resty.http"
321+
local httpc = http.new()
322+
323+
-- Create a TCP connection and return an raw HTTP-response because
324+
-- there is no way to set an "Connection: Upgrade, close" header in nginx.
325+
assert(httpc:connect("127.0.0.1", 12345),
326+
"connect should return positively")
327+
328+
local res = httpc:request({
329+
version = 1.1,
330+
path = "/b",
331+
})
332+
333+
local body = res:read_body()
334+
ngx.print(body)
335+
336+
ngx.say(res.headers["Connection"])
337+
338+
local r, e = httpc:set_keepalive()
339+
ngx.say(r)
340+
ngx.say(e)
341+
342+
httpc:connect("127.0.0.1", ngx.var.server_port)
343+
ngx.say(httpc:get_reused_times())
344+
345+
httpc:set_keepalive()
346+
347+
httpc:connect("127.0.0.1", ngx.var.server_port)
348+
ngx.say(httpc:get_reused_times())
349+
}
350+
}
351+
--- tcp_listen: 12345
352+
--- tcp_reply
353+
HTTP/1.1 200 OK
354+
Date: Wed, 08 Aug 2018 17:00:00 GMT
355+
Server: Apache/2
356+
Upgrade: h2,h2c
357+
Connection: Upgrade, close
358+
359+
OK
360+
--- request
361+
GET /a
362+
--- response_body
363+
OK
364+
Upgrade, close
365+
2
366+
connection must be closed
367+
0
368+
1
369+
--- no_error_log
370+
[error]
371+
[warn]

0 commit comments

Comments
 (0)