Skip to content

Commit

Permalink
Send Connection: close for HTTP/1.1 and no Connection for HTTP/1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
clue committed Jan 18, 2023
1 parent ef1f43e commit 13e6cd3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/Io/Sender.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,10 @@ public function send(RequestInterface $request)
}

// automatically add `Connection: close` request header for HTTP/1.1 requests to avoid connection reuse
if ($request->getProtocolVersion() === '1.1' && !$request->hasHeader('Connection')) {
if ($request->getProtocolVersion() === '1.1') {
$request = $request->withHeader('Connection', 'close');
} else {
$request = $request->withoutHeader('Connection');
}

// automatically add `Authorization: Basic …` request header if URL includes `user:pass@host`
Expand Down
20 changes: 17 additions & 3 deletions tests/Io/SenderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,20 @@ public function getHttp10RequestShouldSendAGetRequestWithoutConnectionHeaderByDe
$sender->send($request);
}

/** @test */
public function getHttp10RequestShouldSendAGetRequestWithoutConnectionHeaderEvenWhenConnectionKeepAliveHeaderIsSpecified()
{
$client = $this->getMockBuilder('React\Http\Client\Client')->disableOriginalConstructor()->getMock();
$client->expects($this->once())->method('request')->with($this->callback(function (RequestInterface $request) {
return !$request->hasHeader('Connection');
}))->willReturn($this->getMockBuilder('React\Http\Io\ClientRequestStream')->disableOriginalConstructor()->getMock());

$sender = new Sender($client);

$request = new Request('GET', 'http://www.example.com', array('Connection' => 'keep-alive'), '', '1.0');
$sender->send($request);
}

/** @test */
public function getHttp11RequestShouldSendAGetRequestWithConnectionCloseHeaderByDefault()
{
Expand All @@ -317,16 +331,16 @@ public function getHttp11RequestShouldSendAGetRequestWithConnectionCloseHeaderBy
}

/** @test */
public function getHttp11RequestShouldSendAGetRequestWithGivenConnectionUpgradeHeader()
public function getHttp11RequestShouldSendAGetRequestWithConnectionCloseHeaderEvenWhenConnectionKeepAliveHeaderIsSpecified()
{
$client = $this->getMockBuilder('React\Http\Client\Client')->disableOriginalConstructor()->getMock();
$client->expects($this->once())->method('request')->with($this->callback(function (RequestInterface $request) {
return $request->getHeaderLine('Connection') === 'upgrade';
return $request->getHeaderLine('Connection') === 'close';
}))->willReturn($this->getMockBuilder('React\Http\Io\ClientRequestStream')->disableOriginalConstructor()->getMock());

$sender = new Sender($client);

$request = new Request('GET', 'http://www.example.com', array('Connection' => 'upgrade'), '', '1.1');
$request = new Request('GET', 'http://www.example.com', array('Connection' => 'keep-alive'), '', '1.1');
$sender->send($request);
}

Expand Down

0 comments on commit 13e6cd3

Please sign in to comment.