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;
16 use Symfony\Component\HttpKernel\HttpKernelInterface;
19 * Ssi implements the SSI capabilities to Request and Response instances.
21 * @author Sebastian Krebs <krebs.seb@gmail.com>
23 class Ssi implements SurrogateInterface
25 private $contentTypes;
26 private $phpEscapeMap = array(
27 array('<?', '<%', '<s', '<S'),
28 array('<?php echo "<?"; ?>', '<?php echo "<%"; ?>', '<?php echo "<s"; ?>', '<?php echo "<S"; ?>'),
34 * @param array $contentTypes An array of content-type that should be parsed for SSI information
35 * (default: text/html, text/xml, application/xhtml+xml, and application/xml)
37 public function __construct(array $contentTypes = array('text/html', 'text/xml', 'application/xhtml+xml', 'application/xml'))
39 $this->contentTypes = $contentTypes;
45 public function getName()
53 public function createCacheStrategy()
55 return new ResponseCacheStrategy();
61 public function hasSurrogateCapability(Request $request)
63 if (null === $value = $request->headers->get('Surrogate-Capability')) {
67 return false !== strpos($value, 'SSI/1.0');
73 public function addSurrogateCapability(Request $request)
75 $current = $request->headers->get('Surrogate-Capability');
76 $new = 'symfony2="SSI/1.0"';
78 $request->headers->set('Surrogate-Capability', $current ? $current.', '.$new : $new);
84 public function addSurrogateControl(Response $response)
86 if (false !== strpos($response->getContent(), '<!--#include')) {
87 $response->headers->set('Surrogate-Control', 'content="SSI/1.0"');
94 public function needsParsing(Response $response)
96 if (!$control = $response->headers->get('Surrogate-Control')) {
100 return (bool) preg_match('#content="[^"]*SSI/1.0[^"]*"#', $control);
106 public function renderIncludeTag($uri, $alt = null, $ignoreErrors = true, $comment = '')
108 return sprintf('<!--#include virtual="%s" -->', $uri);
114 public function process(Request $request, Response $response)
116 $type = $response->headers->get('Content-Type');
121 $parts = explode(';', $type);
122 if (!in_array($parts[0], $this->contentTypes)) {
126 // we don't use a proper XML parser here as we can have SSI tags in a plain text response
127 $content = $response->getContent();
129 $chunks = preg_split('#<!--\#include\s+(.*?)\s*-->#', $content, -1, PREG_SPLIT_DELIM_CAPTURE);
130 $chunks[0] = str_replace($this->phpEscapeMap[0], $this->phpEscapeMap[1], $chunks[0]);
133 while (isset($chunks[$i])) {
135 preg_match_all('/(virtual)="([^"]*?)"/', $chunks[$i], $matches, PREG_SET_ORDER);
136 foreach ($matches as $set) {
137 $options[$set[1]] = $set[2];
140 if (!isset($options['virtual'])) {
141 throw new \RuntimeException('Unable to process an SSI tag without a "virtual" attribute.');
144 $chunks[$i] = sprintf('<?php echo $this->surrogate->handle($this, %s, \'\', false) ?>'."\n",
145 var_export($options['virtual'], true)
148 $chunks[$i] = str_replace($this->phpEscapeMap[0], $this->phpEscapeMap[1], $chunks[$i]);
151 $content = implode('', $chunks);
153 $response->setContent($content);
154 $response->headers->set('X-Body-Eval', 'SSI');
156 // remove SSI/1.0 from the Surrogate-Control header
157 if ($response->headers->has('Surrogate-Control')) {
158 $value = $response->headers->get('Surrogate-Control');
159 if ('content="SSI/1.0"' == $value) {
160 $response->headers->remove('Surrogate-Control');
161 } elseif (preg_match('#,\s*content="SSI/1.0"#', $value)) {
162 $response->headers->set('Surrogate-Control', preg_replace('#,\s*content="SSI/1.0"#', '', $value));
163 } elseif (preg_match('#content="SSI/1.0",\s*#', $value)) {
164 $response->headers->set('Surrogate-Control', preg_replace('#content="SSI/1.0",\s*#', '', $value));
172 public function handle(HttpCache $cache, $uri, $alt, $ignoreErrors)
174 $subRequest = Request::create($uri, 'get', array(), $cache->getRequest()->cookies->all(), array(), $cache->getRequest()->server->all());
177 $response = $cache->handle($subRequest, HttpKernelInterface::SUB_REQUEST, true);
179 if (!$response->isSuccessful()) {
180 throw new \RuntimeException(sprintf('Error when rendering "%s" (Status code is %s).', $subRequest->getUri(), $response->getStatusCode()));
183 return $response->getContent();
184 } catch (\Exception $e) {
186 return $this->handle($cache, $alt, '', $ignoreErrors);
189 if (!$ignoreErrors) {