[Routing] fix absolute url generation when scheme is not known
This commit is contained in:
parent
7a6ce5ff85
commit
8e04222976
@ -222,16 +222,18 @@ class UrlGenerator implements UrlGeneratorInterface, ConfigurableRequirementsInt
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((self::ABSOLUTE_URL === $referenceType || self::NETWORK_PATH === $referenceType) && !empty($host)) {
|
if (self::ABSOLUTE_URL === $referenceType || self::NETWORK_PATH === $referenceType) {
|
||||||
$port = '';
|
if ('' !== $host || ('' !== $scheme && 'http' !== $scheme && 'https' !== $scheme)) {
|
||||||
if ('http' === $scheme && 80 != $this->context->getHttpPort()) {
|
$port = '';
|
||||||
$port = ':'.$this->context->getHttpPort();
|
if ('http' === $scheme && 80 !== $this->context->getHttpPort()) {
|
||||||
} elseif ('https' === $scheme && 443 != $this->context->getHttpsPort()) {
|
$port = ':'.$this->context->getHttpPort();
|
||||||
$port = ':'.$this->context->getHttpsPort();
|
} elseif ('https' === $scheme && 443 !== $this->context->getHttpsPort()) {
|
||||||
}
|
$port = ':'.$this->context->getHttpsPort();
|
||||||
|
}
|
||||||
|
|
||||||
$schemeAuthority = self::NETWORK_PATH === $referenceType ? '//' : "$scheme://";
|
$schemeAuthority = self::NETWORK_PATH === $referenceType || '' === $scheme ? '//' : "$scheme://";
|
||||||
$schemeAuthority .= $host.$port;
|
$schemeAuthority .= $host.$port;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (self::RELATIVE_PATH === $referenceType) {
|
if (self::RELATIVE_PATH === $referenceType) {
|
||||||
|
@ -480,28 +480,27 @@ class UrlGeneratorTest extends TestCase
|
|||||||
|
|
||||||
public function testDefaultHostIsUsedWhenContextHostIsEmpty()
|
public function testDefaultHostIsUsedWhenContextHostIsEmpty()
|
||||||
{
|
{
|
||||||
$routes = $this->getRoutes('test', new Route('/route', ['domain' => 'my.fallback.host'], ['domain' => '.+'], [], '{domain}', ['http']));
|
$routes = $this->getRoutes('test', new Route('/path', ['domain' => 'my.fallback.host'], ['domain' => '.+'], [], '{domain}'));
|
||||||
|
|
||||||
$generator = $this->getGenerator($routes);
|
$generator = $this->getGenerator($routes);
|
||||||
$generator->getContext()->setHost('');
|
$generator->getContext()->setHost('');
|
||||||
|
|
||||||
$this->assertSame('http://my.fallback.host/app.php/route', $generator->generate('test', [], UrlGeneratorInterface::ABSOLUTE_URL));
|
$this->assertSame('http://my.fallback.host/app.php/path', $generator->generate('test', [], UrlGeneratorInterface::ABSOLUTE_URL));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testDefaultHostIsUsedWhenContextHostIsEmptyAndSchemeIsNot()
|
public function testDefaultHostIsUsedWhenContextHostIsEmptyAndPathReferenceType()
|
||||||
{
|
{
|
||||||
$routes = $this->getRoutes('test', new Route('/route', ['domain' => 'my.fallback.host'], ['domain' => '.+'], [], '{domain}', ['http', 'https']));
|
$routes = $this->getRoutes('test', new Route('/path', ['domain' => 'my.fallback.host'], ['domain' => '.+'], [], '{domain}'));
|
||||||
|
|
||||||
$generator = $this->getGenerator($routes);
|
$generator = $this->getGenerator($routes);
|
||||||
$generator->getContext()->setHost('');
|
$generator->getContext()->setHost('');
|
||||||
$generator->getContext()->setScheme('https');
|
|
||||||
|
|
||||||
$this->assertSame('https://my.fallback.host/app.php/route', $generator->generate('test', [], UrlGeneratorInterface::ABSOLUTE_URL));
|
$this->assertSame('//my.fallback.host/app.php/path', $generator->generate('test', [], UrlGeneratorInterface::ABSOLUTE_PATH));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testAbsoluteUrlFallbackToRelativeIfHostIsEmptyAndSchemeIsNot()
|
public function testAbsoluteUrlFallbackToPathIfHostIsEmptyAndSchemeIsHttp()
|
||||||
{
|
{
|
||||||
$routes = $this->getRoutes('test', new Route('/route', [], [], [], '', ['http', 'https']));
|
$routes = $this->getRoutes('test', new Route('/route'));
|
||||||
|
|
||||||
$generator = $this->getGenerator($routes);
|
$generator = $this->getGenerator($routes);
|
||||||
$generator->getContext()->setHost('');
|
$generator->getContext()->setHost('');
|
||||||
@ -510,6 +509,39 @@ class UrlGeneratorTest extends TestCase
|
|||||||
$this->assertSame('/app.php/route', $generator->generate('test', [], UrlGeneratorInterface::ABSOLUTE_URL));
|
$this->assertSame('/app.php/route', $generator->generate('test', [], UrlGeneratorInterface::ABSOLUTE_URL));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testAbsoluteUrlFallbackToNetworkIfSchemeIsEmptyAndHostIsNot()
|
||||||
|
{
|
||||||
|
$routes = $this->getRoutes('test', new Route('/path'));
|
||||||
|
|
||||||
|
$generator = $this->getGenerator($routes);
|
||||||
|
$generator->getContext()->setHost('example.com');
|
||||||
|
$generator->getContext()->setScheme('');
|
||||||
|
|
||||||
|
$this->assertSame('//example.com/app.php/path', $generator->generate('test', [], UrlGeneratorInterface::ABSOLUTE_URL));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testAbsoluteUrlFallbackToPathIfSchemeAndHostAreEmpty()
|
||||||
|
{
|
||||||
|
$routes = $this->getRoutes('test', new Route('/path'));
|
||||||
|
|
||||||
|
$generator = $this->getGenerator($routes);
|
||||||
|
$generator->getContext()->setHost('');
|
||||||
|
$generator->getContext()->setScheme('');
|
||||||
|
|
||||||
|
$this->assertSame('/app.php/path', $generator->generate('test', [], UrlGeneratorInterface::ABSOLUTE_URL));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testAbsoluteUrlWithNonHttpSchemeAndEmptyHost()
|
||||||
|
{
|
||||||
|
$routes = $this->getRoutes('test', new Route('/path', [], [], [], '', ['file']));
|
||||||
|
|
||||||
|
$generator = $this->getGenerator($routes);
|
||||||
|
$generator->getContext()->setBaseUrl('');
|
||||||
|
$generator->getContext()->setHost('');
|
||||||
|
|
||||||
|
$this->assertSame('file:///path', $generator->generate('test', [], UrlGeneratorInterface::ABSOLUTE_URL));
|
||||||
|
}
|
||||||
|
|
||||||
public function testGenerateNetworkPath()
|
public function testGenerateNetworkPath()
|
||||||
{
|
{
|
||||||
$routes = $this->getRoutes('test', new Route('/{name}', [], [], [], '{locale}.example.com', ['http']));
|
$routes = $this->getRoutes('test', new Route('/{name}', [], [], [], '{locale}.example.com', ['http']));
|
||||||
|
Reference in New Issue
Block a user