Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Detach stream before serializing PSR-7 response
  • Loading branch information
nicolas-grekas authored and dbu committed Apr 28, 2023
commit f16e8c63f3c7c23bc9f901917975b1f1d003a0ce
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
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