diff --git a/PEAR/Downloader.php b/PEAR/Downloader.php index c8a2f9ffd..3aefcdd7f 100644 --- a/PEAR/Downloader.php +++ b/PEAR/Downloader.php @@ -1726,19 +1726,43 @@ public static function _downloadHttp( call_user_func($callback, 'start', array(basename($dest_file), $length)); } - while ($data = fread($fp, 1024)) { - $bytes += strlen($data); - if ($callback) { - call_user_func($callback, 'bytesread', $bytes); + if (isset($headers['transfer-encoding']) && strtolower($headers['transfer-encoding']) === 'chunked') { + while (!feof($fp)) { + $chunk_size = hexdec(fgets($fp)); + if ($chunk_size === 0) { + break; + } + $data = fread($fp, $chunk_size); + $bytes += strlen($data); + if ($callback) { + call_user_func($callback, 'bytesread', $bytes); + } + if (!@fwrite($wp, $data)) { + fclose($fp); + if ($callback) { + call_user_func($callback, 'writefailed', + array($dest_file, error_get_last()["message"])); + } + return PEAR::raiseError( + "$dest_file: write failed (" . error_get_last()["message"] . ")"); + } + fgets($fp); // Skip the trailing CRLF } - if (!@fwrite($wp, $data)) { - fclose($fp); + } else { + while ($data = fread($fp, 1024)) { + $bytes += strlen($data); if ($callback) { - call_user_func($callback, 'writefailed', - array($dest_file, error_get_last()["message"])); + call_user_func($callback, 'bytesread', $bytes); + } + if (!@fwrite($wp, $data)) { + fclose($fp); + if ($callback) { + call_user_func($callback, 'writefailed', + array($dest_file, error_get_last()["message"])); + } + return PEAR::raiseError( + "$dest_file: write failed (" . error_get_last()["message"] . ")"); } - return PEAR::raiseError( - "$dest_file: write failed (" . error_get_last()["message"] . ")"); } } diff --git a/PEAR/REST.php b/PEAR/REST.php index 34ec4c03c..a9174f972 100644 --- a/PEAR/REST.php +++ b/PEAR/REST.php @@ -448,8 +448,19 @@ function downloadHttp($url, $lastmodified = null, $accept = false, $channel = fa $length = isset($headers['content-length']) ? $headers['content-length'] : -1; $data = ''; - while ($chunk = @fread($fp, 8192)) { - $data .= $chunk; + if (isset($headers['transfer-encoding']) && strtolower($headers['transfer-encoding']) === 'chunked') { + while (!feof($fp)) { + $chunk_size = hexdec(fgets($fp)); + if ($chunk_size === 0) { + break; + } + $data .= fread($fp, $chunk_size); + fgets($fp); // Skip the trailing CRLF + } + } else { + while ($chunk = @fread($fp, 8192)) { + $data .= $chunk; + } } fclose($fp);