annotate vendor/symfony/http-kernel/HttpCache/AbstractSurrogate.php @ 2:92f882872392

Trusted hosts, + remove migration modules
author Chris Cannam
date Tue, 05 Dec 2017 09:26:43 +0000
parents 4c8ae668cc8c
children 1fec387a4317
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /*
Chris@0 4 * This file is part of the Symfony package.
Chris@0 5 *
Chris@0 6 * (c) Fabien Potencier <fabien@symfony.com>
Chris@0 7 *
Chris@0 8 * For the full copyright and license information, please view the LICENSE
Chris@0 9 * file that was distributed with this source code.
Chris@0 10 */
Chris@0 11
Chris@0 12 namespace Symfony\Component\HttpKernel\HttpCache;
Chris@0 13
Chris@0 14 use Symfony\Component\HttpFoundation\Request;
Chris@0 15 use Symfony\Component\HttpFoundation\Response;
Chris@0 16 use Symfony\Component\HttpKernel\HttpKernelInterface;
Chris@0 17
Chris@0 18 /**
Chris@0 19 * Abstract class implementing Surrogate capabilities to Request and Response instances.
Chris@0 20 *
Chris@0 21 * @author Fabien Potencier <fabien@symfony.com>
Chris@0 22 * @author Robin Chalas <robin.chalas@gmail.com>
Chris@0 23 */
Chris@0 24 abstract class AbstractSurrogate implements SurrogateInterface
Chris@0 25 {
Chris@0 26 protected $contentTypes;
Chris@0 27 protected $phpEscapeMap = array(
Chris@0 28 array('<?', '<%', '<s', '<S'),
Chris@0 29 array('<?php echo "<?"; ?>', '<?php echo "<%"; ?>', '<?php echo "<s"; ?>', '<?php echo "<S"; ?>'),
Chris@0 30 );
Chris@0 31
Chris@0 32 /**
Chris@0 33 * Constructor.
Chris@0 34 *
Chris@0 35 * @param array $contentTypes An array of content-type that should be parsed for Surrogate information
Chris@0 36 * (default: text/html, text/xml, application/xhtml+xml, and application/xml)
Chris@0 37 */
Chris@0 38 public function __construct(array $contentTypes = array('text/html', 'text/xml', 'application/xhtml+xml', 'application/xml'))
Chris@0 39 {
Chris@0 40 $this->contentTypes = $contentTypes;
Chris@0 41 }
Chris@0 42
Chris@0 43 /**
Chris@0 44 * Returns a new cache strategy instance.
Chris@0 45 *
Chris@0 46 * @return ResponseCacheStrategyInterface A ResponseCacheStrategyInterface instance
Chris@0 47 */
Chris@0 48 public function createCacheStrategy()
Chris@0 49 {
Chris@0 50 return new ResponseCacheStrategy();
Chris@0 51 }
Chris@0 52
Chris@0 53 /**
Chris@0 54 * {@inheritdoc}
Chris@0 55 */
Chris@0 56 public function hasSurrogateCapability(Request $request)
Chris@0 57 {
Chris@0 58 if (null === $value = $request->headers->get('Surrogate-Capability')) {
Chris@0 59 return false;
Chris@0 60 }
Chris@0 61
Chris@0 62 return false !== strpos($value, sprintf('%s/1.0', strtoupper($this->getName())));
Chris@0 63 }
Chris@0 64
Chris@0 65 /**
Chris@0 66 * {@inheritdoc}
Chris@0 67 */
Chris@0 68 public function addSurrogateCapability(Request $request)
Chris@0 69 {
Chris@0 70 $current = $request->headers->get('Surrogate-Capability');
Chris@0 71 $new = sprintf('symfony="%s/1.0"', strtoupper($this->getName()));
Chris@0 72
Chris@0 73 $request->headers->set('Surrogate-Capability', $current ? $current.', '.$new : $new);
Chris@0 74 }
Chris@0 75
Chris@0 76 /**
Chris@0 77 * {@inheritdoc}
Chris@0 78 */
Chris@0 79 public function needsParsing(Response $response)
Chris@0 80 {
Chris@0 81 if (!$control = $response->headers->get('Surrogate-Control')) {
Chris@0 82 return false;
Chris@0 83 }
Chris@0 84
Chris@0 85 $pattern = sprintf('#content="[^"]*%s/1.0[^"]*"#', strtoupper($this->getName()));
Chris@0 86
Chris@0 87 return (bool) preg_match($pattern, $control);
Chris@0 88 }
Chris@0 89
Chris@0 90 /**
Chris@0 91 * {@inheritdoc}
Chris@0 92 */
Chris@0 93 public function handle(HttpCache $cache, $uri, $alt, $ignoreErrors)
Chris@0 94 {
Chris@0 95 $subRequest = Request::create($uri, Request::METHOD_GET, array(), $cache->getRequest()->cookies->all(), array(), $cache->getRequest()->server->all());
Chris@0 96
Chris@0 97 try {
Chris@0 98 $response = $cache->handle($subRequest, HttpKernelInterface::SUB_REQUEST, true);
Chris@0 99
Chris@0 100 if (!$response->isSuccessful()) {
Chris@0 101 throw new \RuntimeException(sprintf('Error when rendering "%s" (Status code is %s).', $subRequest->getUri(), $response->getStatusCode()));
Chris@0 102 }
Chris@0 103
Chris@0 104 return $response->getContent();
Chris@0 105 } catch (\Exception $e) {
Chris@0 106 if ($alt) {
Chris@0 107 return $this->handle($cache, $alt, '', $ignoreErrors);
Chris@0 108 }
Chris@0 109
Chris@0 110 if (!$ignoreErrors) {
Chris@0 111 throw $e;
Chris@0 112 }
Chris@0 113 }
Chris@0 114 }
Chris@0 115
Chris@0 116 /**
Chris@0 117 * Remove the Surrogate from the Surrogate-Control header.
Chris@0 118 *
Chris@0 119 * @param Response $response
Chris@0 120 */
Chris@0 121 protected function removeFromControl(Response $response)
Chris@0 122 {
Chris@0 123 if (!$response->headers->has('Surrogate-Control')) {
Chris@0 124 return;
Chris@0 125 }
Chris@0 126
Chris@0 127 $value = $response->headers->get('Surrogate-Control');
Chris@0 128 $upperName = strtoupper($this->getName());
Chris@0 129
Chris@0 130 if (sprintf('content="%s/1.0"', $upperName) == $value) {
Chris@0 131 $response->headers->remove('Surrogate-Control');
Chris@0 132 } elseif (preg_match(sprintf('#,\s*content="%s/1.0"#', $upperName), $value)) {
Chris@0 133 $response->headers->set('Surrogate-Control', preg_replace(sprintf('#,\s*content="%s/1.0"#', $upperName), '', $value));
Chris@0 134 } elseif (preg_match(sprintf('#content="%s/1.0",\s*#', $upperName), $value)) {
Chris@0 135 $response->headers->set('Surrogate-Control', preg_replace(sprintf('#content="%s/1.0",\s*#', $upperName), '', $value));
Chris@0 136 }
Chris@0 137 }
Chris@0 138 }