bug #16265 [BrowserKit] Corrected HTTP_HOST logic (Naktibalda)
This PR was submitted for the master branch but it was merged into the 2.3 branch instead (closes #16265).
Discussion
----------
[BrowserKit] Corrected HTTP_HOST logic
HTTP_HOST is just a header - it does not override URI,
uri does not override HTTP_HOST header.
If HTTP_HOST is not set then and only then hostname is extracted from uri.
| Q | A
| ------------- | ---
| Bug fix? | yes
| New feature? | no
| BC breaks? | yes
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets | #15398
| License | MIT
| Doc PR |
Commits
-------
b5f524d
[BrowserKit] Corrected HTTP_HOST logic #15398
This commit is contained in:
commit
7365bc0e84
@ -110,7 +110,6 @@ abstract class Client
|
||||
public function setServerParameters(array $server)
|
||||
{
|
||||
$this->server = array_merge(array(
|
||||
'HTTP_HOST' => 'localhost',
|
||||
'HTTP_USER_AGENT' => 'Symfony2 BrowserKit',
|
||||
), $server);
|
||||
}
|
||||
@ -273,21 +272,19 @@ abstract class Client
|
||||
|
||||
$uri = $this->getAbsoluteUri($uri);
|
||||
|
||||
if (!empty($server['HTTP_HOST'])) {
|
||||
$uri = preg_replace('{^(https?\://)'.preg_quote($this->extractHost($uri)).'}', '${1}'.$server['HTTP_HOST'], $uri);
|
||||
}
|
||||
$server = array_merge($this->server, $server);
|
||||
|
||||
if (isset($server['HTTPS'])) {
|
||||
$uri = preg_replace('{^'.parse_url($uri, PHP_URL_SCHEME).'}', $server['HTTPS'] ? 'https' : 'http', $uri);
|
||||
}
|
||||
|
||||
$server = array_merge($this->server, $server);
|
||||
|
||||
if (!$this->history->isEmpty()) {
|
||||
$server['HTTP_REFERER'] = $this->history->current()->getUri();
|
||||
}
|
||||
|
||||
if (empty($server['HTTP_HOST'])) {
|
||||
$server['HTTP_HOST'] = $this->extractHost($uri);
|
||||
}
|
||||
$server['HTTPS'] = 'https' == parse_url($uri, PHP_URL_SCHEME);
|
||||
|
||||
$this->internalRequest = new Request($uri, $method, $parameters, $files, $this->cookieJar->allValues($uri), $server, $content);
|
||||
|
@ -93,12 +93,14 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertEquals('http://example.com/', $client->getRequest()->getUri(), '->getCrawler() returns the Request of the last request');
|
||||
}
|
||||
|
||||
public function testGetRequestWithIpAsHost()
|
||||
public function testGetRequestWithIpAsHttpHost()
|
||||
{
|
||||
$client = new TestClient();
|
||||
$client->request('GET', 'https://example.com/foo', array(), array(), array('HTTP_HOST' => '127.0.0.1'));
|
||||
|
||||
$this->assertEquals('https://127.0.0.1/foo', $client->getRequest()->getUri());
|
||||
$this->assertEquals('https://example.com/foo', $client->getRequest()->getUri());
|
||||
$headers = $client->getRequest()->getServer();
|
||||
$this->assertEquals('127.0.0.1', $headers['HTTP_HOST']);
|
||||
}
|
||||
|
||||
public function testGetResponse()
|
||||
@ -212,24 +214,6 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertEquals('http://www.example.com/http', $client->getRequest()->getUri(), '->request() uses the previous request for relative URLs');
|
||||
}
|
||||
|
||||
public function testRequestURIConversionByServerHost()
|
||||
{
|
||||
$client = new TestClient();
|
||||
|
||||
$server = array('HTTP_HOST' => 'www.exampl+e.com:8000');
|
||||
$parameters = array();
|
||||
$files = array();
|
||||
|
||||
$client->request('GET', 'http://exampl+e.com', $parameters, $files, $server);
|
||||
$this->assertEquals('http://www.exampl+e.com:8000', $client->getRequest()->getUri(), '->request() uses HTTP_HOST to add port');
|
||||
|
||||
$client->request('GET', 'http://exampl+e.com:8888', $parameters, $files, $server);
|
||||
$this->assertEquals('http://www.exampl+e.com:8000', $client->getRequest()->getUri(), '->request() uses HTTP_HOST to modify existing port');
|
||||
|
||||
$client->request('GET', 'http://exampl+e.com:8000', $parameters, $files, $server);
|
||||
$this->assertEquals('http://www.exampl+e.com:8000', $client->getRequest()->getUri(), '->request() uses HTTP_HOST respects correct set port');
|
||||
}
|
||||
|
||||
public function testRequestReferer()
|
||||
{
|
||||
$client = new TestClient();
|
||||
@ -572,7 +556,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
||||
public function testGetServerParameter()
|
||||
{
|
||||
$client = new TestClient();
|
||||
$this->assertEquals('localhost', $client->getServerParameter('HTTP_HOST'));
|
||||
$this->assertEquals('', $client->getServerParameter('HTTP_HOST'));
|
||||
$this->assertEquals('Symfony2 BrowserKit', $client->getServerParameter('HTTP_USER_AGENT'));
|
||||
$this->assertEquals('testvalue', $client->getServerParameter('testkey', 'testvalue'));
|
||||
}
|
||||
@ -581,7 +565,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
$client = new TestClient();
|
||||
|
||||
$this->assertEquals('localhost', $client->getServerParameter('HTTP_HOST'));
|
||||
$this->assertEquals('', $client->getServerParameter('HTTP_HOST'));
|
||||
$this->assertEquals('Symfony2 BrowserKit', $client->getServerParameter('HTTP_USER_AGENT'));
|
||||
|
||||
$client->setServerParameter('HTTP_HOST', 'testhost');
|
||||
@ -595,7 +579,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
$client = new TestClient();
|
||||
|
||||
$this->assertEquals('localhost', $client->getServerParameter('HTTP_HOST'));
|
||||
$this->assertEquals('', $client->getServerParameter('HTTP_HOST'));
|
||||
$this->assertEquals('Symfony2 BrowserKit', $client->getServerParameter('HTTP_USER_AGENT'));
|
||||
|
||||
$client->request('GET', 'https://www.example.com/https/www.example.com', array(), array(), array(
|
||||
@ -605,10 +589,10 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
||||
'NEW_SERVER_KEY' => 'new-server-key-value',
|
||||
));
|
||||
|
||||
$this->assertEquals('localhost', $client->getServerParameter('HTTP_HOST'));
|
||||
$this->assertEquals('', $client->getServerParameter('HTTP_HOST'));
|
||||
$this->assertEquals('Symfony2 BrowserKit', $client->getServerParameter('HTTP_USER_AGENT'));
|
||||
|
||||
$this->assertEquals('http://testhost/https/www.example.com', $client->getRequest()->getUri());
|
||||
$this->assertEquals('http://www.example.com/https/www.example.com', $client->getRequest()->getUri());
|
||||
|
||||
$server = $client->getRequest()->getServer();
|
||||
|
||||
|
Reference in New Issue
Block a user