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
Esi.php
92 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 | /** |
| 16 | * Esi implements the ESI capabilities to Request and Response instances. |
| 17 | * |
| 18 | * For more information, read the following W3C notes: |
| 19 | * |
| 20 | * * ESI Language Specification 1.0 (http://www.w3.org/TR/esi-lang) |
| 21 | * |
| 22 | * * Edge Architecture Specification (http://www.w3.org/TR/edge-arch) |
| 23 | * |
| 24 | * @author Fabien Potencier <fabien@symfony.com> |
| 25 | */ |
| 26 | class Esi extends AbstractSurrogate |
| 27 | { |
| 28 | public function getName() |
| 29 | { |
| 30 | return 'esi'; |
| 31 | } |
| 32 | /** |
| 33 | * {@inheritdoc} |
| 34 | */ |
| 35 | public function addSurrogateControl(Response $response) |
| 36 | { |
| 37 | if (str_contains($response->getContent(), '<esi:include')) { |
| 38 | $response->headers->set('Surrogate-Control', 'content="ESI/1.0"'); |
| 39 | } |
| 40 | } |
| 41 | /** |
| 42 | * {@inheritdoc} |
| 43 | */ |
| 44 | public function renderIncludeTag(string $uri, ?string $alt = null, bool $ignoreErrors = \true, string $comment = '') |
| 45 | { |
| 46 | $html = sprintf('<esi:include src="%s"%s%s />', $uri, $ignoreErrors ? ' onerror="continue"' : '', $alt ? sprintf(' alt="%s"', $alt) : ''); |
| 47 | if (!empty($comment)) { |
| 48 | return sprintf("<esi:comment text=\"%s\" />\n%s", $comment, $html); |
| 49 | } |
| 50 | return $html; |
| 51 | } |
| 52 | /** |
| 53 | * {@inheritdoc} |
| 54 | */ |
| 55 | public function process(Request $request, Response $response) |
| 56 | { |
| 57 | $type = $response->headers->get('Content-Type'); |
| 58 | if (empty($type)) { |
| 59 | $type = 'text/html'; |
| 60 | } |
| 61 | $parts = explode(';', $type); |
| 62 | if (!\in_array($parts[0], $this->contentTypes)) { |
| 63 | return $response; |
| 64 | } |
| 65 | // we don't use a proper XML parser here as we can have ESI tags in a plain text response |
| 66 | $content = $response->getContent(); |
| 67 | $content = preg_replace('#<esi\\:remove>.*?</esi\\:remove>#s', '', $content); |
| 68 | $content = preg_replace('#<esi\\:comment[^>]+>#s', '', $content); |
| 69 | $boundary = self::generateBodyEvalBoundary(); |
| 70 | $chunks = preg_split('#<esi\\:include\\s+(.*?)\\s*(?:/|</esi\\:include)>#', $content, -1, \PREG_SPLIT_DELIM_CAPTURE); |
| 71 | $i = 1; |
| 72 | while (isset($chunks[$i])) { |
| 73 | $options = []; |
| 74 | preg_match_all('/(src|onerror|alt)="([^"]*?)"/', $chunks[$i], $matches, \PREG_SET_ORDER); |
| 75 | foreach ($matches as $set) { |
| 76 | $options[$set[1]] = $set[2]; |
| 77 | } |
| 78 | if (!isset($options['src'])) { |
| 79 | throw new \RuntimeException('Unable to process an ESI tag without a "src" attribute.'); |
| 80 | } |
| 81 | $chunks[$i] = $boundary . $options['src'] . "\n" . ($options['alt'] ?? '') . "\n" . ('continue' === ($options['onerror'] ?? '')) . "\n"; |
| 82 | $i += 2; |
| 83 | } |
| 84 | $content = $boundary . implode('', $chunks) . $boundary; |
| 85 | $response->setContent($content); |
| 86 | $response->headers->set('X-Body-Eval', 'ESI'); |
| 87 | // remove ESI/1.0 from the Surrogate-Control header |
| 88 | $this->removeFromControl($response); |
| 89 | return $response; |
| 90 | } |
| 91 | } |
| 92 |