cleaned up composer
This commit is contained in:
@@ -5,16 +5,19 @@
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* This code is partially based on the Rack-Cache library by Ryan Tomayko,
|
||||
* which is released under the MIT license.
|
||||
* (based on commit 02d2b48d75bcb63cf1c0c7149c077ad256542801)
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
/*
|
||||
* This code is partially based on the Rack-Cache library by Ryan Tomayko,
|
||||
* which is released under the MIT license.
|
||||
* (based on commit 02d2b48d75bcb63cf1c0c7149c077ad256542801)
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\HttpKernel\HttpCache;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Exception\SuspiciousOperationException;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpKernel\HttpKernelInterface;
|
||||
@@ -27,6 +30,8 @@ use Symfony\Component\HttpKernel\TerminableInterface;
|
||||
*/
|
||||
class HttpCache implements HttpKernelInterface, TerminableInterface
|
||||
{
|
||||
public const BODY_EVAL_BOUNDARY_LENGTH = 24;
|
||||
|
||||
private $kernel;
|
||||
private $store;
|
||||
private $request;
|
||||
@@ -44,7 +49,7 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
|
||||
* will try to carry on and deliver a meaningful response.
|
||||
*
|
||||
* * trace_level May be one of 'none', 'short' and 'full'. For 'short', a concise trace of the
|
||||
* master request will be added as an HTTP header. 'full' will add traces for all
|
||||
* main request will be added as an HTTP header. 'full' will add traces for all
|
||||
* requests (including ESI subrequests). (default: 'full' if in debug; 'none' otherwise)
|
||||
*
|
||||
* * trace_header Header name to use for traces. (default: X-Symfony-Cache)
|
||||
@@ -77,7 +82,7 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
|
||||
* This setting is overridden by the stale-if-error HTTP Cache-Control extension
|
||||
* (see RFC 5861).
|
||||
*/
|
||||
public function __construct(HttpKernelInterface $kernel, StoreInterface $store, SurrogateInterface $surrogate = null, array $options = [])
|
||||
public function __construct(HttpKernelInterface $kernel, StoreInterface $store, ?SurrogateInterface $surrogate = null, array $options = [])
|
||||
{
|
||||
$this->store = $store;
|
||||
$this->kernel = $kernel;
|
||||
@@ -106,7 +111,7 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
|
||||
/**
|
||||
* Gets the current store.
|
||||
*
|
||||
* @return StoreInterface A StoreInterface instance
|
||||
* @return StoreInterface
|
||||
*/
|
||||
public function getStore()
|
||||
{
|
||||
@@ -116,7 +121,7 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
|
||||
/**
|
||||
* Returns an array of events that took place during processing of the last request.
|
||||
*
|
||||
* @return array An array of events
|
||||
* @return array
|
||||
*/
|
||||
public function getTraces()
|
||||
{
|
||||
@@ -143,7 +148,7 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
|
||||
/**
|
||||
* Returns a log message for the events of the last request processing.
|
||||
*
|
||||
* @return string A log message
|
||||
* @return string
|
||||
*/
|
||||
public function getLog()
|
||||
{
|
||||
@@ -156,9 +161,9 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the Request instance associated with the master request.
|
||||
* Gets the Request instance associated with the main request.
|
||||
*
|
||||
* @return Request A Request instance
|
||||
* @return Request
|
||||
*/
|
||||
public function getRequest()
|
||||
{
|
||||
@@ -168,7 +173,7 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
|
||||
/**
|
||||
* Gets the Kernel instance.
|
||||
*
|
||||
* @return HttpKernelInterface An HttpKernelInterface instance
|
||||
* @return HttpKernelInterface
|
||||
*/
|
||||
public function getKernel()
|
||||
{
|
||||
@@ -178,7 +183,7 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
|
||||
/**
|
||||
* Gets the Surrogate instance.
|
||||
*
|
||||
* @return SurrogateInterface A Surrogate instance
|
||||
* @return SurrogateInterface
|
||||
*
|
||||
* @throws \LogicException
|
||||
*/
|
||||
@@ -190,10 +195,10 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function handle(Request $request, int $type = HttpKernelInterface::MASTER_REQUEST, bool $catch = true)
|
||||
public function handle(Request $request, int $type = HttpKernelInterface::MAIN_REQUEST, bool $catch = true)
|
||||
{
|
||||
// FIXME: catch exceptions and implement a 500 error page here? -> in Varnish, there is a built-in error page mechanism
|
||||
if (HttpKernelInterface::MASTER_REQUEST === $type) {
|
||||
if (HttpKernelInterface::MAIN_REQUEST === $type) {
|
||||
$this->traces = [];
|
||||
// Keep a clone of the original request for surrogates so they can access it.
|
||||
// We must clone here to get a separate instance because the application will modify the request during
|
||||
@@ -224,12 +229,12 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
|
||||
|
||||
$this->restoreResponseBody($request, $response);
|
||||
|
||||
if (HttpKernelInterface::MASTER_REQUEST === $type) {
|
||||
if (HttpKernelInterface::MAIN_REQUEST === $type) {
|
||||
$this->addTraces($response);
|
||||
}
|
||||
|
||||
if (null !== $this->surrogate) {
|
||||
if (HttpKernelInterface::MASTER_REQUEST === $type) {
|
||||
if (HttpKernelInterface::MAIN_REQUEST === $type) {
|
||||
$this->surrogateCacheStrategy->update($response);
|
||||
} else {
|
||||
$this->surrogateCacheStrategy->add($response);
|
||||
@@ -258,7 +263,7 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
|
||||
*
|
||||
* @param bool $catch Whether to process exceptions
|
||||
*
|
||||
* @return Response A Response instance
|
||||
* @return Response
|
||||
*/
|
||||
protected function pass(Request $request, bool $catch = false)
|
||||
{
|
||||
@@ -272,7 +277,7 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
|
||||
*
|
||||
* @param bool $catch Whether to process exceptions
|
||||
*
|
||||
* @return Response A Response instance
|
||||
* @return Response
|
||||
*
|
||||
* @throws \Exception
|
||||
*
|
||||
@@ -320,7 +325,7 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
|
||||
*
|
||||
* @param bool $catch Whether to process exceptions
|
||||
*
|
||||
* @return Response A Response instance
|
||||
* @return Response
|
||||
*
|
||||
* @throws \Exception
|
||||
*/
|
||||
@@ -369,7 +374,7 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
|
||||
*
|
||||
* @param bool $catch Whether to process exceptions
|
||||
*
|
||||
* @return Response A Response instance
|
||||
* @return Response
|
||||
*/
|
||||
protected function validate(Request $request, Response $entry, bool $catch = false)
|
||||
{
|
||||
@@ -382,7 +387,7 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
|
||||
|
||||
// add our cached last-modified validator
|
||||
if ($entry->headers->has('Last-Modified')) {
|
||||
$subRequest->headers->set('if_modified_since', $entry->headers->get('Last-Modified'));
|
||||
$subRequest->headers->set('If-Modified-Since', $entry->headers->get('Last-Modified'));
|
||||
}
|
||||
|
||||
// Add our cached etag validator to the environment.
|
||||
@@ -391,7 +396,7 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
|
||||
$cachedEtags = $entry->getEtag() ? [$entry->getEtag()] : [];
|
||||
$requestEtags = $request->getETags();
|
||||
if ($etags = array_unique(array_merge($cachedEtags, $requestEtags))) {
|
||||
$subRequest->headers->set('if_none_match', implode(', ', $etags));
|
||||
$subRequest->headers->set('If-None-Match', implode(', ', $etags));
|
||||
}
|
||||
|
||||
$response = $this->forward($subRequest, $catch, $entry);
|
||||
@@ -432,7 +437,7 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
|
||||
*
|
||||
* @param bool $catch Whether to process exceptions
|
||||
*
|
||||
* @return Response A Response instance
|
||||
* @return Response
|
||||
*/
|
||||
protected function fetch(Request $request, bool $catch = false)
|
||||
{
|
||||
@@ -444,8 +449,8 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
|
||||
}
|
||||
|
||||
// avoid that the backend sends no content
|
||||
$subRequest->headers->remove('if_modified_since');
|
||||
$subRequest->headers->remove('if_none_match');
|
||||
$subRequest->headers->remove('If-Modified-Since');
|
||||
$subRequest->headers->remove('If-None-Match');
|
||||
|
||||
$response = $this->forward($subRequest, $catch);
|
||||
|
||||
@@ -465,16 +470,16 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
|
||||
* @param bool $catch Whether to catch exceptions or not
|
||||
* @param Response|null $entry A Response instance (the stale entry if present, null otherwise)
|
||||
*
|
||||
* @return Response A Response instance
|
||||
* @return Response
|
||||
*/
|
||||
protected function forward(Request $request, bool $catch = false, Response $entry = null)
|
||||
protected function forward(Request $request, bool $catch = false, ?Response $entry = null)
|
||||
{
|
||||
if ($this->surrogate) {
|
||||
$this->surrogate->addSurrogateCapability($request);
|
||||
}
|
||||
|
||||
// always a "master" request (as the real master request can be in cache)
|
||||
$response = SubRequestHandler::handle($this->kernel, $request, HttpKernelInterface::MASTER_REQUEST, $catch);
|
||||
$response = SubRequestHandler::handle($this->kernel, $request, HttpKernelInterface::MAIN_REQUEST, $catch);
|
||||
|
||||
/*
|
||||
* Support stale-if-error given on Responses or as a config option.
|
||||
@@ -538,7 +543,7 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
|
||||
/**
|
||||
* Checks whether the cache entry is "fresh enough" to satisfy the Request.
|
||||
*
|
||||
* @return bool true if the cache entry if fresh enough, false otherwise
|
||||
* @return bool
|
||||
*/
|
||||
protected function isFreshEnough(Request $request, Response $entry)
|
||||
{
|
||||
@@ -629,12 +634,22 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
|
||||
private function restoreResponseBody(Request $request, Response $response)
|
||||
{
|
||||
if ($response->headers->has('X-Body-Eval')) {
|
||||
\assert(self::BODY_EVAL_BOUNDARY_LENGTH === 24);
|
||||
|
||||
ob_start();
|
||||
|
||||
if ($response->headers->has('X-Body-File')) {
|
||||
include $response->headers->get('X-Body-File');
|
||||
} else {
|
||||
eval('; ?>'.$response->getContent().'<?php ;');
|
||||
$content = $response->getContent();
|
||||
$boundary = substr($content, 0, 24);
|
||||
$j = strpos($content, $boundary, 24);
|
||||
echo substr($content, 24, $j - 24);
|
||||
$i = $j + 24;
|
||||
|
||||
while (false !== $j = strpos($content, $boundary, $i)) {
|
||||
[$uri, $alt, $ignoreErrors, $part] = explode("\n", substr($content, $i, $j - $i), 4);
|
||||
$i = $j + 24;
|
||||
|
||||
echo $this->surrogate->handle($this, $uri, $alt, $ignoreErrors);
|
||||
echo $part;
|
||||
}
|
||||
|
||||
$response->setContent(ob_get_clean());
|
||||
@@ -701,7 +716,11 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
|
||||
$path .= '?'.$qs;
|
||||
}
|
||||
|
||||
return $request->getMethod().' '.$path;
|
||||
try {
|
||||
return $request->getMethod().' '.$path;
|
||||
} catch (SuspiciousOperationException $e) {
|
||||
return '_BAD_METHOD_ '.$path;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -716,7 +735,11 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
|
||||
$timeout = $this->options['stale_while_revalidate'];
|
||||
}
|
||||
|
||||
return abs($entry->getTtl()) < $timeout;
|
||||
$age = $entry->getAge();
|
||||
$maxAge = $entry->getMaxAge() ?? 0;
|
||||
$ttl = $maxAge - $age;
|
||||
|
||||
return abs($ttl) < $timeout;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user