diff --git a/src/Helper/ServerUrl.php b/src/Helper/ServerUrl.php index 2cd80efd..0e11baa2 100644 --- a/src/Helper/ServerUrl.php +++ b/src/Helper/ServerUrl.php @@ -112,6 +112,10 @@ protected function detectPort() } if (isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT']) { + if ($this->isReversedProxy()) { + $this->setPort(443); + return; + } $this->setPort($_SERVER['SERVER_PORT']); return; } @@ -132,6 +136,7 @@ protected function detectScheme() case (isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] === true)): case (isset($_SERVER['HTTP_SCHEME']) && ($_SERVER['HTTP_SCHEME'] == 'https')): case (443 === $this->getPort()): + case $this->isReversedProxy(): $scheme = 'https'; break; default: @@ -142,6 +147,11 @@ protected function detectScheme() $this->setScheme($scheme); } + protected function isReversedProxy() + { + return isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https'; + } + /** * Detect if a proxy is in use, and, if so, set the host based on it * diff --git a/test/Helper/ServerUrlTest.php b/test/Helper/ServerUrlTest.php index a5121515..7245a82d 100644 --- a/test/Helper/ServerUrlTest.php +++ b/test/Helper/ServerUrlTest.php @@ -87,6 +87,16 @@ public function testConstructorWithHostIncludingPortAndHttpsTrue() $this->assertEquals('https://example.com:8181', $url->__invoke()); } + public function testConstructorWithHostReversedProxyHttpsTrue() + { + $_SERVER['HTTP_HOST'] = 'example.com'; + $_SERVER['HTTP_X_FORWARDED_PROTO'] = 'https'; + $_SERVER['SERVER_PORT'] = 80; + + $url = new Helper\ServerUrl(); + $this->assertEquals('https://example.com', $url->__invoke()); + } + public function testConstructorWithHttpHostIncludingPortAndPortSet() { $_SERVER['HTTP_HOST'] = 'example.com:8181';