Basics
I made a very simple test action:
/**
* @Route("/public/debug/varnish", name="debug_varnish")
* @Template
*/
public function varnishAction()
{
return [];
}
{# varnish.html.twig #}
<html>
<body>
<h1>Layout</h1>
<hr />
{{ render_esi(controller('TestBundle:Debug:esi')) }}
</body>
</html>
The included action and twig isn't important now.
The test
The Varnish needs the Surrogate-Control: content="ESI/1.0"
header in the response. Look what happens:
curl -H 'host:www.domain.com' -H 'Surrogate-Capability: abc=ESI/1.0' http://127.0.0.1:8080/public/debug/varnish
The response:
<html>
<body>
<h1>Layout</h1>
<hr />
<esi:include src="/_proxy?_path=_format%3Dhtml%26_locale%3Den_US%26_controller%3DTestBundle%253ADebug%253Aesi" onerror="continue" />
</body>
</html>
Everythings alright! But look the headers:
curl -I -H 'host:www.domain.com' -H 'Surrogate-Capability: abc=ESI/1.0' http://127.0.0.1:8080/public/debug/varnish
The output:
HTTP/1.1 200 OK
Server: nginx/1.2.1
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.5.9-1~dotdeb.1
Set-Cookie: PHPSESSID=k8jii3gkrha1js7edbbbqjjh32; path=/
Cache-Control: no-cache
Date: Tue, 18 Feb 2014 11:21:04 GMT
There isn't Surrogate-Control
header!
\Symfony\Component\HttpKernel\HttpCache\Esi class
There is a very-very simple function, which add Surrogate-Control
header, if content contains <esi:include
string:
/**
* Adds HTTP headers to specify that the Response needs to be parsed for ESI.
*
* This method only adds an ESI HTTP header if the Response has some ESI tags.
*
* @param Response $response A Response instance
*/
public function addSurrogateControl(Response $response)
{
if (false !== strpos($response->getContent(), '<esi:include')) {
$response->headers->set('Surrogate-Control', 'content="ESI/1.0"');
}
}
But it doesn't! The if is "true". When I call with this:
public function addSurrogateControl(Response $response)
{
var_dump(false !== strpos($response->getContent(), '<esi:include'));
if (false !== strpos($response->getContent(), '<esi:include')) {
echo "before add header\n";
$response->headers->set('Surrogate-Control', 'content="ESI/1.0"');
echo "after add header\n";
}
echo "after if"
}
curl -H 'host:www.domain.com' -H 'Surrogate-Capability: abc=ESI/1.0' http://127.0.0.1:8080/public/debug/varnish
The response:
(bool) true
before add header
after add header
after if
<html>
<body>
<h1>Layout</h1>
<hr />
<esi:include src="/_proxy?_path=_format%3Dhtml%26_locale%3Den_US%26_controller%3DTestBundle%253ADebug%253Aesi" onerror="continue" />
</body>
</html>
curl -I -H 'host:www.domain.com' -H 'Surrogate-Capability: abc=ESI/1.0' http://127.0.0.1:8080/public/debug/varnish
The output:
HTTP/1.1 200 OK
Server: nginx/1.2.1
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.5.9-1~dotdeb.1
Set-Cookie: PHPSESSID=k8jii3gkrha1js7edbbbqjjh32; path=/
Cache-Control: no-cache
Date: Tue, 18 Feb 2014 11:21:04 GMT
WHERE IS THE Surrogate-Control
HEADER? :-o
More tests:
public function addSurrogateControl(Response $response)
{
// [1]
if (false !== strpos($response->getContent(), '<esi:include')) {
// [2]
}
// [3]
}
With $response->headers->set('Surrogate-Control', 'content="ESI/1.0"');
and (!!!) header('Test: test OK')
:
- [1] place: WORK!
- [2] original place: doesn't work :(
- [3] place: WORK!
Ok. So if ANY header modification is in the IF
, it doesn't work. Why?
The magic
When I change the $response->getContent()
with 'test <esi:include test'
in the if
condition, then everthing works fine. The Response
class getContent()
function:
public function getContent()
{
return $this->content;
}
I don't understand :-/