Open Journal Systems  3.3.0
guzzlehttp/psr7/src/InflateStream.php
1 <?php
2 namespace GuzzleHttp\Psr7;
3 
5 
18 {
19  use StreamDecoratorTrait;
20 
21  public function __construct(StreamInterface $stream)
22  {
23  // read the first 10 bytes, ie. gzip header
24  $header = $stream->read(10);
25  $filenameHeaderLength = $this->getLengthOfPossibleFilenameHeader($stream, $header);
26  // Skip the header, that is 10 + length of filename + 1 (nil) bytes
27  $stream = new LimitStream($stream, -1, 10 + $filenameHeaderLength);
28  $resource = StreamWrapper::getResource($stream);
29  stream_filter_append($resource, 'zlib.inflate', STREAM_FILTER_READ);
30  $this->stream = $stream->isSeekable() ? new Stream($resource) : new NoSeekStream(new Stream($resource));
31  }
32 
38  private function getLengthOfPossibleFilenameHeader(StreamInterface $stream, $header)
39  {
40  $filename_header_length = 0;
41 
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 
50  return $filename_header_length;
51  }
52 }
Psr\Http\Message\StreamInterface
Definition: vendor/psr/http-message/src/StreamInterface.php:12
GuzzleHttp\Psr7\LimitStream
Definition: LimitStream.php:10
GuzzleHttp\Psr7
Definition: AppendStream.php:2
GuzzleHttp\Psr7\NoSeekStream
Definition: NoSeekStream.php:9
GuzzleHttp\Psr7\StreamWrapper\getResource
static getResource(StreamInterface $stream)
Definition: StreamWrapper.php:37
GuzzleHttp\Psr7\InflateStream
Definition: guzzlehttp/psr7/src/InflateStream.php:17
GuzzleHttp\Psr7\InflateStream\__construct
__construct(StreamInterface $stream)
Definition: guzzlehttp/psr7/src/InflateStream.php:21
GuzzleHttp\Psr7\Stream
Definition: vendor/guzzlehttp/psr7/src/Stream.php:11