Fix #332: Implement Curl::setMaxFilesize()

This commit is contained in:
Zach Borboa
2016-09-23 01:13:49 -07:00
parent f26d659a89
commit 363a362bcd
3 changed files with 129 additions and 0 deletions
+25
View File
@@ -727,6 +727,31 @@ class Curl
return isset($this->responseCookies[$key]) ? $this->responseCookies[$key] : null;
}
/**
* Set Max Filesize
*
* @access public
* @param $bytes
*/
public function setMaxFilesize($bytes)
{
// Make compatible with PHP version both before and after 5.5.0. PHP 5.5.0 added the cURL resource as the first
// argument to the CURLOPT_PROGRESSFUNCTION callback.
$gte_v550 = version_compare(PHP_VERSION, '5.5.0') >= 0;
if ($gte_v550) {
$callback = function($resource, $download_size, $downloaded, $upload_size, $uploaded) use ($bytes) {
// Abort the transfer when $downloaded bytes exceeds maximum $bytes by returning a non-zero value.
return $downloaded > $bytes ? 1 : 0;
};
} else {
$callback = function($download_size, $downloaded, $upload_size, $uploaded) {
return $downloaded > $bytes ? 1 : 0;
};
}
$this->progress($callback);
}
/**
* Set Port
*
+96
View File
@@ -688,6 +688,102 @@ class CurlTest extends PHPUnit_Framework_TestCase
$this->assertFalse(file_exists($uploaded_file_path));
}
public function testMaxFilesize()
{
$tests = array(
array(
'bytes' => 1,
'max_filesize' => false,
'expect_error' => false,
),
array(
'bytes' => 1,
'max_filesize' => 1,
'expect_error' => false,
),
array(
'bytes' => 1,
'max_filesize' => 2,
'expect_error' => false,
),
array(
'bytes' => 1,
'max_filesize' => 0,
'expect_error' => true,
),
array(
'bytes' => 2,
'max_filesize' => false,
'expect_error' => false,
),
array(
'bytes' => 2,
'max_filesize' => 2,
'expect_error' => false,
),
array(
'bytes' => 2,
'max_filesize' => 3,
'expect_error' => false,
),
array(
'bytes' => 2,
'max_filesize' => 1,
'expect_error' => true,
),
array(
'bytes' => 1000,
'max_filesize' => false,
'expect_error' => false,
),
array(
'bytes' => 1000,
'max_filesize' => 1000,
'expect_error' => false,
),
array(
'bytes' => 1000,
'max_filesize' => 1001,
'expect_error' => false,
),
array(
'bytes' => 1000,
'max_filesize' => 999,
'expect_error' => true,
),
array(
'bytes' => 1000,
'max_filesize' => 0,
'expect_error' => true,
),
);
foreach ($tests as $test) {
$bytes = $test['bytes'];
$max_filesize = $test['max_filesize'];
$expect_error = $test['expect_error'];
$test = new Test();
if (!($max_filesize === false)) {
$test->curl->setMaxFilesize($max_filesize);
}
$test->server('download_file_size', 'GET', array(
'bytes' => $bytes,
));
// Ensure exceeding download limit aborts the transfer and sets a CURLE_ABORTED_BY_CALLBACK error.
if ($expect_error) {
$this->assertTrue($test->curl->error);
$this->assertEquals($test->curl->errorCode, CURLE_ABORTED_BY_CALLBACK);
} else {
$str = str_repeat('.', $bytes);
$this->assertEquals($test->curl->responseHeaders['etag'], md5($str));
$this->assertEquals($test->curl->response, $str);
}
}
}
public function testBasicHttpAuth()
{
$test = new Test();
+8
View File
@@ -244,6 +244,14 @@ if ($test === 'http_basic_auth') {
header('ETag: ' . md5_file($unsafe_file_path));
readfile($unsafe_file_path);
exit;
} elseif ($test === 'download_file_size') {
$bytes = $_GET['bytes'];
$str = str_repeat('.', $bytes);
header('Content-Type: application/octet-stream');
header('Content-Length: ' . strlen($str));
header('ETag: ' . md5($str));
echo $str;
exit;
} elseif ($test === 'timeout') {
$unsafe_seconds = $_GET['seconds'];
$start = time();