Skip to content

Commit

Permalink
Merge pull request #46 from karneds/fix-head-requests
Browse files Browse the repository at this point in the history
fix Curl error "transfer closed with ... bytes remaining to read" with HEAD HTTP method
  • Loading branch information
Ocramius authored Dec 31, 2020
2 parents d987f82 + b32890a commit 569e661
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Client/Adapter/Curl.php
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,9 @@ public function write($method, $uri, $httpVersion = 1.1, $headers = [], $body =
curl_setopt($this->curl, CURLOPT_HTTP_VERSION, $curlHttp);
curl_setopt($this->curl, $curlMethod, $curlValue);

// Set the CURLOPT_NOBODY flag for HEAD HTTP method
curl_setopt($this->curl, CURLOPT_NOBODY, $curlMethod === CURLOPT_CUSTOMREQUEST && $curlValue === 'HEAD');

// Set the CURLINFO_HEADER_OUT flag so that we can retrieve the full request string later
curl_setopt($this->curl, CURLINFO_HEADER_OUT, true);

Expand Down
10 changes: 10 additions & 0 deletions test/Client/CurlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,16 @@ public function testHeadRequest()
$this->assertEquals('', $this->client->getResponse()->getBody());
}

public function testHeadRequestWithContentLengthHeader()
{
$this->client->setUri($this->baseuri . 'testHeadMethod.php');
$adapter = new Adapter\Curl();
$this->client->setAdapter($adapter);
$this->client->setMethod('HEAD');
$this->client->send();
$this->assertEquals('', $this->client->getResponse()->getBody());
}

public function testAuthorizeHeader()
{
// We just need someone to talk to
Expand Down
12 changes: 12 additions & 0 deletions test/Client/_files/testHeadMethod.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

/**
* @see https://github.com/laminas/laminas-http for the canonical source repository
* @copyright https://github.com/laminas/laminas-http/blob/master/COPYRIGHT.md
* @license https://github.com/laminas/laminas-http/blob/master/LICENSE.md New BSD License
*/


$clength = filesize(__FILE__);

header(sprintf('Content-length: %s', $clength));

0 comments on commit 569e661

Please sign in to comment.