Skip to content

Commit 363a362

Browse files
committed
Fix php-curl-class#332: Implement Curl::setMaxFilesize()
1 parent f26d659 commit 363a362

File tree

3 files changed

+129
-0
lines changed

3 files changed

+129
-0
lines changed

src/Curl/Curl.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,31 @@ public function getResponseCookie($key)
727727
return isset($this->responseCookies[$key]) ? $this->responseCookies[$key] : null;
728728
}
729729

730+
/**
731+
* Set Max Filesize
732+
*
733+
* @access public
734+
* @param $bytes
735+
*/
736+
public function setMaxFilesize($bytes)
737+
{
738+
// Make compatible with PHP version both before and after 5.5.0. PHP 5.5.0 added the cURL resource as the first
739+
// argument to the CURLOPT_PROGRESSFUNCTION callback.
740+
$gte_v550 = version_compare(PHP_VERSION, '5.5.0') >= 0;
741+
if ($gte_v550) {
742+
$callback = function($resource, $download_size, $downloaded, $upload_size, $uploaded) use ($bytes) {
743+
// Abort the transfer when $downloaded bytes exceeds maximum $bytes by returning a non-zero value.
744+
return $downloaded > $bytes ? 1 : 0;
745+
};
746+
} else {
747+
$callback = function($download_size, $downloaded, $upload_size, $uploaded) {
748+
return $downloaded > $bytes ? 1 : 0;
749+
};
750+
}
751+
752+
$this->progress($callback);
753+
}
754+
730755
/**
731756
* Set Port
732757
*

tests/PHPCurlClass/PHPCurlClassTest.php

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,102 @@ public function testDownloadCallback()
688688
$this->assertFalse(file_exists($uploaded_file_path));
689689
}
690690

691+
public function testMaxFilesize()
692+
{
693+
$tests = array(
694+
array(
695+
'bytes' => 1,
696+
'max_filesize' => false,
697+
'expect_error' => false,
698+
),
699+
array(
700+
'bytes' => 1,
701+
'max_filesize' => 1,
702+
'expect_error' => false,
703+
),
704+
array(
705+
'bytes' => 1,
706+
'max_filesize' => 2,
707+
'expect_error' => false,
708+
),
709+
array(
710+
'bytes' => 1,
711+
'max_filesize' => 0,
712+
'expect_error' => true,
713+
),
714+
715+
array(
716+
'bytes' => 2,
717+
'max_filesize' => false,
718+
'expect_error' => false,
719+
),
720+
array(
721+
'bytes' => 2,
722+
'max_filesize' => 2,
723+
'expect_error' => false,
724+
),
725+
array(
726+
'bytes' => 2,
727+
'max_filesize' => 3,
728+
'expect_error' => false,
729+
),
730+
array(
731+
'bytes' => 2,
732+
'max_filesize' => 1,
733+
'expect_error' => true,
734+
),
735+
736+
array(
737+
'bytes' => 1000,
738+
'max_filesize' => false,
739+
'expect_error' => false,
740+
),
741+
array(
742+
'bytes' => 1000,
743+
'max_filesize' => 1000,
744+
'expect_error' => false,
745+
),
746+
array(
747+
'bytes' => 1000,
748+
'max_filesize' => 1001,
749+
'expect_error' => false,
750+
),
751+
array(
752+
'bytes' => 1000,
753+
'max_filesize' => 999,
754+
'expect_error' => true,
755+
),
756+
array(
757+
'bytes' => 1000,
758+
'max_filesize' => 0,
759+
'expect_error' => true,
760+
),
761+
);
762+
foreach ($tests as $test) {
763+
$bytes = $test['bytes'];
764+
$max_filesize = $test['max_filesize'];
765+
$expect_error = $test['expect_error'];
766+
767+
$test = new Test();
768+
if (!($max_filesize === false)) {
769+
$test->curl->setMaxFilesize($max_filesize);
770+
}
771+
$test->server('download_file_size', 'GET', array(
772+
'bytes' => $bytes,
773+
));
774+
775+
// Ensure exceeding download limit aborts the transfer and sets a CURLE_ABORTED_BY_CALLBACK error.
776+
if ($expect_error) {
777+
$this->assertTrue($test->curl->error);
778+
$this->assertEquals($test->curl->errorCode, CURLE_ABORTED_BY_CALLBACK);
779+
} else {
780+
$str = str_repeat('.', $bytes);
781+
$this->assertEquals($test->curl->responseHeaders['etag'], md5($str));
782+
$this->assertEquals($test->curl->response, $str);
783+
}
784+
}
785+
}
786+
691787
public function testBasicHttpAuth()
692788
{
693789
$test = new Test();

tests/PHPCurlClass/server.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,14 @@
244244
header('ETag: ' . md5_file($unsafe_file_path));
245245
readfile($unsafe_file_path);
246246
exit;
247+
} elseif ($test === 'download_file_size') {
248+
$bytes = $_GET['bytes'];
249+
$str = str_repeat('.', $bytes);
250+
header('Content-Type: application/octet-stream');
251+
header('Content-Length: ' . strlen($str));
252+
header('ETag: ' . md5($str));
253+
echo $str;
254+
exit;
247255
} elseif ($test === 'timeout') {
248256
$unsafe_seconds = $_GET['seconds'];
249257
$start = time();

0 commit comments

Comments
 (0)