@@ -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
0 commit comments