Skip to content

Detach stream before serializing PSR-7 response #84

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
Apr 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ jobs:

- name: Install dependencies
run: |
composer require "friends-of-phpspec/phpspec-code-coverage:^4.3.2" --no-interaction --no-update
composer require "friends-of-phpspec/phpspec-code-coverage" --no-interaction --no-update
composer update --prefer-dist --no-interaction --no-progress

- name: Execute tests
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

## 1.8.0 - 2023-04-28

- Avoid PHP warning about serializing resources when serializing the response by detaching the stream.

## 1.7.6 - 2023-04-28

- Test with PHP 8.1 and 8.2
Expand Down
31 changes: 31 additions & 0 deletions spec/CachePluginSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace spec\Http\Client\Common\Plugin;

use Http\Client\Common\Plugin\Cache\Generator\SimpleGenerator;
use PhpSpec\Wrapper\Collaborator;
use Prophecy\Argument;
use Http\Message\StreamFactory;
use Http\Promise\FulfilledPromise;
Expand All @@ -18,8 +19,14 @@

class CachePluginSpec extends ObjectBehavior
{
/**
* @var StreamFactory&Collaborator
*/
private $streamFactory;

function let(CacheItemPoolInterface $pool, StreamFactory $streamFactory)
{
$this->streamFactory = $streamFactory;
$this->beConstructedWith($pool, $streamFactory, [
'default_ttl' => 60,
'cache_lifetime' => 1000
Expand All @@ -42,6 +49,7 @@ function it_caches_responses(CacheItemPoolInterface $pool, CacheItemInterface $i
$stream->__toString()->willReturn($httpBody);
$stream->isSeekable()->willReturn(true);
$stream->rewind()->shouldBeCalled();
$stream->detach()->shouldBeCalled();

$request->getMethod()->willReturn('GET');
$request->getUri()->willReturn($uri);
Expand All @@ -53,6 +61,9 @@ function it_caches_responses(CacheItemPoolInterface $pool, CacheItemInterface $i
$response->getHeader('Cache-Control')->willReturn([])->shouldBeCalled();
$response->getHeader('Expires')->willReturn([])->shouldBeCalled();
$response->getHeader('ETag')->willReturn([])->shouldBeCalled();
$response->withBody($stream)->shouldBeCalled()->willReturn($response);

$this->streamFactory->createStream($httpBody)->shouldBeCalled()->willReturn($stream);

$pool->getItem(Argument::any())->shouldBeCalled()->willReturn($item);
$item->isHit()->willReturn(false);
Expand Down Expand Up @@ -128,6 +139,7 @@ function it_stores_post_requests_when_allowed(
$stream->__toString()->willReturn($httpBody);
$stream->isSeekable()->willReturn(true);
$stream->rewind()->shouldBeCalled();
$stream->detach()->shouldBeCalled();

$request->getMethod()->willReturn('POST');
$request->getUri()->willReturn($uri);
Expand All @@ -139,6 +151,9 @@ function it_stores_post_requests_when_allowed(
$response->getHeader('Cache-Control')->willReturn([])->shouldBeCalled();
$response->getHeader('Expires')->willReturn([])->shouldBeCalled();
$response->getHeader('ETag')->willReturn([])->shouldBeCalled();
$response->withBody($stream)->shouldBeCalled()->willReturn($response);

$this->streamFactory->createStream($httpBody)->shouldBeCalled()->willReturn($stream);

$pool->getItem(Argument::any())->shouldBeCalled()->willReturn($item);
$item->isHit()->willReturn(false);
Expand Down Expand Up @@ -186,6 +201,7 @@ function it_calculate_age_from_response(CacheItemPoolInterface $pool, CacheItemI
$stream->__toString()->willReturn($httpBody);
$stream->isSeekable()->willReturn(true);
$stream->rewind()->shouldBeCalled();
$stream->detach()->shouldBeCalled();

$request->getMethod()->willReturn('GET');
$request->getUri()->willReturn($uri);
Expand All @@ -198,6 +214,9 @@ function it_calculate_age_from_response(CacheItemPoolInterface $pool, CacheItemI
$response->getHeader('Age')->willReturn(['15']);
$response->getHeader('Expires')->willReturn([]);
$response->getHeader('ETag')->willReturn([]);
$response->withBody($stream)->shouldBeCalled()->willReturn($response);

$this->streamFactory->createStream($httpBody)->shouldBeCalled()->willReturn($stream);

$pool->getItem(Argument::any())->shouldBeCalled()->willReturn($item);
$item->isHit()->willReturn(false);
Expand Down Expand Up @@ -226,6 +245,7 @@ function it_saves_etag(CacheItemPoolInterface $pool, CacheItemInterface $item, R
$stream->__toString()->willReturn($httpBody);
$stream->isSeekable()->willReturn(true);
$stream->rewind()->shouldBeCalled();
$stream->detach()->shouldBeCalled();
$request->getBody()->shouldBeCalled()->willReturn($stream);

$request->getMethod()->willReturn('GET');
Expand All @@ -236,6 +256,9 @@ function it_saves_etag(CacheItemPoolInterface $pool, CacheItemInterface $item, R
$response->getHeader('Cache-Control')->willReturn([]);
$response->getHeader('Expires')->willReturn([]);
$response->getHeader('ETag')->willReturn(['foo_etag']);
$response->withBody($stream)->shouldBeCalled()->willReturn($response);

$this->streamFactory->createStream($httpBody)->shouldBeCalled()->willReturn($stream);

$pool->getItem(Argument::any())->shouldBeCalled()->willReturn($item);
$item->isHit()->willReturn(false);
Expand Down Expand Up @@ -387,6 +410,7 @@ function it_caches_private_responses_when_allowed(
$stream->__toString()->willReturn($httpBody);
$stream->isSeekable()->willReturn(true);
$stream->rewind()->shouldBeCalled();
$stream->detach()->shouldBeCalled();

$request->getMethod()->willReturn('GET');
$request->getUri()->willReturn($uri);
Expand All @@ -398,6 +422,9 @@ function it_caches_private_responses_when_allowed(
$response->getHeader('Cache-Control')->willReturn(['private'])->shouldBeCalled();
$response->getHeader('Expires')->willReturn([])->shouldBeCalled();
$response->getHeader('ETag')->willReturn([])->shouldBeCalled();
$response->withBody($stream)->shouldBeCalled()->willReturn($response);

$this->streamFactory->createStream($httpBody)->shouldBeCalled()->willReturn($stream);

$pool->getItem(Argument::any())->shouldBeCalled()->willReturn($item);
$item->isHit()->willReturn(false);
Expand Down Expand Up @@ -484,6 +511,7 @@ function it_stores_responses_of_requests_not_in_blacklisted_paths(
$stream->__toString()->willReturn($httpBody);
$stream->isSeekable()->willReturn(true);
$stream->rewind()->shouldBeCalled();
$stream->detach()->shouldBeCalled();

$request->getMethod()->willReturn('GET');
$request->getUri()->willReturn($uri);
Expand All @@ -495,6 +523,9 @@ function it_stores_responses_of_requests_not_in_blacklisted_paths(
$response->getHeader('Cache-Control')->willReturn([])->shouldBeCalled();
$response->getHeader('Expires')->willReturn([])->shouldBeCalled();
$response->getHeader('ETag')->willReturn([])->shouldBeCalled();
$response->withBody($stream)->shouldBeCalled()->willReturn($response);

$this->streamFactory->createStream($httpBody)->shouldBeCalled()->willReturn($stream);

$pool->getItem(Argument::any())->shouldBeCalled()->willReturn($item);
$item->isHit()->willReturn(false);
Expand Down
13 changes: 8 additions & 5 deletions src/CachePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,7 @@ protected function doHandleRequest(RequestInterface $request, callable $next, ca
if ($this->isCacheable($response) && $this->isCacheableRequest($request)) {
$bodyStream = $response->getBody();
$body = $bodyStream->__toString();
if ($bodyStream->isSeekable()) {
$bodyStream->rewind();
} else {
$response = $response->withBody($this->streamFactory->createStream($body));
}
$bodyStream->detach();

$maxAge = $this->getMaxAge($response);
$cacheItem
Expand All @@ -212,6 +208,13 @@ protected function doHandleRequest(RequestInterface $request, callable $next, ca
'etag' => $response->getHeader('ETag'),
]);
$this->pool->save($cacheItem);

$bodyStream = $this->streamFactory->createStream($body);
if ($bodyStream->isSeekable()) {
$bodyStream->rewind();
}

$response = $response->withBody($bodyStream);
}

return $this->handleCacheListeners($request, $response, false, $cacheItem);
Expand Down