Skip to content

Commit 001cfd6

Browse files
committed
Add CURLOPT_RETURNTRANSFER reset when using download method; Fix download method test failing on PHP 5.3 due to different png files being created on the server and client when using imagepng().
1 parent 63f4764 commit 001cfd6

File tree

3 files changed

+58
-21
lines changed

3 files changed

+58
-21
lines changed

src/Curl/Curl.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,18 @@ public function download($url, $filename)
188188
$this->setOpt(CURLOPT_FILE, $fh);
189189
$this->get($url);
190190
fclose($fh);
191+
192+
// Reset CURLOPT_FILE with STDOUT to avoid: "curl_exec(): CURLOPT_FILE
193+
// resource has gone away, resetting to default". Using null causes
194+
// "curl_setopt(): supplied argument is not a valid File-Handle
195+
// resource".
191196
$this->setOpt(CURLOPT_FILE, STDOUT);
197+
198+
// Reset CURLOPT_RETURNTRANSFER to tell cURL to return subsequent
199+
// responses as the return value of curl_exec(). Without this,
200+
// curl_exec() will revert to returning boolean values.
201+
$this->setOpt(CURLOPT_RETURNTRANSFER, true);
202+
192203
return ! $this->error;
193204
}
194205

tests/PHPCurlClass/PHPCurlClassTest.php

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -332,23 +332,43 @@ public function testOptionsRequestMethod()
332332

333333
public function testDownload()
334334
{
335-
$save_to_path = tempnam('/tmp', 'php-curl-class.');
336-
$file_path = Helper\get_png();
337-
338-
$test = new Test();
339-
$test->curl->setHeader('X-DEBUG-TEST', 'download_response');
340-
$this->assertTrue($test->curl->download(Test::TEST_URL, $save_to_path));
341-
$this->assertEquals(filesize($file_path), filesize($save_to_path));
342-
$this->assertEquals(md5_file($file_path), md5_file($save_to_path));
343-
$this->assertEquals(md5_file($file_path), $test->curl->response_headers['ETag']);
344-
345-
$test->curl->setHeader('X-DEBUG-TEST', 'get');
346-
$test->curl->get(Test::TEST_URL);
347-
348-
unlink($file_path);
349-
unlink($save_to_path);
350-
$this->assertFalse(file_exists($file_path));
351-
$this->assertFalse(file_exists($save_to_path));
335+
// Upload a file.
336+
$upload_file_path = Helper\get_png();
337+
$upload_test = new Test();
338+
$upload_test->server('upload_response', 'POST', array(
339+
'image' => '@' . $upload_file_path,
340+
));
341+
$uploaded_file_path = $upload_test->curl->response;
342+
$this->assertNotEquals($upload_file_path, $uploaded_file_path);
343+
$this->assertEquals(md5_file($upload_file_path), md5_file($uploaded_file_path));
344+
$this->assertEquals(md5_file($upload_file_path), $upload_test->curl->response_headers['ETag']);
345+
346+
// Download the file.
347+
$downloaded_file_path = tempnam('/tmp', 'php-curl-class.');
348+
$download_test = new Test();
349+
$download_test->curl->setHeader('X-DEBUG-TEST', 'download_response');
350+
$this->assertTrue($download_test->curl->download(Test::TEST_URL . '?' . http_build_query(array(
351+
'file_path' => $uploaded_file_path,
352+
)), $downloaded_file_path));
353+
$this->assertNotEquals($uploaded_file_path, $downloaded_file_path);
354+
355+
$this->assertEquals(filesize($upload_file_path), filesize($downloaded_file_path));
356+
$this->assertEquals(md5_file($upload_file_path), md5_file($downloaded_file_path));
357+
$this->assertEquals(md5_file($upload_file_path), $download_test->curl->response_headers['ETag']);
358+
359+
// Ensure successive requests set the appropriate values.
360+
$this->assertEquals('GET', $download_test->server('server', 'GET', array(
361+
'key' => 'REQUEST_METHOD',
362+
)));
363+
$this->assertFalse(is_bool($download_test->curl->response));
364+
$this->assertFalse(is_bool($download_test->curl->raw_response));
365+
366+
unlink($upload_file_path);
367+
unlink($uploaded_file_path);
368+
unlink($downloaded_file_path);
369+
$this->assertFalse(file_exists($upload_file_path));
370+
$this->assertFalse(file_exists($uploaded_file_path));
371+
$this->assertFalse(file_exists($downloaded_file_path));
352372
}
353373

354374
public function testBasicHttpAuth401Unauthorized()

tests/PHPCurlClass/server.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,19 @@
121121
$rss->appendChild($channel);
122122
echo $doc->saveXML();
123123
exit;
124+
} elseif ($test === 'upload_response') {
125+
$tmp_filename = tempnam('/tmp', 'php-curl-class.');
126+
move_uploaded_file($_FILES['image']['tmp_name'], $tmp_filename);
127+
header('ETag: ' . md5_file($tmp_filename));
128+
echo $tmp_filename;
129+
exit;
124130
} elseif ($test === 'download_response') {
125-
$png = Helper\create_png();
131+
$unsafe_file_path = $_GET['file_path'];
126132
header('Content-Type: image/png');
127133
header('Content-Disposition: attachment; filename="image.png"');
128-
header('Content-Length: ' . strlen($png));
129-
header('ETag: ' . md5($png));
130-
echo $png;
134+
header('Content-Length: ' . filesize($unsafe_file_path));
135+
header('ETag: ' . md5_file($unsafe_file_path));
136+
readfile($unsafe_file_path);
131137
exit;
132138
} elseif ($test === 'error_message') {
133139
if (function_exists('http_response_code')) {

0 commit comments

Comments
 (0)