Skip to content
This repository was archived by the owner on Apr 12, 2025. It is now read-only.

Commit 1c7e162

Browse files
committed
Fix HTTP chunked encoding handling
Fixes pear#151 Update `PEAR/REST.php` and `PEAR/Downloader.php` to handle HTTP chunked encoding responses properly. --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/pear/pear-core/issues/151?shareId=XXXX-XXXX-XXXX-XXXX).
1 parent 9d3ac5e commit 1c7e162

File tree

2 files changed

+47
-12
lines changed

2 files changed

+47
-12
lines changed

PEAR/Downloader.php

+34-10
Original file line numberDiff line numberDiff line change
@@ -1726,19 +1726,43 @@ public static function _downloadHttp(
17261726
call_user_func($callback, 'start', array(basename($dest_file), $length));
17271727
}
17281728

1729-
while ($data = fread($fp, 1024)) {
1730-
$bytes += strlen($data);
1731-
if ($callback) {
1732-
call_user_func($callback, 'bytesread', $bytes);
1729+
if (isset($headers['transfer-encoding']) && strtolower($headers['transfer-encoding']) === 'chunked') {
1730+
while (!feof($fp)) {
1731+
$chunk_size = hexdec(fgets($fp));
1732+
if ($chunk_size === 0) {
1733+
break;
1734+
}
1735+
$data = fread($fp, $chunk_size);
1736+
$bytes += strlen($data);
1737+
if ($callback) {
1738+
call_user_func($callback, 'bytesread', $bytes);
1739+
}
1740+
if (!@fwrite($wp, $data)) {
1741+
fclose($fp);
1742+
if ($callback) {
1743+
call_user_func($callback, 'writefailed',
1744+
array($dest_file, error_get_last()["message"]));
1745+
}
1746+
return PEAR::raiseError(
1747+
"$dest_file: write failed (" . error_get_last()["message"] . ")");
1748+
}
1749+
fgets($fp); // Skip the trailing CRLF
17331750
}
1734-
if (!@fwrite($wp, $data)) {
1735-
fclose($fp);
1751+
} else {
1752+
while ($data = fread($fp, 1024)) {
1753+
$bytes += strlen($data);
17361754
if ($callback) {
1737-
call_user_func($callback, 'writefailed',
1738-
array($dest_file, error_get_last()["message"]));
1755+
call_user_func($callback, 'bytesread', $bytes);
1756+
}
1757+
if (!@fwrite($wp, $data)) {
1758+
fclose($fp);
1759+
if ($callback) {
1760+
call_user_func($callback, 'writefailed',
1761+
array($dest_file, error_get_last()["message"]));
1762+
}
1763+
return PEAR::raiseError(
1764+
"$dest_file: write failed (" . error_get_last()["message"] . ")");
17391765
}
1740-
return PEAR::raiseError(
1741-
"$dest_file: write failed (" . error_get_last()["message"] . ")");
17421766
}
17431767
}
17441768

PEAR/REST.php

+13-2
Original file line numberDiff line numberDiff line change
@@ -448,8 +448,19 @@ function downloadHttp($url, $lastmodified = null, $accept = false, $channel = fa
448448
$length = isset($headers['content-length']) ? $headers['content-length'] : -1;
449449

450450
$data = '';
451-
while ($chunk = @fread($fp, 8192)) {
452-
$data .= $chunk;
451+
if (isset($headers['transfer-encoding']) && strtolower($headers['transfer-encoding']) === 'chunked') {
452+
while (!feof($fp)) {
453+
$chunk_size = hexdec(fgets($fp));
454+
if ($chunk_size === 0) {
455+
break;
456+
}
457+
$data .= fread($fp, $chunk_size);
458+
fgets($fp); // Skip the trailing CRLF
459+
}
460+
} else {
461+
while ($chunk = @fread($fp, 8192)) {
462+
$data .= $chunk;
463+
}
453464
}
454465
fclose($fp);
455466

0 commit comments

Comments
 (0)