| @@ -1,51 +1,33 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | +declare (strict_types=1); | |
| 3 | 4 | namespace Dudlewebs\WPMCS\s3\GuzzleHttp\Psr7; |
| 4 | 5 | |
| 5 | 6 | use Dudlewebs\WPMCS\s3\Psr\Http\Message\StreamInterface; |
| 6 | 7 | /** |
| 7 | - * Uses PHP's zlib.inflate filter to inflate deflate or gzipped content. | |
| 8 | + * Uses PHP's zlib.inflate filter to inflate zlib (HTTP deflate, RFC1950) or gzipped (RFC1952) content. | |
| 8 | 9 | * |
| 9 | - * This stream decorator skips the first 10 bytes of the given stream to remove | |
| 10 | - * the gzip header, converts the provided stream to a PHP stream resource, | |
| 10 | + * This stream decorator converts the provided stream to a PHP stream resource, | |
| 11 | 11 | * then appends the zlib.inflate filter. The stream is then converted back |
| 12 | 12 | * to a Guzzle stream resource to be used as a Guzzle stream. |
| 13 | 13 | * |
| 14 | - * @link http://tools.ietf.org/html/rfc1952 | |
| 15 | - * @link http://php.net/manual/en/filters.compression.php | |
| 16 | - * | |
| 17 | - * @final | |
| 14 | + * @see https://datatracker.ietf.org/doc/html/rfc1950 | |
| 15 | + * @see https://datatracker.ietf.org/doc/html/rfc1952 | |
| 16 | + * @see https://www.php.net/manual/en/filters.compression.php | |
| 18 | 17 | */ |
| 19 | -class InflateStream implements StreamInterface | |
| 18 | +final class InflateStream implements StreamInterface | |
| 20 | 19 | { |
| 21 | 20 | use StreamDecoratorTrait; |
| 21 | + /** @var StreamInterface */ | |
| 22 | + private $stream; | |
| 22 | 23 | public function __construct(StreamInterface $stream) |
| 23 | 24 | { |
| 24 | - // read the first 10 bytes, ie. gzip header | |
| 25 | - $header = $stream->read(10); | |
| 26 | - $filenameHeaderLength = $this->getLengthOfPossibleFilenameHeader($stream, $header); | |
| 27 | - // Skip the header, that is 10 + length of filename + 1 (nil) bytes | |
| 28 | - $stream = new LimitStream($stream, -1, 10 + $filenameHeaderLength); | |
| 29 | 25 | $resource = StreamWrapper::getResource($stream); |
| 30 | - \stream_filter_append($resource, 'zlib.inflate', \STREAM_FILTER_READ); | |
| 26 | + // Specify window=15+32, so zlib will use header detection to both gzip (with header) and zlib data | |
| 27 | + // See https://www.zlib.net/manual.html#Advanced definition of inflateInit2 | |
| 28 | + // "Add 32 to windowBits to enable zlib and gzip decoding with automatic header detection" | |
| 29 | + // Default window size is 15. | |
| 30 | + \stream_filter_append($resource, 'zlib.inflate', \STREAM_FILTER_READ, ['window' => 15 + 32]); | |
| 31 | 31 | $this->stream = $stream->isSeekable() ? new Stream($resource) : new NoSeekStream(new Stream($resource)); |
| 32 | - } | |
| 33 | - /** | |
| 34 | - * @param StreamInterface $stream | |
| 35 | - * @param $header | |
| 36 | - * | |
| 37 | - * @return int | |
| 38 | - */ | |
| 39 | - private function getLengthOfPossibleFilenameHeader(StreamInterface $stream, $header) | |
| 40 | - { | |
| 41 | - $filename_header_length = 0; | |
| 42 | - if (\substr(\bin2hex($header), 6, 2) === '08') { | |
| 43 | - // we have a filename, read until nil | |
| 44 | - $filename_header_length = 1; | |
| 45 | - while ($stream->read(1) !== \chr(0)) { | |
| 46 | - $filename_header_length++; | |
| 47 | - } | |
| 48 | - } | |
| 49 | - return $filename_header_length; | |
| 50 | 32 | } |
| 51 | 33 | } |