Skip to content

Commit 81ebb2d

Browse files
committed
Route, SimpleRouter: by default keeps the currently used HTTP/HTTPS protocol (BC break) [Closes nette/nette#1196][Closes nette/routing#14]
1 parent 5863e84 commit 81ebb2d

File tree

5 files changed

+73
-16
lines changed

5 files changed

+73
-16
lines changed

src/Application/Routers/Route.php

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -383,25 +383,30 @@ public function constructUrl(Application\Request $appRequest, Nette\Http\Url $re
383383
} while (TRUE);
384384

385385

386-
if ($this->type !== self::HOST) {
386+
if ($this->type === self::HOST) {
387+
$host = $refUrl->getHost();
388+
$parts = ip2long($host) ? [$host] : array_reverse(explode('.', $host));
389+
$url = strtr($url, [
390+
'/%basePath%/' => $refUrl->getBasePath(),
391+
'%tld%' => $parts[0],
392+
'%domain%' => isset($parts[1]) ? "$parts[1].$parts[0]" : $parts[0],
393+
'%sld%' => isset($parts[1]) ? $parts[1] : '',
394+
]);
395+
if ($this->flags & self::SECURED) {
396+
$url = 'https:' . $url;
397+
} elseif (strncmp($url, "//$host/", strlen($host) + 3) === 0) {
398+
$url = $refUrl->getScheme() . ':' . $url;
399+
} else {
400+
$url = 'http:' . $url;
401+
}
402+
} else {
387403
if ($this->lastRefUrl !== $refUrl) {
388-
$scheme = ($this->flags & self::SECURED ? 'https://' : 'http://');
404+
$scheme = ($this->flags & self::SECURED ? 'https://' : $refUrl->getScheme() . '://');
389405
$basePath = ($this->type === self::RELATIVE ? $refUrl->getBasePath() : '');
390406
$this->lastBaseUrl = $scheme . $refUrl->getAuthority() . $basePath;
391407
$this->lastRefUrl = $refUrl;
392408
}
393409
$url = $this->lastBaseUrl . $url;
394-
395-
} else {
396-
$host = $refUrl->getHost();
397-
$host = ip2long($host) ? [$host] : array_reverse(explode('.', $host));
398-
$url = strtr($url, [
399-
'/%basePath%/' => $refUrl->getBasePath(),
400-
'%tld%' => $host[0],
401-
'%domain%' => isset($host[1]) ? "$host[1].$host[0]" : $host[0],
402-
'%sld%' => isset($host[1]) ? $host[1] : '',
403-
]);
404-
$url = ($this->flags & self::SECURED ? 'https:' : 'http:') . $url;
405410
}
406411

407412
if (strpos($url, '//', 7) !== FALSE) {

src/Application/Routers/SimpleRouter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public function constructUrl(Application\Request $appRequest, Nette\Http\Url $re
115115
}
116116
}
117117

118-
$url = ($this->flags & self::SECURED ? 'https://' : 'http://') . $refUrl->getAuthority() . $refUrl->getPath();
118+
$url = ($this->flags & self::SECURED ? 'https://' : $refUrl->getScheme() . '://') . $refUrl->getAuthority() . $refUrl->getPath();
119119
$sep = ini_get('arg_separator.input');
120120
$query = http_build_query($params, '', $sep ? $sep[0] : '&');
121121
if ($query != '') { // intentionally ==

tests/Routers/Route.secured.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ $url = $route->constructUrl(
2323
new Request('Presenter', NULL, ['param' => 'any']),
2424
new Url('https://example.org')
2525
);
26-
Assert::same('http://example.org/any', $url);
26+
Assert::same('https://example.org/any', $url);
2727

2828

2929
$route = new Route('<param>', [
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
/**
4+
* Test: Nette\Application\Routers\Route with WithHost
5+
*/
6+
7+
use Nette\Application\Routers\Route;
8+
use Nette\Application\Request;
9+
use Nette\Http\Url;
10+
use Tester\Assert;
11+
12+
13+
require __DIR__ . '/../bootstrap.php';
14+
15+
require __DIR__ . '/Route.php';
16+
17+
18+
$route = new Route('//example.org/test', [
19+
'presenter' => 'Default',
20+
'action' => 'default',
21+
]);
22+
23+
$url = $route->constructUrl(
24+
new Request('Default', NULL, ['action' => 'default']),
25+
new Url('https://example.org')
26+
);
27+
Assert::same('https://example.org/test', $url);
28+
29+
$url = $route->constructUrl(
30+
new Request('Default', NULL, ['action' => 'default']),
31+
new Url('https://example.com')
32+
);
33+
Assert::same('http://example.org/test', $url);
34+
35+
36+
37+
$route = new Route('//example.org/test', [
38+
'presenter' => 'Default',
39+
'action' => 'default',
40+
], Route::SECURED);
41+
42+
$url = $route->constructUrl(
43+
new Request('Default', NULL, ['action' => 'default']),
44+
new Url('https://example.org')
45+
);
46+
Assert::same('https://example.org/test', $url);
47+
48+
$url = $route->constructUrl(
49+
new Request('Default', NULL, ['action' => 'default']),
50+
new Url('https://example.com')
51+
);
52+
Assert::same('https://example.org/test', $url);

tests/Routers/SimpleRouter.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,4 @@ Assert::same('http://nette.org/file.php?action=action&test=testvalue&presenter=m
4040

4141
$url = new Http\UrlScript('https://nette.org/file.php');
4242
$res = $router->constructUrl($req, $url);
43-
Assert::same('http://nette.org/file.php?action=action&test=testvalue&presenter=myPresenter', $res);
43+
Assert::same('https://nette.org/file.php?action=action&test=testvalue&presenter=myPresenter', $res);

0 commit comments

Comments
 (0)