From 2d4feca43c448123c27a46aee0e6db239607011e Mon Sep 17 00:00:00 2001 From: Guy Lewin Date: Fri, 10 Dec 2021 15:10:16 -0500 Subject: [PATCH] fix: mark keepalive_ready if no body and no trailer --- lib/resty/http.lua | 5 +++++ t/07-keepalive.t | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/lib/resty/http.lua b/lib/resty/http.lua index 8e17f0d..e1239fd 100644 --- a/lib/resty/http.lua +++ b/lib/resty/http.lua @@ -855,6 +855,11 @@ function _M.read_response(self, params) body_reader, err = _body_reader(body_reader_keepalive_ready_callback, sock, length) end + else + if not has_trailer then + -- If there's no body and no trailer - it's ready for keep-alive + self.keepalive_ready = true + end end if has_trailer then diff --git a/t/07-keepalive.t b/t/07-keepalive.t index 61e6ac0..0dc661e 100644 --- a/t/07-keepalive.t +++ b/t/07-keepalive.t @@ -601,3 +601,48 @@ response not fully read --- no_error_log [error] [warn] + +=== TEST 12 pool_only_after_response is on. Test the connection is reused on non-body requests. +--- http_config eval: $::HttpConfig +--- config + location = /a { + content_by_lua ' + local http = require "resty.http" + local httpc = http.new() + httpc:connect({ + scheme = "http", + host = "127.0.0.1", + port = ngx.var.server_port, + pool_only_after_response = true + }) + + local res, err = httpc:request{ + method = "HEAD", + path = "/b" + } + + ngx.say(res.headers["Connection"]) + ngx.say(httpc:set_keepalive()) + + httpc:connect({ + scheme = "http", + host = "127.0.0.1", + port = ngx.var.server_port + }) + ngx.say(httpc:get_reused_times()) + '; + } + location = /b { + content_by_lua ' + ngx.say("OK") + '; + } +--- request +GET /a +--- response_body +keep-alive +1 +1 +--- no_error_log +[error] +[warn]