Skip to content

Commit db93fbf

Browse files
committed
Improve keep-alive check
1 parent 00e6cc5 commit db93fbf

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

lib/resty/http.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

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)