Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/Io/StreamingServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 = '';
}
Expand Down
34 changes: 34 additions & 0 deletions tests/FunctionalHttpServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
77 changes: 77 additions & 0 deletions tests/Io/StreamingServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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/');
Expand Down
Loading