From 783e0345020bdb350392c1108e4999bb062c0fd1 Mon Sep 17 00:00:00 2001 From: abu-tahir-0 Date: Tue, 24 Nov 2026 06:35:40 +0530 Subject: [PATCH] fix: close HTTP/1.0 responses without a known body length Only retain HTTP/1.0 keep-alive when the response boundary is known or no response body is expected. --- src/Io/StreamingServer.php | 7 +-- tests/FunctionalHttpServerTest.php | 34 +++++++++++++ tests/Io/StreamingServerTest.php | 77 ++++++++++++++++++++++++++++++ 3 files changed, 115 insertions(+), 3 deletions(-) diff --git a/src/Io/StreamingServer.php b/src/Io/StreamingServer.php index 6d12d359..14c1f175 100644 --- a/src/Io/StreamingServer.php +++ b/src/Io/StreamingServer.php @@ -242,6 +242,7 @@ public function handleResponse(ConnectionInterface $connection, ServerRequestInt $code = $response->getStatusCode(); $method = $request->getMethod(); + $bodyless = $method === 'HEAD' || ($code >= 100 && $code < 200 && $code !== Response::STATUS_SWITCHING_PROTOCOLS) || $code === Response::STATUS_NO_CONTENT || $code === Response::STATUS_NOT_MODIFIED; // assign HTTP protocol version from request automatically $version = $request->getProtocolVersion(); @@ -300,8 +301,8 @@ public function handleResponse(ConnectionInterface $connection, ServerRequestInt } elseif ($version === '1.1') { // HTTP/1.1 assumes persistent connection support by default, so we don't need to inform client $persist = true; - } elseif (strtolower($request->getHeaderLine('Connection')) === 'keep-alive') { - // obey explicit "Connection: keep-alive" request header and inform client + } elseif (strtolower($request->getHeaderLine('Connection')) === 'keep-alive' && ($bodyless || $response->hasHeader('Content-Length') || ($method === 'CONNECT' && $code >= 200 && $code < 300))) { + // obey explicit "Connection: keep-alive" request header if response body length is known or no body is expected $persist = true; $response = $response->withHeader('Connection', 'keep-alive'); } else { @@ -349,7 +350,7 @@ public function handleResponse(ConnectionInterface $connection, ServerRequestInt // response to HEAD and 1xx, 204 and 304 responses MUST NOT include a body // exclude status 101 (Switching Protocols) here for Upgrade request handling above - if ($method === 'HEAD' || ($code >= 100 && $code < 200 && $code !== Response::STATUS_SWITCHING_PROTOCOLS) || $code === Response::STATUS_NO_CONTENT || $code === Response::STATUS_NOT_MODIFIED) { + if ($bodyless) { $body->close(); $body = ''; } diff --git a/tests/FunctionalHttpServerTest.php b/tests/FunctionalHttpServerTest.php index a146067c..866fe947 100644 --- a/tests/FunctionalHttpServerTest.php +++ b/tests/FunctionalHttpServerTest.php @@ -464,6 +464,40 @@ public function testClosedStreamFromRequestHandlerWillSendEmptyBody() $socket->close(); } + public function testHttp10StreamingResponseWithKeepAliveWillCloseConnectionAfterBody() + { + $connector = new Connector(['dns' => false]); + + $stream = new ThroughStream(); + + $http = new HttpServer(function (RequestInterface $request) use ($stream) { + Loop::futureTick(function () use ($stream) { + $stream->end("hello\n"); + }); + + return new Response(200, [], $stream); + }); + + $socket = new SocketServer('127.0.0.1:0'); + $http->listen($socket); + + try { + $result = $connector->connect($socket->getAddress())->then(function (ConnectionInterface $conn) { + $conn->write("GET / HTTP/1.0\r\nConnection: keep-alive\r\n\r\n"); + + return buffer($conn); + }); + + $response = await(timeout($result, 1.0)); + } finally { + $socket->close(); + } + + $this->assertStringStartsWith("HTTP/1.0 200 OK\r\n", $response); + $this->assertStringNotContainsString("Connection: keep-alive", $response); + $this->assertStringEndsWith("\r\n\r\nhello\n", $response); + } + public function testRequestHandlerWithStreamingRequestWillReceiveCloseEventIfConnectionClosesWhileSendingBody() { $connector = new Connector(); diff --git a/tests/Io/StreamingServerTest.php b/tests/Io/StreamingServerTest.php index 1a6fcad7..8176de63 100644 --- a/tests/Io/StreamingServerTest.php +++ b/tests/Io/StreamingServerTest.php @@ -3196,6 +3196,83 @@ public function testNewConnectionWillInvokeParserTwiceAfterInvokingRequestHandle $server->handleRequest($this->connection, $request); } + public function testNewConnectionWillCloseConnectionAfterStreamingResponseBodyWithUnknownLengthForHttp10ConnectionKeepAlive() + { + $request = new ServerRequest('GET', 'http://localhost/', ['Connection' => 'keep-alive'], '', '1.0'); + + $body = new ThroughStream(); + $server = new StreamingServer(Loop::get(), function () use ($body) { + return new Response(200, [], $body); + }); + + $parser = $this->createMock(RequestHeaderParser::class); + $parser->expects($this->once())->method('handle'); + + $ref = new \ReflectionProperty($server, 'parser'); + if (PHP_VERSION_ID < 80100) { + $ref->setAccessible(true); + } + $ref->setValue($server, $parser); + + $server->listen($this->socket); + $this->socket->emit('connection', [$this->connection]); + + $buffer = ''; + $this->connection + ->expects($this->exactly(2)) + ->method('write') + ->willReturnCallback(function ($data) use (&$buffer) { + $buffer .= $data; + }); + $this->connection->expects($this->once())->method('end'); + + // pretend parser just finished parsing + $server->handleRequest($this->connection, $request); + $body->end('hello'); + + $this->assertStringNotContainsString("Connection: keep-alive", $buffer); + $this->assertStringEndsWith("\r\n\r\nhello", $buffer); + } + + public function testNewConnectionWillInvokeParserTwiceAfterStreamingResponseBodyWithContentLengthForHttp10ConnectionKeepAlive() + { + $request = new ServerRequest('GET', 'http://localhost/', ['Connection' => 'keep-alive'], '', '1.0'); + + $body = new ThroughStream(); + $server = new StreamingServer(Loop::get(), function () use ($body) { + return new Response(200, ['Content-Length' => 5], $body); + }); + + $parser = $this->createMock(RequestHeaderParser::class); + $parser->expects($this->exactly(2))->method('handle'); + + $ref = new \ReflectionProperty($server, 'parser'); + if (PHP_VERSION_ID < 80100) { + $ref->setAccessible(true); + } + $ref->setValue($server, $parser); + + $server->listen($this->socket); + $this->socket->emit('connection', [$this->connection]); + + $buffer = ''; + $this->connection + ->expects($this->exactly(2)) + ->method('write') + ->willReturnCallback(function ($data) use (&$buffer) { + $buffer .= $data; + }); + $this->connection->expects($this->never())->method('end'); + + // pretend parser just finished parsing + $server->handleRequest($this->connection, $request); + $body->end('hello'); + + $this->assertStringContainsString("Content-Length: 5\r\n", $buffer); + $this->assertStringContainsString("Connection: keep-alive\r\n", $buffer); + $this->assertStringEndsWith("\r\n\r\nhello", $buffer); + } + public function testNewConnectionWillInvokeParserOnceAfterInvokingRequestHandlerWhenStreamingResponseBodyKeepsStreaming() { $request = new ServerRequest('GET', 'http://localhost/');