Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BrowserKit] Corrected HTTP_HOST logic #16265

Closed

Conversation

Naktibalda
Copy link
Contributor

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

if (!$this->history->isEmpty()) {
$server['HTTP_REFERER'] = $this->history->current()->getUri();
}

$server['HTTP_HOST'] = $this->extractHost($uri);
if (empty($server['HTTP_HOST'])) {
$server['HTTP_HOST'] = $this->extractHost($uri);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indentation is wrong, should be increments of 4 spaces.

@fabpot
Copy link
Member

fabpot commented Jan 27, 2016

Looks like a bug to me and as such should be merged in 2.3 (@Naktibalda no need to re-create another PR as I can do the switch when merging). ping @dunglas

@fabpot
Copy link
Member

fabpot commented Jan 27, 2016

👍

@dunglas
Copy link
Member

dunglas commented Jan 27, 2016

👍, but maybe should we emulate $_SERVER['SERVER_NAME'] as well (they are not the same thing): http://shiflett.org/blog/2006/mar/server-name-versus-http-host

@fabpot
Copy link
Member

fabpot commented Jan 27, 2016

@dunglas but that's a different topic.

@fabpot
Copy link
Member

fabpot commented Jan 27, 2016

Thank you @Naktibalda.

fabpot added a commit that referenced this pull request Jan 27, 2016
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
@fabpot fabpot closed this Jan 27, 2016
@fabpot fabpot mentioned this pull request Feb 3, 2016
This was referenced Feb 28, 2016
@advancingu
Copy link

This change broke our test cases for testing domain specific behavior that we were testing with

$this->request('GET', '/api/...', [], [], ['HTTP_HOST' => 'ourhost.com']);

In case anyone runs into the same issue, we now use this code for the test:

$this->request('GET', 'http://ourhost.com/api/...');

@marcw
Copy link
Contributor

marcw commented Sep 15, 2016

I ran into the same issue as @advancingu.

@jakzal
Copy link
Contributor

jakzal commented Sep 15, 2016

Please open a new issue if you think there's been a regression introduced.

fabpot added a commit that referenced this pull request Nov 26, 2018
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants