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