cvs: pear /HTTP_Download Download.php
| From: | Michael Wallner | Date: | Tue, 31 Aug 2004 11:18:05 +0000 |
| Subject: | cvs: pear /HTTP_Download Download.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-23529@lists.php.net to get a copy of this message | ||
mike Tue Aug 31 07:18:05 2004 EDT
Modified files:
/pear/HTTP_Download Download.php
Log:
* ob_flush() after headers were sent
* implemented kinda throttling mechanism
+ added $throttleDelay & setThrottleDelay()
http://cvs.php.net/diff.php/pear/HTTP_Download/Download.php?r1=1.42&r2=1.43&ty=u
Index: pear/HTTP_Download/Download.php
diff -u pear/HTTP_Download/Download.php:1.42 pear/HTTP_Download/Download.php:1.43
--- pear/HTTP_Download/Download.php:1.42 Thu Aug 12 07:14:51 2004
+++ pear/HTTP_Download/Download.php Tue Aug 31 07:18:05 2004
@@ -11,7 +11,7 @@
// | Copyright (c) 2003-2004 Michael Wallner <mike@iworks.at> |
// +----------------------------------------------------------------------+
//
-// $Id: Download.php,v 1.42 2004/08/12 11:14:51 mike Exp $
+// $Id: Download.php,v 1.43 2004/08/31 11:18:05 mike Exp $
/**
* Manage HTTP Downloads.
@@ -93,7 +93,7 @@
* if you want to send already gzipped data!
*
* @access public
- * @version $Revision: 1.42 $
+ * @version $Revision: 1.43 $
*/
class HTTP_Download
{
@@ -166,7 +166,7 @@
var $headers = array(
'Content-Type' => 'application/x-octetstream',
'Pragma' => 'cache',
- 'Cache-Control' => 'public',
+ 'Cache-Control' => 'public, must-revalidate, max-age=0',
'Accept-Ranges' => 'bytes',
'Connection' => 'close',
'X-Sent-By' => 'PEAR::HTTP::Download'
@@ -195,10 +195,18 @@
* @var int
*/
var $bufferSize = 2097152;
+
+ /**
+ * Throttle Delay
+ *
+ * @access protected
+ * @var int
+ */
+ var $throttleDelay = 0;
// }}}
// {{{ constructor
- /**
+ /**
* Constructor
*
* Set supplied parameters.
@@ -218,6 +226,7 @@
* o 'contenttype' => content type of download
* o 'contentdisposition' => content disposition
* o 'buffersize' => amount of bytes to buffer
+ * o 'throttledelay' => amount of secs to sleep
*
* <br />
* 'Content-Disposition' is not HTTP compliant, but most browsers
@@ -430,6 +439,35 @@
}
/**
+ * Set Throttle Delay
+ *
+ * Set the amount of seconds to sleep after each chunck that has been
+ * sent. One can implement some sort of throttle through adjusting the
+ * buffer size and the throttle delay. With the following settings
+ * HTTP_Download will sleep a second after each 25 K of data sent.
+ *
+ * <code>
+ * Array(
+ * 'throttledelay' => 1,
+ * 'buffersize' => 1024 * 25,
+ * )
+ * </code>
+ *
+ * Just be aware that if gzipp'ing is enabled, decreasing the chunk size
+ * too much leads to proportionally increased network traffic due to added
+ * gzip header and bottom bytes around each chunk.
+ *
+ * @access public
+ * @return void
+ * @param int $sec Amount of seconds to sleep after each
+ * chunk that has been sent.
+ */
+ function setThrottleDelay($sec = 0)
+ {
+ $this->throttleDelay = abs((int) $sec);
+ }
+
+ /**
* Set "Last-Modified"
*
* This is usually determined by filemtime() in HTTP_Download::setFile()
@@ -561,6 +599,8 @@
);
}
+ set_time_limit(0);
+
if (!isset($this->headers['Content-Disposition'])) {
$this->setContentDisposition();
}
@@ -814,10 +854,12 @@
$this->handle = fopen($this->file, 'rb');
}
fseek($this->handle, $offset);
- // only read 2M at once
while (($length -= $this->bufferSize) > 0) {
echo fread($this->handle, $this->bufferSize);
ob_flush();
+ if ($this->throttleDelay) {
+ sleep($this->throttleDelay);
+ }
}
if ($length) {
echo fread($this->handle, $this->bufferSize + $length);
@@ -943,6 +985,7 @@
$this->HTTP->setHeader($header, $value);
}
$this->HTTP->sendHeaders();
+ ob_flush();
}
// }}}
}