Nav apraksta

Esi.php 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpKernel\HttpCache;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. /**
  14. * Esi implements the ESI capabilities to Request and Response instances.
  15. *
  16. * For more information, read the following W3C notes:
  17. *
  18. * * ESI Language Specification 1.0 (http://www.w3.org/TR/esi-lang)
  19. *
  20. * * Edge Architecture Specification (http://www.w3.org/TR/edge-arch)
  21. *
  22. * @author Fabien Potencier <fabien@symfony.com>
  23. */
  24. class Esi extends AbstractSurrogate
  25. {
  26. public function getName()
  27. {
  28. return 'esi';
  29. }
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public function addSurrogateControl(Response $response)
  34. {
  35. if (false !== strpos($response->getContent(), '<esi:include')) {
  36. $response->headers->set('Surrogate-Control', 'content="ESI/1.0"');
  37. }
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function renderIncludeTag($uri, $alt = null, $ignoreErrors = true, $comment = '')
  43. {
  44. $html = sprintf('<esi:include src="%s"%s%s />',
  45. $uri,
  46. $ignoreErrors ? ' onerror="continue"' : '',
  47. $alt ? sprintf(' alt="%s"', $alt) : ''
  48. );
  49. if (!empty($comment)) {
  50. return sprintf("<esi:comment text=\"%s\" />\n%s", $comment, $html);
  51. }
  52. return $html;
  53. }
  54. /**
  55. * {@inheritdoc}
  56. */
  57. public function process(Request $request, Response $response)
  58. {
  59. $type = $response->headers->get('Content-Type');
  60. if (empty($type)) {
  61. $type = 'text/html';
  62. }
  63. $parts = explode(';', $type);
  64. if (!in_array($parts[0], $this->contentTypes)) {
  65. return $response;
  66. }
  67. // we don't use a proper XML parser here as we can have ESI tags in a plain text response
  68. $content = $response->getContent();
  69. $content = preg_replace('#<esi\:remove>.*?</esi\:remove>#s', '', $content);
  70. $content = preg_replace('#<esi\:comment[^>]+>#s', '', $content);
  71. $chunks = preg_split('#<esi\:include\s+(.*?)\s*(?:/|</esi\:include)>#', $content, -1, PREG_SPLIT_DELIM_CAPTURE);
  72. $chunks[0] = str_replace($this->phpEscapeMap[0], $this->phpEscapeMap[1], $chunks[0]);
  73. $i = 1;
  74. while (isset($chunks[$i])) {
  75. $options = array();
  76. preg_match_all('/(src|onerror|alt)="([^"]*?)"/', $chunks[$i], $matches, PREG_SET_ORDER);
  77. foreach ($matches as $set) {
  78. $options[$set[1]] = $set[2];
  79. }
  80. if (!isset($options['src'])) {
  81. throw new \RuntimeException('Unable to process an ESI tag without a "src" attribute.');
  82. }
  83. $chunks[$i] = sprintf('<?php echo $this->surrogate->handle($this, %s, %s, %s) ?>'."\n",
  84. var_export($options['src'], true),
  85. var_export(isset($options['alt']) ? $options['alt'] : '', true),
  86. isset($options['onerror']) && 'continue' === $options['onerror'] ? 'true' : 'false'
  87. );
  88. ++$i;
  89. $chunks[$i] = str_replace($this->phpEscapeMap[0], $this->phpEscapeMap[1], $chunks[$i]);
  90. ++$i;
  91. }
  92. $content = implode('', $chunks);
  93. $response->setContent($content);
  94. $response->headers->set('X-Body-Eval', 'ESI');
  95. // remove ESI/1.0 from the Surrogate-Control header
  96. $this->removeFromControl($response);
  97. }
  98. }