cvs: pear /HTTP_Download Download.php

From: Date: Mon, 07 Jun 2004 18:30:34 +0000
Subject: cvs: pear /HTTP_Download Download.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-20317@lists.php.net to get a copy of this message
mike Mon Jun 7 14:30:35 2004 EDT Modified files: /pear/HTTP_Download Download.php Log: * allow caching to be configurable http://cvs.php.net/diff.php/pear/HTTP_Download/Download.php?r1=1.29&r2=1.30&ty=u Index: pear/HTTP_Download/Download.php diff -u pear/HTTP_Download/Download.php:1.29 pear/HTTP_Download/Download.php:1.30 --- pear/HTTP_Download/Download.php:1.29 Tue May 4 10:36:31 2004 +++ pear/HTTP_Download/Download.php Mon Jun 7 14:30:34 2004 @@ -11,7 +11,7 @@ // | Copyright (c) 2003-2004 Michael Wallner <mike@iworks.at> | // +----------------------------------------------------------------------+ // -// $Id: Download.php,v 1.29 2004/05/04 14:36:31 mike Exp $ +// $Id: Download.php,v 1.30 2004/06/07 18:30:34 mike Exp $ /** * Manage HTTP Downloads. @@ -127,7 +127,7 @@ * </code> * * @author Michael Wallner <mike@php.net> -* @version $Revision: 1.29 $ +* @version $Revision: 1.30 $ * @access public */ class HTTP_Download @@ -169,6 +169,14 @@ var $gzip = false; /** + * Whether to allow caching of the download on the clients side + * + * @access protected + * @var bool + */ + var $cache = true; + + /** * Size of download * * @access protected @@ -400,6 +408,24 @@ } /** + * Whether to allow caching + * + * If set to true (default) we'll send some headers that are commonly + * used for caching purposes like ETag, Cache-Control and Last-Modified. + * + * If caching is disabled, we'll send the download no matter if it + * would actually be cached at the clients side. + * + * @access public + * @return void + * @param bool $cache whether to allow caching + */ + function setCache($cache = true) + { + $this->cache = (bool) $cache; + } + + /** * Set "Last-Modified" * * This is usually determined by filemtime($file) in HTTP_Download::setFile() @@ -523,18 +549,24 @@ ); } - $this->headers['ETag'] = $this->generateETag(); - if (!isset($this->headers['Content-Disposition'])) { $this->setContentDisposition(); } - if ($this->isCached()) { - $this->HTTP->sendStatusCode(304); - $this->sendHeaders(); - return true; + if ($this->cache) { + $this->headers['ETag'] = $this->generateETag(); + if ($this->isCached()) { + $this->HTTP->sendStatusCode(304); + $this->sendHeaders(); + return true; + } + } else { + unset( + $this->headers['Last-Modified'], + $this->headers['Cache-Control'] + ); } - + if ($this->gzip) { @ob_start('ob_gzhandler'); } else { @@ -566,7 +598,7 @@ * @see HTTP_Download::HTTP_Download() * @see HTTP_Download::send() * - * @note This method should be called statically. + * @static * @access public * @return mixed Returns true on success or PEAR_Error on failure. * @param array $params associative array of parameters @@ -605,7 +637,7 @@ * </code> * * @see Archive_Tar::createModify() - * @note This method should be called statically. + * @static * @access public * @return mixed Returns true on success or PEAR_Error on failure. * @param string $name name the sent archive should have @@ -691,20 +723,19 @@ { if (count($chunks) == 1) { return $this->sendChunk(array_shift($chunks)); - } else { + } - $bound = uniqid('HTTP_DOWNLOAD-', true); - $cType = $this->headers['Content-Type']; - $this->headers['Content-Type'] = 'multipart/byteranges; '; - $this->headers['Content-Type'] .= 'boundary=' . $bound; - - foreach ($chunks as $chunk){ - if (true !== $e = $this->sendChunk($chunk, $cType, $bound)) { - return $e; - } + $bound = uniqid('HTTP_DOWNLOAD-', true); + $cType = $this->headers['Content-Type']; + $this->headers['Content-Type'] = + 'multipart/byteranges; boundary=' . $bound; + + foreach ($chunks as $chunk){ + if (true !== $e = $this->sendChunk($chunk, $cType, $bound)) { + return $e; } - echo "\n--$bound"; } + echo "\n--$bound"; return true; }

« previous php.pear.cvs (#20317) next »