Skip to content
Permalink
Browse files

Fix _ssl and _full not working well together.

When _ssl=true and _full=true we should generate URLs with SSL
protocols, and not HTTP ones.

Add tests to cover cases described in #11583 & #11582
  • Loading branch information...
markstory committed Jan 31, 2018
1 parent 63aaf06 commit 82acd2beef98f2b4adcd46a8335a865d86d3a759
Showing with 49 additions and 11 deletions.
  1. +9 −11 src/Routing/Router.php
  2. +40 −0 tests/TestCase/Routing/RouterTest.php
@@ -618,19 +618,16 @@ public static function url($url = null, $full = false)
}
if (is_array($url)) {
if (isset($url['_ssl'])) {
if (!isset($url['_full'])) {
$url['_scheme'] = ($url['_ssl'] === true) ? 'https' : 'http';
}
unset($url['_ssl']);
$url['_scheme'] = ($url['_ssl'] === true) ? 'https' : 'http';
}
if (isset($url['_full']) && $url['_full'] === true) {
$full = true;
unset($url['_full']);
}
if (isset($url['#'])) {
$frag = '#' . $url['#'];
unset($url['#']);
}
unset($url['_ssl'], $url['_full'], $url['#']);
$url = static::_applyUrlFilters($url);
@@ -655,6 +652,12 @@ public static function url($url = null, $full = false)
];
}
// If a full URL is requested with a scheme the host should default
// to App.fullBaseUrl to avoid corrupt URLs
if ($full && isset($url['_scheme']) && !isset($url['_host'])) {
$url['_host'] = parse_url(static::fullBaseUrl(), PHP_URL_HOST);
}
$output = static::$_collection->match($url, static::$_requestContext + ['params' => $params]);
} else {
$plainString = (
@@ -681,11 +684,6 @@ public static function url($url = null, $full = false)
}
}
$protocolError = preg_match('#^[a-z][a-z0-9+\-.]*\:///#i', $output);
if ($protocolError === 1) {
$output = str_replace(':///', '://', $output);
}
return $output . $frag;
}
@@ -2059,6 +2059,46 @@ public function testGenerationWithSslOption()
$this->assertEquals('http://localhost/images/index', $result);
}
/**
* Test that the _ssl + _full options work together.
*
* @return void
*/
public function testGenerationWithSslAndFullOption()
{
Configure::write('App.fullBaseUrl', 'http://app.localhost');
Router::connect('/:controller/:action/*');
$request = new ServerRequest([
'environment' => ['HTTP_HOST' => 'localhost']
]);
Router::pushRequest($request);
$result = Router::url([
'controller' => 'images',
'action' => 'index',
'_ssl' => true,
'_full' => true
]);
$this->assertEquals('https://app.localhost/images/index', $result);
$result = Router::url([
'controller' => 'images',
'action' => 'index',
'_ssl' => false,
'_full' => true,
]);
$this->assertEquals('http://app.localhost/images/index', $result);
$result = Router::url([
'controller' => 'images',
'action' => 'index',
'_full' => false,
'_ssl' => false
]);
$this->assertEquals('http://localhost/images/index', $result);
}
/**
* Test ssl option when the current request is ssl.
*

0 comments on commit 82acd2b

Please sign in to comment.
You can’t perform that action at this time.