Skip to content

Commit

Permalink
Do not drain on HEAD requests
Browse files Browse the repository at this point in the history
  • Loading branch information
mtdowling committed Jul 1, 2016
1 parent a6ed049 commit 5b3279a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/Handler/StreamHandler.php
Expand Up @@ -105,7 +105,12 @@ private function createResponse(
$headers = \GuzzleHttp\headers_from_lines($hdrs);
list ($stream, $headers) = $this->checkDecode($options, $headers, $stream);
$stream = Psr7\stream_for($stream);
$sink = $this->createSink($stream, $options);
$sink = $stream;

if (strcasecmp('HEAD', $request->getMethod())) {
$sink = $this->createSink($stream, $options);
}

$response = new Psr7\Response($status, $headers, $sink, $ver, $reason);

if (isset($options['on_headers'])) {
Expand All @@ -118,6 +123,8 @@ private function createResponse(
}
}

// Do not drain when the request is a HEAD request because they have
// no body.
if ($sink !== $stream) {
$this->drain(
$stream,
Expand Down
19 changes: 19 additions & 0 deletions tests/Handler/StreamHandlerTest.php
Expand Up @@ -160,6 +160,25 @@ public function testDrainsResponseAndReadsOnlyContentLengthBytes()
fclose($stream);
}

public function testDoesNotDrainWhenHeadRequest()
{
Server::flush();
// Say the content-length is 8, but return no response.
Server::enqueue([
new Response(200, [
'Foo' => 'Bar',
'Content-Length' => 8,
], '')
]);
$handler = new StreamHandler();
$request = new Request('HEAD', Server::$url);
$response = $handler($request, [])->wait();
$body = $response->getBody();
$stream = $body->detach();
$this->assertEquals('', stream_get_contents($stream));
fclose($stream);
}

public function testAutomaticallyDecompressGzip()
{
Server::flush();
Expand Down

0 comments on commit 5b3279a

Please sign in to comment.