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 * Abstract class implementing Surrogate capabilities to Request and Response instances.
21 * @author Fabien Potencier <fabien@symfony.com>
22 * @author Robin Chalas <robin.chalas@gmail.com>
24 abstract class AbstractSurrogate implements SurrogateInterface
26 protected $contentTypes;
27 protected $phpEscapeMap = array(
28 array('<?', '<%', '<s', '<S'),
29 array('<?php echo "<?"; ?>', '<?php echo "<%"; ?>', '<?php echo "<s"; ?>', '<?php echo "<S"; ?>'),
35 * @param array $contentTypes An array of content-type that should be parsed for Surrogate information
36 * (default: text/html, text/xml, application/xhtml+xml, and application/xml)
38 public function __construct(array $contentTypes = array('text/html', 'text/xml', 'application/xhtml+xml', 'application/xml'))
40 $this->contentTypes = $contentTypes;
44 * Returns a new cache strategy instance.
46 * @return ResponseCacheStrategyInterface A ResponseCacheStrategyInterface instance
48 public function createCacheStrategy()
50 return new ResponseCacheStrategy();
56 public function hasSurrogateCapability(Request $request)
58 if (null === $value = $request->headers->get('Surrogate-Capability')) {
62 return false !== strpos($value, sprintf('%s/1.0', strtoupper($this->getName())));
68 public function addSurrogateCapability(Request $request)
70 $current = $request->headers->get('Surrogate-Capability');
71 $new = sprintf('symfony="%s/1.0"', strtoupper($this->getName()));
73 $request->headers->set('Surrogate-Capability', $current ? $current.', '.$new : $new);
79 public function needsParsing(Response $response)
81 if (!$control = $response->headers->get('Surrogate-Control')) {
85 $pattern = sprintf('#content="[^"]*%s/1.0[^"]*"#', strtoupper($this->getName()));
87 return (bool) preg_match($pattern, $control);
93 public function handle(HttpCache $cache, $uri, $alt, $ignoreErrors)
95 $subRequest = Request::create($uri, Request::METHOD_GET, array(), $cache->getRequest()->cookies->all(), array(), $cache->getRequest()->server->all());
98 $response = $cache->handle($subRequest, HttpKernelInterface::SUB_REQUEST, true);
100 if (!$response->isSuccessful()) {
101 throw new \RuntimeException(sprintf('Error when rendering "%s" (Status code is %s).', $subRequest->getUri(), $response->getStatusCode()));
104 return $response->getContent();
105 } catch (\Exception $e) {
107 return $this->handle($cache, $alt, '', $ignoreErrors);
110 if (!$ignoreErrors) {
117 * Remove the Surrogate from the Surrogate-Control header.
119 * @param Response $response
121 protected function removeFromControl(Response $response)
123 if (!$response->headers->has('Surrogate-Control')) {
127 $value = $response->headers->get('Surrogate-Control');
128 $upperName = strtoupper($this->getName());
130 if (sprintf('content="%s/1.0"', $upperName) == $value) {
131 $response->headers->remove('Surrogate-Control');
132 } elseif (preg_match(sprintf('#,\s*content="%s/1.0"#', $upperName), $value)) {
133 $response->headers->set('Surrogate-Control', preg_replace(sprintf('#,\s*content="%s/1.0"#', $upperName), '', $value));
134 } elseif (preg_match(sprintf('#content="%s/1.0",\s*#', $upperName), $value)) {
135 $response->headers->set('Surrogate-Control', preg_replace(sprintf('#content="%s/1.0",\s*#', $upperName), '', $value));