Skip to content

Commit

Permalink
bug #26244 [BrowserKit] fixed BC Break for HTTP_HOST header (brizzz)
Browse files Browse the repository at this point in the history
This PR was merged into the 2.8 branch.

Discussion
----------

[BrowserKit] fixed BC Break for HTTP_HOST header

| Q             | A
| ------------- | ---
| Branch?       | 2.7, 2.8, 3.x, 4.x
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes/no
| Fixed tickets | #22933
| License       | MIT
| Doc PR        | n/a

The situation well described in the original issue. I will add only:

- #10549 - makes server parameters to take precedence over URI.
- #16265 - partially revererts  #10549. Makes server parameters do not affect URI. But this is only true for `Client::request()`. It is still possible to set host for URI by `Client::setServerParameters()` when URI is realative (see examples below).

I propose a compromise solution: add to HTTP_HOST header power to override URI when it is relative.

Proposed solution:
- if the request URI is relative, then use the HTTP_HOST header passed to Client::request() to generate an absolute URI
- if the request URI is absolute, then ignore the HTTP_HOST header (as it now works)
- do the same with HTTPS server parameter

Profit:
- fix BC Break
- the documentation will be correct
  - http://symfony.com/doc/2.8/routing/hostname_pattern.html#testing-your-controllers
  - https://symfony.com/doc/2.8/testing.html#testing-configuration

Before:

```
$client->setServerParameters(['HTTP_HOST' => 'example.com']);
$client->request('GET', '/');
$this->assertEquals('http://example.com/', $client->getRequest()->getUri());

$client->request('GET', '/', [], [], ['HTTP_HOST' => 'example.com']);
$this->assertEquals('http://localhost/', $client->getRequest()->getUri());
```

Fixed (see last line):

```
$client->setServerParameters(['HTTP_HOST' => 'example.com']);
$client->request('GET', '/');
$this->assertEquals('http://example.com/', $client->getRequest()->getUri());

$client->request('GET', '/', [], [], ['HTTP_HOST' => 'example.com']);
$this->assertEquals('http://example.com/', $client->getRequest()->getUri());
```

Commits
-------

8c4a594 [BrowserKit] fixed BC Break for HTTP_HOST header; implemented same behaviour for HTTPS server parameter
  • Loading branch information
fabpot committed Nov 26, 2018
2 parents 57c1432 + 8c4a594 commit 8a60f98
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
8 changes: 7 additions & 1 deletion src/Symfony/Component/BrowserKit/Client.php
Expand Up @@ -279,11 +279,17 @@ public function request($method, $uri, array $parameters = array(), array $files
++$this->redirectCount;
}

$originalUri = $uri;

$uri = $this->getAbsoluteUri($uri);

$server = array_merge($this->server, $server);

if (isset($server['HTTPS'])) {
if (!empty($server['HTTP_HOST']) && null === parse_url($originalUri, PHP_URL_HOST)) {
$uri = preg_replace('{^(https?\://)'.preg_quote($this->extractHost($uri)).'}', '${1}'.$server['HTTP_HOST'], $uri);
}

if (isset($server['HTTPS']) && null === parse_url($originalUri, PHP_URL_SCHEME)) {
$uri = preg_replace('{^'.parse_url($uri, PHP_URL_SCHEME).'}', $server['HTTPS'] ? 'https' : 'http', $uri);
}

Expand Down
21 changes: 19 additions & 2 deletions src/Symfony/Component/BrowserKit/Tests/ClientTest.php
Expand Up @@ -638,7 +638,7 @@ public function testSetServerParameterInRequest()
$this->assertEquals('', $client->getServerParameter('HTTP_HOST'));
$this->assertEquals('Symfony2 BrowserKit', $client->getServerParameter('HTTP_USER_AGENT'));

$this->assertEquals('http://www.example.com/https/www.example.com', $client->getRequest()->getUri());
$this->assertEquals('https://www.example.com/https/www.example.com', $client->getRequest()->getUri());

$server = $client->getRequest()->getServer();

Expand All @@ -652,7 +652,24 @@ public function testSetServerParameterInRequest()
$this->assertEquals('new-server-key-value', $server['NEW_SERVER_KEY']);

$this->assertArrayHasKey('HTTPS', $server);
$this->assertFalse($server['HTTPS']);
$this->assertTrue($server['HTTPS']);
}

public function testRequestWithRelativeUri()
{
$client = new TestClient();

$client->request('GET', '/', array(), array(), array(
'HTTP_HOST' => 'testhost',
'HTTPS' => true,
));
$this->assertEquals('https://testhost/', $client->getRequest()->getUri());

$client->request('GET', 'https://www.example.com/', array(), array(), array(
'HTTP_HOST' => 'testhost',
'HTTPS' => false,
));
$this->assertEquals('https://www.example.com/', $client->getRequest()->getUri());
}

public function testInternalRequest()
Expand Down

0 comments on commit 8a60f98

Please sign in to comment.