Skip to content

Commit

Permalink
Enabling the Request object to generate the correct scheme for SSL UR…
Browse files Browse the repository at this point in the history
…I 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)
  • Loading branch information
jeffery committed Feb 20, 2014
1 parent c83d831 commit bb61145
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
10 changes: 8 additions & 2 deletions library/Zend/Http/PhpEnvironment/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions tests/ZendTest/Http/PhpEnvironment/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit bb61145

Please sign in to comment.