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
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/Symfony/Component/BrowserKit/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,11 @@ 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;
}

$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');

}

if (isset($server['HTTPS'])) {
Expand Down
18 changes: 18 additions & 0 deletions src/Symfony/Component/BrowserKit/Tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,24 @@ public function testRequestURIConversion()
$this->assertEquals('http://www.example.com/foo/bar', $client->getRequest()->getUri(), '->request() uses the previous request for relative URLs');
}

public function testRequestURIConversionByServerHost()
{
$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');

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

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

public function testRequestReferer()
{
$client = new TestClient();
Expand Down