diff --git a/src/WordPress/AsyncHttp/Client.php b/src/WordPress/AsyncHttp/Client.php index 3af08da5..1681a240 100644 --- a/src/WordPress/AsyncHttp/Client.php +++ b/src/WordPress/AsyncHttp/Client.php @@ -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(); @@ -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", diff --git a/src/WordPress/AsyncHttp/Request.php b/src/WordPress/AsyncHttp/Request.php index 2320fd37..4e10dbb3 100644 --- a/src/WordPress/AsyncHttp/Request.php +++ b/src/WordPress/AsyncHttp/Request.php @@ -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']; diff --git a/src/WordPress/Zip/NewZipStreamReader.php b/src/WordPress/Zip/NewZipStreamReader.php index a7197d05..b49552b2 100644 --- a/src/WordPress/Zip/NewZipStreamReader.php +++ b/src/WordPress/Zip/NewZipStreamReader.php @@ -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 {