Skip to content

Fix deflate encoding handling and disable compress encoding #61

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 17, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 8 additions & 30 deletions spec/Plugin/DecoderPluginSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ function it_decodes(RequestInterface $request, ResponseInterface $response, Stre
throw new SkippingException('Skipping test on hhvm, as there is no chunk encoding on hhvm');
}

$request->withHeader('TE', ['gzip', 'deflate', 'compress', 'chunked'])->shouldBeCalled()->willReturn($request);
$request->withHeader('Accept-Encoding', ['gzip', 'deflate', 'compress'])->shouldBeCalled()->willReturn($request);
$request->withHeader('TE', ['gzip', 'deflate', 'chunked'])->shouldBeCalled()->willReturn($request);
$request->withHeader('Accept-Encoding', ['gzip', 'deflate'])->shouldBeCalled()->willReturn($request);
$next = function () use($response) {
return new HttpFulfilledPromise($response->getWrappedObject());
};
Expand All @@ -50,8 +50,8 @@ function it_decodes(RequestInterface $request, ResponseInterface $response, Stre

function it_decodes_gzip(RequestInterface $request, ResponseInterface $response, StreamInterface $stream)
{
$request->withHeader('TE', ['gzip', 'deflate', 'compress', 'chunked'])->shouldBeCalled()->willReturn($request);
$request->withHeader('Accept-Encoding', ['gzip', 'deflate', 'compress'])->shouldBeCalled()->willReturn($request);
$request->withHeader('TE', ['gzip', 'deflate', 'chunked'])->shouldBeCalled()->willReturn($request);
$request->withHeader('Accept-Encoding', ['gzip', 'deflate'])->shouldBeCalled()->willReturn($request);
$next = function () use($response) {
return new HttpFulfilledPromise($response->getWrappedObject());
};
Expand All @@ -72,8 +72,8 @@ function it_decodes_gzip(RequestInterface $request, ResponseInterface $response,

function it_decodes_deflate(RequestInterface $request, ResponseInterface $response, StreamInterface $stream)
{
$request->withHeader('TE', ['gzip', 'deflate', 'compress', 'chunked'])->shouldBeCalled()->willReturn($request);
$request->withHeader('Accept-Encoding', ['gzip', 'deflate', 'compress'])->shouldBeCalled()->willReturn($request);
$request->withHeader('TE', ['gzip', 'deflate', 'chunked'])->shouldBeCalled()->willReturn($request);
$request->withHeader('Accept-Encoding', ['gzip', 'deflate'])->shouldBeCalled()->willReturn($request);
$next = function () use($response) {
return new HttpFulfilledPromise($response->getWrappedObject());
};
Expand All @@ -82,28 +82,6 @@ function it_decodes_deflate(RequestInterface $request, ResponseInterface $respon
$response->hasHeader('Content-Encoding')->willReturn(true);
$response->getHeader('Content-Encoding')->willReturn(['deflate']);
$response->getBody()->willReturn($stream);
$response->withBody(Argument::type('Http\Message\Encoding\InflateStream'))->willReturn($response);
$response->withHeader('Content-Encoding', [])->willReturn($response);

$stream->isReadable()->willReturn(true);
$stream->isWritable()->willReturn(false);
$stream->eof()->willReturn(false);

$this->handleRequest($request, $next, function () {});
}

function it_decodes_inflate(RequestInterface $request, ResponseInterface $response, StreamInterface $stream)
{
$request->withHeader('TE', ['gzip', 'deflate', 'compress', 'chunked'])->shouldBeCalled()->willReturn($request);
$request->withHeader('Accept-Encoding', ['gzip', 'deflate', 'compress'])->shouldBeCalled()->willReturn($request);
$next = function () use($response) {
return new HttpFulfilledPromise($response->getWrappedObject());
};

$response->hasHeader('Transfer-Encoding')->willReturn(false);
$response->hasHeader('Content-Encoding')->willReturn(true);
$response->getHeader('Content-Encoding')->willReturn(['compress']);
$response->getBody()->willReturn($stream);
$response->withBody(Argument::type('Http\Message\Encoding\DecompressStream'))->willReturn($response);
$response->withHeader('Content-Encoding', [])->willReturn($response);

Expand All @@ -118,8 +96,8 @@ function it_does_not_decode_with_content_encoding(RequestInterface $request, Res
{
$this->beConstructedWith(['use_content_encoding' => false]);

$request->withHeader('TE', ['gzip', 'deflate', 'compress', 'chunked'])->shouldBeCalled()->willReturn($request);
$request->withHeader('Accept-Encoding', ['gzip', 'deflate', 'compress'])->shouldNotBeCalled();
$request->withHeader('TE', ['gzip', 'deflate', 'chunked'])->shouldBeCalled()->willReturn($request);
$request->withHeader('Accept-Encoding', ['gzip', 'deflate'])->shouldNotBeCalled();
$next = function () use($response) {
return new HttpFulfilledPromise($response->getWrappedObject());
};
Expand Down
8 changes: 2 additions & 6 deletions src/Plugin/DecoderPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function __construct(array $config = [])
*/
public function handleRequest(RequestInterface $request, callable $next, callable $first)
{
$encodings = extension_loaded('zlib') ? ['gzip', 'deflate', 'compress'] : ['identity'];
$encodings = extension_loaded('zlib') ? ['gzip', 'deflate'] : ['identity'];

if ($this->useContentEncoding) {
$request = $request->withHeader('Accept-Encoding', $encodings);
Expand Down Expand Up @@ -127,12 +127,8 @@ private function decorateStream($encoding, StreamInterface $stream)
return new Encoding\DechunkStream($stream);
}

if (strtolower($encoding) == 'compress') {
return new Encoding\DecompressStream($stream);
}

if (strtolower($encoding) == 'deflate') {
return new Encoding\InflateStream($stream);
return new Encoding\DecompressStream($stream);
}

if (strtolower($encoding) == 'gzip') {
Expand Down