Skip to content

Commit 8127ece

Browse files
committed
Route: added support for scheme:// in mask
1 parent 0a0a884 commit 8127ece

File tree

3 files changed

+55
-9
lines changed

3 files changed

+55
-9
lines changed

src/Application/Routers/Route.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ class Route implements Application\IRouter
9696
/** @var int HOST, PATH, RELATIVE */
9797
private $type;
9898

99+
/** @var string http | https */
100+
private $scheme;
101+
99102
/** @var int */
100103
private $flags;
101104

@@ -136,8 +139,9 @@ public function __construct($mask, $metadata = [], $flags = 0)
136139
$this->setMask($mask, $metadata);
137140
if (static::$defaultFlags) {
138141
trigger_error('Route::$defaultFlags is deprecated, router by default keeps the used protocol.', E_USER_DEPRECATED);
139-
} elseif ($this->type !== self::HOST && $flags & self::SECURED) {
140-
trigger_error('Router::SECURED is deprecated for routes without host, router by default keeps the used protocol.', E_USER_DEPRECATED);
142+
} elseif ($flags & self::SECURED) {
143+
trigger_error('Router::SECURED is deprecated, specify scheme in mask.', E_USER_DEPRECATED);
144+
$this->scheme = 'https';
141145
}
142146
}
143147

@@ -397,18 +401,18 @@ public function constructUrl(Application\Request $appRequest, Nette\Http\Url $re
397401
'%domain%' => isset($parts[1]) ? "$parts[1].$parts[0]" : $parts[0],
398402
'%sld%' => isset($parts[1]) ? $parts[1] : '',
399403
]);
400-
if ($this->flags & self::SECURED) {
401-
$url = 'https:' . $url;
404+
if ($this->scheme) {
405+
$url = $this->scheme . ':' . $url;
402406
} elseif (strncmp($url, "//$host/", strlen($host) + 3) === 0) {
403407
$url = $refUrl->getScheme() . ':' . $url;
404408
} else {
405409
$url = 'http:' . $url;
406410
}
407411
} else {
408412
if ($this->lastRefUrl !== $refUrl) {
409-
$scheme = ($this->flags & self::SECURED ? 'https://' : $refUrl->getScheme() . '://');
413+
$scheme = ($this->scheme ?: $refUrl->getScheme());
410414
$basePath = ($this->type === self::RELATIVE ? $refUrl->getBasePath() : '');
411-
$this->lastBaseUrl = $scheme . $refUrl->getAuthority() . $basePath;
415+
$this->lastBaseUrl = $scheme . '://' . $refUrl->getAuthority() . $basePath;
412416
$this->lastRefUrl = $refUrl;
413417
}
414418
$url = $this->lastBaseUrl . $url;
@@ -444,8 +448,9 @@ private function setMask($mask, array $metadata)
444448
$this->mask = $mask;
445449

446450
// detect '//host/path' vs. '/abs. path' vs. 'relative path'
447-
if (substr($mask, 0, 2) === '//') {
451+
if (preg_match('#(?:(https?):)?(//.*)#A', $mask, $m)) {
448452
$this->type = self::HOST;
453+
list(, $this->scheme, $mask) = $m;
449454

450455
} elseif (substr($mask, 0, 1) === '/') {
451456
$this->type = self::PATH;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
/**
4+
* Test: Nette\Application\Routers\Route with host & protocol
5+
*/
6+
7+
use Nette\Application\Routers\Route;
8+
use Tester\Assert;
9+
10+
11+
require __DIR__ . '/../bootstrap.php';
12+
13+
require __DIR__ . '/Route.php';
14+
15+
16+
$route = new Route('http://<host>.<domain>/<path>', [
17+
'presenter' => 'Default',
18+
'action' => 'default',
19+
]);
20+
21+
testRouteIn($route, '/abc', 'Default', [
22+
'host' => 'example',
23+
'domain' => 'com',
24+
'path' => 'abc',
25+
'action' => 'default',
26+
'test' => 'testvalue',
27+
], '/abc?test=testvalue');
28+
29+
30+
$route = new Route('https://<host>.<domain>/<path>', [
31+
'presenter' => 'Default',
32+
'action' => 'default',
33+
]);
34+
35+
testRouteIn($route, '/abc', 'Default', [
36+
'host' => 'example',
37+
'domain' => 'com',
38+
'path' => 'abc',
39+
'action' => 'default',
40+
'test' => 'testvalue',
41+
], 'https://example.com/abc?test=testvalue');

tests/Routers/Route.withHost.secured.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ Assert::same('http://example.org/test', $url);
3434

3535

3636

37-
$route = new Route('//example.org/test', [
37+
$route = new Route('https://example.org/test', [
3838
'presenter' => 'Default',
3939
'action' => 'default',
40-
], Route::SECURED);
40+
]);
4141

4242
$url = $route->constructUrl(
4343
new Request('Default', NULL, ['action' => 'default']),

0 commit comments

Comments
 (0)