Laravel version update
Laravel version update
This commit is contained in:
134
vendor/symfony/http-kernel/HttpCache/AbstractSurrogate.php
vendored
Normal file
134
vendor/symfony/http-kernel/HttpCache/AbstractSurrogate.php
vendored
Normal file
@@ -0,0 +1,134 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\HttpKernel\HttpCache;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpKernel\HttpKernelInterface;
|
||||
|
||||
/**
|
||||
* Abstract class implementing Surrogate capabilities to Request and Response instances.
|
||||
*
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
* @author Robin Chalas <robin.chalas@gmail.com>
|
||||
*/
|
||||
abstract class AbstractSurrogate implements SurrogateInterface
|
||||
{
|
||||
protected $contentTypes;
|
||||
protected $phpEscapeMap = array(
|
||||
array('<?', '<%', '<s', '<S'),
|
||||
array('<?php echo "<?"; ?>', '<?php echo "<%"; ?>', '<?php echo "<s"; ?>', '<?php echo "<S"; ?>'),
|
||||
);
|
||||
|
||||
/**
|
||||
* @param array $contentTypes An array of content-type that should be parsed for Surrogate information
|
||||
* (default: text/html, text/xml, application/xhtml+xml, and application/xml)
|
||||
*/
|
||||
public function __construct(array $contentTypes = array('text/html', 'text/xml', 'application/xhtml+xml', 'application/xml'))
|
||||
{
|
||||
$this->contentTypes = $contentTypes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new cache strategy instance.
|
||||
*
|
||||
* @return ResponseCacheStrategyInterface A ResponseCacheStrategyInterface instance
|
||||
*/
|
||||
public function createCacheStrategy()
|
||||
{
|
||||
return new ResponseCacheStrategy();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function hasSurrogateCapability(Request $request)
|
||||
{
|
||||
if (null === $value = $request->headers->get('Surrogate-Capability')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return false !== strpos($value, sprintf('%s/1.0', strtoupper($this->getName())));
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function addSurrogateCapability(Request $request)
|
||||
{
|
||||
$current = $request->headers->get('Surrogate-Capability');
|
||||
$new = sprintf('symfony="%s/1.0"', strtoupper($this->getName()));
|
||||
|
||||
$request->headers->set('Surrogate-Capability', $current ? $current.', '.$new : $new);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function needsParsing(Response $response)
|
||||
{
|
||||
if (!$control = $response->headers->get('Surrogate-Control')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$pattern = sprintf('#content="[^"]*%s/1.0[^"]*"#', strtoupper($this->getName()));
|
||||
|
||||
return (bool) preg_match($pattern, $control);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function handle(HttpCache $cache, $uri, $alt, $ignoreErrors)
|
||||
{
|
||||
$subRequest = Request::create($uri, Request::METHOD_GET, array(), $cache->getRequest()->cookies->all(), array(), $cache->getRequest()->server->all());
|
||||
|
||||
try {
|
||||
$response = $cache->handle($subRequest, HttpKernelInterface::SUB_REQUEST, true);
|
||||
|
||||
if (!$response->isSuccessful()) {
|
||||
throw new \RuntimeException(sprintf('Error when rendering "%s" (Status code is %s).', $subRequest->getUri(), $response->getStatusCode()));
|
||||
}
|
||||
|
||||
return $response->getContent();
|
||||
} catch (\Exception $e) {
|
||||
if ($alt) {
|
||||
return $this->handle($cache, $alt, '', $ignoreErrors);
|
||||
}
|
||||
|
||||
if (!$ignoreErrors) {
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the Surrogate from the Surrogate-Control header.
|
||||
*/
|
||||
protected function removeFromControl(Response $response)
|
||||
{
|
||||
if (!$response->headers->has('Surrogate-Control')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$value = $response->headers->get('Surrogate-Control');
|
||||
$upperName = strtoupper($this->getName());
|
||||
|
||||
if (sprintf('content="%s/1.0"', $upperName) == $value) {
|
||||
$response->headers->remove('Surrogate-Control');
|
||||
} elseif (preg_match(sprintf('#,\s*content="%s/1.0"#', $upperName), $value)) {
|
||||
$response->headers->set('Surrogate-Control', preg_replace(sprintf('#,\s*content="%s/1.0"#', $upperName), '', $value));
|
||||
} elseif (preg_match(sprintf('#content="%s/1.0",\s*#', $upperName), $value)) {
|
||||
$response->headers->set('Surrogate-Control', preg_replace(sprintf('#content="%s/1.0",\s*#', $upperName), '', $value));
|
||||
}
|
||||
}
|
||||
}
|
146
vendor/symfony/http-kernel/HttpCache/Esi.php
vendored
146
vendor/symfony/http-kernel/HttpCache/Esi.php
vendored
@@ -13,7 +13,6 @@ namespace Symfony\Component\HttpKernel\HttpCache;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpKernel\HttpKernelInterface;
|
||||
|
||||
/**
|
||||
* Esi implements the ESI capabilities to Request and Response instances.
|
||||
@@ -26,75 +25,15 @@ use Symfony\Component\HttpKernel\HttpKernelInterface;
|
||||
*
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
*/
|
||||
class Esi implements SurrogateInterface
|
||||
class Esi extends AbstractSurrogate
|
||||
{
|
||||
private $contentTypes;
|
||||
private $phpEscapeMap = array(
|
||||
array('<?', '<%', '<s', '<S'),
|
||||
array('<?php echo "<?"; ?>', '<?php echo "<%"; ?>', '<?php echo "<s"; ?>', '<?php echo "<S"; ?>'),
|
||||
);
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param array $contentTypes An array of content-type that should be parsed for ESI information
|
||||
* (default: text/html, text/xml, application/xhtml+xml, and application/xml)
|
||||
*/
|
||||
public function __construct(array $contentTypes = array('text/html', 'text/xml', 'application/xhtml+xml', 'application/xml'))
|
||||
{
|
||||
$this->contentTypes = $contentTypes;
|
||||
}
|
||||
|
||||
public function getName()
|
||||
{
|
||||
return 'esi';
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new cache strategy instance.
|
||||
*
|
||||
* @return ResponseCacheStrategyInterface A ResponseCacheStrategyInterface instance
|
||||
*/
|
||||
public function createCacheStrategy()
|
||||
{
|
||||
return new ResponseCacheStrategy();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks that at least one surrogate has ESI/1.0 capability.
|
||||
*
|
||||
* @param Request $request A Request instance
|
||||
*
|
||||
* @return bool true if one surrogate has ESI/1.0 capability, false otherwise
|
||||
*/
|
||||
public function hasSurrogateCapability(Request $request)
|
||||
{
|
||||
if (null === $value = $request->headers->get('Surrogate-Capability')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return false !== strpos($value, 'ESI/1.0');
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds ESI/1.0 capability to the given Request.
|
||||
*
|
||||
* @param Request $request A Request instance
|
||||
*/
|
||||
public function addSurrogateCapability(Request $request)
|
||||
{
|
||||
$current = $request->headers->get('Surrogate-Capability');
|
||||
$new = 'symfony2="ESI/1.0"';
|
||||
|
||||
$request->headers->set('Surrogate-Capability', $current ? $current.', '.$new : $new);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds HTTP headers to specify that the Response needs to be parsed for ESI.
|
||||
*
|
||||
* This method only adds an ESI HTTP header if the Response has some ESI tags.
|
||||
*
|
||||
* @param Response $response A Response instance
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function addSurrogateControl(Response $response)
|
||||
{
|
||||
@@ -104,30 +43,7 @@ class Esi implements SurrogateInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks that the Response needs to be parsed for ESI tags.
|
||||
*
|
||||
* @param Response $response A Response instance
|
||||
*
|
||||
* @return bool true if the Response needs to be parsed, false otherwise
|
||||
*/
|
||||
public function needsParsing(Response $response)
|
||||
{
|
||||
if (!$control = $response->headers->get('Surrogate-Control')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return (bool) preg_match('#content="[^"]*ESI/1.0[^"]*"#', $control);
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders an ESI tag.
|
||||
*
|
||||
* @param string $uri A URI
|
||||
* @param string $alt An alternate URI
|
||||
* @param bool $ignoreErrors Whether to ignore errors or not
|
||||
* @param string $comment A comment to add as an esi:include tag
|
||||
*
|
||||
* @return string
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function renderIncludeTag($uri, $alt = null, $ignoreErrors = true, $comment = '')
|
||||
{
|
||||
@@ -145,12 +61,7 @@ class Esi implements SurrogateInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* Replaces a Response ESI tags with the included resource content.
|
||||
*
|
||||
* @param Request $request A Request instance
|
||||
* @param Response $response A Response instance
|
||||
*
|
||||
* @return Response
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function process(Request $request, Response $response)
|
||||
{
|
||||
@@ -160,7 +71,7 @@ class Esi implements SurrogateInterface
|
||||
}
|
||||
|
||||
$parts = explode(';', $type);
|
||||
if (!in_array($parts[0], $this->contentTypes)) {
|
||||
if (!\in_array($parts[0], $this->contentTypes)) {
|
||||
return $response;
|
||||
}
|
||||
|
||||
@@ -199,51 +110,6 @@ class Esi implements SurrogateInterface
|
||||
$response->headers->set('X-Body-Eval', 'ESI');
|
||||
|
||||
// remove ESI/1.0 from the Surrogate-Control header
|
||||
if ($response->headers->has('Surrogate-Control')) {
|
||||
$value = $response->headers->get('Surrogate-Control');
|
||||
if ('content="ESI/1.0"' == $value) {
|
||||
$response->headers->remove('Surrogate-Control');
|
||||
} elseif (preg_match('#,\s*content="ESI/1.0"#', $value)) {
|
||||
$response->headers->set('Surrogate-Control', preg_replace('#,\s*content="ESI/1.0"#', '', $value));
|
||||
} elseif (preg_match('#content="ESI/1.0",\s*#', $value)) {
|
||||
$response->headers->set('Surrogate-Control', preg_replace('#content="ESI/1.0",\s*#', '', $value));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles an ESI from the cache.
|
||||
*
|
||||
* @param HttpCache $cache An HttpCache instance
|
||||
* @param string $uri The main URI
|
||||
* @param string $alt An alternative URI
|
||||
* @param bool $ignoreErrors Whether to ignore errors or not
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @throws \RuntimeException
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function handle(HttpCache $cache, $uri, $alt, $ignoreErrors)
|
||||
{
|
||||
$subRequest = Request::create($uri, 'get', array(), $cache->getRequest()->cookies->all(), array(), $cache->getRequest()->server->all());
|
||||
|
||||
try {
|
||||
$response = $cache->handle($subRequest, HttpKernelInterface::SUB_REQUEST, true);
|
||||
|
||||
if (!$response->isSuccessful()) {
|
||||
throw new \RuntimeException(sprintf('Error when rendering "%s" (Status code is %s).', $subRequest->getUri(), $response->getStatusCode()));
|
||||
}
|
||||
|
||||
return $response->getContent();
|
||||
} catch (\Exception $e) {
|
||||
if ($alt) {
|
||||
return $this->handle($cache, $alt, '', $ignoreErrors);
|
||||
}
|
||||
|
||||
if (!$ignoreErrors) {
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
$this->removeFromControl($response);
|
||||
}
|
||||
}
|
||||
|
248
vendor/symfony/http-kernel/HttpCache/HttpCache.php
vendored
248
vendor/symfony/http-kernel/HttpCache/HttpCache.php
vendored
@@ -15,10 +15,10 @@
|
||||
|
||||
namespace Symfony\Component\HttpKernel\HttpCache;
|
||||
|
||||
use Symfony\Component\HttpKernel\HttpKernelInterface;
|
||||
use Symfony\Component\HttpKernel\TerminableInterface;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpKernel\HttpKernelInterface;
|
||||
use Symfony\Component\HttpKernel\TerminableInterface;
|
||||
|
||||
/**
|
||||
* Cache provides HTTP caching.
|
||||
@@ -69,11 +69,6 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
|
||||
* the cache can serve a stale response when an error is encountered (default: 60).
|
||||
* This setting is overridden by the stale-if-error HTTP Cache-Control extension
|
||||
* (see RFC 5861).
|
||||
*
|
||||
* @param HttpKernelInterface $kernel An HttpKernelInterface instance
|
||||
* @param StoreInterface $store A Store instance
|
||||
* @param SurrogateInterface $surrogate A SurrogateInterface instance
|
||||
* @param array $options An array of options
|
||||
*/
|
||||
public function __construct(HttpKernelInterface $kernel, StoreInterface $store, SurrogateInterface $surrogate = null, array $options = array())
|
||||
{
|
||||
@@ -170,30 +165,35 @@ 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::MASTER_REQUEST === $type) {
|
||||
$this->traces = array();
|
||||
$this->request = $request;
|
||||
// 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
|
||||
// and adding the X-Forwarded-For header, see HttpCache::forward()).
|
||||
$this->request = clone $request;
|
||||
if (null !== $this->surrogate) {
|
||||
$this->surrogateCacheStrategy = $this->surrogate->createCacheStrategy();
|
||||
}
|
||||
}
|
||||
|
||||
$path = $request->getPathInfo();
|
||||
if ($qs = $request->getQueryString()) {
|
||||
$path .= '?'.$qs;
|
||||
}
|
||||
$this->traces[$request->getMethod().' '.$path] = array();
|
||||
$this->traces[$this->getTraceKey($request)] = array();
|
||||
|
||||
if (!$request->isMethodSafe()) {
|
||||
if (!$request->isMethodSafe(false)) {
|
||||
$response = $this->invalidate($request, $catch);
|
||||
} elseif ($request->headers->has('expect')) {
|
||||
} elseif ($request->headers->has('expect') || !$request->isMethodCacheable()) {
|
||||
$response = $this->pass($request, $catch);
|
||||
} elseif ($this->options['allow_reload'] && $request->isNoCache()) {
|
||||
/*
|
||||
If allow_reload is configured and the client requests "Cache-Control: no-cache",
|
||||
reload the cache by fetching a fresh response and caching it (if possible).
|
||||
*/
|
||||
$this->record($request, 'reload');
|
||||
$response = $this->fetch($request, $catch);
|
||||
} else {
|
||||
$response = $this->lookup($request, $catch);
|
||||
}
|
||||
|
||||
$this->restoreResponseBody($request, $response);
|
||||
|
||||
$response->setDate(\DateTime::createFromFormat('U', time(), new \DateTimeZone('UTC')));
|
||||
|
||||
if (HttpKernelInterface::MASTER_REQUEST === $type && $this->options['debug']) {
|
||||
$response->headers->set('X-Symfony-Cache', $this->getLog());
|
||||
}
|
||||
@@ -291,7 +291,7 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
|
||||
* it triggers "miss" processing.
|
||||
*
|
||||
* @param Request $request A Request instance
|
||||
* @param bool $catch whether to process exceptions
|
||||
* @param bool $catch Whether to process exceptions
|
||||
*
|
||||
* @return Response A Response instance
|
||||
*
|
||||
@@ -299,13 +299,6 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
|
||||
*/
|
||||
protected function lookup(Request $request, $catch = false)
|
||||
{
|
||||
// if allow_reload and no-cache Cache-Control, allow a cache reload
|
||||
if ($this->options['allow_reload'] && $request->isNoCache()) {
|
||||
$this->record($request, 'reload');
|
||||
|
||||
return $this->fetch($request, $catch);
|
||||
}
|
||||
|
||||
try {
|
||||
$entry = $this->store->lookup($request);
|
||||
} catch (\Exception $e) {
|
||||
@@ -354,7 +347,9 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
|
||||
$subRequest = clone $request;
|
||||
|
||||
// send no head requests because we want content
|
||||
$subRequest->setMethod('GET');
|
||||
if ('HEAD' === $request->getMethod()) {
|
||||
$subRequest->setMethod('GET');
|
||||
}
|
||||
|
||||
// add our cached last-modified validator
|
||||
$subRequest->headers->set('if_modified_since', $entry->headers->get('Last-Modified'));
|
||||
@@ -375,7 +370,7 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
|
||||
|
||||
// return the response and not the cache entry if the response is valid but not cached
|
||||
$etag = $response->getEtag();
|
||||
if ($etag && in_array($etag, $requestEtags) && !in_array($etag, $cachedEtags)) {
|
||||
if ($etag && \in_array($etag, $requestEtags) && !\in_array($etag, $cachedEtags)) {
|
||||
return $response;
|
||||
}
|
||||
|
||||
@@ -401,12 +396,11 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* Forwards the Request to the backend and determines whether the response should be stored.
|
||||
*
|
||||
* This methods is triggered when the cache missed or a reload is required.
|
||||
* Unconditionally fetches a fresh response from the backend and
|
||||
* stores it in the cache if is cacheable.
|
||||
*
|
||||
* @param Request $request A Request instance
|
||||
* @param bool $catch whether to process exceptions
|
||||
* @param bool $catch Whether to process exceptions
|
||||
*
|
||||
* @return Response A Response instance
|
||||
*/
|
||||
@@ -415,7 +409,9 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
|
||||
$subRequest = clone $request;
|
||||
|
||||
// send no head requests because we want content
|
||||
$subRequest->setMethod('GET');
|
||||
if ('HEAD' === $request->getMethod()) {
|
||||
$subRequest->setMethod('GET');
|
||||
}
|
||||
|
||||
// avoid that the backend sends no content
|
||||
$subRequest->headers->remove('if_modified_since');
|
||||
@@ -433,6 +429,9 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
|
||||
/**
|
||||
* Forwards the Request to the backend and returns the Response.
|
||||
*
|
||||
* All backend requests (cache passes, fetches, cache validations)
|
||||
* run through this method.
|
||||
*
|
||||
* @param Request $request A Request instance
|
||||
* @param bool $catch Whether to catch exceptions or not
|
||||
* @param Response $entry A Response instance (the stale entry if present, null otherwise)
|
||||
@@ -445,30 +444,11 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
|
||||
$this->surrogate->addSurrogateCapability($request);
|
||||
}
|
||||
|
||||
// modify the X-Forwarded-For header if needed
|
||||
$forwardedFor = $request->headers->get('X-Forwarded-For');
|
||||
if ($forwardedFor) {
|
||||
$request->headers->set('X-Forwarded-For', $forwardedFor.', '.$request->server->get('REMOTE_ADDR'));
|
||||
} else {
|
||||
$request->headers->set('X-Forwarded-For', $request->server->get('REMOTE_ADDR'));
|
||||
}
|
||||
|
||||
// fix the client IP address by setting it to 127.0.0.1 as HttpCache
|
||||
// is always called from the same process as the backend.
|
||||
$request->server->set('REMOTE_ADDR', '127.0.0.1');
|
||||
|
||||
// make sure HttpCache is a trusted proxy
|
||||
if (!in_array('127.0.0.1', $trustedProxies = Request::getTrustedProxies())) {
|
||||
$trustedProxies[] = '127.0.0.1';
|
||||
Request::setTrustedProxies($trustedProxies);
|
||||
}
|
||||
|
||||
// always a "master" request (as the real master request can be in cache)
|
||||
$response = $this->kernel->handle($request, HttpKernelInterface::MASTER_REQUEST, $catch);
|
||||
// FIXME: we probably need to also catch exceptions if raw === true
|
||||
$response = SubRequestHandler::handle($this->kernel, $request, HttpKernelInterface::MASTER_REQUEST, $catch);
|
||||
|
||||
// we don't implement the stale-if-error on Requests, which is nonetheless part of the RFC
|
||||
if (null !== $entry && in_array($response->getStatusCode(), array(500, 502, 503, 504))) {
|
||||
if (null !== $entry && \in_array($response->getStatusCode(), array(500, 502, 503, 504))) {
|
||||
if (null === $age = $entry->headers->getCacheControlDirective('stale-if-error')) {
|
||||
$age = $this->options['stale_if_error'];
|
||||
}
|
||||
@@ -480,6 +460,17 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
RFC 7231 Sect. 7.1.1.2 says that a server that does not have a reasonably accurate
|
||||
clock MUST NOT send a "Date" header, although it MUST send one in most other cases
|
||||
except for 1xx or 5xx responses where it MAY do so.
|
||||
|
||||
Anyway, a client that received a message without a "Date" header MUST add it.
|
||||
*/
|
||||
if (!$response->headers->has('Date')) {
|
||||
$response->setDate(\DateTime::createFromFormat('U', time()));
|
||||
}
|
||||
|
||||
$this->processResponseBody($request, $response);
|
||||
|
||||
if ($this->isPrivateRequest($request) && !$response->headers->hasCacheControlDirective('public')) {
|
||||
@@ -494,9 +485,6 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
|
||||
/**
|
||||
* Checks whether the cache entry is "fresh enough" to satisfy the Request.
|
||||
*
|
||||
* @param Request $request A Request instance
|
||||
* @param Response $entry A Response instance
|
||||
*
|
||||
* @return bool true if the cache entry if fresh enough, false otherwise
|
||||
*/
|
||||
protected function isFreshEnough(Request $request, Response $entry)
|
||||
@@ -515,9 +503,6 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
|
||||
/**
|
||||
* Locks a Request during the call to the backend.
|
||||
*
|
||||
* @param Request $request A Request instance
|
||||
* @param Response $entry A Response instance
|
||||
*
|
||||
* @return bool true if the cache entry can be returned even if it is staled, false otherwise
|
||||
*/
|
||||
protected function lock(Request $request, Response $entry)
|
||||
@@ -525,64 +510,48 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
|
||||
// try to acquire a lock to call the backend
|
||||
$lock = $this->store->lock($request);
|
||||
|
||||
if (true === $lock) {
|
||||
// we have the lock, call the backend
|
||||
return false;
|
||||
}
|
||||
|
||||
// there is already another process calling the backend
|
||||
if (true !== $lock) {
|
||||
// check if we can serve the stale entry
|
||||
if (null === $age = $entry->headers->getCacheControlDirective('stale-while-revalidate')) {
|
||||
$age = $this->options['stale_while_revalidate'];
|
||||
}
|
||||
|
||||
if (abs($entry->getTtl()) < $age) {
|
||||
$this->record($request, 'stale-while-revalidate');
|
||||
|
||||
// server the stale response while there is a revalidation
|
||||
return true;
|
||||
}
|
||||
|
||||
// wait for the lock to be released
|
||||
$wait = 0;
|
||||
while ($this->store->isLocked($request) && $wait < 5000000) {
|
||||
usleep(50000);
|
||||
$wait += 50000;
|
||||
}
|
||||
|
||||
if ($wait < 5000000) {
|
||||
// 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);
|
||||
}
|
||||
// May we serve a stale response?
|
||||
if ($this->mayServeStaleWhileRevalidate($entry)) {
|
||||
$this->record($request, 'stale-while-revalidate');
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// we have the lock, call the backend
|
||||
return false;
|
||||
// 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);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes the Response to the cache.
|
||||
*
|
||||
* @param Request $request A Request instance
|
||||
* @param Response $response A Response instance
|
||||
*
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function store(Request $request, Response $response)
|
||||
{
|
||||
if (!$response->headers->has('Date')) {
|
||||
$response->setDate(\DateTime::createFromFormat('U', time()));
|
||||
}
|
||||
try {
|
||||
$this->store->write($request, $response);
|
||||
|
||||
@@ -603,20 +572,9 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
|
||||
|
||||
/**
|
||||
* Restores the Response body.
|
||||
*
|
||||
* @param Request $request A Request instance
|
||||
* @param Response $response A Response instance
|
||||
*/
|
||||
private function restoreResponseBody(Request $request, Response $response)
|
||||
{
|
||||
if ($request->isMethod('HEAD') || 304 === $response->getStatusCode()) {
|
||||
$response->setContent(null);
|
||||
$response->headers->remove('X-Body-Eval');
|
||||
$response->headers->remove('X-Body-File');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if ($response->headers->has('X-Body-Eval')) {
|
||||
ob_start();
|
||||
|
||||
@@ -629,10 +587,14 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
|
||||
$response->setContent(ob_get_clean());
|
||||
$response->headers->remove('X-Body-Eval');
|
||||
if (!$response->headers->has('Transfer-Encoding')) {
|
||||
$response->headers->set('Content-Length', strlen($response->getContent()));
|
||||
$response->headers->set('Content-Length', \strlen($response->getContent()));
|
||||
}
|
||||
} elseif ($response->headers->has('X-Body-File')) {
|
||||
$response->setContent(file_get_contents($response->headers->get('X-Body-File')));
|
||||
// Response does not include possibly dynamic content (ESI, SSI), so we need
|
||||
// not handle the content for HEAD requests
|
||||
if (!$request->isMethod('HEAD')) {
|
||||
$response->setContent(file_get_contents($response->headers->get('X-Body-File')));
|
||||
}
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
@@ -651,8 +613,6 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
|
||||
* Checks if the Request includes authorization or other sensitive information
|
||||
* that should cause the Response to be considered private by default.
|
||||
*
|
||||
* @param Request $request A Request instance
|
||||
*
|
||||
* @return bool true if the Request is private, false otherwise
|
||||
*/
|
||||
private function isPrivateRequest(Request $request)
|
||||
@@ -661,7 +621,7 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
|
||||
$key = strtolower(str_replace('HTTP_', '', $key));
|
||||
|
||||
if ('cookie' === $key) {
|
||||
if (count($request->cookies->all())) {
|
||||
if (\count($request->cookies->all())) {
|
||||
return true;
|
||||
}
|
||||
} elseif ($request->headers->has($key)) {
|
||||
@@ -679,11 +639,61 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
|
||||
* @param string $event The event name
|
||||
*/
|
||||
private function record(Request $request, $event)
|
||||
{
|
||||
$this->traces[$this->getTraceKey($request)][] = $event;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the key we use in the "trace" array for a given request.
|
||||
*
|
||||
* @param Request $request
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function getTraceKey(Request $request)
|
||||
{
|
||||
$path = $request->getPathInfo();
|
||||
if ($qs = $request->getQueryString()) {
|
||||
$path .= '?'.$qs;
|
||||
}
|
||||
$this->traces[$request->getMethod().' '.$path][] = $event;
|
||||
|
||||
return $request->getMethod().' '.$path;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether the given (cached) response may be served as "stale" when a revalidation
|
||||
* is currently in progress.
|
||||
*
|
||||
* @param Response $entry
|
||||
*
|
||||
* @return bool true when the stale response may be served, false otherwise
|
||||
*/
|
||||
private function mayServeStaleWhileRevalidate(Response $entry)
|
||||
{
|
||||
$timeout = $entry->headers->getCacheControlDirective('stale-while-revalidate');
|
||||
|
||||
if (null === $timeout) {
|
||||
$timeout = $this->options['stale_while_revalidate'];
|
||||
}
|
||||
|
||||
return abs($entry->getTtl()) < $timeout;
|
||||
}
|
||||
|
||||
/**
|
||||
* Waits for the store to release a locked entry.
|
||||
*
|
||||
* @param Request $request The request to wait for
|
||||
*
|
||||
* @return bool true if the lock was released before the internal timeout was hit; false if the wait timeout was exceeded
|
||||
*/
|
||||
private function waitForLock(Request $request)
|
||||
{
|
||||
$wait = 0;
|
||||
while ($this->store->isLocked($request) && $wait < 100) {
|
||||
usleep(50000);
|
||||
++$wait;
|
||||
}
|
||||
|
||||
return $wait < 100;
|
||||
}
|
||||
}
|
||||
|
@@ -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 @@ class ResponseCacheStrategy implements ResponseCacheStrategyInterface
|
||||
if ($response->isValidateable()) {
|
||||
$response->setEtag(null);
|
||||
$response->setLastModified(null);
|
||||
}
|
||||
|
||||
if (!$response->isFresh() || !$response->isCacheable()) {
|
||||
$this->cacheable = false;
|
||||
}
|
||||
|
||||
|
@@ -27,15 +27,11 @@ interface ResponseCacheStrategyInterface
|
||||
{
|
||||
/**
|
||||
* Adds a Response.
|
||||
*
|
||||
* @param Response $response
|
||||
*/
|
||||
public function add(Response $response);
|
||||
|
||||
/**
|
||||
* Updates the Response HTTP headers based on the embedded Responses.
|
||||
*
|
||||
* @param Response $response
|
||||
*/
|
||||
public function update(Response $response);
|
||||
}
|
||||
|
102
vendor/symfony/http-kernel/HttpCache/Ssi.php
vendored
102
vendor/symfony/http-kernel/HttpCache/Ssi.php
vendored
@@ -13,32 +13,14 @@ namespace Symfony\Component\HttpKernel\HttpCache;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpKernel\HttpKernelInterface;
|
||||
|
||||
/**
|
||||
* Ssi implements the SSI capabilities to Request and Response instances.
|
||||
*
|
||||
* @author Sebastian Krebs <krebs.seb@gmail.com>
|
||||
*/
|
||||
class Ssi implements SurrogateInterface
|
||||
class Ssi extends AbstractSurrogate
|
||||
{
|
||||
private $contentTypes;
|
||||
private $phpEscapeMap = array(
|
||||
array('<?', '<%', '<s', '<S'),
|
||||
array('<?php echo "<?"; ?>', '<?php echo "<%"; ?>', '<?php echo "<s"; ?>', '<?php echo "<S"; ?>'),
|
||||
);
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param array $contentTypes An array of content-type that should be parsed for SSI information
|
||||
* (default: text/html, text/xml, application/xhtml+xml, and application/xml)
|
||||
*/
|
||||
public function __construct(array $contentTypes = array('text/html', 'text/xml', 'application/xhtml+xml', 'application/xml'))
|
||||
{
|
||||
$this->contentTypes = $contentTypes;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
@@ -47,37 +29,6 @@ class Ssi implements SurrogateInterface
|
||||
return 'ssi';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function createCacheStrategy()
|
||||
{
|
||||
return new ResponseCacheStrategy();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function hasSurrogateCapability(Request $request)
|
||||
{
|
||||
if (null === $value = $request->headers->get('Surrogate-Capability')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return false !== strpos($value, 'SSI/1.0');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function addSurrogateCapability(Request $request)
|
||||
{
|
||||
$current = $request->headers->get('Surrogate-Capability');
|
||||
$new = 'symfony2="SSI/1.0"';
|
||||
|
||||
$request->headers->set('Surrogate-Capability', $current ? $current.', '.$new : $new);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
@@ -88,18 +39,6 @@ class Ssi implements SurrogateInterface
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function needsParsing(Response $response)
|
||||
{
|
||||
if (!$control = $response->headers->get('Surrogate-Control')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return (bool) preg_match('#content="[^"]*SSI/1.0[^"]*"#', $control);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
@@ -119,7 +58,7 @@ class Ssi implements SurrogateInterface
|
||||
}
|
||||
|
||||
$parts = explode(';', $type);
|
||||
if (!in_array($parts[0], $this->contentTypes)) {
|
||||
if (!\in_array($parts[0], $this->contentTypes)) {
|
||||
return $response;
|
||||
}
|
||||
|
||||
@@ -154,41 +93,6 @@ class Ssi implements SurrogateInterface
|
||||
$response->headers->set('X-Body-Eval', 'SSI');
|
||||
|
||||
// remove SSI/1.0 from the Surrogate-Control header
|
||||
if ($response->headers->has('Surrogate-Control')) {
|
||||
$value = $response->headers->get('Surrogate-Control');
|
||||
if ('content="SSI/1.0"' == $value) {
|
||||
$response->headers->remove('Surrogate-Control');
|
||||
} elseif (preg_match('#,\s*content="SSI/1.0"#', $value)) {
|
||||
$response->headers->set('Surrogate-Control', preg_replace('#,\s*content="SSI/1.0"#', '', $value));
|
||||
} elseif (preg_match('#content="SSI/1.0",\s*#', $value)) {
|
||||
$response->headers->set('Surrogate-Control', preg_replace('#content="SSI/1.0",\s*#', '', $value));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function handle(HttpCache $cache, $uri, $alt, $ignoreErrors)
|
||||
{
|
||||
$subRequest = Request::create($uri, 'get', array(), $cache->getRequest()->cookies->all(), array(), $cache->getRequest()->server->all());
|
||||
|
||||
try {
|
||||
$response = $cache->handle($subRequest, HttpKernelInterface::SUB_REQUEST, true);
|
||||
|
||||
if (!$response->isSuccessful()) {
|
||||
throw new \RuntimeException(sprintf('Error when rendering "%s" (Status code is %s).', $subRequest->getUri(), $response->getStatusCode()));
|
||||
}
|
||||
|
||||
return $response->getContent();
|
||||
} catch (\Exception $e) {
|
||||
if ($alt) {
|
||||
return $this->handle($cache, $alt, '', $ignoreErrors);
|
||||
}
|
||||
|
||||
if (!$ignoreErrors) {
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
$this->removeFromControl($response);
|
||||
}
|
||||
}
|
||||
|
68
vendor/symfony/http-kernel/HttpCache/Store.php
vendored
68
vendor/symfony/http-kernel/HttpCache/Store.php
vendored
@@ -29,8 +29,6 @@ class Store implements StoreInterface
|
||||
private $locks;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param string $root The path to the cache directory
|
||||
*
|
||||
* @throws \RuntimeException
|
||||
@@ -62,8 +60,6 @@ class Store implements StoreInterface
|
||||
/**
|
||||
* Tries to lock the cache for a given Request, without blocking.
|
||||
*
|
||||
* @param Request $request A Request instance
|
||||
*
|
||||
* @return bool|string true if the lock is acquired, the path to the current lock otherwise
|
||||
*/
|
||||
public function lock(Request $request)
|
||||
@@ -72,7 +68,7 @@ class Store implements StoreInterface
|
||||
|
||||
if (!isset($this->locks[$key])) {
|
||||
$path = $this->getPath($key);
|
||||
if (!file_exists(dirname($path)) && false === @mkdir(dirname($path), 0777, true) && !is_dir(dirname($path))) {
|
||||
if (!file_exists(\dirname($path)) && false === @mkdir(\dirname($path), 0777, true) && !is_dir(\dirname($path))) {
|
||||
return $path;
|
||||
}
|
||||
$h = fopen($path, 'cb');
|
||||
@@ -91,8 +87,6 @@ class Store implements StoreInterface
|
||||
/**
|
||||
* Releases the lock for the given Request.
|
||||
*
|
||||
* @param Request $request A Request instance
|
||||
*
|
||||
* @return bool False if the lock file does not exist or cannot be unlocked, true otherwise
|
||||
*/
|
||||
public function unlock(Request $request)
|
||||
@@ -133,8 +127,6 @@ class Store implements StoreInterface
|
||||
/**
|
||||
* Locates a cached Response for the Request provided.
|
||||
*
|
||||
* @param Request $request A Request instance
|
||||
*
|
||||
* @return Response|null A Response instance, or null if no cache entry was found
|
||||
*/
|
||||
public function lookup(Request $request)
|
||||
@@ -159,7 +151,7 @@ class Store implements StoreInterface
|
||||
return;
|
||||
}
|
||||
|
||||
list($req, $headers) = $match;
|
||||
$headers = $match[1];
|
||||
if (file_exists($body = $this->getPath($headers['x-content-digest'][0]))) {
|
||||
return $this->restoreResponse($headers, $body);
|
||||
}
|
||||
@@ -175,9 +167,6 @@ class Store implements StoreInterface
|
||||
* Existing entries are read and any that match the response are removed. This
|
||||
* method calls write with the new list of cache entries.
|
||||
*
|
||||
* @param Request $request A Request instance
|
||||
* @param Response $response A Response instance
|
||||
*
|
||||
* @return string The key under which the response is stored
|
||||
*
|
||||
* @throws \RuntimeException
|
||||
@@ -198,7 +187,7 @@ class Store implements StoreInterface
|
||||
$response->headers->set('X-Content-Digest', $digest);
|
||||
|
||||
if (!$response->headers->has('Transfer-Encoding')) {
|
||||
$response->headers->set('Content-Length', strlen($response->getContent()));
|
||||
$response->headers->set('Content-Length', \strlen($response->getContent()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -210,7 +199,7 @@ class Store implements StoreInterface
|
||||
$entry[1]['vary'] = array('');
|
||||
}
|
||||
|
||||
if ($vary != $entry[1]['vary'][0] || !$this->requestsMatch($vary, $entry[0], $storedEnv)) {
|
||||
if ($entry[1]['vary'][0] != $vary || !$this->requestsMatch($vary, $entry[0], $storedEnv)) {
|
||||
$entries[] = $entry;
|
||||
}
|
||||
}
|
||||
@@ -230,8 +219,6 @@ class Store implements StoreInterface
|
||||
/**
|
||||
* Returns content digest for $response.
|
||||
*
|
||||
* @param Response $response
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function generateContentDigest(Response $response)
|
||||
@@ -242,8 +229,6 @@ class Store implements StoreInterface
|
||||
/**
|
||||
* Invalidates all cache entries that match the request.
|
||||
*
|
||||
* @param Request $request A Request instance
|
||||
*
|
||||
* @throws \RuntimeException
|
||||
*/
|
||||
public function invalidate(Request $request)
|
||||
@@ -314,6 +299,26 @@ class Store implements StoreInterface
|
||||
return unserialize($entries);
|
||||
}
|
||||
|
||||
/**
|
||||
* Purges data for the given URL.
|
||||
*
|
||||
* This method purges both the HTTP and the HTTPS version of the cache entry.
|
||||
*
|
||||
* @param string $url A URL
|
||||
*
|
||||
* @return bool true if the URL exists with either HTTP or HTTPS scheme and has been purged, false otherwise
|
||||
*/
|
||||
public function purge($url)
|
||||
{
|
||||
$http = preg_replace('#^https:#', 'http:', $url);
|
||||
$https = preg_replace('#^http:#', 'https:', $url);
|
||||
|
||||
$purgedHttp = $this->doPurge($http);
|
||||
$purgedHttps = $this->doPurge($https);
|
||||
|
||||
return $purgedHttp || $purgedHttps;
|
||||
}
|
||||
|
||||
/**
|
||||
* Purges data for the given URL.
|
||||
*
|
||||
@@ -321,10 +326,9 @@ class Store implements StoreInterface
|
||||
*
|
||||
* @return bool true if the URL exists and has been purged, false otherwise
|
||||
*/
|
||||
public function purge($url)
|
||||
private function doPurge($url)
|
||||
{
|
||||
$key = $this->getCacheKey(Request::create($url));
|
||||
|
||||
if (isset($this->locks[$key])) {
|
||||
flock($this->locks[$key], LOCK_UN);
|
||||
fclose($this->locks[$key]);
|
||||
@@ -371,28 +375,34 @@ class Store implements StoreInterface
|
||||
@ftruncate($fp, 0);
|
||||
@fseek($fp, 0);
|
||||
$len = @fwrite($fp, $data);
|
||||
if (strlen($data) !== $len) {
|
||||
if (\strlen($data) !== $len) {
|
||||
@ftruncate($fp, 0);
|
||||
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
if (!file_exists(dirname($path)) && false === @mkdir(dirname($path), 0777, true) && !is_dir(dirname($path))) {
|
||||
if (!file_exists(\dirname($path)) && false === @mkdir(\dirname($path), 0777, true) && !is_dir(\dirname($path))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$tmpFile = tempnam(dirname($path), basename($path));
|
||||
$tmpFile = tempnam(\dirname($path), basename($path));
|
||||
if (false === $fp = @fopen($tmpFile, 'wb')) {
|
||||
@unlink($tmpFile);
|
||||
|
||||
return false;
|
||||
}
|
||||
@fwrite($fp, $data);
|
||||
@fclose($fp);
|
||||
|
||||
if ($data != file_get_contents($tmpFile)) {
|
||||
@unlink($tmpFile);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (false === @rename($tmpFile, $path)) {
|
||||
@unlink($tmpFile);
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -402,7 +412,7 @@ class Store implements StoreInterface
|
||||
|
||||
public function getPath($key)
|
||||
{
|
||||
return $this->root.DIRECTORY_SEPARATOR.substr($key, 0, 2).DIRECTORY_SEPARATOR.substr($key, 2, 2).DIRECTORY_SEPARATOR.substr($key, 4, 2).DIRECTORY_SEPARATOR.substr($key, 6);
|
||||
return $this->root.\DIRECTORY_SEPARATOR.substr($key, 0, 2).\DIRECTORY_SEPARATOR.substr($key, 2, 2).\DIRECTORY_SEPARATOR.substr($key, 4, 2).\DIRECTORY_SEPARATOR.substr($key, 6);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -415,8 +425,6 @@ class Store implements StoreInterface
|
||||
* headers, use a Vary header to indicate them, and each representation will
|
||||
* be stored independently under the same cache key.
|
||||
*
|
||||
* @param Request $request A Request instance
|
||||
*
|
||||
* @return string A key for the given Request
|
||||
*/
|
||||
protected function generateCacheKey(Request $request)
|
||||
@@ -427,8 +435,6 @@ class Store implements StoreInterface
|
||||
/**
|
||||
* Returns a cache key for the given Request.
|
||||
*
|
||||
* @param Request $request A Request instance
|
||||
*
|
||||
* @return string A key for the given Request
|
||||
*/
|
||||
private function getCacheKey(Request $request)
|
||||
@@ -443,8 +449,6 @@ class Store implements StoreInterface
|
||||
/**
|
||||
* Persists the Request HTTP headers.
|
||||
*
|
||||
* @param Request $request A Request instance
|
||||
*
|
||||
* @return array An array of HTTP headers
|
||||
*/
|
||||
private function persistRequest(Request $request)
|
||||
@@ -455,8 +459,6 @@ class Store implements StoreInterface
|
||||
/**
|
||||
* Persists the Response HTTP headers.
|
||||
*
|
||||
* @param Response $response A Response instance
|
||||
*
|
||||
* @return array An array of HTTP headers
|
||||
*/
|
||||
private function persistResponse(Response $response)
|
||||
|
@@ -27,8 +27,6 @@ interface StoreInterface
|
||||
/**
|
||||
* Locates a cached Response for the Request provided.
|
||||
*
|
||||
* @param Request $request A Request instance
|
||||
*
|
||||
* @return Response|null A Response instance, or null if no cache entry was found
|
||||
*/
|
||||
public function lookup(Request $request);
|
||||
@@ -39,25 +37,18 @@ interface StoreInterface
|
||||
* Existing entries are read and any that match the response are removed. This
|
||||
* method calls write with the new list of cache entries.
|
||||
*
|
||||
* @param Request $request A Request instance
|
||||
* @param Response $response A Response instance
|
||||
*
|
||||
* @return string The key under which the response is stored
|
||||
*/
|
||||
public function write(Request $request, Response $response);
|
||||
|
||||
/**
|
||||
* Invalidates all cache entries that match the request.
|
||||
*
|
||||
* @param Request $request A Request instance
|
||||
*/
|
||||
public function invalidate(Request $request);
|
||||
|
||||
/**
|
||||
* Locks the cache for a given Request.
|
||||
*
|
||||
* @param Request $request A Request instance
|
||||
*
|
||||
* @return bool|string true if the lock is acquired, the path to the current lock otherwise
|
||||
*/
|
||||
public function lock(Request $request);
|
||||
@@ -65,8 +56,6 @@ interface StoreInterface
|
||||
/**
|
||||
* Releases the lock for the given Request.
|
||||
*
|
||||
* @param Request $request A Request instance
|
||||
*
|
||||
* @return bool False if the lock file does not exist or cannot be unlocked, true otherwise
|
||||
*/
|
||||
public function unlock(Request $request);
|
||||
@@ -74,8 +63,6 @@ interface StoreInterface
|
||||
/**
|
||||
* Returns whether or not a lock exists.
|
||||
*
|
||||
* @param Request $request A Request instance
|
||||
*
|
||||
* @return bool true if lock exists, false otherwise
|
||||
*/
|
||||
public function isLocked(Request $request);
|
||||
|
104
vendor/symfony/http-kernel/HttpCache/SubRequestHandler.php
vendored
Normal file
104
vendor/symfony/http-kernel/HttpCache/SubRequestHandler.php
vendored
Normal file
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\HttpKernel\HttpCache;
|
||||
|
||||
use Symfony\Component\HttpFoundation\IpUtils;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpKernel\HttpKernelInterface;
|
||||
|
||||
/**
|
||||
* @author Nicolas Grekas <p@tchwork.com>
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
class SubRequestHandler
|
||||
{
|
||||
/**
|
||||
* @return Response
|
||||
*/
|
||||
public static function handle(HttpKernelInterface $kernel, Request $request, $type, $catch)
|
||||
{
|
||||
// save global state related to trusted headers and proxies
|
||||
$trustedProxies = Request::getTrustedProxies();
|
||||
$trustedHeaderSet = Request::getTrustedHeaderSet();
|
||||
if (\method_exists(Request::class, 'getTrustedHeaderName')) {
|
||||
Request::setTrustedProxies($trustedProxies, -1);
|
||||
$trustedHeaders = array(
|
||||
Request::HEADER_FORWARDED => Request::getTrustedHeaderName(Request::HEADER_FORWARDED, false),
|
||||
Request::HEADER_X_FORWARDED_FOR => Request::getTrustedHeaderName(Request::HEADER_X_FORWARDED_FOR, false),
|
||||
Request::HEADER_X_FORWARDED_HOST => Request::getTrustedHeaderName(Request::HEADER_X_FORWARDED_HOST, false),
|
||||
Request::HEADER_X_FORWARDED_PROTO => Request::getTrustedHeaderName(Request::HEADER_X_FORWARDED_PROTO, false),
|
||||
Request::HEADER_X_FORWARDED_PORT => Request::getTrustedHeaderName(Request::HEADER_X_FORWARDED_PORT, false),
|
||||
);
|
||||
Request::setTrustedProxies($trustedProxies, $trustedHeaderSet);
|
||||
} else {
|
||||
$trustedHeaders = array(
|
||||
Request::HEADER_FORWARDED => 'FORWARDED',
|
||||
Request::HEADER_X_FORWARDED_FOR => 'X_FORWARDED_FOR',
|
||||
Request::HEADER_X_FORWARDED_HOST => 'X_FORWARDED_HOST',
|
||||
Request::HEADER_X_FORWARDED_PROTO => 'X_FORWARDED_PROTO',
|
||||
Request::HEADER_X_FORWARDED_PORT => 'X_FORWARDED_PORT',
|
||||
);
|
||||
}
|
||||
|
||||
// remove untrusted values
|
||||
$remoteAddr = $request->server->get('REMOTE_ADDR');
|
||||
if (!IpUtils::checkIp($remoteAddr, $trustedProxies)) {
|
||||
foreach ($trustedHeaders as $key => $name) {
|
||||
if ($trustedHeaderSet & $key) {
|
||||
$request->headers->remove($name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// compute trusted values, taking any trusted proxies into account
|
||||
$trustedIps = array();
|
||||
$trustedValues = array();
|
||||
foreach (array_reverse($request->getClientIps()) as $ip) {
|
||||
$trustedIps[] = $ip;
|
||||
$trustedValues[] = sprintf('for="%s"', $ip);
|
||||
}
|
||||
if ($ip !== $remoteAddr) {
|
||||
$trustedIps[] = $remoteAddr;
|
||||
$trustedValues[] = sprintf('for="%s"', $remoteAddr);
|
||||
}
|
||||
|
||||
// set trusted values, reusing as much as possible the global trusted settings
|
||||
if (Request::HEADER_FORWARDED & $trustedHeaderSet) {
|
||||
$trustedValues[0] .= sprintf(';host="%s";proto=%s', $request->getHttpHost(), $request->getScheme());
|
||||
$request->headers->set($trustedHeaders[Request::HEADER_FORWARDED], implode(', ', $trustedValues));
|
||||
}
|
||||
if (Request::HEADER_X_FORWARDED_FOR & $trustedHeaderSet) {
|
||||
$request->headers->set($trustedHeaders[Request::HEADER_X_FORWARDED_FOR], implode(', ', $trustedIps));
|
||||
} elseif (!(Request::HEADER_FORWARDED & $trustedHeaderSet)) {
|
||||
Request::setTrustedProxies($trustedProxies, $trustedHeaderSet | Request::HEADER_X_FORWARDED_FOR);
|
||||
$request->headers->set($trustedHeaders[Request::HEADER_X_FORWARDED_FOR], implode(', ', $trustedIps));
|
||||
}
|
||||
|
||||
// fix the client IP address by setting it to 127.0.0.1,
|
||||
// which is the core responsibility of this method
|
||||
$request->server->set('REMOTE_ADDR', '127.0.0.1');
|
||||
|
||||
// ensure 127.0.0.1 is set as trusted proxy
|
||||
if (!IpUtils::checkIp('127.0.0.1', $trustedProxies)) {
|
||||
Request::setTrustedProxies(array_merge($trustedProxies, array('127.0.0.1')), Request::getTrustedHeaderSet());
|
||||
}
|
||||
|
||||
try {
|
||||
return $kernel->handle($request, $type, $catch);
|
||||
} finally {
|
||||
// restore global state
|
||||
Request::setTrustedProxies($trustedProxies, $trustedHeaderSet);
|
||||
}
|
||||
}
|
||||
}
|
@@ -33,16 +33,12 @@ interface SurrogateInterface
|
||||
/**
|
||||
* Checks that at least one surrogate has Surrogate capability.
|
||||
*
|
||||
* @param Request $request A Request instance
|
||||
*
|
||||
* @return bool true if one surrogate has Surrogate capability, false otherwise
|
||||
*/
|
||||
public function hasSurrogateCapability(Request $request);
|
||||
|
||||
/**
|
||||
* Adds Surrogate-capability to the given Request.
|
||||
*
|
||||
* @param Request $request A Request instance
|
||||
*/
|
||||
public function addSurrogateCapability(Request $request);
|
||||
|
||||
@@ -50,16 +46,12 @@ interface SurrogateInterface
|
||||
* Adds HTTP headers to specify that the Response needs to be parsed for Surrogate.
|
||||
*
|
||||
* This method only adds an Surrogate HTTP header if the Response has some Surrogate tags.
|
||||
*
|
||||
* @param Response $response A Response instance
|
||||
*/
|
||||
public function addSurrogateControl(Response $response);
|
||||
|
||||
/**
|
||||
* Checks that the Response needs to be parsed for Surrogate tags.
|
||||
*
|
||||
* @param Response $response A Response instance
|
||||
*
|
||||
* @return bool true if the Response needs to be parsed, false otherwise
|
||||
*/
|
||||
public function needsParsing(Response $response);
|
||||
@@ -79,9 +71,6 @@ interface SurrogateInterface
|
||||
/**
|
||||
* Replaces a Response Surrogate tags with the included resource content.
|
||||
*
|
||||
* @param Request $request A Request instance
|
||||
* @param Response $response A Response instance
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function process(Request $request, Response $response);
|
||||
|
Reference in New Issue
Block a user