Esi.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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(): string
  27. {
  28. return 'esi';
  29. }
  30. /**
  31. * @return void
  32. */
  33. public function addSurrogateControl(Response $response)
  34. {
  35. if (str_contains($response->getContent(), '<esi:include')) {
  36. $response->headers->set('Surrogate-Control', 'content="ESI/1.0"');
  37. }
  38. }
  39. public function renderIncludeTag(string $uri, ?string $alt = null, bool $ignoreErrors = true, string $comment = ''): string
  40. {
  41. $html = \sprintf('<esi:include src="%s"%s%s />',
  42. $uri,
  43. $ignoreErrors ? ' onerror="continue"' : '',
  44. $alt ? \sprintf(' alt="%s"', $alt) : ''
  45. );
  46. if (!empty($comment)) {
  47. return \sprintf("<esi:comment text=\"%s\" />\n%s", $comment, $html);
  48. }
  49. return $html;
  50. }
  51. public function process(Request $request, Response $response): Response
  52. {
  53. $type = $response->headers->get('Content-Type');
  54. if (empty($type)) {
  55. $type = 'text/html';
  56. }
  57. $parts = explode(';', $type);
  58. if (!\in_array($parts[0], $this->contentTypes)) {
  59. return $response;
  60. }
  61. // we don't use a proper XML parser here as we can have ESI tags in a plain text response
  62. $content = $response->getContent();
  63. $content = preg_replace('#<esi\:remove>.*?</esi\:remove>#s', '', $content);
  64. $content = preg_replace('#<esi\:comment[^>]+>#s', '', $content);
  65. $boundary = self::generateBodyEvalBoundary();
  66. $chunks = preg_split('#<esi\:include\s+(.*?)\s*(?:/|</esi\:include)>#', $content, -1, \PREG_SPLIT_DELIM_CAPTURE);
  67. $i = 1;
  68. while (isset($chunks[$i])) {
  69. $options = [];
  70. preg_match_all('/(src|onerror|alt)="([^"]*?)"/', $chunks[$i], $matches, \PREG_SET_ORDER);
  71. foreach ($matches as $set) {
  72. $options[$set[1]] = $set[2];
  73. }
  74. if (!isset($options['src'])) {
  75. throw new \RuntimeException('Unable to process an ESI tag without a "src" attribute.');
  76. }
  77. $chunks[$i] = $boundary.$options['src']."\n".($options['alt'] ?? '')."\n".('continue' === ($options['onerror'] ?? ''))."\n";
  78. $i += 2;
  79. }
  80. $content = $boundary.implode('', $chunks).$boundary;
  81. $response->setContent($content);
  82. $response->headers->set('X-Body-Eval', 'ESI');
  83. // remove ESI/1.0 from the Surrogate-Control header
  84. $this->removeFromControl($response);
  85. return $response;
  86. }
  87. }