Skip to content

Commit

Permalink
AsyncHttp\Client – Don't combine gzip encoding with the HTTP Range he…
Browse files Browse the repository at this point in the history
…ader.

When we're requesting a byte range AND gzipped transfer encoding,
our intention is to get compressed bytes 0-X of the original file.

However, some servers will compress the file first, and then return
the compressed bytes 0-X. The result is both unpredictable and impossible
to decompress.

This commit also makes other cosmetic changes – exposes http response as
an integer, and ensures the request headers are lowercase
  • Loading branch information
adamziel committed Nov 17, 2024
1 parent fe120d2 commit 32b937d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
26 changes: 19 additions & 7 deletions src/WordPress/AsyncHttp/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,7 @@ private function parse_http_headers( string $headers ) {
$status = explode( ' ', $status );
$status = array(
'protocol' => $status[0],
'code' => $status[1],
'code' => (int) $status[1],
'message' => $status[2],
);
$headers = array();
Expand Down Expand Up @@ -822,17 +822,29 @@ static private function prepare_request_headers( Request $request ) {
$path = ( isset( $parts['path'] ) ? $parts['path'] : '/' ) . ( isset( $parts['query'] ) ? '?' . $parts['query'] : '' );

$headers = [
"Host" => $host,
"User-Agent" => "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36",
"Accept" => "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"Accept-Encoding" => "gzip",
"Accept-Language" => "en-US,en;q=0.9",
"Connection" => "close",
"host" => $host,
"user-agent" => "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36",
"accept" => "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"accept-language" => "en-US,en;q=0.9",
"connection" => "close",
];
foreach ( $request->headers as $k => $v ) {
$headers[ $k ] = $v;
}

/**
* Disable the gzip transfer compression when requesting a byte range.
*
* When we're requesting a byte range AND gzipped transfer encoding,
* our intention is to get compressed bytes 0-X of the original file.
*
* However, some servers will compress the file first, and then return
* the compressed bytes 0-X. The result is both unpredictable and impossible
* to decompress.
*/
if(!array_key_exists('range', $headers)) {
$headers['accept-encoding'] = 'gzip';
}

$request_parts = array(
"$request->method $path HTTP/$request->http_version",
Expand Down
2 changes: 1 addition & 1 deletion src/WordPress/AsyncHttp/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function __construct( string $url, $request_info = array() ) {
$this->is_ssl = strpos( $url, 'https://' ) === 0;

$this->method = $request_info['method'];
$this->headers = $request_info['headers'];
$this->headers = array_change_key_case($request_info['headers'], CASE_LOWER);
$this->upload_body_stream = $request_info['body_stream'];
$this->http_version = $request_info['http_version'];
$this->redirected_from = $request_info['redirected_from'];
Expand Down
1 change: 1 addition & 0 deletions src/WordPress/Zip/NewZipStreamReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*
* @TODO: Replace ZipStreamReader with this class once the consumers of
* ZipStreamReader have been updated to use the new interface.
* @TODO: Replace fopen() et al. with the Stream interface (append_bytes() etc.)
*/
class NewZipStreamReader {

Expand Down

0 comments on commit 32b937d

Please sign in to comment.