PluginProbe
WCPOS – Point of Sale (POS) plugin for WooCommerce / 1.10.20
WCPOS – Point of Sale (POS) plugin for WooCommerce v1.10.20
1.10.20 1.10.19 1.10.18 1.10.17 1.10.16 1.10.15 1.10.13 1.10.14 1.10.12 1.10.11 1.10.10 1.10.9 1.10.8 untagged-3d9b7ccddc54df87c672 1.10.7 1.10.6 1.10.5 1.10.3 1.10.4 1.10.2 1.10.1 1.10.0 1.9.17 1.9.15 1.9.16 All 164 releases
woocommerce-pos / vendor_prefixed / guzzlehttp / psr7 / src / InflateStream.php

InflateStream.php in WCPOS – Point of Sale (POS) plugin for WooCommerce 1.10.20, at vendor_prefixed/guzzlehttp/psr7/src/InflateStream.php

85 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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