Skip to content

Commit

Permalink
Better support various ranges as described in RFC2616
Browse files Browse the repository at this point in the history
Refs #3914
  • Loading branch information
markstory committed Jul 18, 2013
1 parent 3f9e8e8 commit f725779
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/Cake/Network/CakeResponse.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -1302,6 +1302,15 @@ protected function _fileRange($file, $httpRange) {


$fileSize = $file->size(); $fileSize = $file->size();
$lastByte = $fileSize - 1; $lastByte = $fileSize - 1;

if ($start === '') {
$start = $fileSize - $end;
$end = $lastByte;
}
if ($end === '') {
$end = $lastByte;
}

if ($start > $end || $end > $lastByte || $start > $lastByte) { if ($start > $end || $end > $lastByte || $start > $lastByte) {
$this->statusCode(416); $this->statusCode(416);
$this->header(array( $this->header(array(
Expand Down
70 changes: 70 additions & 0 deletions lib/Cake/Test/Case/Network/CakeResponseTest.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -1387,6 +1387,76 @@ public function testFileExtensionNotSet() {
$response->file(CAKE . 'Test' . DS . 'test_app' . DS . 'Vendor' . DS . 'img' . DS . 'test_2.JPG'); $response->file(CAKE . 'Test' . DS . 'test_app' . DS . 'Vendor' . DS . 'img' . DS . 'test_2.JPG');
} }


/**
* A data provider for testing various ranges
*
* @return array
*/
public static function rangeProvider() {
return array(
// suffix-byte-range
array(
'bytes=-25', 25, 'bytes 13-37/38'
),

array(
'bytes=0-', 38, 'bytes 0-37/38'
),
array(
'bytes=10-', 28, 'bytes 10-37/38'
),
array(
'bytes=10-20', 11, 'bytes 10-20/38'
),
);
}

/**
* Test the various range offset types.
*
* @dataProvider rangeProvider
* @return void
*/
public function testFileRangeOffsets($range, $length, $offsetResponse) {
$_SERVER['HTTP_RANGE'] = $range;
$response = $this->getMock('CakeResponse', array(
'header',
'type',
'_sendHeader',
'_isActive',
'_clearBuffer',
'_flushBuffer'
));

$response->expects($this->at(1))
->method('header')
->with('Content-Disposition', 'attachment; filename="test_asset.css"');

$response->expects($this->at(2))
->method('header')
->with('Accept-Ranges', 'bytes');

$response->expects($this->at(3))
->method('header')
->with(array(
'Content-Length' => $length,
'Content-Range' => $offsetResponse,
));

$response->expects($this->any())
->method('_isActive')
->will($this->returnValue(true));

$response->file(
CAKE . 'Test' . DS . 'test_app' . DS . 'Vendor' . DS . 'css' . DS . 'test_asset.css',
array('download' => true)
);

ob_start();
$result = $response->send();
ob_get_clean();
}

/** /**
* Test fetching ranges from a file. * Test fetching ranges from a file.
* *
Expand Down

0 comments on commit f725779

Please sign in to comment.