From eaf6deb8bccc778ea53d2c87d07a03458551a11b Mon Sep 17 00:00:00 2001 From: "J. King" Date: Thu, 7 May 2020 18:22:32 -0400 Subject: [PATCH] Fix authority handling for file scheme --- lib/Url.php | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/lib/Url.php b/lib/Url.php index 558eba0..d7d92d0 100644 --- a/lib/Url.php +++ b/lib/Url.php @@ -103,10 +103,13 @@ PCRE; $this->setScheme($scheme); if ($authority && !in_array($authority[1] ?? "", ["/", "\\"])) { // the URI is something like x:/example.com/ - if ($baseUrl && ($base = new static($baseUrl)) && $scheme === $base->scheme) { + if ($baseUrl && ($base = new static($baseUrl)) && $this->scheme === $base->scheme) { // URI is a relative URL; add authority to path instead $path = $authority.$path; $authority = ""; + } elseif ($this->scheme === "file") { + $path = $authority.$path; + $authority = "//"; } elseif ($this->specialScheme) { // URI is an absolute URL with a typo; add a slash to the authority $authority = "/$authority"; @@ -117,13 +120,6 @@ PCRE; } } } - $this->setPath($path); - if ($query) { - $this->setQuery(substr($query, 1)); - } - if ($fragment) { - $this->setFragment(substr($fragment, 1)); - } if (strlen($authority)) { $authority = substr($authority, 2); if (($cleft = strrpos($authority, "@")) !== false) { @@ -139,11 +135,20 @@ PCRE; $this->setHost($match[1]); $this->setPort($match[2] ?? ""); } - } - if ((!$this->scheme || ($this->host === null && array_key_exists($this->scheme, self::SPECIAL_SCHEMES))) && strlen($baseUrl ?? "")) { + $this->setPath($path); + if ($query) { + $this->setQuery(substr($query, 1)); + } + if ($fragment) { + $this->setFragment(substr($fragment, 1)); + } + if ((!$this->scheme || ($this->host === null && $this->specialScheme)) && strlen($baseUrl ?? "")) { $this->resolve($base ?? new static($baseUrl)); } + if ($this->scheme === "file" && !($this->host === "" || $this->host === "localhost")) { + throw new \InvalidArgumentException("Invalid authority for file: scheme"); + } } else { throw new \InvalidArgumentException("String is not a valid URI"); }