From 9999e05660637c77947c71656d2f03d841e19ab9 Mon Sep 17 00:00:00 2001 From: Piotr Filiciak Date: Fri, 20 May 2016 18:16:44 +0200 Subject: [PATCH 1/2] Http Range requests support in downloads Http range requests support is required for video preview --- apps/files/ajax/download.php | 11 +- .../lib/controllers/sharecontroller.php | 13 +- lib/private/Files/View.php | 34 +++++ lib/private/legacy/files.php | 143 ++++++++++++++++-- lib/public/Files/UnseekableException.php | 34 +++++ 5 files changed, 220 insertions(+), 15 deletions(-) create mode 100644 lib/public/Files/UnseekableException.php diff --git a/apps/files/ajax/download.php b/apps/files/ajax/download.php index 28ce4c6542..aedd86b641 100644 --- a/apps/files/ajax/download.php +++ b/apps/files/ajax/download.php @@ -50,4 +50,13 @@ if(isset($_GET['downloadStartSecret']) setcookie('ocDownloadStarted', $_GET['downloadStartSecret'], time() + 20, '/'); } -OC_Files::get($dir, $files_list, $_SERVER['REQUEST_METHOD'] == 'HEAD'); +$server_params = array( 'head' => \OC::$server->getRequest()->getMethod() == 'HEAD' ); + +/** + * Http range requests support + */ +if (isset($_SERVER['HTTP_RANGE'])) { + $server_params['range'] = \OC::$server->getRequest()->getHeader('Range'); +} + +OC_Files::get($dir, $files_list, $server_params); diff --git a/apps/files_sharing/lib/controllers/sharecontroller.php b/apps/files_sharing/lib/controllers/sharecontroller.php index 72294f6b26..8662765d19 100644 --- a/apps/files_sharing/lib/controllers/sharecontroller.php +++ b/apps/files_sharing/lib/controllers/sharecontroller.php @@ -484,16 +484,25 @@ class ShareController extends Controller { $this->emitAccessShareHook($share); + $server_params = array( 'head' => $this->request->getMethod() == 'HEAD' ); + + /** + * Http range requests support + */ + if (isset($_SERVER['HTTP_RANGE'])) { + $server_params['range'] = $this->request->getHeader('Range'); + } + // download selected files if (!is_null($files) && $files !== '') { // FIXME: The exit is required here because otherwise the AppFramework is trying to add headers as well // after dispatching the request which results in a "Cannot modify header information" notice. - OC_Files::get($originalSharePath, $files_list, $_SERVER['REQUEST_METHOD'] == 'HEAD'); + OC_Files::get($originalSharePath, $files_list, $server_params); exit(); } else { // FIXME: The exit is required here because otherwise the AppFramework is trying to add headers as well // after dispatching the request which results in a "Cannot modify header information" notice. - OC_Files::get(dirname($originalSharePath), basename($originalSharePath), $_SERVER['REQUEST_METHOD'] == 'HEAD'); + OC_Files::get(dirname($originalSharePath), basename($originalSharePath), $server_params); exit(); } } diff --git a/lib/private/Files/View.php b/lib/private/Files/View.php index 22e53a0070..136b2c2ac3 100644 --- a/lib/private/Files/View.php +++ b/lib/private/Files/View.php @@ -53,6 +53,7 @@ use OCP\Files\InvalidCharacterInPathException; use OCP\Files\InvalidPathException; use OCP\Files\NotFoundException; use OCP\Files\ReservedWordException; +use OCP\Files\UnseekableException; use OCP\Files\Storage\ILockingStorage; use OCP\IUser; use OCP\Lock\ILockingProvider; @@ -423,6 +424,39 @@ class View { return false; } + /** + * @param string $path + * @param int $from + * @param int $to + * @return bool|mixed + * @throws \OCP\Files\InvalidPathException, \OCP\Files\UnseekableException + */ + public function readfilePart($path, $from, $to) { + $this->assertPathLength($path); + @ob_end_clean(); + $handle = $this->fopen($path, 'rb'); + if ($handle) { + if (fseek($handle, $from) === 0) { + $chunkSize = 8192; // 8 kB chunks + $end = $to + 1; + while (!feof($handle) && ftell($handle) < $end) { + $len = $end-ftell($handle); + if ($len > $chunkSize) { + $len = $chunkSize; + } + echo fread($handle, $len); + flush(); + } + $size = ftell($handle) - $from; + return $size; + } + else { + throw new \OCP\Files\UnseekableException('fseek error'); + } + } + return false; + } + /** * @param string $path * @return mixed diff --git a/lib/private/legacy/files.php b/lib/private/legacy/files.php index 9b6a1a4465..6327a0895e 100644 --- a/lib/private/legacy/files.php +++ b/lib/private/legacy/files.php @@ -16,6 +16,7 @@ * @author Thomas Müller * @author Victor Dubiniuk * @author Vincent Petry + * @author Piotr Filiciak * * @copyright Copyright (c) 2016, ownCloud, Inc. * @license AGPL-3.0 @@ -49,20 +50,48 @@ class OC_Files { const UPLOAD_MIN_LIMIT_BYTES = 1048576; // 1 MiB + + private static $MULTIPART_BOUNDARY = ''; + + /** + * @return string + */ + private static function getBoundary() { + if (empty(self::$MULTIPART_BOUNDARY)) { + self::$MULTIPART_BOUNDARY = md5(mt_rand()); + } + return self::$MULTIPART_BOUNDARY; + } + /** * @param string $filename * @param string $name + * @param array $rangeArray ('from'=>int,'to'=>int), ... */ - private static function sendHeaders($filename, $name) { + private static function sendHeaders($filename, $name, array $rangeArray) { OC_Response::setContentDispositionHeader($name, 'attachment'); - header('Content-Transfer-Encoding: binary'); + header('Content-Transfer-Encoding: binary', true); OC_Response::disableCaching(); $fileSize = \OC\Files\Filesystem::filesize($filename); $type = \OC::$server->getMimeTypeDetector()->getSecureMimeType(\OC\Files\Filesystem::getMimeType($filename)); - header('Content-Type: '.$type); if ($fileSize > -1) { - OC_Response::setContentLengthHeader($fileSize); + if (!empty($rangeArray)) { + header('HTTP/1.1 206 Partial Content', true); + header('Accept-Ranges: bytes', true); + if (count($rangeArray) > 1) { + $type = 'multipart/byteranges; boundary='.self::getBoundary(); + // no Content-Length header here + } + else { + header(sprintf('Content-Range: bytes %d-%d/%d', $rangeArray[0]['from'], $rangeArray[0]['to'], $fileSize), true); + OC_Response::setContentLengthHeader($rangeArray[0]['to'] - $rangeArray[0]['from'] + 1); + } + } + else { + OC_Response::setContentLengthHeader($fileSize); + } } + header('Content-Type: '.$type, true); } /** @@ -70,9 +99,9 @@ class OC_Files { * * @param string $dir * @param string $files ; separated list of files to download - * @param boolean $onlyHeader ; boolean to only send header of the request + * @param array $params ; 'head' boolean to only send header of the request ; 'range' http range header */ - public static function get($dir, $files, $onlyHeader = false) { + public static function get($dir, $files, $params = array( 'head' => false )) { $view = \OC\Files\Filesystem::getView(); $getType = self::FILE; @@ -86,7 +115,7 @@ class OC_Files { if (!is_array($files)) { $filename = $dir . '/' . $files; if (!$view->is_dir($filename)) { - self::getSingleFile($view, $dir, $files, $onlyHeader); + self::getSingleFile($view, $dir, $files, $params); return; } } @@ -156,19 +185,78 @@ class OC_Files { } } + /** + * @param string $rangeHeaderPos + * @param int $fileSize + * @return array $rangeArray ('from'=>int,'to'=>int), ... + */ + private static function parseHttpRangeHeader($rangeHeaderPos, $fileSize) { + $rArray=split(',', $rangeHeaderPos); + $minOffset = 0; + $ind = 0; + + $rangeArray = array(); + + foreach ($rArray as $value) { + $ranges = explode('-', $value); + if (is_numeric($ranges[0])) { + if ($ranges[0] < $minOffset) { // case: bytes=500-700,601-999 + $ranges[0] = $minOffset; + } + if ($ind > 0 && $rangeArray[$ind-1]['to']+1 == $ranges[0]) { // case: bytes=500-600,601-999 + $ind--; + $ranges[0] = $rangeArray[$ind]['from']; + } + } + + if (is_numeric($ranges[0]) && is_numeric($ranges[1]) && $ranges[0] < $fileSize && $ranges[0] <= $ranges[1]) { + // case: x-x + if ($ranges[1] >= $fileSize) { + $ranges[1] = $fileSize-1; + } + $rangeArray[$ind++] = array( 'from' => $ranges[0], 'to' => $ranges[1], 'size' => $fileSize ); + $minOffset = $ranges[1] + 1; + if ($minOffset >= $fileSize) { + break; + } + } + elseif (is_numeric($ranges[0]) && $ranges[0] < $fileSize) { + // case: x- + $rangeArray[$ind++] = array( 'from' => $ranges[0], 'to' => $fileSize-1, 'size' => $fileSize ); + break; + } + elseif (is_numeric($ranges[1])) { + // case: -x + if ($ranges[1] > $fileSize) { + $ranges[1] = $fileSize; + } + $rangeArray[$ind++] = array( 'from' => $fileSize-$ranges[1], 'to' => $fileSize-1, 'size' => $fileSize ); + break; + } + } + return $rangeArray; + } + /** * @param View $view * @param string $name * @param string $dir - * @param boolean $onlyHeader + * @param array $params ; 'head' boolean to only send header of the request ; 'range' http range header */ - private static function getSingleFile($view, $dir, $name, $onlyHeader) { + private static function getSingleFile($view, $dir, $name, $params) { $filename = $dir . '/' . $name; OC_Util::obEnd(); $view->lockFile($filename, ILockingProvider::LOCK_SHARED); + + $rangeArray = array(); + if (isset($params['range']) && substr($params['range'], 0, 6) === 'bytes=') { + $rangeArray = self::parseHttpRangeHeader(substr($params['range'], 6), + \OC\Files\Filesystem::filesize($filename)); + } + if (\OC\Files\Filesystem::isReadable($filename)) { - self::sendHeaders($filename, $name); + self::sendHeaders($filename, $name, $rangeArray); } elseif (!\OC\Files\Filesystem::file_exists($filename)) { header("HTTP/1.0 404 Not Found"); $tmpl = new OC_Template('', '404', 'guest'); @@ -178,10 +266,41 @@ class OC_Files { header("HTTP/1.0 403 Forbidden"); die('403 Forbidden'); } - if ($onlyHeader) { + if (isset($params['head']) && $params['head']) { return; } - $view->readfile($filename); + if (!empty($rangeArray)) { + try { + if (count($rangeArray) == 1) { + $view->readfilePart($filename, $rangeArray[0]['from'], $rangeArray[0]['to']); + } + else { + // check if file is seekable (if not throw UnseekableException) + // we have to check it before body contents + $view->readfilePart($filename, $rangeArray[0]['size'], $rangeArray[0]['size']); + + $type = \OC::$server->getMimeTypeDetector()->getSecureMimeType(\OC\Files\Filesystem::getMimeType($filename)); + + foreach ($rangeArray as $range) { + echo "\r\n--".self::getBoundary()."\r\n". + "Content-type: ".$type."\r\n". + "Content-range: bytes ".$range['from']."-".$range['to']."/".$range['size']."\r\n\r\n"; + $view->readfilePart($filename, $range['from'], $range['to']); + } + echo "\r\n--".self::getBoundary()."--\r\n"; + } + } catch (\OCP\Files\UnseekableException $ex) { + // file is unseekable + header_remove('Accept-Ranges'); + header_remove('Content-Range'); + header("HTTP/1.1 200 OK"); + self::sendHeaders($filename, $name, array()); + $view->readfile($filename); + } + } + else { + $view->readfile($filename); + } } /** diff --git a/lib/public/Files/UnseekableException.php b/lib/public/Files/UnseekableException.php new file mode 100644 index 0000000000..fda9369769 --- /dev/null +++ b/lib/public/Files/UnseekableException.php @@ -0,0 +1,34 @@ + + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see + * + */ + +/** + * Public interface of ownCloud for apps to use. + * Files/UnseekableException class + */ + +// use OCP namespace for all classes that are considered public. +// This means that they should be used by apps instead of the internal ownCloud classes +namespace OCP\Files; + +/** + * Exception for seek problem + */ +class UnseekableException extends \Exception {} From 6577bbe887840889e16634b9bf1c4ce247ec265e Mon Sep 17 00:00:00 2001 From: Piotr Filiciak Date: Mon, 23 May 2016 15:17:00 +0200 Subject: [PATCH 2/2] Code style and doc fix --- lib/private/Files/View.php | 8 ++++---- lib/private/legacy/files.php | 12 ++++++------ lib/public/Files/UnseekableException.php | 1 + 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/lib/private/Files/View.php b/lib/private/Files/View.php index 136b2c2ac3..2761390308 100644 --- a/lib/private/Files/View.php +++ b/lib/private/Files/View.php @@ -429,7 +429,8 @@ class View { * @param int $from * @param int $to * @return bool|mixed - * @throws \OCP\Files\InvalidPathException, \OCP\Files\UnseekableException + * @throws \OCP\Files\InvalidPathException + * @throws \OCP\Files\UnseekableException */ public function readfilePart($path, $from, $to) { $this->assertPathLength($path); @@ -450,9 +451,8 @@ class View { $size = ftell($handle) - $from; return $size; } - else { - throw new \OCP\Files\UnseekableException('fseek error'); - } + + throw new \OCP\Files\UnseekableException('fseek error'); } return false; } diff --git a/lib/private/legacy/files.php b/lib/private/legacy/files.php index 6327a0895e..ad5ba6d94a 100644 --- a/lib/private/legacy/files.php +++ b/lib/private/legacy/files.php @@ -51,16 +51,16 @@ class OC_Files { const UPLOAD_MIN_LIMIT_BYTES = 1048576; // 1 MiB - private static $MULTIPART_BOUNDARY = ''; + private static $multipartBoundary = ''; /** * @return string */ private static function getBoundary() { - if (empty(self::$MULTIPART_BOUNDARY)) { - self::$MULTIPART_BOUNDARY = md5(mt_rand()); + if (empty(self::$multipartBoundary)) { + self::$multipartBoundary = md5(mt_rand()); } - return self::$MULTIPART_BOUNDARY; + return self::$multipartBoundary; } /** @@ -101,7 +101,7 @@ class OC_Files { * @param string $files ; separated list of files to download * @param array $params ; 'head' boolean to only send header of the request ; 'range' http range header */ - public static function get($dir, $files, $params = array( 'head' => false )) { + public static function get($dir, $files, $params = null) { $view = \OC\Files\Filesystem::getView(); $getType = self::FILE; @@ -115,7 +115,7 @@ class OC_Files { if (!is_array($files)) { $filename = $dir . '/' . $files; if (!$view->is_dir($filename)) { - self::getSingleFile($view, $dir, $files, $params); + self::getSingleFile($view, $dir, $files, is_null($params) ? array() : $params); return; } } diff --git a/lib/public/Files/UnseekableException.php b/lib/public/Files/UnseekableException.php index fda9369769..b59f844ce2 100644 --- a/lib/public/Files/UnseekableException.php +++ b/lib/public/Files/UnseekableException.php @@ -30,5 +30,6 @@ namespace OCP\Files; /** * Exception for seek problem + * @since 9.1.0 */ class UnseekableException extends \Exception {}