From 758447cfa419f1a11bc022ef8168447992364e52 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Fri, 13 Mar 2009 20:02:47 +0100 Subject: Support for multirange requests for media FS#1630 Ignore-this: 50de569608231b910a62327d2f3af1de This patch moves all HTTP sending related functions to inc/httputils.php Handling of range requests was rewritten completely to support mutirange requests. This should fix problems with Adobe Reader but needs testing. darcs-hash:20090313190247-7ad00-e6ec1f81acb9f7ac651357dd034c2689aea6868d.gz --- lib/exe/fetch.php | 56 ++----------------------------------------------------- 1 file changed, 2 insertions(+), 54 deletions(-) (limited to 'lib/exe/fetch.php') diff --git a/lib/exe/fetch.php b/lib/exe/fetch.php index ddfff4b4e..728b0b448 100644 --- a/lib/exe/fetch.php +++ b/lib/exe/fetch.php @@ -12,12 +12,12 @@ require_once(DOKU_INC.'inc/common.php'); require_once(DOKU_INC.'inc/media.php'); require_once(DOKU_INC.'inc/pageutils.php'); + require_once(DOKU_INC.'inc/httputils.php'); require_once(DOKU_INC.'inc/confutils.php'); require_once(DOKU_INC.'inc/auth.php'); //close sesseion session_write_close(); - if(!defined('CHUNK_SIZE')) define('CHUNK_SIZE',16*1024); $mimetypes = getMimeTypes(); @@ -139,68 +139,16 @@ function sendFile($file,$mime,$dl,$cache){ //use x-sendfile header to pass the delivery to compatible webservers if (http_sendfile($file)) exit; - //support download continueing - header('Accept-Ranges: bytes'); - list($start,$len) = http_rangeRequest(filesize($file)); - // send file contents $fp = @fopen($file,"rb"); if($fp){ - fseek($fp,$start); //seek to start of range - - $chunk = ($len > CHUNK_SIZE) ? CHUNK_SIZE : $len; - while (!feof($fp) && $chunk > 0) { - @set_time_limit(30); // large files can take a lot of time - print fread($fp, $chunk); - flush(); - $len -= $chunk; - $chunk = ($len > CHUNK_SIZE) ? CHUNK_SIZE : $len; - } - fclose($fp); + http_rangeRequest($fp,filesize($file),$mime); }else{ header("HTTP/1.0 500 Internal Server Error"); print "Could not read $file - bad permissions?"; } } -/** - * Checks and sets headers to handle range requets - * - * @author Andreas Gohr - * @returns array The start byte and the amount of bytes to send - */ -function http_rangeRequest($size){ - if(!isset($_SERVER['HTTP_RANGE'])){ - // no range requested - send the whole file - header("Content-Length: $size"); - return array(0,$size); - } - - $t = explode('=', $_SERVER['HTTP_RANGE']); - if (!$t[0]=='bytes') { - // we only understand byte ranges - send the whole file - header("Content-Length: $size"); - return array(0,$size); - } - - $r = explode('-', $t[1]); - $start = (int)$r[0]; - $end = (int)$r[1]; - if (!$end) $end = $size - 1; - if ($start > $end || $start > $size || $end > $size){ - header('HTTP/1.1 416 Requested Range Not Satisfiable'); - print 'Bad Range Request!'; - exit; - } - - $tot = $end - $start + 1; - header('HTTP/1.1 206 Partial Content'); - header("Content-Range: bytes {$start}-{$end}/{$size}"); - header("Content-Length: $tot"); - - return array($start,$tot); -} - /** * Returns the wanted cachetime in seconds * -- cgit v1.2.3