|
0
|
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 |
* Esi implements the ESI capabilities to Request and Response instances. |
|
|
20 |
* |
|
|
21 |
* For more information, read the following W3C notes: |
|
|
22 |
* |
|
|
23 |
* * ESI Language Specification 1.0 (http://www.w3.org/TR/esi-lang) |
|
|
24 |
* |
|
|
25 |
* * Edge Architecture Specification (http://www.w3.org/TR/edge-arch) |
|
|
26 |
* |
|
|
27 |
* @author Fabien Potencier <fabien@symfony.com> |
|
|
28 |
*/ |
|
|
29 |
class Esi |
|
|
30 |
{ |
|
|
31 |
private $contentTypes; |
|
|
32 |
|
|
|
33 |
/** |
|
|
34 |
* Constructor. |
|
|
35 |
* |
|
|
36 |
* @param array $contentTypes An array of content-type that should be parsed for ESI information. |
|
|
37 |
* (default: text/html, text/xml, and application/xml) |
|
|
38 |
*/ |
|
|
39 |
public function __construct(array $contentTypes = array('text/html', 'text/xml', 'application/xml')) |
|
|
40 |
{ |
|
|
41 |
$this->contentTypes = $contentTypes; |
|
|
42 |
} |
|
|
43 |
|
|
|
44 |
/** |
|
|
45 |
* Returns a new cache strategy instance. |
|
|
46 |
* |
|
|
47 |
* @return EsiResponseCacheStrategyInterface A EsiResponseCacheStrategyInterface instance |
|
|
48 |
*/ |
|
|
49 |
public function createCacheStrategy() |
|
|
50 |
{ |
|
|
51 |
return new EsiResponseCacheStrategy(); |
|
|
52 |
} |
|
|
53 |
|
|
|
54 |
/** |
|
|
55 |
* Checks that at least one surrogate has ESI/1.0 capability. |
|
|
56 |
* |
|
|
57 |
* @param Request $request A Request instance |
|
|
58 |
* |
|
|
59 |
* @return Boolean true if one surrogate has ESI/1.0 capability, false otherwise |
|
|
60 |
*/ |
|
|
61 |
public function hasSurrogateEsiCapability(Request $request) |
|
|
62 |
{ |
|
|
63 |
if (null === $value = $request->headers->get('Surrogate-Capability')) { |
|
|
64 |
return false; |
|
|
65 |
} |
|
|
66 |
|
|
|
67 |
return (Boolean) preg_match('#ESI/1.0#', $value); |
|
|
68 |
} |
|
|
69 |
|
|
|
70 |
/** |
|
|
71 |
* Adds ESI/1.0 capability to the given Request. |
|
|
72 |
* |
|
|
73 |
* @param Request $request A Request instance |
|
|
74 |
*/ |
|
|
75 |
public function addSurrogateEsiCapability(Request $request) |
|
|
76 |
{ |
|
|
77 |
$current = $request->headers->get('Surrogate-Capability'); |
|
|
78 |
$new = 'symfony2="ESI/1.0"'; |
|
|
79 |
|
|
|
80 |
$request->headers->set('Surrogate-Capability', $current ? $current.', '.$new : $new); |
|
|
81 |
} |
|
|
82 |
|
|
|
83 |
/** |
|
|
84 |
* Adds HTTP headers to specify that the Response needs to be parsed for ESI. |
|
|
85 |
* |
|
|
86 |
* This method only adds an ESI HTTP header if the Response has some ESI tags. |
|
|
87 |
* |
|
|
88 |
* @param Response $response A Response instance |
|
|
89 |
*/ |
|
|
90 |
public function addSurrogateControl(Response $response) |
|
|
91 |
{ |
|
|
92 |
if (false !== strpos($response->getContent(), '<esi:include')) { |
|
|
93 |
$response->headers->set('Surrogate-Control', 'content="ESI/1.0"'); |
|
|
94 |
} |
|
|
95 |
} |
|
|
96 |
|
|
|
97 |
/** |
|
|
98 |
* Checks that the Response needs to be parsed for ESI tags. |
|
|
99 |
* |
|
|
100 |
* @param Response $response A Response instance |
|
|
101 |
* |
|
|
102 |
* @return Boolean true if the Response needs to be parsed, false otherwise |
|
|
103 |
*/ |
|
|
104 |
public function needsEsiParsing(Response $response) |
|
|
105 |
{ |
|
|
106 |
if (!$control = $response->headers->get('Surrogate-Control')) { |
|
|
107 |
return false; |
|
|
108 |
} |
|
|
109 |
|
|
|
110 |
return (Boolean) preg_match('#content="[^"]*ESI/1.0[^"]*"#', $control); |
|
|
111 |
} |
|
|
112 |
|
|
|
113 |
/** |
|
|
114 |
* Renders an ESI tag. |
|
|
115 |
* |
|
|
116 |
* @param string $uri A URI |
|
|
117 |
* @param string $alt An alternate URI |
|
|
118 |
* @param Boolean $ignoreErrors Whether to ignore errors or not |
|
|
119 |
* @param string $comment A comment to add as an esi:include tag |
|
|
120 |
*/ |
|
|
121 |
public function renderIncludeTag($uri, $alt = null, $ignoreErrors = true, $comment = '') |
|
|
122 |
{ |
|
|
123 |
$html = sprintf('<esi:include src="%s"%s%s />', |
|
|
124 |
$uri, |
|
|
125 |
$ignoreErrors ? ' onerror="continue"' : '', |
|
|
126 |
$alt ? sprintf(' alt="%s"', $alt) : '' |
|
|
127 |
); |
|
|
128 |
|
|
|
129 |
if (!empty($comment)) { |
|
|
130 |
return sprintf("<esi:comment text=\"%s\" />\n%s", $comment, $html); |
|
|
131 |
} |
|
|
132 |
|
|
|
133 |
return $html; |
|
|
134 |
} |
|
|
135 |
|
|
|
136 |
/** |
|
|
137 |
* Replaces a Response ESI tags with the included resource content. |
|
|
138 |
* |
|
|
139 |
* @param Request $request A Request instance |
|
|
140 |
* @param Response $response A Response instance |
|
|
141 |
*/ |
|
|
142 |
public function process(Request $request, Response $response) |
|
|
143 |
{ |
|
|
144 |
$this->request = $request; |
|
|
145 |
$type = $response->headers->get('Content-Type'); |
|
|
146 |
if (empty($type)) { |
|
|
147 |
$type = 'text/html'; |
|
|
148 |
} |
|
|
149 |
|
|
|
150 |
$parts = explode(';', $type); |
|
|
151 |
if (!in_array($parts[0], $this->contentTypes)) { |
|
|
152 |
return $response; |
|
|
153 |
} |
|
|
154 |
|
|
|
155 |
// we don't use a proper XML parser here as we can have ESI tags in a plain text response |
|
|
156 |
$content = $response->getContent(); |
|
|
157 |
$content = preg_replace_callback('#<esi\:include\s+(.*?)\s*/>#', array($this, 'handleEsiIncludeTag'), $content); |
|
|
158 |
$content = preg_replace('#<esi\:comment[^>]*/>#', '', $content); |
|
|
159 |
$content = preg_replace('#<esi\:remove>.*?</esi\:remove>#', '', $content); |
|
|
160 |
|
|
|
161 |
$response->setContent($content); |
|
|
162 |
$response->headers->set('X-Body-Eval', 'ESI'); |
|
|
163 |
|
|
|
164 |
// remove ESI/1.0 from the Surrogate-Control header |
|
|
165 |
if ($response->headers->has('Surrogate-Control')) { |
|
|
166 |
$value = $response->headers->get('Surrogate-Control'); |
|
|
167 |
if ('content="ESI/1.0"' == $value) { |
|
|
168 |
$response->headers->remove('Surrogate-Control'); |
|
|
169 |
} elseif (preg_match('#,\s*content="ESI/1.0"#', $value)) { |
|
|
170 |
$response->headers->set('Surrogate-Control', preg_replace('#,\s*content="ESI/1.0"#', '', $value)); |
|
|
171 |
} elseif (preg_match('#content="ESI/1.0",\s*#', $value)) { |
|
|
172 |
$response->headers->set('Surrogate-Control', preg_replace('#content="ESI/1.0",\s*#', '', $value)); |
|
|
173 |
} |
|
|
174 |
} |
|
|
175 |
} |
|
|
176 |
|
|
|
177 |
/** |
|
|
178 |
* Handles an ESI from the cache. |
|
|
179 |
* |
|
|
180 |
* @param HttpCache $cache An HttpCache instance |
|
|
181 |
* @param string $uri The main URI |
|
|
182 |
* @param string $alt An alternative URI |
|
|
183 |
* @param Boolean $ignoreErrors Whether to ignore errors or not |
|
|
184 |
*/ |
|
|
185 |
public function handle(HttpCache $cache, $uri, $alt, $ignoreErrors) |
|
|
186 |
{ |
|
|
187 |
$subRequest = Request::create($uri, 'get', array(), $cache->getRequest()->cookies->all(), array(), $cache->getRequest()->server->all()); |
|
|
188 |
|
|
|
189 |
try { |
|
|
190 |
$response = $cache->handle($subRequest, HttpKernelInterface::SUB_REQUEST, true); |
|
|
191 |
|
|
|
192 |
if (!$response->isSuccessful()) { |
|
|
193 |
throw new \RuntimeException(sprintf('Error when rendering "%s" (Status code is %s).', $subRequest->getUri(), $response->getStatusCode())); |
|
|
194 |
} |
|
|
195 |
|
|
|
196 |
return $response->getContent(); |
|
|
197 |
} catch (\Exception $e) { |
|
|
198 |
if ($alt) { |
|
|
199 |
return $this->handle($cache, $alt, '', $ignoreErrors); |
|
|
200 |
} |
|
|
201 |
|
|
|
202 |
if (!$ignoreErrors) { |
|
|
203 |
throw $e; |
|
|
204 |
} |
|
|
205 |
} |
|
|
206 |
} |
|
|
207 |
|
|
|
208 |
/** |
|
|
209 |
* Handles an ESI include tag (called internally). |
|
|
210 |
* |
|
|
211 |
* @param array $attributes An array containing the attributes. |
|
|
212 |
* |
|
|
213 |
* @return string The response content for the include. |
|
|
214 |
*/ |
|
|
215 |
private function handleEsiIncludeTag($attributes) |
|
|
216 |
{ |
|
|
217 |
$options = array(); |
|
|
218 |
preg_match_all('/(src|onerror|alt)="([^"]*?)"/', $attributes[1], $matches, PREG_SET_ORDER); |
|
|
219 |
foreach ($matches as $set) { |
|
|
220 |
$options[$set[1]] = $set[2]; |
|
|
221 |
} |
|
|
222 |
|
|
|
223 |
if (!isset($options['src'])) { |
|
|
224 |
throw new \RuntimeException('Unable to process an ESI tag without a "src" attribute.'); |
|
|
225 |
} |
|
|
226 |
|
|
|
227 |
return sprintf('<?php echo $this->esi->handle($this, \'%s\', \'%s\', %s) ?>'."\n", |
|
|
228 |
$options['src'], |
|
|
229 |
isset($options['alt']) ? $options['alt'] : null, |
|
|
230 |
isset($options['onerror']) && 'continue' == $options['onerror'] ? 'true' : 'false' |
|
|
231 |
); |
|
|
232 |
} |
|
|
233 |
} |