Skip to content

Commit

Permalink
Route, SimpleRouter: by default keeps the currently used HTTP/HTTPS p…
Browse files Browse the repository at this point in the history
…rotocol (BC break) [Closes nette/nette#1196][Closes #14]
  • Loading branch information
dg committed Feb 3, 2019
1 parent a19d24b commit 60aa50e
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 16 deletions.
31 changes: 18 additions & 13 deletions src/Routing/Route.php
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/Routing/SimpleRouter.php
Expand Up @@ -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 ==
Expand Down
2 changes: 1 addition & 1 deletion tests/Route/secured.phpt
Expand Up @@ -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('<param>', [
Expand Down
52 changes: 52 additions & 0 deletions tests/Route/withHost.secured.phpt
@@ -0,0 +1,52 @@
<?php

/**
* Test: Nette\Application\Routers\Route with WithHost
*/

use Nette\Application\Routers\Route;
use Nette\Application\Request;
use Nette\Http\Url;
use Tester\Assert;


require __DIR__ . '/../bootstrap.php';

require __DIR__ . '/Route.php';


$route = new Route('//example.org/test', [
'presenter' => '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);
2 changes: 1 addition & 1 deletion tests/SimpleRouter/basic.phpt
Expand Up @@ -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);

0 comments on commit 60aa50e

Please sign in to comment.