This commit is contained in:
1 parent
f7c532a648
commit
640bd67556
5801 files changed
+484532
-55857
No files matched your search
+34
-20
@@ -17,6 +17,7 @@
|
||||
|
||||
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;
|
||||
@@ -35,6 +36,8 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
|
||||
private ?ResponseCacheStrategyInterface $surrogateCacheStrategy = null;
|
||||
private array $options = [];
|
||||
private array $traces = [];
|
||||
private ?Request $forwardedRequest = null;
|
||||
private ?Request $backendRequest = null;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
@@ -87,7 +90,6 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
|
||||
private ?SurrogateInterface $surrogate = null,
|
||||
array $options = [],
|
||||
) {
|
||||
|
||||
// needed in case there is a fatal error because the backend is too slow to respond
|
||||
register_shutdown_function($this->store->cleanup(...));
|
||||
|
||||
@@ -149,7 +151,7 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
|
||||
{
|
||||
$log = [];
|
||||
foreach ($this->traces as $request => $traces) {
|
||||
$log[] = sprintf('%s: %s', $request, implode(', ', $traces));
|
||||
$log[] = \sprintf('%s: %s', $request, implode(', ', $traces));
|
||||
}
|
||||
|
||||
return implode('; ', $log);
|
||||
@@ -186,6 +188,8 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
|
||||
// FIXME: catch exceptions and implement a 500 error page here? -> in Varnish, there is a built-in error page mechanism
|
||||
if (HttpKernelInterface::MAIN_REQUEST === $type) {
|
||||
$this->traces = [];
|
||||
$this->forwardedRequest = null;
|
||||
$this->backendRequest = null;
|
||||
// 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
|
||||
// the application flow (we know it always does because we do ourselves by setting REMOTE_ADDR to 127.0.0.1
|
||||
@@ -210,7 +214,19 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
|
||||
$this->record($request, 'reload');
|
||||
$response = $this->fetch($request, $catch);
|
||||
} else {
|
||||
$response = $this->lookup($request, $catch);
|
||||
$response = null;
|
||||
do {
|
||||
try {
|
||||
$response = $this->lookup($request, $catch);
|
||||
} catch (CacheWasLockedException) {
|
||||
}
|
||||
} while (null === $response);
|
||||
}
|
||||
|
||||
if (HttpKernelInterface::MAIN_REQUEST === $type) {
|
||||
// Expose the request actually handled by the backend (a sub-request on a cache miss)
|
||||
// to kernel.terminate listeners, as would happen behind a real reverse proxy.
|
||||
$this->backendRequest = $this->forwardedRequest ?? $request;
|
||||
}
|
||||
|
||||
$this->restoreResponseBody($request, $response);
|
||||
@@ -246,7 +262,7 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
|
||||
}
|
||||
|
||||
if ($this->getKernel() instanceof TerminableInterface) {
|
||||
$this->getKernel()->terminate($request, $response);
|
||||
$this->getKernel()->terminate($this->backendRequest ?? $request, $response);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -458,6 +474,7 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
|
||||
|
||||
// always a "master" request (as the real master request can be in cache)
|
||||
$response = SubRequestHandler::handle($this->kernel, $request, HttpKernelInterface::MAIN_REQUEST, $catch);
|
||||
$this->forwardedRequest = $request;
|
||||
|
||||
/*
|
||||
* Support stale-if-error given on Responses or as a config option.
|
||||
@@ -476,7 +493,7 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
|
||||
* stale-if-error case even if they have a `s-maxage` Cache-Control directive.
|
||||
*/
|
||||
if (null !== $entry
|
||||
&& \in_array($response->getStatusCode(), [500, 502, 503, 504])
|
||||
&& \in_array($response->getStatusCode(), [500, 502, 503, 504], true)
|
||||
&& !$entry->headers->hasCacheControlDirective('no-cache')
|
||||
&& !$entry->mustRevalidate()
|
||||
) {
|
||||
@@ -558,23 +575,16 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
|
||||
return true;
|
||||
}
|
||||
|
||||
$this->record($request, 'waiting');
|
||||
|
||||
// wait for the lock to be released
|
||||
if ($this->waitForLock($request)) {
|
||||
// replace the current entry with the fresh one
|
||||
$new = $this->lookup($request);
|
||||
$entry->headers = $new->headers;
|
||||
$entry->setContent($new->getContent());
|
||||
$entry->setStatusCode($new->getStatusCode());
|
||||
$entry->setProtocolVersion($new->getProtocolVersion());
|
||||
foreach ($new->headers->getCookies() as $cookie) {
|
||||
$entry->headers->setCookie($cookie);
|
||||
}
|
||||
} else {
|
||||
// backend is slow as hell, send a 503 response (to avoid the dog pile effect)
|
||||
$entry->setStatusCode(503);
|
||||
$entry->setContent('503 Service Unavailable');
|
||||
$entry->headers->set('Retry-After', 10);
|
||||
throw new CacheWasLockedException(); // unwind back to handle(), try again
|
||||
}
|
||||
// backend is slow as hell, send a 503 response (to avoid the dog pile effect)
|
||||
$entry->setStatusCode(503);
|
||||
$entry->setContent('503 Service Unavailable');
|
||||
$entry->headers->set('Retry-After', 10);
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -705,7 +715,11 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
|
||||
$path .= '?'.$qs;
|
||||
}
|
||||
|
||||
return $request->getMethod().' '.$path;
|
||||
try {
|
||||
return $request->getMethod().' '.$path;
|
||||
} catch (SuspiciousOperationException) {
|
||||
return '_BAD_METHOD_ '.$path;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user