Skip to content
This repository has been archived by the owner on Jan 31, 2020. It is now read-only.

Commit

Permalink
Merge pull request zendframework/zendframework#7233 from rstgroup/master
Browse files Browse the repository at this point in the history
Detect https on reversed proxy
  • Loading branch information
weierophinney committed Feb 25, 2015
2 parents 7a3db60 + e66c3d4 commit 47a9e6c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Helper/ServerUrl.php
Expand Up @@ -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;
}
Expand All @@ -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:
Expand All @@ -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
*
Expand Down
10 changes: 10 additions & 0 deletions test/Helper/ServerUrlTest.php
Expand Up @@ -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';
Expand Down

0 comments on commit 47a9e6c

Please sign in to comment.