| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Http\Client\Common\Plugin; |
| 6 |
|
| 7 |
use Http\Client\Common\Plugin; |
| 8 |
use Http\Message\Encoding; |
| 9 |
use Http\Promise\Promise; |
| 10 |
use Psr\Http\Message\RequestInterface; |
| 11 |
use Psr\Http\Message\ResponseInterface; |
| 12 |
use Psr\Http\Message\StreamInterface; |
| 13 |
use Symfony\Component\OptionsResolver\OptionsResolver; |
| 14 |
|
| 15 |
/** |
| 16 |
* Allow to decode response body with a chunk, deflate, compress or gzip encoding. |
| 17 |
* |
| 18 |
* If zlib is not installed, only chunked encoding can be handled. |
| 19 |
* |
| 20 |
* If Content-Encoding is not disabled, the plugin will add an Accept-Encoding header for the encoding methods it supports. |
| 21 |
* |
| 22 |
* @author Joel Wurtz <joel.wurtz@gmail.com> |
| 23 |
*/ |
| 24 |
final class DecoderPlugin implements Plugin |
| 25 |
{ |
| 26 |
/** |
| 27 |
* @var bool Whether this plugin decode stream with value in the Content-Encoding header (default to true). |
| 28 |
* |
| 29 |
* If set to false only the Transfer-Encoding header will be used |
| 30 |
*/ |
| 31 |
private $useContentEncoding; |
| 32 |
|
| 33 |
/** |
| 34 |
* @param array{'use_content_encoding'?: bool} $config |
| 35 |
* |
| 36 |
* Configuration options: |
| 37 |
* - use_content_encoding: Whether this plugin should look at the Content-Encoding header first or only at the Transfer-Encoding (defaults to true) |
| 38 |
*/ |
| 39 |
public function __construct(array $config = []) |
| 40 |
{ |
| 41 |
$resolver = new OptionsResolver(); |
| 42 |
$resolver->setDefaults([ |
| 43 |
'use_content_encoding' => true, |
| 44 |
]); |
| 45 |
$resolver->setAllowedTypes('use_content_encoding', 'bool'); |
| 46 |
$options = $resolver->resolve($config); |
| 47 |
|
| 48 |
$this->useContentEncoding = $options['use_content_encoding']; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* {@inheritdoc} |
| 53 |
*/ |
| 54 |
public function handleRequest(RequestInterface $request, callable $next, callable $first): Promise |
| 55 |
{ |
| 56 |
$encodings = extension_loaded('zlib') ? ['gzip', 'deflate'] : ['identity']; |
| 57 |
|
| 58 |
if ($this->useContentEncoding) { |
| 59 |
$request = $request->withHeader('Accept-Encoding', $encodings); |
| 60 |
} |
| 61 |
$encodings[] = 'chunked'; |
| 62 |
$request = $request->withHeader('TE', $encodings); |
| 63 |
|
| 64 |
return $next($request)->then(function (ResponseInterface $response) { |
| 65 |
return $this->decodeResponse($response); |
| 66 |
}); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Decode a response body given its Transfer-Encoding or Content-Encoding value. |
| 71 |
*/ |
| 72 |
private function decodeResponse(ResponseInterface $response): ResponseInterface |
| 73 |
{ |
| 74 |
$response = $this->decodeOnEncodingHeader('Transfer-Encoding', $response); |
| 75 |
|
| 76 |
if ($this->useContentEncoding) { |
| 77 |
$response = $this->decodeOnEncodingHeader('Content-Encoding', $response); |
| 78 |
} |
| 79 |
|
| 80 |
return $response; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Decode a response on a specific header (content encoding or transfer encoding mainly). |
| 85 |
*/ |
| 86 |
private function decodeOnEncodingHeader(string $headerName, ResponseInterface $response): ResponseInterface |
| 87 |
{ |
| 88 |
if ($response->hasHeader($headerName)) { |
| 89 |
$encodings = $response->getHeader($headerName); |
| 90 |
$newEncodings = []; |
| 91 |
|
| 92 |
while ($encoding = array_pop($encodings)) { |
| 93 |
$stream = $this->decorateStream($encoding, $response->getBody()); |
| 94 |
|
| 95 |
if (false === $stream) { |
| 96 |
array_unshift($newEncodings, $encoding); |
| 97 |
|
| 98 |
continue; |
| 99 |
} |
| 100 |
|
| 101 |
$response = $response->withBody($stream); |
| 102 |
} |
| 103 |
|
| 104 |
if (\count($newEncodings) > 0) { |
| 105 |
$response = $response->withHeader($headerName, $newEncodings); |
| 106 |
} else { |
| 107 |
$response = $response->withoutHeader($headerName); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
return $response; |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Decorate a stream given an encoding. |
| 116 |
* |
| 117 |
* @return StreamInterface|false A new stream interface or false if encoding is not supported |
| 118 |
*/ |
| 119 |
private function decorateStream(string $encoding, StreamInterface $stream) |
| 120 |
{ |
| 121 |
if ('chunked' === strtolower($encoding)) { |
| 122 |
return new Encoding\DechunkStream($stream); |
| 123 |
} |
| 124 |
|
| 125 |
if ('deflate' === strtolower($encoding)) { |
| 126 |
return new Encoding\DecompressStream($stream); |
| 127 |
} |
| 128 |
|
| 129 |
if ('gzip' === strtolower($encoding)) { |
| 130 |
return new Encoding\GzipDecodeStream($stream); |
| 131 |
} |
| 132 |
|
| 133 |
return false; |
| 134 |
} |
| 135 |
} |
| 136 |
|