InflateStream.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. * @final
  16. */
  17. class InflateStream implements StreamInterface
  18. {
  19. use StreamDecoratorTrait;
  20. public function __construct(StreamInterface $stream)
  21. {
  22. // read the first 10 bytes, ie. gzip header
  23. $header = $stream->read(10);
  24. $filenameHeaderLength = $this->getLengthOfPossibleFilenameHeader($stream, $header);
  25. // Skip the header, that is 10 + length of filename + 1 (nil) bytes
  26. $stream = new LimitStream($stream, -1, 10 + $filenameHeaderLength);
  27. $resource = StreamWrapper::getResource($stream);
  28. stream_filter_append($resource, 'zlib.inflate', STREAM_FILTER_READ);
  29. $this->stream = $stream->isSeekable() ? new Stream($resource) : new NoSeekStream(new Stream($resource));
  30. }
  31. /**
  32. * @param StreamInterface $stream
  33. * @param $header
  34. *
  35. * @return int
  36. */
  37. private function getLengthOfPossibleFilenameHeader(StreamInterface $stream, $header)
  38. {
  39. $filename_header_length = 0;
  40. if (substr(bin2hex($header), 6, 2) === '08') {
  41. // we have a filename, read until nil
  42. $filename_header_length = 1;
  43. while ($stream->read(1) !== chr(0)) {
  44. $filename_header_length++;
  45. }
  46. }
  47. return $filename_header_length;
  48. }
  49. }