| 1 |
<?php |
| 2 |
|
| 3 |
declare (strict_types=1); |
| 4 |
namespace WCPOS\Vendor\GuzzleHttp\Psr7; |
| 5 |
|
| 6 |
use WCPOS\Vendor\GuzzleHttp\Psr7\Exception\TimeoutException; |
| 7 |
use WCPOS\Vendor\Psr\Http\Message\StreamInterface; |
| 8 |
/** |
| 9 |
* Uses PHP's zlib.inflate filter to inflate zlib (HTTP deflate, RFC1950) or gzipped (RFC1952) content. |
| 10 |
* |
| 11 |
* This stream decorator converts the provided stream to a PHP stream resource, |
| 12 |
* then appends the zlib.inflate filter. The stream is then converted back |
| 13 |
* to a Guzzle stream resource to be used as a Guzzle stream. |
| 14 |
* |
| 15 |
* @see https://datatracker.ietf.org/doc/html/rfc1950 |
| 16 |
* @see https://datatracker.ietf.org/doc/html/rfc1952 |
| 17 |
* @see https://www.php.net/manual/en/filters.compression.php |
| 18 |
*/ |
| 19 |
final class InflateStream implements StreamInterface |
| 20 |
{ |
| 21 |
use StreamDecoratorTrait; |
| 22 |
use NonSerializableStreamTrait; |
| 23 |
private StreamInterface $stream; |
| 24 |
private ?StreamInterface $source; |
| 25 |
public function __construct(StreamInterface $stream) |
| 26 |
{ |
| 27 |
$this->source = $stream; |
| 28 |
$resource = StreamWrapper::getResource($stream); |
| 29 |
// Specify window=15+32, so zlib will use header detection to both gzip (with header) and zlib data |
| 30 |
// See https://www.zlib.net/manual.html#Advanced definition of inflateInit2 |
| 31 |
// "Add 32 to windowBits to enable zlib and gzip decoding with automatic header detection" |
| 32 |
// Default window size is 15. |
| 33 |
\stream_filter_append($resource, 'zlib.inflate', \STREAM_FILTER_READ, ['window' => 15 + 32]); |
| 34 |
$this->stream = $stream->isSeekable() ? new Stream($resource) : new NoSeekStream(new Stream($resource)); |
| 35 |
} |
| 36 |
public function read(int $length) : string |
| 37 |
{ |
| 38 |
if ($length <= 0 || $this->source === null) { |
| 39 |
return $this->stream->read($length); |
| 40 |
} |
| 41 |
try { |
| 42 |
$data = $this->stream->read($length); |
| 43 |
} catch (TimeoutException $e) { |
| 44 |
throw $e; |
| 45 |
} catch (\RuntimeException $e) { |
| 46 |
if (StreamTimeout::isReadTimedOut($this->source)) { |
| 47 |
throw new TimeoutException('Unable to read from stream: timed out', 0, $e); |
| 48 |
} |
| 49 |
throw $e; |
| 50 |
} |
| 51 |
if ($data === '' && StreamTimeout::isReadTimedOut($this->source)) { |
| 52 |
throw new TimeoutException('Unable to read from stream: timed out'); |
| 53 |
} |
| 54 |
return $data; |
| 55 |
} |
| 56 |
public function close() : void |
| 57 |
{ |
| 58 |
$source = $this->source; |
| 59 |
$this->source = null; |
| 60 |
$exception = null; |
| 61 |
try { |
| 62 |
$this->stream->close(); |
| 63 |
} catch (\Throwable $e) { |
| 64 |
$exception = $e; |
| 65 |
} |
| 66 |
if ($source !== null) { |
| 67 |
try { |
| 68 |
$source->close(); |
| 69 |
} catch (\Throwable $e) { |
| 70 |
if ($exception === null) { |
| 71 |
$exception = $e; |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
if ($exception !== null) { |
| 76 |
throw $exception; |
| 77 |
} |
| 78 |
} |
| 79 |
public function detach() |
| 80 |
{ |
| 81 |
$this->source = null; |
| 82 |
return $this->stream->detach(); |
| 83 |
} |
| 84 |
} |
| 85 |
|