Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c025335

Browse files
committed
Fixed surrogates could not access the original request
1 parent dd2c4c5 commit c025335

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,11 @@ public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQ
165165
// FIXME: catch exceptions and implement a 500 error page here? -> in Varnish, there is a built-in error page mechanism
166166
if (HttpKernelInterface::MASTER_REQUEST === $type) {
167167
$this->traces = array();
168-
$this->request = $request;
168+
// Keep a duplicate of the original request for surrogates so they can access it.
169+
// We must duplicate here to get a separate instance because the application will modify the request during
170+
// the application flow (we know it always does because we do ourselves by setting REMOTE_ADDR to 127.0.0.1
171+
// and adding the X-Forwarded-For header, see HttpCache::forward()).
172+
$this->request = $request->duplicate();
169173
if (null !== $this->surrogate) {
170174
$this->surrogateCacheStrategy = $this->surrogate->createCacheStrategy();
171175
}

src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@
1111

1212
namespace Symfony\Component\HttpKernel\Tests\HttpCache;
1313

14+
use Symfony\Component\HttpKernel\HttpCache\Esi;
1415
use Symfony\Component\HttpKernel\HttpCache\HttpCache;
1516
use Symfony\Component\HttpFoundation\Request;
1617
use Symfony\Component\HttpFoundation\Response;
18+
use Symfony\Component\HttpKernel\HttpCache\Store;
19+
use Symfony\Component\HttpKernel\HttpCache\StoreInterface;
1720
use Symfony\Component\HttpKernel\HttpKernelInterface;
1821

1922
/**
@@ -1465,6 +1468,43 @@ public function testDoesNotCacheOptionsRequest()
14651468
$this->assertHttpKernelIsNotCalled();
14661469
$this->assertSame('get', $this->response->getContent());
14671470
}
1471+
1472+
public function testUsesOriginalRequestForSurrogate()
1473+
{
1474+
$kernel = $this->createMock(HttpKernelInterface::class);
1475+
$store = $this->createMock(StoreInterface::class);
1476+
1477+
$kernel
1478+
->expects($this->exactly(2))
1479+
->method('handle')
1480+
->willReturnCallback(function(Request $request) {
1481+
1482+
$this->assertSame('127.0.0.1', $request->server->get('REMOTE_ADDR'));
1483+
1484+
return new Response();
1485+
});
1486+
1487+
$cache = new HttpCache($kernel,
1488+
$store,
1489+
new Esi()
1490+
);
1491+
1492+
$request = Request::create('/');
1493+
$request->server->set('REMOTE_ADDR', '10.0.0.1');
1494+
1495+
// Main request
1496+
$cache->handle($request, HttpKernelInterface::MASTER_REQUEST);
1497+
1498+
// Main request was now modified by HttpCache
1499+
// The surrogate will ask for the request using $this->cache->getRequest()
1500+
// which MUST return the original request so the surrogate
1501+
// can actually behave like a reverse proxy like e.g. Varnish would.
1502+
$this->assertSame('10.0.0.1', $cache->getRequest()->getClientIp());
1503+
$this->assertSame('10.0.0.1', $cache->getRequest()->server->get('REMOTE_ADDR'));
1504+
1505+
// Surrogate request
1506+
$cache->handle($request, HttpKernelInterface::SUB_REQUEST);
1507+
}
14681508
}
14691509

14701510
class TestKernel implements HttpKernelInterface

0 commit comments

Comments
 (0)