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