From 60aa50e821137e4d739a30549a52aa3d9fc5dc45 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Fri, 20 May 2016 00:34:23 +0200 Subject: [PATCH] Route, SimpleRouter: by default keeps the currently used HTTP/HTTPS protocol (BC break) [Closes nette/nette#1196][Closes nette/routing#14] --- src/Routing/Route.php | 31 ++++++++++-------- src/Routing/SimpleRouter.php | 2 +- tests/Route/secured.phpt | 2 +- tests/Route/withHost.secured.phpt | 52 +++++++++++++++++++++++++++++++ tests/SimpleRouter/basic.phpt | 2 +- 5 files changed, 73 insertions(+), 16 deletions(-) create mode 100644 tests/Route/withHost.secured.phpt diff --git a/src/Routing/Route.php b/src/Routing/Route.php index 2777fb5b..058f8c9e 100644 --- a/src/Routing/Route.php +++ b/src/Routing/Route.php @@ -384,25 +384,30 @@ public function constructUrl(Application\Request $appRequest, Nette\Http\Url $re } while (TRUE); - if ($this->type !== self::HOST) { + if ($this->type === self::HOST) { + $host = $refUrl->getHost(); + $parts = ip2long($host) ? [$host] : array_reverse(explode('.', $host)); + $url = strtr($url, [ + '/%basePath%/' => $refUrl->getBasePath(), + '%tld%' => $parts[0], + '%domain%' => isset($parts[1]) ? "$parts[1].$parts[0]" : $parts[0], + '%sld%' => isset($parts[1]) ? $parts[1] : '', + ]); + if ($this->flags & self::SECURED) { + $url = 'https:' . $url; + } elseif (strncmp($url, "//$host/", strlen($host) + 3) === 0) { + $url = $refUrl->getScheme() . ':' . $url; + } else { + $url = 'http:' . $url; + } + } else { if ($this->lastRefUrl !== $refUrl) { - $scheme = ($this->flags & self::SECURED ? 'https://' : 'http://'); + $scheme = ($this->flags & self::SECURED ? 'https://' : $refUrl->getScheme() . '://'); $basePath = ($this->type === self::RELATIVE ? $refUrl->getBasePath() : ''); $this->lastBaseUrl = $scheme . $refUrl->getAuthority() . $basePath; $this->lastRefUrl = $refUrl; } $url = $this->lastBaseUrl . $url; - - } else { - $host = $refUrl->getHost(); - $host = ip2long($host) ? [$host] : array_reverse(explode('.', $host)); - $url = strtr($url, [ - '/%basePath%/' => $refUrl->getBasePath(), - '%tld%' => $host[0], - '%domain%' => isset($host[1]) ? "$host[1].$host[0]" : $host[0], - '%sld%' => isset($host[1]) ? $host[1] : '', - ]); - $url = ($this->flags & self::SECURED ? 'https:' : 'http:') . $url; } if (strpos($url, '//', 7) !== FALSE) { diff --git a/src/Routing/SimpleRouter.php b/src/Routing/SimpleRouter.php index c75392f0..8ae7e417 100644 --- a/src/Routing/SimpleRouter.php +++ b/src/Routing/SimpleRouter.php @@ -118,7 +118,7 @@ public function constructUrl(Application\Request $appRequest, Nette\Http\Url $re } } - $url = ($this->flags & self::SECURED ? 'https://' : 'http://') . $refUrl->getAuthority() . $refUrl->getPath(); + $url = ($this->flags & self::SECURED ? 'https://' : $refUrl->getScheme() . '://') . $refUrl->getAuthority() . $refUrl->getPath(); $sep = ini_get('arg_separator.input'); $query = http_build_query($params, '', $sep ? $sep[0] : '&'); if ($query != '') { // intentionally == diff --git a/tests/Route/secured.phpt b/tests/Route/secured.phpt index 0dae2cb6..0d4ff919 100644 --- a/tests/Route/secured.phpt +++ b/tests/Route/secured.phpt @@ -23,7 +23,7 @@ $url = $route->constructUrl( new Request('Presenter', NULL, ['param' => 'any']), new Url('https://example.org') ); -Assert::same('http://example.org/any', $url); +Assert::same('https://example.org/any', $url); $route = new Route('', [ diff --git a/tests/Route/withHost.secured.phpt b/tests/Route/withHost.secured.phpt new file mode 100644 index 00000000..5265fb25 --- /dev/null +++ b/tests/Route/withHost.secured.phpt @@ -0,0 +1,52 @@ + 'Default', + 'action' => 'default', +]); + +$url = $route->constructUrl( + new Request('Default', NULL, ['action' => 'default']), + new Url('https://example.org') +); +Assert::same('https://example.org/test', $url); + +$url = $route->constructUrl( + new Request('Default', NULL, ['action' => 'default']), + new Url('https://example.com') +); +Assert::same('http://example.org/test', $url); + + + +$route = new Route('//example.org/test', [ + 'presenter' => 'Default', + 'action' => 'default', +], Route::SECURED); + +$url = $route->constructUrl( + new Request('Default', NULL, ['action' => 'default']), + new Url('https://example.org') +); +Assert::same('https://example.org/test', $url); + +$url = $route->constructUrl( + new Request('Default', NULL, ['action' => 'default']), + new Url('https://example.com') +); +Assert::same('https://example.org/test', $url); diff --git a/tests/SimpleRouter/basic.phpt b/tests/SimpleRouter/basic.phpt index e1660b56..53c647f2 100644 --- a/tests/SimpleRouter/basic.phpt +++ b/tests/SimpleRouter/basic.phpt @@ -40,4 +40,4 @@ Assert::same('http://nette.org/file.php?action=action&test=testvalue&presenter=m $url = new Http\UrlScript('https://nette.org/file.php'); $res = $router->constructUrl($req, $url); -Assert::same('http://nette.org/file.php?action=action&test=testvalue&presenter=myPresenter', $res); +Assert::same('https://nette.org/file.php?action=action&test=testvalue&presenter=myPresenter', $res);