Skip to content

Commit

Permalink
fixes for CURLOPT_FILE handling (#68)
Browse files Browse the repository at this point in the history
* add more safeguards against invalid headers
* re-open file to avoid weirdness
  • Loading branch information
anlutro authored Jun 14, 2021
1 parent d7e21e7 commit 836c7f7
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 11 deletions.
9 changes: 8 additions & 1 deletion src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ public function __construct($body, $headers, $info = array())
*/
protected function parseHeader($header)
{
if ($header === "") {
throw new \UnexpectedValueException('Empty header string passed!');
}
$headers = explode("\r\n", trim($header));
$this->parseHeaders($headers);
}
Expand All @@ -83,12 +86,16 @@ protected function parseHeader($header)
*/
protected function parseHeaders(array $headers)
{
if (count($headers) === 0) {
throw new \UnexpectedValueException('No headers passed!');
}

$this->headers = array();

// find and set the HTTP status code and reason
$firstHeader = array_shift($headers);
if (!preg_match('/^HTTP\/\d(\.\d)? [0-9]{3}/', $firstHeader)) {
throw new \InvalidArgumentException('Invalid response header');
throw new \UnexpectedValueException('Invalid response header');
}
list(, $status) = explode(' ', $firstHeader, 2);
$code = explode(' ', $status);
Expand Down
13 changes: 5 additions & 8 deletions src/cURL.php
Original file line number Diff line number Diff line change
Expand Up @@ -313,15 +313,12 @@ protected function createResponseObject($response, Request $request)
$info[CURLINFO_HTTPAUTH_AVAIL] = curl_getinfo($this->ch, CURLINFO_HTTPAUTH_AVAIL);

if ($file = $request->getOption(CURLOPT_FILE)) {
// TODO: is this even possible?
if (!is_resource($file)) {
$file = fopen($file, "r");
}
// TODO: what if resource isn't seekable? e.g. network socket?
$oldPosition = ftell($file);
fseek($file, 0);
// file may be opened write-only, and even when it isn't,
// seeking/reading seems to be buggy
$fileMeta = stream_get_meta_data($file);
$file = fopen($fileMeta['uri'], 'r');
$headers = fread($file, $headerSize);
fseek($file, $oldPosition);
fclose($file);
$body = null;
} else {
$headers = substr($response, 0, $headerSize);
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/cURLTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public function curloptFileWorks()
{
$r = $this->makeCurl()
->newRequest('get', static::URL.'/success.php')
->setOption(CURLOPT_FILE, tmpfile())
->setOption(CURLOPT_FILE, $fh = tmpfile())
->send();
$this->assertEquals(200, $r->statusCode);
$this->assertEquals('200 OK', $r->statusText);
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function httpContinueResponsesAreHandled()
/** @test */
public function throwsExceptionIfHeaderDoesntStartWithHttpStatus()
{
$this->setExpectedException('InvalidArgumentException', 'Invalid response header');
$this->setExpectedException('UnexpectedValueException', 'Invalid response header');
$this->makeResponse('', 'x-var: foo');
}

Expand Down

0 comments on commit 836c7f7

Please sign in to comment.