Skip to content

Commit

Permalink
[HttpFoundation] Fixed Request::getPort returns incorrect value under…
Browse files Browse the repository at this point in the history
… IPv6

Fixed issue with Request::getPort method returning an incorrect value when the HTTP_HOST header is a IPv6 address.
  • Loading branch information
kicken committed Jun 17, 2014
1 parent 185aafa commit 2a0e8e3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Symfony/Component/HttpFoundation/Request.php
Expand Up @@ -932,7 +932,13 @@ public function getPort()
}

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));
}

Expand Down
8 changes: 8 additions & 0 deletions src/Symfony/Component/HttpFoundation/Tests/RequestTest.php
Expand Up @@ -163,6 +163,14 @@ public function testCreate()
$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());
Expand Down

0 comments on commit 2a0e8e3

Please sign in to comment.