Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\HttpKernel\HttpCache; Chris@0: Chris@0: use Symfony\Component\HttpFoundation\Request; Chris@0: use Symfony\Component\HttpFoundation\Response; Chris@0: Chris@0: /** Chris@0: * Esi implements the ESI capabilities to Request and Response instances. Chris@0: * Chris@0: * For more information, read the following W3C notes: Chris@0: * Chris@0: * * ESI Language Specification 1.0 (http://www.w3.org/TR/esi-lang) Chris@0: * Chris@0: * * Edge Architecture Specification (http://www.w3.org/TR/edge-arch) Chris@0: * Chris@0: * @author Fabien Potencier Chris@0: */ Chris@0: class Esi extends AbstractSurrogate Chris@0: { Chris@0: public function getName() Chris@0: { Chris@0: return 'esi'; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function addSurrogateControl(Response $response) Chris@0: { Chris@0: if (false !== strpos($response->getContent(), 'headers->set('Surrogate-Control', 'content="ESI/1.0"'); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function renderIncludeTag($uri, $alt = null, $ignoreErrors = true, $comment = '') Chris@0: { Chris@0: $html = sprintf('', Chris@0: $uri, Chris@0: $ignoreErrors ? ' onerror="continue"' : '', Chris@0: $alt ? sprintf(' alt="%s"', $alt) : '' Chris@0: ); Chris@0: Chris@0: if (!empty($comment)) { Chris@0: return sprintf("\n%s", $comment, $html); Chris@0: } Chris@0: Chris@0: return $html; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function process(Request $request, Response $response) Chris@0: { Chris@0: $type = $response->headers->get('Content-Type'); Chris@0: if (empty($type)) { Chris@0: $type = 'text/html'; Chris@0: } Chris@0: Chris@0: $parts = explode(';', $type); Chris@17: if (!\in_array($parts[0], $this->contentTypes)) { Chris@0: return $response; Chris@0: } Chris@0: Chris@0: // we don't use a proper XML parser here as we can have ESI tags in a plain text response Chris@0: $content = $response->getContent(); Chris@0: $content = preg_replace('#.*?#s', '', $content); Chris@0: $content = preg_replace('#]+>#s', '', $content); Chris@0: Chris@0: $chunks = preg_split('##', $content, -1, PREG_SPLIT_DELIM_CAPTURE); Chris@0: $chunks[0] = str_replace($this->phpEscapeMap[0], $this->phpEscapeMap[1], $chunks[0]); Chris@0: Chris@0: $i = 1; Chris@0: while (isset($chunks[$i])) { Chris@17: $options = []; Chris@0: preg_match_all('/(src|onerror|alt)="([^"]*?)"/', $chunks[$i], $matches, PREG_SET_ORDER); Chris@0: foreach ($matches as $set) { Chris@0: $options[$set[1]] = $set[2]; Chris@0: } Chris@0: Chris@0: if (!isset($options['src'])) { Chris@0: throw new \RuntimeException('Unable to process an ESI tag without a "src" attribute.'); Chris@0: } Chris@0: Chris@0: $chunks[$i] = sprintf('surrogate->handle($this, %s, %s, %s) ?>'."\n", Chris@0: var_export($options['src'], true), Chris@0: var_export(isset($options['alt']) ? $options['alt'] : '', true), Chris@0: isset($options['onerror']) && 'continue' === $options['onerror'] ? 'true' : 'false' Chris@0: ); Chris@0: ++$i; Chris@0: $chunks[$i] = str_replace($this->phpEscapeMap[0], $this->phpEscapeMap[1], $chunks[$i]); Chris@0: ++$i; Chris@0: } Chris@0: $content = implode('', $chunks); Chris@0: Chris@0: $response->setContent($content); Chris@0: $response->headers->set('X-Body-Eval', 'ESI'); Chris@0: Chris@0: // remove ESI/1.0 from the Surrogate-Control header Chris@0: $this->removeFromControl($response); Chris@0: } Chris@0: }