8000 Move duplicated logic from Esi/Ssi to an AbstractSurrogate · symfony/symfony@6b3835c · GitHub
[go: up one dir, main page]

Skip to content

Commit 6b3835c

Browse files
committed
Move duplicated logic from Esi/Ssi to an AbstractSurrogate
Fabbot fixes
1 parent ea9d6e7 commit 6b3835c

File tree

3 files changed

+119
-217
lines changed

3 files changed

+119
-217
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpKernel\HttpCache;
13+
14+
use Symfony\Component\HttpFoundation\Request;
15+
use Symfony\Component\HttpFoundation\Response;
16+
use Symfony\Component\HttpKernel\HttpKernelInterface;
17+
18+
/**
19+
* Abstract class implementing Surrogate capabilities to Request and Response instances.
20+
*
21+
* @author Fabien Potencier <fabien@symfony.com>
22+
*/
23+
abstract class AbstractSurrogate implements SurrogateInterface
24+
{
25+
protected $contentTypes;
26+
protected $phpEscapeMap = array(
27+
array('<?', '<%', '<s', '<S'),
28+
array('<?php echo "<?"; ?>', '<?php echo "<%"; ?>', '<?php echo "<s"; ?>', '<?php echo "<S"; ?>'),
29+
);
30+
31+
/**
32+
* Constructor.
33+
*
34+
* @param array $contentTypes An array of content-type that should be parsed for Surrogate information.
35+
* (default: text/html, text/xml, application/xhtml+xml, and application/xml)
36+
*/
37+
public function __construct(array $contentTypes = array('text/html', 'text/xml', 'application/xhtml+xml', 'application/xml'))
38+
{
39+
$this->contentTypes = $contentTypes;
40+
}
41+
42+
/**
43+
* Returns a new cache strategy instance.
44+
*
45+
* @return ResponseCacheStrategyInterface A ResponseCacheStrategyInterface instance
46+
*/
47+
public function createCacheStrategy()
48+
{
49+
return new ResponseCacheStrategy();
50+
}
51+
52+
/**
53+
* {@inheritdoc}
54+
*/
55+
public function hasSurrogateCapability(Request $request)
56+
{
57+
if (null === $value = $request->headers->get('Surrogate-Capability')) {
58+
return false;
59+
}
60+
61+
return false !== strpos($value, sprintf('%s/1.0', strtoupper($this->getName())));
62+
}
63+
64+
/**
65+
* {@inheritdoc}
66+
*/
67+
public function addSurrogateCapability(Request $request)
68+
{
69+
$current = $request->headers->get('Surrogate-Capability');
70+
$new = sprintf('symfony2="%s/1.0"', strtoupper($this->getName()));
71+
72+
$request->headers->set('Surrogate-Capability', $current ? $current.', '.$new : $new);
73+
}
74+
75+
/**
76+
* {@inheritdoc}
77+
*/
78+
public function needsParsing(Response $response)
79+
{
80+
if (!$control = $response->headers->get('Surrogate-Control')) {
81+
return false;
82+
}
83+
84+
$pattern = sprintf('#content="[^"]*%s/1.0[^"]*"#', strtoupper($this->getName()));
85+
86+
return (bool) preg_match($pattern, $control);
87+
}
88+
89+
/**
90+
* {@inheritdoc}
91+
*/
92+
public function handle(HttpCache $cache, $uri, $alt, $ignoreErrors)
93+
{
94+
$subRequest = Request::create($uri, 'get', array(), $cache->getRequest()->cookies->all(), array(), $cache->getRequest()->server->all());
95+
96+
try {
97+
$response = $cache->handle($subRequest, HttpKernelInterface::SUB_REQUEST, true);
98+
99+
if (!$response->isSuccessful()) {
100+
throw new \RuntimeException(sprintf('Error when rendering "%s" (Status code is %s).', $subRequest->getUri(), $response->getStatusCode()));
101+
}
102+
103+
return $response->getContent();
104+
} catch (\Exception $e) {
105+
if ($alt) {
106+
return $this->handle($cache, $alt, '', $ignoreErrors);
107+
}
108+
109+
if (!$ignoreErrors) {
110+
throw $e;
111+
}
112+
}
113+
}
114+
}

src/Symfony/Component/HttpKernel/HttpCache/Esi.php

Lines changed: 4 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
use Symfony\Component\HttpFoundation\Request;
1515
use Symfony\Component\HttpFoundation\Response;
16-
use Symfony\Component\HttpKernel\HttpKernelInterface;
1716

