[HttpFoundation] Fixed Request::getPort returns incorrect value under IPv6

Fixed issue with Request::getPort method returning an incorrect value when the HTTP_HOST header is a IPv6 address.
This commit is contained in:
Keith Maika 2014-06-17 09:10:30 -04:00
parent 185aafadd0
commit 2a0e8e39b8
2 changed files with 15 additions and 1 deletions

View File

@ -932,7 +932,13 @@ class Request
}
if ($host = $this->headers->get('HOST')) {
if (false !== $pos = strrpos($host, ':')) {
if ($host[0] === '[') {
$pos = strpos($host, ':', strrpos($host, ']'));
} else {
$pos = strrpos($host, ':');
}
if (false !== $pos) {
return intval(substr($host, $pos + 1));
}

View File

@ -163,6 +163,14 @@ class RequestTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(90, $request->getPort());
$this->assertTrue($request->isSecure());
$request = Request::create('https://[::1]/foo');
$this->assertEquals('https://[::1]/foo', $request->getUri());
$this->assertEquals('/foo', $request->getPathInfo());
$this->assertEquals('[::1]', $request->getHost());
$this->assertEquals('[::1]', $request->getHttpHost());
$this->assertEquals(443, $request->getPort());
$this->assertTrue($request->isSecure());
$json = '{"jsonrpc":"2.0","method":"echo","id":7,"params":["Hello World"]}';
$request = Request::create('http://example.com/jsonrpc', 'POST', array(), array(), array(), array(), $json);
$this->assertEquals($json, $request->getContent());