Skip to content

Commit

Permalink
Merge pull request #159 from oleg-andreyev/rewind-issue
Browse files Browse the repository at this point in the history
after calling CacheEntry::__sleep we need to rewind
  • Loading branch information
Kevinrob authored Mar 15, 2022
2 parents 22d79bb + 04ea3a1 commit 0a61532
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 6 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"cache/array-adapter": "^0.4 || ^0.5 || ^1.0",
"illuminate/cache": "^5.0",
"cache/simple-cache-bridge": "^0.1 || ^1.0",
"symfony/phpunit-bridge": "^4.4 || ^5.0"
"symfony/phpunit-bridge": "^4.4 || ^5.0",
"symfony/cache": "^4.4 || ^5.0"
},
"autoload": {
"psr-4": {
Expand Down
17 changes: 12 additions & 5 deletions src/CacheMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ class CacheMiddleware
*/
protected $httpMethods = ['GET' => true];

/**
/**
* List of safe methods
*
*
* https://datatracker.ietf.org/doc/html/rfc7231#section-4.2.1
*
*
* @var array
*/
protected $safeMethods = ['GET' => true, 'HEAD' => true, 'OPTIONS' => true, 'TRACE' => true];
Expand Down Expand Up @@ -263,10 +263,12 @@ protected static function addToCache(
ResponseInterface $response,
$update = false
) {
$body = $response->getBody();

// If the body is not seekable, we have to replace it by a seekable one
if (!$response->getBody()->isSeekable()) {
if (!$body->isSeekable()) {
$response = $response->withBody(
\GuzzleHttp\Psr7\Utils::streamFor($response->getBody()->getContents())
\GuzzleHttp\Psr7\Utils::streamFor($body->getContents())
);
}

Expand All @@ -276,6 +278,11 @@ protected static function addToCache(
$cache->cache($request, $response);
}

// always rewind back to the start otherwise other middlewares may get empty "content"
if ($body->isSeekable()) {
$response->getBody()->rewind();
}

return $response;
}

Expand Down
46 changes: 46 additions & 0 deletions tests/CacheMiddlewareTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Kevinrob\GuzzleCache\Tests;

use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7\Utils;
use Kevinrob\GuzzleCache\CacheMiddleware as BaseCacheMiddleware;
use Kevinrob\GuzzleCache\Storage\Psr6CacheStorage;
use Kevinrob\GuzzleCache\Strategy\CacheStrategyInterface;
use Kevinrob\GuzzleCache\Strategy\PrivateCacheStrategy;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;

class CacheMiddlewareTest extends TestCase
{
public function testRewindAfterReadingStream()
{
$stream = Utils::streamFor('seekable stream');
$strategy = new PrivateCacheStrategy(
new Psr6CacheStorage(
new FilesystemAdapter('', -1, sys_get_temp_dir())
)
);
$request = new Request('GET', '/uri');
$response = (new Response())->withBody($stream)->withHeader('Cache-Control', 'max-age=3600');

CacheMiddleware::addToCache(
$strategy,
$request,
$response
);

$this->assertEquals('seekable stream', $response->getBody()->getContents());
}
}

class CacheMiddleware extends BaseCacheMiddleware
{
public static function addToCache(CacheStrategyInterface $cache, RequestInterface $request, ResponseInterface $response, $update = false)
{
return parent::addToCache($cache, $request, $response, $update);
}
}

0 comments on commit 0a61532

Please sign in to comment.