Skip to content

Commit

Permalink
Requests::decompress(): move magic markers to constant
Browse files Browse the repository at this point in the history
... and document the source and what the marker is indicating.

Includes switching to using `isset()` instead of `in_array()` for a tiny performance boost.
  • Loading branch information
jrfnl committed Sep 17, 2021
1 parent b0eea74 commit 53f025b
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/Requests.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,25 @@ class Requests {
Fsockopen::class => Fsockopen::class,
);

/**
* All (known) valid deflate, gzip header magic markers.
*
* These markers related to different compression levels.
*
* @link https://stackoverflow.com/a/43170354/482864 Marker source.
*
* @since 2.0.0
*
* @var array
*/
const MAGIC_COMPRESSION_HEADERS = array(
"\x1f\x8b" => true, // Gzip marker.
"\x78\x01" => true, // Zlib marker - level 1.
"\x78\x5e" => true, // Zlib marker - level 2 to 5.
"\x78\x9c" => true, // Zlib marker - level 6.
"\x78\xda" => true, // Zlib marker - level 7 to 9.
);

/**
* Current version of Requests
*
Expand Down Expand Up @@ -823,9 +842,8 @@ public static function flatten($array) {
* @return string Decompressed string
*/
public static function decompress($data) {
// All valid deflate, gzip header magic markers
$valid_magic = array("\x1f\x8b", "\x78\x01", "\x78\x5e", "\x78\x9c", "\x78\xda");
if (!in_array(substr($data, 0, 2), $valid_magic, true)) {
$marker = substr($data, 0, 2);
if (!isset(self::MAGIC_COMPRESSION_HEADERS[$marker])) {
// Not actually compressed. Probably cURL ruining this for us.
return $data;
}
Expand Down

0 comments on commit 53f025b

Please sign in to comment.