Skip to content

[BrowserKit] Fixed server HTTP_HOST port uri conversion #11356

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

Closed
wants to merge 1 commit into from

Conversation

bcremer
Copy link
Contributor

@bcremer bcremer commented Jul 9, 2014

Q A
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets n/a
License MIT
Doc PR n/a

This will fix URI conversion using the HTTP_HOST server header.

Steps to reproduce:

$client = new TestClient();
$server = array('HTTP_HOST' => 'example.com:8000');
$client->request('GET', 'http://example.com:8000', [], [], $server);

echo $client->getRequest()->getUri(); 
// outputs:  http://example.com:8000:8000
// expected: http://example.com:8000

We discovered this issue during mink testing using goutte on a non standard port.

@stof
Copy link
Member

stof commented Jul 9, 2014

The issue is that you specify HTTP_HOST the wrong way. HTTP_HOST does not include the port in PHP. the port is in SERVER_PORT

@stof
Copy link
Member

stof commented Jul 9, 2014

hmm, actually, it does include it. It is SERVER_NAME which does not include it.

@bcremer
Copy link
Contributor Author

bcremer commented Jul 9, 2014

So this means this commit should be reverted?
This would also fix the problem for us.

We stumbled upon this error using goutte`s history functions:

$client = new \Goutte\Client();

$client->request('GET', 'http://localhost:8080/a.html');
$client->request('GET', 'http://localhost:8080/b.html');

echo $client->getHistory()->current()->getServer()['HTTP_HOST'];
// outputs  localhost:8080

// this fails
$client->back();

@bcremer
Copy link
Contributor Author

bcremer commented Jul 10, 2014

Any news here, or anything I can help to get this merged?

@@ -304,7 +304,15 @@ public function request($method, $uri, array $parameters = array(), array $files
$uri = $this->getAbsoluteUri($uri);

if (isset($server['HTTP_HOST'])) {
$uri = preg_replace('{^(https?\://)'.parse_url($uri, PHP_URL_HOST).'}', '${1}'.$server['HTTP_HOST'], $uri);
if ($port = parse_url($uri, PHP_URL_PORT)) {
$port = ':' . $port;
Copy link
Member

Choose a reason for hiding this comment

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

Can you remove the spaces around the dot?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

@fabpot
Copy link
Member

fabpot commented Jul 23, 2014

👍

1 similar comment
@romainneutron
Copy link
Contributor

👍

$port = ':'.$port;
}

$uri = preg_replace('{^(https?\://)'.parse_url($uri, PHP_URL_HOST).$port.'}', '${1}'.$server['HTTP_HOST'], $uri);
Copy link
Member

Choose a reason for hiding this comment

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

a preg_quote is required around parse_url($uri, PHP_URL_HOST).$port, because:

var_dump(parse_url('abc.+def:9090', PHP_URL_HOST));
string(8) "abc.+def"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@nicolas-grekas
I'm not sure if I can follow you.
You propose to change the code to:

if ($port = parse_url($uri, PHP_URL_PORT)) {
    $port = ':'.$port;
}

$uri = preg_replace('{^(https?\://)'.preg_quote(parse_url($uri, PHP_URL_HOST).$port).'}', '${1}'.$server['HTTP_HOST'], $uri);

May you please provide me with a failing testcase like the following so I can verify the problem?

$client = new TestClient();

$server = array('HTTP_HOST' => 'www.example.com:8000');
$parameters = array();
$files = array();

$client->request('GET', 'http://example.com', $parameters, $files, $server);
$this->assertEquals('http://www.example.com:8000', $client->getRequest()->getUri(), '->request() uses HTTP_HOST to add port');

@fabpot
Copy link
Member

fabpot commented Jul 25, 2014

Closing in favor of #11469

@fabpot fabpot closed this Jul 25, 2014
fabpot added a commit that referenced this pull request Jul 25, 2014
…cremer, fabpot)

This PR was merged into the 2.3 branch.

Discussion
----------

 [BrowserKit] Fixed server HTTP_HOST port uri conversion

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #11356
| License       | MIT
| Doc PR        | n/a

See #11356

Commits
-------

103fd88 [BrowserKit] refactor code and fix unquoted regex
f401ab9 Fixed server HTTP_HOST port uri conversion
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.

5 participants