added support for the X-Forwarded-For header (closes #6982, closes #7000)

This commit is contained in:
Fabien Potencier 2013-02-09 21:11:21 +01:00
parent 6a9c5102b2
commit 42d3c4c9ca
2 changed files with 32 additions and 0 deletions

View File

@ -413,6 +413,14 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
$subRequest->headers->remove('if_modified_since');
$subRequest->headers->remove('if_none_match');
// modify the X-Forwarded-For header if needed
$forwardedFor = $subRequest->headers->get('X-Forwarded-For');
if ($forwardedFor) {
$subRequest->headers->set('X-Forwarded-For', $forwardedFor.', '.$subRequest->server->get('REMOTE_ADDR'));
} else {
$subRequest->headers->set('X-Forwarded-For', $subRequest->server->get('REMOTE_ADDR'));
}
// fix the client IP address by setting it to 127.0.0.1 as HttpCache
// is always called from the same process as the backend.
$subRequest->server->set('REMOTE_ADDR', '127.0.0.1');

View File

@ -1042,4 +1042,28 @@ class HttpCacheTest extends HttpCacheTestCase
$this->assertEquals('127.0.0.1', $this->kernel->getBackendRequest()->server->get('REMOTE_ADDR'));
}
/**
* @dataProvider getXForwardedForData
*/
public function testXForwarderForHeaderForForwardedRequests($xForwardedFor, $expected)
{
$this->setNextResponse();
$server = array('REMOTE_ADDR' => '10.0.0.1');
if (false !== $xForwardedFor) {
$server['HTTP_X_FORWARDED_FOR'] = $xForwardedFor;
}
$this->request('GET', '/', $server);
$this->assertEquals($expected, $this->kernel->getBackendRequest()->headers->get('X-Forwarded-For'));
}
public function getXForwardedForData()
{
return array(
array(false, '10.0.0.1'),
array('10.0.0.2', '10.0.0.2, 10.0.0.1'),
array('10.0.0.2, 10.0.0.3', '10.0.0.2, 10.0.0.3, 10.0.0.1'),
);
}
}