Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 8e04222

Browse filesBrowse files
committed
[Routing] fix absolute url generation when scheme is not known
1 parent 7a6ce5f commit 8e04222
Copy full SHA for 8e04222

File tree

2 files changed

+51
-17
lines changed
Filter options

2 files changed

+51
-17
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Generator/UrlGenerator.php
+11-9Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Tests/Generator/UrlGeneratorTest.php
+40-8Lines 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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.