8000 bug #32000 [Routing] fix absolute url generation when scheme is not k… · symfony/symfony@bd9d0a4 · GitHub
[go: up one dir, main page]

Skip to content

Commit bd9d0a4

Browse files
committed
bug #32000 [Routing] fix absolute url generation when scheme is not known (Tobion)
This PR was merged into the 3.4 branch. Discussion ---------- [Routing] fix absolute url generation when scheme is not known | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | BC breaks? | no <!-- see https://symfony.com/bc --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tests pass? | yes <!-- please add some, will be required by reviewers --> | Fixed tickets | #25491 | License | MIT | Doc PR | This fixes two edge cases in the url generator: 1. when the context scheme is not known (empty) generating an absolute url would return an invalid url starting with `://host/path`. #25491 handled the case when the host is unknown which makes sense. but the way it was done, created this new problem. 2. non-http(s) urls do not require a host. e.g. typical `file:///path` urls. url generator is fixed to be in line with rfc3986 Commits ------- 8e04222 [Routing] fix absolute url generation when scheme is not known
2 parents faf7b30 + 8e04222 commit bd9d0a4

File tree

2 files changed

+51
-17
lines changed

2 files changed

+51
-17
lines changed

src/Symfony/Component/Routing/Generator/UrlGenerator.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -222,16 +222,18 @@ protected function doGenerate($variables, $defaults, $requirements, $tokens, $pa
222222
}
223223
}
224224

225-
if ((self::ABSOLUTE_URL === $referenceType || self::NETWORK_PATH === $referenceType) && !empty($host)) {
226-
$port = '';
227-
if ('http' === $scheme && 80 != $this->context->getHttpPort()) {
228-
$port = ':'.$this->context->getHttpPort();
229-
} elseif ('https' === $scheme && 443 != $this->context->getHttpsPort()) {
230-
$port = ':'.$this->context->getHttpsPort();
231-
}
225+
if (self::ABSOLUTE_URL === $referenceType || self::NETWORK_PATH === $referenceType) {
226+
if ('' !== $host || ('' !== $scheme && 'http' !== $scheme && 'https' !== $scheme)) {
227+
$port = '';
228+
if ('http' === $scheme && 80 !== $this->context->getHttpPort()) {
229+
$port = ':'.$this->context->getHttpPort();
230+
} elseif ('https' === $scheme && 443 !== $this->context->getHttpsPort()) {
231+
$port = ':'.$this->context->getHttpsPort();
232+
}
232233

233-
$schemeAuthority = self::NETWORK_PATH === $referenceType ? '//' : "$scheme://";
234-
$schemeAuthority .= $host.$port;
234+
$schemeAuthority = self::NETWORK_PATH === $referenceType || '' === $scheme ? '//' : "$scheme://";
235+
$schemeAuthority .= $host.$port;
236+
}
235237
}
236238

237239
if (self::RELATIVE_PATH === $referenceType) {

src/Symfony/Component/Routing/Tests/Generator/UrlGeneratorTest.php

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -480,28 +480,27 @@ public function testHostIsCaseInsensitive()
480480

481481
public function testDefaultHostIsUsedWhenContextHostIsEmpty()
482482
{
483-
$routes = $this->getRoutes('test', new Route('/route', ['domain' => 'my.fallback.host'], ['domain' => '.+'], [], '{domain}', ['http']));
483+
$routes = $this->getRoutes('test', new Route('/path', ['domain' => 'my.fallback.host'], ['domain' => '.+'], [], '{domain}'));
484484

485485
$generator = $this->getGenerator($routes);
486486
$generator->getContext()->setHost('');
487487

488-
$this->assertSame('http://my.fallback.host/app.php/route', $generator->generate('test', [], UrlGeneratorInterface::ABSOLUTE_URL));
488+
$this->assertSame('http://my.fallback.host/app.php/path', $generator->generate('test', [], UrlGeneratorInterface::ABSOLUTE_URL));
489489
}
490490

491-
public function testDefaultHostIsUsedWhenContextHostIsEmptyAndSchemeIsNot()
491+
public function testDefaultHostIsUsedWhenContextHostIsEmptyAndPathReferenceType()
492492
{
493-
$routes = $this->getRoutes('test', new Route('/route', ['domain' => 'my.fallback.host'], ['domain' => '.+'], [], '{domain}', ['http', 'https']));
493+
$routes = $this->getRoutes('test', new Route('/path', ['domain' => 'my.fallback.host'], ['domain' => '.+'], [], '{domain}'));
494494

495495
$generator = $this->getGenerator($routes);
496496
$generator->getContext()->setHost('');
497-
$generator->getContext()->setScheme('https');
498497

499-
$this->assertSame('https://my.fallback.host/app.php/route', $generator->generate('test', [], UrlGeneratorInterface::ABSOLUTE_URL));
498+
$this->assertSame('//my.fallback.host/app.php/path', $generator->generate('test', [], UrlGeneratorInterface::ABSOLUTE_PATH));
500499
}
501500

502-
public function testAbsoluteUrlFallbackToRelativeIfHostIsEmptyAndSchemeIsNot()
501+
public function testAbsoluteUrlFallbackToPathIfHostIsEmptyAndSchemeIsHttp()
503502
{
504-
$routes = $this->getRoutes('test', new Route('/route', [], [], [], '', ['http', 'https']));
503+
$routes = $this->getRoutes('test', new Route('/route'));
505504

506505
$generator = $this->getGenerator($routes);
507506
$generator->getContext()->setHost('');
@@ -510,6 +509,39 @@ public function testAbsoluteUrlFallbackToRelativeIfHostIsEmptyAndSchemeIsNot()
510509
$this->assertSame('/app.php/route', $generator->generate('test', [], UrlGeneratorInterface::ABSOLUTE_URL));
511510
}
512511

512+
public function testAbsoluteUrlFallbackToNetworkIfSchemeIsEmptyAndHostIsNot()
513+
{
514+
$routes = $this->getRoutes('test', new Route('/path'));
515+
516+
$generator = $this->getGenerator($routes);
517+
$generator->getContext()->setHost('example.com');
518+
$generator->getContext()->setScheme('');
519+
520+
$this->assertSame('//example.com/app.php/path', $generator->generate('test', [], UrlGeneratorInterface::ABSOLUTE_URL));
521+
}
522+
523+
public function testAbsoluteUrlFallbackToPathIfSchemeAndHostAreEmpty()
524+
{
525+
$routes = $this->getRoutes('test', new Route('/path'));
526+
527+
$generator = $this->getGenerator($routes);
528+
$generator->getContext()->setHost('');
529+
$generator->getContext()->setScheme('');
530+
531+
$this->assertSame('/app.php/path', $generator->generate('test', [], UrlGeneratorInterface::ABSOLUTE_URL));
532+
}
533+
534+
public function testAbsoluteUrlWithNonHttpSchemeAndEmptyHost()
535+
{
536+
$routes = $this->getRoutes('test', new Route('/path', [], [], [], '', ['file']));
537+
538+
$generator = $this->getGenerator($routes);
539+
$generator->getContext()->setBaseUrl('');
540+
$generator->getContext()->setHost('');
541+
542+
$this->assertSame('file:///path', $generator->generate('test', [], UrlGeneratorInterface::ABSOLUTE_URL));
543+
}
544+
513545
public function testGenerateNetworkPath()
514546
{
515547
$routes = $this->getRoutes('test', new Route('/{name}', [], [], [], '{locale}.example.com', ['http']));

0 commit comments

Comments
 (0)
0