From a50f7964bd78dfdbd52ff7dee4e004b7ab9417ce Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Mon, 1 Aug 2016 23:39:14 +0100 Subject: [PATCH 1/6] Use sha1 to reduce the risk of collisions --- src/CachePlugin.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CachePlugin.php b/src/CachePlugin.php index 956be5b..ed8ccd7 100644 --- a/src/CachePlugin.php +++ b/src/CachePlugin.php @@ -150,7 +150,7 @@ private function getCacheControlDirective(ResponseInterface $response, $name) */ private function createCacheKey(RequestInterface $request) { - return md5($request->getMethod().' '.$request->getUri()); + return sha1($request->getMethod().' '.$request->getUri()); } /** From 5d3d98ccc02cead7724955447abe7f3ba6091fe8 Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Mon, 1 Aug 2016 23:43:24 +0100 Subject: [PATCH 2/6] Only cache if the max age is positive --- src/CachePlugin.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/CachePlugin.php b/src/CachePlugin.php index ed8ccd7..c588a82 100644 --- a/src/CachePlugin.php +++ b/src/CachePlugin.php @@ -77,7 +77,7 @@ public function handleRequest(RequestInterface $request, callable $next, callabl } return $next($request)->then(function (ResponseInterface $response) use ($cacheItem) { - if ($this->isCacheable($response)) { + if ($this->isCacheable($response) && ($maxAge = $this->getMaxAge($response)) > 0) { $bodyStream = $response->getBody(); $body = $bodyStream->__toString(); if ($bodyStream->isSeekable()) { @@ -87,7 +87,7 @@ public function handleRequest(RequestInterface $request, callable $next, callabl } $cacheItem->set(['response' => $response, 'body' => $body]) - ->expiresAfter($this->getMaxAge($response)); + ->expiresAfter($maxAge); $this->pool->save($cacheItem); } From 49369f001128c7b741d41fc61a289dd41b8588d2 Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Mon, 1 Aug 2016 23:50:38 +0100 Subject: [PATCH 3/6] Updated tests --- spec/CachePluginSpec.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/spec/CachePluginSpec.php b/spec/CachePluginSpec.php index a46327e..808806d 100644 --- a/spec/CachePluginSpec.php +++ b/spec/CachePluginSpec.php @@ -42,7 +42,7 @@ function it_caches_responses(CacheItemPoolInterface $pool, CacheItemInterface $i $response->getHeader('Cache-Control')->willReturn(array()); $response->getHeader('Expires')->willReturn(array()); - $pool->getItem('e3b717d5883a45ef9493d009741f7c64')->shouldBeCalled()->willReturn($item); + $pool->getItem('d20f64acc6e70b6079845f2fe357732929550ae1')->shouldBeCalled()->willReturn($item); $item->isHit()->willReturn(false); $item->set(['response' => $response, 'body' => $httpBody])->willReturn($item)->shouldBeCalled(); $item->expiresAfter(60)->willReturn($item)->shouldBeCalled(); @@ -63,7 +63,7 @@ function it_doesnt_store_failed_responses(CacheItemPoolInterface $pool, CacheIte $response->getHeader('Cache-Control')->willReturn(array()); $response->getHeader('Expires')->willReturn(array()); - $pool->getItem('e3b717d5883a45ef9493d009741f7c64')->shouldBeCalled()->willReturn($item); + $pool->getItem('d20f64acc6e70b6079845f2fe357732929550ae1')->shouldBeCalled()->willReturn($item); $item->isHit()->willReturn(false); $next = function (RequestInterface $request) use ($response) { @@ -101,7 +101,7 @@ function it_calculate_age_from_response(CacheItemPoolInterface $pool, CacheItemI $response->getHeader('Age')->willReturn(array('15')); $response->getHeader('Expires')->willReturn(array()); - $pool->getItem('e3b717d5883a45ef9493d009741f7c64')->shouldBeCalled()->willReturn($item); + $pool->getItem('d20f64acc6e70b6079845f2fe357732929550ae1')->shouldBeCalled()->willReturn($item); $item->isHit()->willReturn(false); // 40-15 should be 25 From 5094fef214d3610e2bd4c7c9bd72e4dea6d6a3bf Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Tue, 2 Aug 2016 00:08:03 +0100 Subject: [PATCH 4/6] Support setting a hash_algo --- src/CachePlugin.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/CachePlugin.php b/src/CachePlugin.php index c588a82..bd8194e 100644 --- a/src/CachePlugin.php +++ b/src/CachePlugin.php @@ -39,6 +39,7 @@ final class CachePlugin implements Plugin * * @var bool $respect_cache_headers Whether to look at the cache directives or ignore them * @var int $default_ttl If we do not respect cache headers or can't calculate a good ttl, use this value. + * @var string $hash_algo The hashing algorithm to use when generating cache keys. * } */ public function __construct(CacheItemPoolInterface $pool, StreamFactory $streamFactory, array $config = []) @@ -150,7 +151,7 @@ private function getCacheControlDirective(ResponseInterface $response, $name) */ private function createCacheKey(RequestInterface $request) { - return sha1($request->getMethod().' '.$request->getUri()); + return hash($this->config['hash_algo'], $request->getMethod().' '.$request->getUri()); } /** @@ -196,9 +197,11 @@ private function configureOptions(OptionsResolver $resolver) $resolver->setDefaults([ 'default_ttl' => null, 'respect_cache_headers' => true, + 'hash_algo' => 'sha1', ]); $resolver->setAllowedTypes('default_ttl', ['int', 'null']); $resolver->setAllowedTypes('respect_cache_headers', 'bool'); + $resolver->setAllowedValues('hash_algo', hash_algos()); } } From b3aa063778e96f41831b0a147ce7f40d8e0967b4 Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Tue, 2 Aug 2016 00:09:58 +0100 Subject: [PATCH 5/6] CS --- src/CachePlugin.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CachePlugin.php b/src/CachePlugin.php index bd8194e..1dd4cc3 100644 --- a/src/CachePlugin.php +++ b/src/CachePlugin.php @@ -38,7 +38,7 @@ final class CachePlugin implements Plugin * @param array $config { * * @var bool $respect_cache_headers Whether to look at the cache directives or ignore them - * @var int $default_ttl If we do not respect cache headers or can't calculate a good ttl, use this value. + * @var int $default_ttl If we do not respect cache headers or can't calculate a good ttl, use this value * @var string $hash_algo The hashing algorithm to use when generating cache keys. * } */ From 82b565bea14dd5c930be9b691f8978fa8d6bd310 Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Tue, 2 Aug 2016 00:12:43 +0100 Subject: [PATCH 6/6] Reverted accidental change --- src/CachePlugin.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/CachePlugin.php b/src/CachePlugin.php index 1dd4cc3..f0bdbd4 100644 --- a/src/CachePlugin.php +++ b/src/CachePlugin.php @@ -78,7 +78,7 @@ public function handleRequest(RequestInterface $request, callable $next, callabl } return $next($request)->then(function (ResponseInterface $response) use ($cacheItem) { - if ($this->isCacheable($response) && ($maxAge = $this->getMaxAge($response)) > 0) { + if ($this->isCacheable($response)) { $bodyStream = $response->getBody(); $body = $bodyStream->__toString(); if ($bodyStream->isSeekable()) { @@ -88,7 +88,7 @@ public function handleRequest(RequestInterface $request, callable $next, callabl } $cacheItem->set(['response' => $response, 'body' => $body]) - ->expiresAfter($maxAge); + ->expiresAfter($this->getMaxAge($response)); $this->pool->save($cacheItem); }