Permalink
Browse files

Merge branch '3.4'

* 3.4: (83 commits)
  add missing version attribute
  Show exception is checked twice in ExceptionController of twig
  allow SSI fragments configuration in XML files
  Display a better error message when the toolbar cannot be displayed
  [SecurityBundle] Add user impersonation info and exit action to the profiler
  render hidden _method field in form_rest()
  Add Doctrine Cache to dev dependencies to fix failing unit tests.
  return fallback locales whenever possible
  Fix Predis client cluster with pipeline
  [Dotenv] Test load() with multiple paths
  [Console] Fix catching exception type in QuestionHelper
  Improved the exception page when there is no message
  [WebProfilerBundle] Eliminate line wrap on count columnt (routing)
  [Profiler][Validator] Add a validator panel in profiler
  [Validator] replace hardcoded service id
  [Routing] Fix XmlFileLoader exception message
  Remove duplicate changelog entries
  [DI] Dedup tags when using instanceof/autoconfigure
  [Translation] Fix FileLoader::loadResource() php doc
  [Serializer] Fix workaround min php version
  ...
  • Loading branch information...
2 parents 4d123b3 + 02fbb68 commit d33de85a5b55ea96cceb90c620d961e36ca21118 @xabbuh xabbuh committed Jun 24, 2017
@@ -12,6 +12,7 @@
namespace Symfony\Component\HttpKernel\EventListener;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
+use Symfony\Component\HttpKernel\HttpCache\HttpCache;
use Symfony\Component\HttpKernel\HttpCache\SurrogateInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
@@ -42,11 +43,24 @@ public function __construct(SurrogateInterface $surrogate = null)
*/
public function onKernelResponse(FilterResponseEvent $event)
{
- if (!$event->isMasterRequest() || null === $this->surrogate) {
+ if (!$event->isMasterRequest()) {
return;
}
- $this->surrogate->addSurrogateControl($event->getResponse());
+ $kernel = $event->getKernel();
+ $surrogate = $this->surrogate;
+ if ($kernel instanceof HttpCache) {
+ $surrogate = $kernel->getSurrogate();
+ if (null !== $this->surrogate && $this->surrogate->getName() !== $surrogate->getName()) {
+ $surrogate = $this->surrogate;
+ }
+ }
+
+ if (null === $surrogate) {
+ return;
+ }
+
+ $surrogate->addSurrogateControl($event->getResponse());
}
public static function getSubscribedEvents()
@@ -39,7 +39,7 @@ class ResponseCacheStrategy implements ResponseCacheStrategyInterface
*/
public function add(Response $response)
{
- if ($response->isValidateable()) {
+ if (!$response->isFresh() || !$response->isCacheable()) {
$this->cacheable = false;
} else {
$maxAge = $response->getMaxAge();
@@ -70,6 +70,9 @@ public function update(Response $response)
if ($response->isValidateable()) {
$response->setEtag(null);
$response->setLastModified(null);
+ }
+
+ if (!$response->isFresh()) {
$this->cacheable = false;
}
@@ -75,4 +75,148 @@ public function testSharedMaxAgeNotSetIfNotSetInMasterRequest()
$this->assertFalse($response->headers->hasCacheControlDirective('s-maxage'));
}
+
+ public function testMasterResponseNotCacheableWhenEmbeddedResponseRequiresValidation()
+ {
+ $cacheStrategy = new ResponseCacheStrategy();
+
+ $embeddedResponse = new Response();
+ $embeddedResponse->setLastModified(new \DateTime());
+ $cacheStrategy->add($embeddedResponse);
+
+ $masterResponse = new Response();
+ $masterResponse->setSharedMaxAge(3600);
+ $cacheStrategy->update($masterResponse);
+
+ $this->assertTrue($masterResponse->headers->hasCacheControlDirective('no-cache'));
+ $this->assertTrue($masterResponse->headers->hasCacheControlDirective('must-revalidate'));
+ $this->assertFalse($masterResponse->isFresh());
+ }
+
+ public function testValidationOnMasterResponseIsNotPossibleWhenItContainsEmbeddedResponses()
+ {
+ $cacheStrategy = new ResponseCacheStrategy();
+
+ // This master response uses the "validation" model
+ $masterResponse = new Response();
+ $masterResponse->setLastModified(new \DateTime());
+ $masterResponse->setEtag('foo');
+
+ // Embedded response uses "expiry" model
+ $embeddedResponse = new Response();
+ $masterResponse->setSharedMaxAge(3600);
+ $cacheStrategy->add($embeddedResponse);
+
+ $cacheStrategy->update($masterResponse);
+
+ $this->assertFalse($masterResponse->isValidateable());
+ $this->assertFalse($masterResponse->headers->has('Last-Modified'));
+ $this->assertFalse($masterResponse->headers->has('ETag'));
+ $this->assertTrue($masterResponse->headers->hasCacheControlDirective('no-cache'));
+ $this->assertTrue($masterResponse->headers->hasCacheControlDirective('must-revalidate'));
+ }
+
+ public function testMasterResponseWithValidationIsUnchangedWhenThereIsNoEmbeddedResponse()
+ {
+ $cacheStrategy = new ResponseCacheStrategy();
+
+ $masterResponse = new Response();
+ $masterResponse->setLastModified(new \DateTime());
+ $cacheStrategy->update($masterResponse);
+
+ $this->assertTrue($masterResponse->isValidateable());
+ }
+
+ public function testMasterResponseWithExpirationIsUnchangedWhenThereIsNoEmbeddedResponse()
+ {
+ $cacheStrategy = new ResponseCacheStrategy();
+
+ $masterResponse = new Response();
+ $masterResponse->setSharedMaxAge(3600);
+ $cacheStrategy->update($masterResponse);
+
+ $this->assertTrue($masterResponse->isFresh());
+ }
+
+ public function testMasterResponseIsNotCacheableWhenEmbeddedResponseIsNotCacheable()
+ {
+ $cacheStrategy = new ResponseCacheStrategy();
+
+ $masterResponse = new Response();
+ $masterResponse->setSharedMaxAge(3600); // Public, cacheable
+
+ /* This response has no validation or expiration information.
+ That makes it uncacheable, it is always stale.
+ (It does *not* make this private, though.) */
+ $embeddedResponse = new Response();
+ $this->assertFalse($embeddedResponse->isFresh()); // not fresh, as no lifetime is provided
+
+ $cacheStrategy->add($embeddedResponse);
+ $cacheStrategy->update($masterResponse);
+
+ $this->assertTrue($masterResponse->headers->hasCacheControlDirective('no-cache'));
+ $this->assertTrue($masterResponse->headers->hasCacheControlDirective('must-revalidate'));
+ $this->assertFalse($masterResponse->isFresh());
+ }
+
+ public function testEmbeddingPrivateResponseMakesMainResponsePrivate()
+ {
+ $cacheStrategy = new ResponseCacheStrategy();
+
+ $masterResponse = new Response();
+ $masterResponse->setSharedMaxAge(3600); // public, cacheable
+
+ // The embedded response might for example contain per-user data that remains valid for 60 seconds
+ $embeddedResponse = new Response();
+ $embeddedResponse->setPrivate();
+ $embeddedResponse->setMaxAge(60); // this would implicitly set "private" as well, but let's be explicit
+
+ $cacheStrategy->add($embeddedResponse);
+ $cacheStrategy->update($masterResponse);
+
+ $this->assertTrue($masterResponse->headers->hasCacheControlDirective('private'));
+ // Not sure if we should pass "max-age: 60" in this case, as long as the response is private and
+ // that's the more conservative of both the master and embedded response...?
+ }
+
+ public function testResponseIsExiprableWhenEmbeddedResponseCombinesExpiryAndValidation()
+ {
+ /* When "expiration wins over validation" (https://symfony.com/doc/current/http_cache/validation.html)
+ * and both the main and embedded response provide s-maxage, then the more restricting value of both
+ * should be fine, regardless of whether the embedded response can be validated later on or must be
+ * completely regenerated.
+ */
+ $cacheStrategy = new ResponseCacheStrategy();
+
+ $masterResponse = new Response();
+ $masterResponse->setSharedMaxAge(3600);
+
+ $embeddedResponse = new Response();
+ $embeddedResponse->setSharedMaxAge(60);
+ $embeddedResponse->setEtag('foo');
+
+ $cacheStrategy->add($embeddedResponse);
+ $cacheStrategy->update($masterResponse);
+
+ $this->assertSame('60', $masterResponse->headers->getCacheControlDirective('s-maxage'));
+ }
+
+ public function testResponseIsExpirableButNotValidateableWhenMasterResponseCombinesExpirationAndValidation()
+ {
+ $cacheStrategy = new ResponseCacheStrategy();
+
+ $masterResponse = new Response();
+ $masterResponse->setSharedMaxAge(3600);
+ $masterResponse->setEtag('foo');
+ $masterResponse->setLastModified(new \DateTime());
+
+ $embeddedResponse = new Response();
+ $embeddedResponse->setSharedMaxAge(60);
+
+ $cacheStrategy->add($embeddedResponse);
+ $cacheStrategy->update($masterResponse);
+
+ $this->assertSame('60', $masterResponse->headers->getCacheControlDirective('s-maxage'));
+ $this->assertFalse($masterResponse->isValidateable());
+ }
}

0 comments on commit d33de85

Please sign in to comment.