-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Routing] Use the default host even if context is empty #25491
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
…e URL if empty host
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -181,63 +181,62 @@ protected function doGenerate($variables, $defaults, $requirements, $tokens, $pa | |
} | ||
|
||
$schemeAuthority = ''; | ||
if ($host = $this->context->getHost()) { | ||
$scheme = $this->context->getScheme(); | ||
$host = $this->context->getHost(); | ||
$scheme = $this->context->getScheme(); | ||
|
||
if ($requiredSchemes) { | ||
if (!in_array($scheme, $requiredSchemes, true)) { | ||
$referenceType = self::ABSOLUTE_URL; | ||
$scheme = current($requiredSchemes); | ||
} | ||
} elseif (isset($requirements['_scheme']) && ($req = strtolower($requirements['_scheme'])) && $scheme !== $req) { | ||
// We do this for BC; to be removed if _scheme is not supported anymore | ||
if ($requiredSchemes) { | ||
if (!in_array($scheme, $requiredSchemes, true)) { | ||
$referenceType = self::ABSOLUTE_URL; | ||
$scheme = $req; | ||
$scheme = current($requiredSchemes); | ||
} | ||
} elseif (isset($requirements['_scheme']) && ($req = strtolower($requirements['_scheme'])) && $scheme !== $req) { | ||
// We do this for BC; to be removed if _scheme is not supported anymore | ||
$referenceType = self::ABSOLUTE_URL; | ||
$scheme = $req; | ||
} | ||
|
||
if ($hostTokens) { | ||
$routeHost = ''; | ||
foreach ($hostTokens as $token) { | ||
if ('variable' === $token[0]) { | ||
if (null !== $this->strictRequirements && !preg_match('#^'.$token[2].'$#i', $mergedParams[$token[3]])) { | ||
$message = sprintf('Parameter "%s" for route "%s" must match "%s" ("%s" given) to generate a corresponding URL.', $token[3], $name, $token[2], $mergedParams[$token[3]]); | ||
|
||
if ($this->strictRequirements) { | ||
throw new InvalidParameterException($message); | ||
} | ||
if ($hostTokens) { | ||
$routeHost = ''; | ||
foreach ($hostTokens as $token) { | ||
if ('variable' === $token[0]) { | ||
if (null !== $this->strictRequirements && !preg_match('#^'.$token[2].'$#i', $mergedParams[$token[3]])) { | ||
$message = sprintf('Parameter "%s" for route "%s" must match "%s" ("%s" given) to generate a corresponding URL.', $token[3], $name, $token[2], $mergedParams[$token[3]]); | ||
|
||
if ($this->logger) { | ||
$this->logger->error($message); | ||
} | ||
if ($this->strictRequirements) { | ||
throw new InvalidParameterException($message); | ||
} | ||
|
||
return; | ||
if ($this->logger) { | ||
$this->logger->error($message); | ||
} | ||
|
||
< 8000 span class='blob-code-inner blob-code-marker js-skip-tagsearch' data-code-marker="-"> $routeHost = $token[1].$mergedParams[$token[3]].$routeHost; | ||
} else { | ||
$routeHost = $token[1].$routeHost; | ||
return; | ||
} | ||
} | ||
|
||
if ($routeHost !== $host) { | ||
$host = $routeHost; | ||
if (self::ABSOLUTE_URL !== $referenceType) { | ||
$referenceType = self::NETWORK_PATH; | ||
} | ||
$routeHost = $token[1].$mergedParams[$token[3]].$routeHost; | ||
} else { | ||
$routeHost = $token[1].$routeHost; | ||
} | ||
} | ||
|
||
if (self::ABSOLUTE_URL === $referenceType || self::NETWORK_PATH === $referenceType) { | ||
$port = ''; | ||
if ('http' === $scheme && 80 != $this->context->getHttpPort()) { | ||
$port = ':'.$this->context->getHttpPort(); | ||
} elseif ('https' === $scheme && 443 != $this->context->getHttpsPort()) { | ||
$port = ':'.$this->context->getHttpsPort(); | ||
if ($routeHost !== $host) { | ||
$host = $routeHost; | ||
if (self::ABSOLUTE_URL !== $referenceType) { | ||
$referenceType = self::NETWORK_PATH; | ||
} | ||
} | ||
} | ||
|
||
$schemeAuthority = self::NETWORK_PATH === $referenceType ? '//' : "$scheme://"; | ||
$schemeAuthority .= $host.$port; | ||
if ((self::ABSOLUTE_URL === $referenceType || self::NETWORK_PATH === $referenceType) && !empty($host)) { | ||
$port = ''; | ||
if ('http' === $scheme && 80 != $this->context->getHttpPort()) { | ||
$port = ':'.$this->context->getHttpPort(); | ||
} elseif ('https' === $scheme && 443 != $this->context->getHttpsPort()) { | ||
$port = ':'.$this->context->getHttpsPort(); | ||
} | ||
|
||
$schemeAuthority = self::NETWORK_PATH === $referenceType ? '//' : "$scheme://"; | ||
$schemeAuthority .= $host.$port; | ||
} | ||
|
||
if (self::RELATIVE_PATH === $referenceType) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be a different behaviour and the route would be resolved to |
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it possible to have a non empty scheme with an empty host? if yes, we should review the behavior we want in this situation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't get what you mean @nicolas-grekas. That's the point of this PR, right? 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the point of this PR is to deal with empty hosts
but it doesn't yet care about non-empty scheme + empty-host.
that's my point.
eg can/should an empty-host url be considered absolute?
looking at the code, it looks like yes after this PR.
does that make sense? that's one of the things I'm wondering.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought about it... and I don't see what else we can consider in this example you gave... We need to assume it is going to be absolute, else we need to throw an exception. I'd go for absolute (to keep the current behaviour) unless a big counter-indication.