diff --git a/library/Zend/Http/PhpEnvironment/Request.php b/library/Zend/Http/PhpEnvironment/Request.php index 5d3955f17d6..4a6cf81c50a 100644 --- a/library/Zend/Http/PhpEnvironment/Request.php +++ b/library/Zend/Http/PhpEnvironment/Request.php @@ -251,8 +251,13 @@ public function setServer(ParametersInterface $server) $uri = new HttpUri(); // URI scheme - $scheme = (!empty($this->serverParams['HTTPS']) - && $this->serverParams['HTTPS'] !== 'off') ? 'https' : 'http'; + if ((!empty($this->serverParams['HTTPS']) && $this->serverParams['HTTPS'] !== 'off') + || (!empty($this->serverParams['HTTP_X_FORWARDED_PROTO']) && $this->serverParams['HTTP_X_FORWARDED_PROTO'] == 'https') + ) { + $scheme = 'https'; + } else { + $scheme = 'http'; + } $uri->setScheme($scheme); // URI host & port diff --git a/tests/ZendTest/Http/PhpEnvironment/RequestTest.php b/tests/ZendTest/Http/PhpEnvironment/RequestTest.php index ef307594b69..da4848df65b 100644 --- a/tests/ZendTest/Http/PhpEnvironment/RequestTest.php +++ b/tests/ZendTest/Http/PhpEnvironment/RequestTest.php @@ -387,6 +387,18 @@ public static function serverHostnameProvider() '443', '/news', ), + // Test for HTTPS requests which are forwarded over a reverse proxy/load balancer + array( + array( + 'SERVER_NAME' => 'test.example.com', + 'SERVER_PORT' => '443', + 'HTTP_X_FORWARDED_PROTO' => 'https', + 'REQUEST_URI' => 'https://test.example.com/news', + ), + 'test.example.com', + '443', + '/news', + ), //Test when url quert contains a full http url array( @@ -416,6 +428,12 @@ public function testServerHostnameProvider(array $server, $expectedHost, $expect $host = $request->getUri()->getHost(); $this->assertEquals($expectedHost, $host); + $uriParts = parse_url($_SERVER['REQUEST_URI']); + if (isset($uriParts['scheme'])) { + $scheme = $request->getUri()->getScheme(); + $this->assertEquals($uriParts['scheme'], $scheme); + } + $port = $request->getUri()->getPort(); $this->assertEquals($expectedPort, $port);