1817
/**
1918
* Esi implements the ESI capabilities to Request and Response instances.
@@ -26,75 +25,15 @@
2625
*
2726
* @author Fabien Potencier <fabien@symfony.com>
2827
*/
29-
class Esi implements SurrogateInterface
28+
class Esi extends AbstractSurrogate
3029
{
31-
private $contentTypes;
32-
private $phpEscapeMap = array(
33-
array('<?', '<%', '<s', '<S'),
34-
array('<?php echo "<?"; ?>', '<?php echo "<%"; ?>', '<?php echo "<s"; ?>', '<?php echo "<S"; ?>'),
35-
);
36-
37-
/**
38-
* Constructor.
39-
*
40-
* @param array $contentTypes An array of content-type that should be parsed for ESI information.
41-
* (default: text/html, text/xml, application/xhtml+xml, and application/xml)
42-
*/
43-
public function __construct(array $contentTypes = array('text/html', 'text/xml', 'application/xhtml+xml', 'application/xml'))
44-
{
45-
$this->contentTypes = $contentTypes;
46-
}
47-
4830
public function getName()
4931
{
5032
return 'esi';
5133
}
5234

5335
/**
54-
* Returns a new cache strategy instance.
55-
*
56-
* @return ResponseCacheStrategyInterface A ResponseCacheStrategyInterface instance
57-
*/
58-
public function createCacheStrategy()
59-
{
60-
return new ResponseCacheStrategy();
61-
}
62-
63-
/**
64-
* Checks that at least one surrogate has ESI/1.0 capability.
65-
*
66-
* @param Request $request A Request instance
67-
*
68-
* @return bool true if one surrogate has ESI/1.0 capability, false otherwise
69-
*/
70-
public function hasSurrogateCapability(Request $request)
71-
{
72-
if (null === $value = $request->headers->get('Surrogate-Capability')) {
73-
return false;
74-
}
75-
76-
return false !== strpos($value, 'ESI/1.0');
77-
}
78-
79-
/**
80-
* Adds ESI/1.0 capability to the given Request.
81-
*
82-
* @param Request $request A Request instance
83-
*/
84-
public function addSurrogateCapability(Request $request)
85-
{
86-
$current = $request->headers->get('Surrogate-Capability');
87-
$new = 'symfony2="ESI/1.0"';
88-
89-
$request->headers->set('Surrogate-Capability', $current ? $current.', '.$new : $new);
90-
}
91-
92-
/**
93-
* Adds HTTP headers to specify that the Response needs to be parsed for ESI.
94-
*
95-
* This method only adds an ESI HTTP header if the Response has some ESI tags.
96-
*
97-
* @param Response $response A Response instance
36+
* {@inheritdoc}
9837
*/
9938
public function addSurrogateControl(Response $response)
10039
{
@@ -104,30 +43,7 @@ public function addSurrogateControl(Response $response)
10443
}
10544

10645
/**
107-
* Checks that the Response needs to be parsed for ESI tags.
108-
*
109-
* @param Response $response A Response instance
110-
*
111-
* @return bool true if the Response needs to be parsed, false otherwise
112-
*/
113-
public function needsParsing(Response $response)
114-
{
115-
if (!$control = $response->headers->get('Surrogate-Control')) {
116-
return false;
117-
}
118-
119-
return (bool) preg_match('#content="[^"]*ESI/1.0[^"]*"#', $control);
120-
}
121-
122-
/**
123-
* Renders an ESI tag.
124-
*
125-
* @param string $uri A URI
126-
* @param string $alt An alternate URI
127-
* @param bool $ignoreErrors Whether to ignore errors or not
128-
* @param string $comment A comment to add as an esi:include tag
129-
*
130-
* @return string
46+
* {@inheritdoc}
13147
*/
13248
public function renderIncludeTag($uri, $alt = null, $ignoreErrors = true, $comment = '')
13349
{
@@ -145,12 +61,7 @@ public function renderIncludeTag($uri, $alt = null, $ignoreErrors = true, $comme
14561
}
14662

14763
/**
148-
* Replaces a Response ESI tags with the included resource content.
149-
*
150-
* @param Request $request A Request instance
151-
* @param Response $response A Response instance
152-
*
153-
* @return Response
64+
* {@inheritdoc}
15465
*/
15566
public function process(Request $request, Response $response)
15667
{
@@ -210,40 +121,4 @@ public function process(Request $request, Response $response)
210121
}
211122
}
212123
}
213-
214-
/**
215-
* Handles an ESI from the cache.
216-
*
217-
* @param HttpCache $cache An HttpCache instance
218-
* @param string $uri The main URI
219-
* @param string $alt An alternative URI
220-
* @param bool $ignoreErrors Whether to ignore errors or not
221-
*
222-
* @return string
223-
*
224-
* @throws \RuntimeException
225-
* @throws \Exception
226-
*/
227-
public function handle(HttpCache $cache, $uri, $alt, $ignoreErrors)
228-
{
229-
$subRequest = Request::create($uri, 'get', array(), $cache->getRequest()->cookies->all(), array(), $cache->getRequest()->server->all());
230-
231-
try {
232-
$response = $cache->handle($subRequest, HttpKernelInterface::SUB_REQUEST, true);
233-
234-
if (!$response->isSuccessful()) {
235-
throw new \RuntimeException(sprintf('Error when rendering "%s" (Status code is %s).', $subRequest->getUri(), $response->getStatusCode()));
236-
}
237-
238-
return $response->getContent();
239-
} catch (\Exception $e) {
240-
if ($alt) {
241-
return $this->handle($cache, $alt, '', $ignoreErrors);
242-
}
243-
244-
if (!$ignoreErrors) {
245-
throw $e;
246-
}
247-
}
248-
}
249124
}

0 commit comments

Comments
 (0)
0