UriNormalize filter enforces scheme on the following condition:
if ($this->enforcedScheme && (! $uri->getScheme())) {
$this->enforceScheme($uri);
}
This causes a scheme to be enforced only when no scheme is given.
Would it not be better to always enforce a given scheme? Changing that if statement to the following would suffice:
if ($this->enforcedScheme !== $uri->getScheme()) {
$uri->setScheme($this->enforcedScheme);
}
Related: isn’t the enforceScheme
function broken?
It expects a Uri object. Then it gets the path property of that object. From the path it tries to deduce the host and the path (why? Host is available in Uri and path is what was requested)
It then checks if it should enforce the scheme by if the host not being empty, which it always will be if its a uri with more than just the host (exploding on slash).
e.g. : enforcedScheme === “https”
Received URI: http://example.com/test-this
In function below, the “path” is set in the “$uri”, causing $path to equal: “/test-this”. That causes “$host” to equal “” (empty string).
protected function enforceScheme(Uri $uri)
{
$path = $uri->getPath();
if (strpos($path, '/') !== false) {
list($host, $path) = explode('/', $path, 2);
$path = '/' . $path;
} else {
$host = $path;
$path = '';
}
// We have nothing to do if we have no host
if (! $host) {
return;
}
$uri->setScheme($this->enforcedScheme)
->setHost($host)
->setPath($path);
}