AbstractSurrogate.php 4.4 KB

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