You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

136 lines
4.3 KiB

3 years ago
  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. protected $contentTypes;
  23. protected $phpEscapeMap = [
  24. ['<?', '<%', '<s', '<S'],
  25. ['<?php echo "<?"; ?>', '<?php echo "<%"; ?>', '<?php echo "<s"; ?>', '<?php echo "<S"; ?>'],
  26. ];
  27. /**
  28. * @param array $contentTypes An array of content-type that should be parsed for Surrogate information
  29. * (default: text/html, text/xml, application/xhtml+xml, and application/xml)
  30. */
  31. public function __construct(array $contentTypes = ['text/html', 'text/xml', 'application/xhtml+xml', 'application/xml'])
  32. {
  33. $this->contentTypes = $contentTypes;
  34. }
  35. /**
  36. * Returns a new cache strategy instance.
  37. *
  38. * @return ResponseCacheStrategyInterface A ResponseCacheStrategyInterface instance
  39. */
  40. public function createCacheStrategy()
  41. {
  42. return new ResponseCacheStrategy();
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function hasSurrogateCapability(Request $request)
  48. {
  49. if (null === $value = $request->headers->get('Surrogate-Capability')) {
  50. return false;
  51. }
  52. return false !== strpos($value, sprintf('%s/1.0', strtoupper($this->getName())));
  53. }
  54. /**
  55. * {@inheritdoc}
  56. */
  57. public function addSurrogateCapability(Request $request)
  58. {
  59. $current = $request->headers->get('Surrogate-Capability');
  60. $new = sprintf('symfony="%s/1.0"', strtoupper($this->getName()));
  61. $request->headers->set('Surrogate-Capability', $current ? $current.', '.$new : $new);
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. public function needsParsing(Response $response)
  67. {
  68. if (!$control = $response->headers->get('Surrogate-Control')) {
  69. return false;
  70. }
  71. $pattern = sprintf('#content="[^"]*%s/1.0[^"]*"#', strtoupper($this->getName()));
  72. return (bool) preg_match($pattern, $control);
  73. }
  74. /**
  75. * {@inheritdoc}
  76. */
  77. public function handle(HttpCache $cache, string $uri, string $alt, bool $ignoreErrors)
  78. {
  79. $subRequest = Request::create($uri, Request::METHOD_GET, [], $cache->getRequest()->cookies->all(), [], $cache->getRequest()->server->all());
  80. try {
  81. $response = $cache->handle($subRequest, HttpKernelInterface::SUB_REQUEST, true);
  82. if (!$response->isSuccessful()) {
  83. throw new \RuntimeException(sprintf('Error when rendering "%s" (Status code is %d).', $subRequest->getUri(), $response->getStatusCode()));
  84. }
  85. return $response->getContent();
  86. } catch (\Exception $e) {
  87. if ($alt) {
  88. return $this->handle($cache, $alt, '', $ignoreErrors);
  89. }
  90. if (!$ignoreErrors) {
  91. throw $e;
  92. }
  93. }
  94. return '';
  95. }
  96. /**
  97. * Remove the Surrogate from the Surrogate-Control header.
  98. */
  99. protected function removeFromControl(Response $response)
  100. {
  101. if (!$response->headers->has('Surrogate-Control')) {
  102. return;
  103. }
  104. $value = $response->headers->get('Surrogate-Control');
  105. $upperName = strtoupper($this->getName());
  106. if (sprintf('content="%s/1.0"', $upperName) == $value) {
  107. $response->headers->remove('Surrogate-Control');
  108. } elseif (preg_match(sprintf('#,\s*content="%s/1.0"#', $upperName), $value)) {
  109. $response->headers->set('Surrogate-Control', preg_replace(sprintf('#,\s*content="%s/1.0"#', $upperName), '', $value));
  110. } elseif (preg_match(sprintf('#content="%s/1.0",\s*#', $upperName), $value)) {
  111. $response->headers->set('Surrogate-Control', preg_replace(sprintf('#content="%s/1.0",\s*#', $upperName), '', $value));
  112. }
  113. }
  114. }