Esi.php 3.2 KB

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