InflateStream.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. namespace GuzzleHttp\Psr7;
  3. use Psr\Http\Message\StreamInterface;
  4. /**
  5. * Uses PHP's zlib.inflate filter to inflate deflate or gzipped content.
  6. *
  7. * This stream decorator skips the first 10 bytes of the given stream to remove
  8. * the gzip header, converts the provided stream to a PHP stream resource,
  9. * then appends the zlib.inflate filter. The stream is then converted back
  10. * to a Guzzle stream resource to be used as a Guzzle stream.
  11. *
  12. * @link http://tools.ietf.org/html/rfc1952
  13. * @link http://php.net/manual/en/filters.compression.php
  14. */
  15. class InflateStream implements StreamInterface
  16. {
  17. use StreamDecoratorTrait;
  18. public function __construct(StreamInterface $stream)
  19. {
  20. // read the first 10 bytes, ie. gzip header
  21. $header = $stream->read(10);
  22. $filenameHeaderLength = $this->getLengthOfPossibleFilenameHeader($stream, $header);
  23. // Skip the header, that is 10 + length of filename + 1 (nil) bytes
  24. $stream = new LimitStream($stream, -1, 10 + $filenameHeaderLength);
  25. $resource = StreamWrapper::getResource($stream);
  26. stream_filter_append($resource, 'zlib.inflate', STREAM_FILTER_READ);
  27. $this->stream = $stream->isSeekable() ? new Stream($resource) : new NoSeekStream(new Stream($resource));
  28. }
  29. /**
  30. * @param StreamInterface $stream
  31. * @param $header
  32. * @return int
  33. */
  34. private function getLengthOfPossibleFilenameHeader(StreamInterface $stream, $header)
  35. {
  36. $filename_header_length = 0;
  37. if (substr(bin2hex($header), 6, 2) === '08') {
  38. // we have a filename, read until nil
  39. $filename_header_length = 1;
  40. while ($stream->read(1) !== chr(0)) {
  41. $filename_header_length++;
  42. }
  43. }
  44. return $filename_header_length;
  45. }
  46. }