4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpKernel\HttpCache;
14 use Symfony\Component\HttpFoundation\Request;
15 use Symfony\Component\HttpFoundation\Response;
18 * Ssi implements the SSI capabilities to Request and Response instances.
20 * @author Sebastian Krebs <krebs.seb@gmail.com>
22 class Ssi extends AbstractSurrogate
27 public function getName()
35 public function addSurrogateControl(Response $response)
37 if (false !== strpos($response->getContent(), '<!--#include')) {
38 $response->headers->set('Surrogate-Control', 'content="SSI/1.0"');
45 public function renderIncludeTag($uri, $alt = null, $ignoreErrors = true, $comment = '')
47 return sprintf('<!--#include virtual="%s" -->', $uri);
53 public function process(Request $request, Response $response)
55 $type = $response->headers->get('Content-Type');
60 $parts = explode(';', $type);
61 if (!in_array($parts[0], $this->contentTypes)) {
65 // we don't use a proper XML parser here as we can have SSI tags in a plain text response
66 $content = $response->getContent();
68 $chunks = preg_split('#<!--\#include\s+(.*?)\s*-->#', $content, -1, PREG_SPLIT_DELIM_CAPTURE);
69 $chunks[0] = str_replace($this->phpEscapeMap[0], $this->phpEscapeMap[1], $chunks[0]);
72 while (isset($chunks[$i])) {
74 preg_match_all('/(virtual)="([^"]*?)"/', $chunks[$i], $matches, PREG_SET_ORDER);
75 foreach ($matches as $set) {
76 $options[$set[1]] = $set[2];
79 if (!isset($options['virtual'])) {
80 throw new \RuntimeException('Unable to process an SSI tag without a "virtual" attribute.');
83 $chunks[$i] = sprintf('<?php echo $this->surrogate->handle($this, %s, \'\', false) ?>'."\n",
84 var_export($options['virtual'], true)
87 $chunks[$i] = str_replace($this->phpEscapeMap[0], $this->phpEscapeMap[1], $chunks[$i]);
90 $content = implode('', $chunks);
92 $response->setContent($content);
93 $response->headers->set('X-Body-Eval', 'SSI');
95 // remove SSI/1.0 from the Surrogate-Control header
96 $this->removeFromControl($response);