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: * Ssi implements the SSI capabilities to Request and Response instances. Chris@0: * Chris@0: * @author Sebastian Krebs Chris@0: */ Chris@0: class Ssi extends AbstractSurrogate Chris@0: { Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getName() Chris@0: { Chris@0: return 'ssi'; 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(), '', $uri); 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 SSI tags in a plain text response Chris@0: $content = $response->getContent(); 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('/(virtual)="([^"]*?)"/', $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['virtual'])) { Chris@0: throw new \RuntimeException('Unable to process an SSI tag without a "virtual" attribute.'); Chris@0: } Chris@0: Chris@0: $chunks[$i] = sprintf('surrogate->handle($this, %s, \'\', false) ?>'."\n", Chris@0: var_export($options['virtual'], true) 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', 'SSI'); Chris@0: Chris@0: // remove SSI/1.0 from the Surrogate-Control header Chris@0: $this->removeFromControl($response); Chris@0: } Chris@0: }