Skip to content

Commit 836c7f7

Browse files
authored
fixes for CURLOPT_FILE handling (#68)
* add more safeguards against invalid headers * re-open file to avoid weirdness
1 parent d7e21e7 commit 836c7f7

File tree

4 files changed

+15
-11
lines changed

4 files changed

+15
-11
lines changed

src/Response.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ public function __construct($body, $headers, $info = array())
7070
*/
7171
protected function parseHeader($header)
7272
{
73+
if ($header === "") {
74+
throw new \UnexpectedValueException('Empty header string passed!');
75+
}
7376
$headers = explode("\r\n", trim($header));
7477
$this->parseHeaders($headers);
7578
}
@@ -83,12 +86,16 @@ protected function parseHeader($header)
8386
*/
8487
protected function parseHeaders(array $headers)
8588
{
89+
if (count($headers) === 0) {
90+
throw new \UnexpectedValueException('No headers passed!');
91+
}
92+
8693
$this->headers = array();
8794

8895
// find and set the HTTP status code and reason
8996
$firstHeader = array_shift($headers);
9097
if (!preg_match('/^HTTP\/\d(\.\d)? [0-9]{3}/', $firstHeader)) {
91-
throw new \InvalidArgumentException('Invalid response header');
98+
throw new \UnexpectedValueException('Invalid response header');
9299
}
93100
list(, $status) = explode(' ', $firstHeader, 2);
94101
$code = explode(' ', $status);

src/cURL.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -313,15 +313,12 @@ protected function createResponseObject($response, Request $request)
313313
$info[CURLINFO_HTTPAUTH_AVAIL] = curl_getinfo($this->ch, CURLINFO_HTTPAUTH_AVAIL);
314314

315315
if ($file = $request->getOption(CURLOPT_FILE)) {
316-
// TODO: is this even possible?
317-
if (!is_resource($file)) {
318-
$file = fopen($file, "r");
319-
}
320-
// TODO: what if resource isn't seekable? e.g. network socket?
321-
$oldPosition = ftell($file);
322-
fseek($file, 0);
316+
// file may be opened write-only, and even when it isn't,
317+
// seeking/reading seems to be buggy
318+
$fileMeta = stream_get_meta_data($file);
319+
$file = fopen($fileMeta['uri'], 'r');
323320
$headers = fread($file, $headerSize);
324-
fseek($file, $oldPosition);
321+
fclose($file);
325322
$body = null;
326323
} else {
327324
$headers = substr($response, 0, $headerSize);

tests/functional/cURLTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ public function curloptFileWorks()
169169
{
170170
$r = $this->makeCurl()
171171
->newRequest('get', static::URL.'/success.php')
172-
->setOption(CURLOPT_FILE, tmpfile())
172+
->setOption(CURLOPT_FILE, $fh = tmpfile())
173173
->send();
174174
$this->assertEquals(200, $r->statusCode);
175175
$this->assertEquals('200 OK', $r->statusText);

tests/unit/ResponseTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function httpContinueResponsesAreHandled()
5959
/** @test */
6060
public function throwsExceptionIfHeaderDoesntStartWithHttpStatus()
6161
{
62-
$this->setExpectedException('InvalidArgumentException', 'Invalid response header');
62+
$this->setExpectedException('UnexpectedValueException', 'Invalid response header');
6363
$this->makeResponse('', 'x-var: foo');
6464
}
6565

0 commit comments

Comments
 (0)