From bb61145d2d248eea1d9b64566d00b17d2a7bec1b Mon Sep 17 00:00:00 2001 From: Jeffery Fernandez Date: Thu, 20 Feb 2014 22:27:33 +1100 Subject: [PATCH 1/2] Enabling the Request object to generate the correct scheme for SSL URI requests to a webserver through an HTTP proxy or load balance HTTP_X_FORWARDED_PROTO request header is sent by loadbalancers insted of HTTPS to indicate the URI was requested via a secure connection. An example of this useage is Amazon Elastic Beanstalk applications which can sit behind a load balancer (when autoscaling) --- library/Zend/Http/PhpEnvironment/Request.php | 10 ++++++++-- .../Http/PhpEnvironment/RequestTest.php | 18 ++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/library/Zend/Http/PhpEnvironment/Request.php b/library/Zend/Http/PhpEnvironment/Request.php index 5d3955f17d6..3cd4f56a7c8 100644 --- a/library/Zend/Http/PhpEnvironment/Request.php +++ b/library/Zend/Http/PhpEnvironment/Request.php @@ -251,8 +251,14 @@ 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); From ef7cc337fa81ab54903845f329db5eb4ff68e501 Mon Sep 17 00:00:00 2001 From: Jeffery Fernandez Date: Thu, 20 Feb 2014 23:08:41 +1100 Subject: [PATCH 2/2] Fixing php coding style errors --- library/Zend/Http/PhpEnvironment/Request.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/library/Zend/Http/PhpEnvironment/Request.php b/library/Zend/Http/PhpEnvironment/Request.php index 3cd4f56a7c8..4a6cf81c50a 100644 --- a/library/Zend/Http/PhpEnvironment/Request.php +++ b/library/Zend/Http/PhpEnvironment/Request.php @@ -255,9 +255,8 @@ public function setServer(ParametersInterface $server) || (!empty($this->serverParams['HTTP_X_FORWARDED_PROTO']) && $this->serverParams['HTTP_X_FORWARDED_PROTO'] == 'https') ) { $scheme = 'https'; - } - else { - $scheme = 'http'; + } else { + $scheme = 'http'; } $uri->setScheme($scheme);