Merge branch '2.1' into 2.2

* 2.1:
  Fix getPort() returning 80 instead of 443 when X-FORWARDED-PROTO is set to https
This commit is contained in:
Fabien Potencier 2013-04-30 19:05:10 +02:00
commit 01ff0765b2
2 changed files with 43 additions and 2 deletions

View File

@ -837,8 +837,14 @@ class Request
*/
public function getPort()
{
if (self::$trustProxy && self::$trustedHeaders[self::HEADER_CLIENT_PORT] && $port = $this->headers->get(self::$trustedHeaders[self::HEADER_CLIENT_PORT])) {
return $port;
if (self::$trustProxy) {
if (self::$trustedHeaders[self::HEADER_CLIENT_PORT] && $port = $this->headers->get(self::$trustedHeaders[self::HEADER_CLIENT_PORT])) {
return $port;
}
if (self::$trustedHeaders[self::HEADER_CLIENT_PROTO] && 'https' === $this->headers->get(self::$trustedHeaders[self::HEADER_CLIENT_PROTO], 'http')) {
return 443;
}
}
return $this->server->get('SERVER_PORT');

View File

@ -678,6 +678,41 @@ class RequestTest extends \PHPUnit_Framework_TestCase
$this->stopTrustingProxyData();
}
public function testGetPort()
{
$request = Request::create('http://example.com', 'GET', array(), array(), array(), array(
'HTTP_X_FORWARDED_PROTO' => 'https',
'HTTP_X_FORWARDED_PORT' => '443'
));
$port = $request->getPort();
$this->assertEquals(80, $port, 'Without trusted proxies FORWARDED_PROTO and FORWARDED_PORT are ignored.');
Request::setTrustedProxies(array('1.1.1.1'));
$request = Request::create('http://example.com', 'GET', array(), array(), array(), array(
'HTTP_X_FORWARDED_PROTO' => 'https',
'HTTP_X_FORWARDED_PORT' => '8443'
));
$port = $request->getPort();
$this->assertEquals(8443, $port, 'With PROTO and PORT set PORT takes precedence.');
$request = Request::create('http://example.com', 'GET', array(), array(), array(), array(
'HTTP_X_FORWARDED_PROTO' => 'https'
));
$port = $request->getPort();
$this->assertEquals(443, $port, 'With only PROTO set getPort() defaults to 443.');
$request = Request::create('http://example.com', 'GET', array(), array(), array(), array(
'HTTP_X_FORWARDED_PROTO' => 'http'
));
$port = $request->getPort();
$this->assertEquals(80, $port, 'If X_FORWARDED_PROTO is set to http return 80.');
Request::setTrustedProxies(array());
}
/**
* @expectedException RuntimeException
*/