/** * Adds cache headers to the response. * * Called via a signal triggered by the MVC Dispatcher * * @param RequestInterface $request * @param ResponseInterface $response * @param ControllerInterface $controller * @return void */ public function addHeaders(RequestInterface $request, ResponseInterface $response, ControllerInterface $controller) { if (isset($this->settings['cacheHeaders']['disabled']) && $this->settings['cacheHeaders']['disabled'] === TRUE) { $this->logger->log(sprintf('Varnish cache headers disabled (see configuration setting MOC.Varnish.cacheHeaders.disabled)'), LOG_DEBUG); return; } if (!$response instanceof Response || !$controller instanceof NodeController) { return; } $arguments = $controller->getControllerContext()->getArguments(); if (!$arguments->hasArgument('node')) { return; } $node = $arguments->getArgument('node')->getValue(); if (!$node instanceof NodeInterface) { return; } if ($node->getContext()->getWorkspaceName() !== 'live') { return; } if ($node->hasProperty('disableVarnishCache') && $node->getProperty('disableVarnishCache') === TRUE) { $this->logger->log(sprintf('Varnish cache headers skipped due to property "disableVarnishCache" for node "%s" (%s)', $node->getLabel(), $node->getPath()), LOG_DEBUG); return; } if ($this->contentCacheAspect->isEvaluatedUncached()) { $this->logger->log(sprintf('Varnish cache disabled due to uncachable content for node "%s" (%s)', $node->getLabel(), $node->getPath()), LOG_DEBUG); $response->getHeaders()->setCacheControlDirective('no-cache'); } else { list($tags, $cacheLifetime) = $this->getCacheTagsAndLifetime(); if (count($tags) > 0) { $response->setHeader('X-Cache-Tags', implode(',', $tags)); } $response->setHeader('X-Site', $this->tokenStorage->getToken()); $nodeLifetime = $node->getProperty('cacheTimeToLive'); if ($nodeLifetime === '' || $nodeLifetime === NULL) { $defaultLifetime = isset($this->settings['cacheHeaders']['defaultSharedMaximumAge']) ? $this->settings['cacheHeaders']['defaultSharedMaximumAge'] : NULL; $timeToLive = $defaultLifetime; if ($defaultLifetime === NULL) { $timeToLive = $cacheLifetime; } elseif ($cacheLifetime !== NULL) { $timeToLive = min($defaultLifetime, $cacheLifetime); } } else { $timeToLive = $nodeLifetime; } if ($timeToLive !== NULL) { $response->setSharedMaximumAge(intval($timeToLive)); $this->logger->log(sprintf('Varnish cache enabled for node "%s" (%s) with max-age "%u"', $node->getLabel(), $node->getPath(), $timeToLive), LOG_DEBUG); } else { $this->logger->log(sprintf('Varnish cache headers not sent for node "%s" (%s) due to no max-age', $node->getLabel(), $node->getPath()), LOG_DEBUG); } } }
/** * Adds cache headers to the response. * * Called via a signal triggered by the MVC Dispatcher * * @param RequestInterface $request * @param ResponseInterface $response * @param ControllerInterface $controller * @return void */ public function addHeaders(RequestInterface $request, ResponseInterface $response, ControllerInterface $controller) { if (!$response instanceof Response || !$controller instanceof NodeController) { return; } $arguments = $controller->getControllerContext()->getArguments(); if (!$arguments->hasArgument('node')) { return; } $node = $arguments->getArgument('node')->getValue(); if (!$node instanceof NodeInterface) { return; } if ($node->getContext()->getWorkspaceName() !== 'live') { return; } if ($node->hasProperty('disableVarnishCache') && $node->getProperty('disableVarnishCache') === TRUE) { return; } if ($this->contentCacheAspect->isEvaluatedUncached()) { $response->getHeaders()->setCacheControlDirective('no-cache'); } else { list($tags, $cacheLifetime) = $this->getCacheTagsAndLifetime(); if (count($tags) > 0) { $response->setHeader('X-Cache-Tags', implode(',', $tags)); } $response->setHeader('X-Site', $this->tokenStorage->getToken()); $nodeLifetime = $node->getProperty('cacheTimeToLive'); if ($nodeLifetime === '' || $nodeLifetime === NULL) { $defaultLifetime = $this->settings['cacheHeaders']['defaultSharedMaximumAge']; $timeToLive = $defaultLifetime; if ($defaultLifetime === NULL) { $timeToLive = $cacheLifetime; } elseif ($cacheLifetime !== NULL) { $timeToLive = min($defaultLifetime, $cacheLifetime); } } else { $timeToLive = $nodeLifetime; } if ($timeToLive !== NULL) { $response->setSharedMaximumAge(intval($timeToLive)); } } }
/** * @test */ public function dispatchContinuesWithNextRequestFoundInAForwardException() { /** @var ActionRequest|\PHPUnit_Framework_MockObject_MockObject $nextRequest */ $nextRequest = $this->getMockBuilder(\TYPO3\Flow\Mvc\ActionRequest::class)->disableOriginalConstructor()->getMock(); $nextRequest->expects($this->atLeastOnce())->method('isDispatched')->will($this->returnValue(TRUE)); $this->mockParentRequest->expects($this->atLeastOnce())->method('isDispatched')->will($this->returnValue(FALSE)); $this->mockController->expects($this->at(0))->method('processRequest')->with($this->mockActionRequest)->will($this->throwException(new StopActionException())); $forwardException = new ForwardException(); $forwardException->setNextRequest($nextRequest); $this->mockController->expects($this->at(1))->method('processRequest')->with($this->mockParentRequest)->will($this->throwException($forwardException)); $this->dispatcher->dispatch($this->mockActionRequest, $this->mockHttpResponse); }
/** * @test */ public function dispatchContinuesWithNextRequestFoundInAForwardException() { $subRequest = $this->getMockBuilder('TYPO3\\Flow\\Mvc\\ActionRequest')->disableOriginalConstructor()->getMock(); $subRequest->expects($this->atLeastOnce())->method('isMainRequest')->will($this->returnValue(FALSE)); $subRequest->expects($this->atLeastOnce())->method('getParentRequest')->will($this->returnValue($this->mockRequest)); $subRequest->expects($this->atLeastOnce())->method('isDispatched')->will($this->returnValue(FALSE)); $nextRequest = $this->getMockBuilder('TYPO3\\Flow\\Mvc\\ActionRequest')->disableOriginalConstructor()->getMock(); $nextRequest->expects($this->atLeastOnce())->method('isDispatched')->will($this->returnValue(TRUE)); $this->mockRequest->expects($this->atLeastOnce())->method('isDispatched')->will($this->returnValue(FALSE)); $this->mockController->expects($this->at(0))->method('processRequest')->with($subRequest)->will($this->throwException(new \TYPO3\Flow\Mvc\Exception\StopActionException())); $forwardException = new ForwardException(); $forwardException->setNextRequest($nextRequest); $this->mockController->expects($this->at(1))->method('processRequest')->with($this->mockRequest)->will($this->throwException($forwardException)); $this->dispatcher->dispatch($subRequest, $this->mockResponse); }