Skip to content

Commit

Permalink
Make scheme() have a trustProxy mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Jul 8, 2012
1 parent c248470 commit 8c2a447
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
14 changes: 7 additions & 7 deletions lib/Cake/Network/Request.php
Expand Up @@ -69,7 +69,7 @@ class Request implements \ArrayAccess {
*
* @var string
*/
public $base = false;
public $base;

/**
* webroot path segment for the request.
Expand All @@ -83,7 +83,7 @@ class Request implements \ArrayAccess {
*
* @var string
*/
public $here = null;
public $here;

/**
* Whether or not to trust HTTP_X headers set by most load balancers.
Expand Down Expand Up @@ -298,14 +298,11 @@ protected static function _url($config) {
* @return array Base URL, webroot dir ending in /
*/
protected static function _base() {
$dir = $webroot = null;
$base = $dir = $webroot = null;
$config = Configure::read('App');
extract($config);

if (!isset($base)) {
$base = $this->base;
}
if ($base !== false) {
if ($base !== false && $base !== null) {
return array($base, $base . '/');
}

Expand Down Expand Up @@ -677,6 +674,9 @@ public function port() {
* @return string The scheme used for the request.
*/
public function scheme() {
if ($this->trustProxy && env('HTTP_X_FORWARDED_PROTO')) {
return env('HTTP_X_FORWARDED_PROTO');
}
return env('HTTPS') ? 'https' : 'http';
}

Expand Down
5 changes: 4 additions & 1 deletion lib/Cake/Test/TestCase/Network/RequestTest.php
Expand Up @@ -649,11 +649,14 @@ public function testDomain() {
public function testScheme() {
$_SERVER['HTTPS'] = 'on';
$request = new Request();

$this->assertEquals('https', $request->scheme());

unset($_SERVER['HTTPS']);
$this->assertEquals('http', $request->scheme());

$_SERVER['HTTP_X_FORWARDED_PROTO'] = 'https';
$request->trustProxy = true;
$this->assertEquals('https', $request->scheme());
}

/**
Expand Down

0 comments on commit 8c2a447

Please sign in to comment.