AbstractSurrogate.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. use Symfony\Component\HttpKernel\HttpKernelInterface;
  14. /**
  15. * Abstract class implementing Surrogate capabilities to Request and Response instances.
  16. *
  17. * @author Fabien Potencier <fabien@symfony.com>
  18. * @author Robin Chalas <robin.chalas@gmail.com>
  19. */
  20. abstract class AbstractSurrogate implements SurrogateInterface
  21. {
  22. /**
  23. * @param array $contentTypes An array of content-type that should be parsed for Surrogate information
  24. * (default: text/html, text/xml, application/xhtml+xml, and application/xml)
  25. */
  26. public function __construct(
  27. protected array $contentTypes = ['text/html', 'text/xml', 'application/xhtml+xml', 'application/xml'],
  28. ) {
  29. }
  30. /**
  31. * Returns a new cache strategy instance.
  32. */
  33. public function createCacheStrategy(): ResponseCacheStrategyInterface
  34. {
  35. return new ResponseCacheStrategy();
  36. }
  37. public function hasSurrogateCapability(Request $request): bool
  38. {
  39. if (null === $value = $request->headers->get('Surrogate-Capability')) {
  40. return false;
  41. }
  42. return str_contains($value, \sprintf('%s/1.0', strtoupper($this->getName())));
  43. }
  44. public function addSurrogateCapability(Request $request): void
  45. {
  46. $current = $request->headers->get('Surrogate-Capability');
  47. $new = \sprintf('symfony="%s/1.0"', strtoupper($this->getName()));
  48. $request->headers->set('Surrogate-Capability', $current ? $current.', '.$new : $new);
  49. }
  50. public function needsParsing(Response $response): bool
  51. {
  52. if (!$control = $response->headers->get('Surrogate-Control')) {
  53. return false;
  54. }
  55. $pattern = \sprintf('#content="[^"]*%s/1.0[^"]*"#', strtoupper($this->getName()));
  56. return (bool) preg_match($pattern, $control);
  57. }
  58. public function handle(HttpCache $cache, string $uri, string $alt, bool $ignoreErrors): string
  59. {
  60. $subRequest = Request::create($uri, Request::METHOD_GET, [], $cache->getRequest()->cookies->all(), [], $cache->getRequest()->server->all());
  61. try {
  62. $response = $cache->handle($subRequest, HttpKernelInterface::SUB_REQUEST, true);
  63. if (!$response->isSuccessful() && Response::HTTP_NOT_MODIFIED !== $response->getStatusCode()) {
  64. throw new \RuntimeException(\sprintf('Error when rendering "%s" (Status code is %d).', $subRequest->getUri(), $response->getStatusCode()));
  65. }
  66. return $response->getContent();
  67. } catch (\Exception $e) {
  68. if ($alt) {
  69. return $this->handle($cache, $alt, '', $ignoreErrors);
  70. }
  71. if (!$ignoreErrors) {
  72. throw $e;
  73. }
  74. }
  75. return '';
  76. }
  77. /**
  78. * Remove the Surrogate from the Surrogate-Control header.
  79. */
  80. protected function removeFromControl(Response $response): void
  81. {
  82. if (!$response->headers->has('Surrogate-Control')) {
  83. return;
  84. }
  85. $value = $response->headers->get('Surrogate-Control');
  86. $upperName = strtoupper($this->getName());
  87. if (\sprintf('content="%s/1.0"', $upperName) == $value) {
  88. $response->headers->remove('Surrogate-Control');
  89. } elseif (preg_match(\sprintf('#,\s*content="%s/1.0"#', $upperName), $value)) {
  90. $response->headers->set('Surrogate-Control', preg_replace(\sprintf('#,\s*content="%s/1.0"#', $upperName), '', $value));
  91. } elseif (preg_match(\sprintf('#content="%s/1.0",\s*#', $upperName), $value)) {
  92. $response->headers->set('Surrogate-Control', preg_replace(\sprintf('#content="%s/1.0",\s*#', $upperName), '', $value));
  93. }
  94. }
  95. protected static function generateBodyEvalBoundary(): string
  96. {
  97. static $cookie;
  98. $cookie = hash('xxh128', $cookie ?? $cookie = random_bytes(16), true);
  99. $boundary = base64_encode($cookie);
  100. \assert(HttpCache::BODY_EVAL_BOUNDARY_LENGTH === \strlen($boundary));
  101. return $boundary;
  102. }
  103. }