Page MenuHomeDevCentral

D2285.diff
No OneTemporary

D2285.diff

diff --git a/src/HTTP/URL.php b/src/HTTP/URL.php
--- a/src/HTTP/URL.php
+++ b/src/HTTP/URL.php
@@ -73,20 +73,37 @@
return "";
}
+ private function getUrlParts() : array {
+ preg_match("@://(.*)@", $this->url, $matches);
+ return explode("/", $matches[1], 2);
+ }
+
public function getDomain () : string {
- if (preg_match("@://(.*?)/@", $this->url, $matches)) {
- return self::beautifyDomain($matches[1]);
+ if (strpos($this->url, "://") === false) {
+ return "";
}
- return "";
+ $domain = $this->getUrlParts()[0];
+
+ if ($domain === "") {
+ return "";
+ }
+
+ return self::beautifyDomain($domain);
}
public function getQuery () : string {
- if (preg_match("@(://.*?)?(/.*)@", $this->url, $matches)) {
- return $this->beautifyQuery($matches[2]);
+ if (strpos($this->url, "://") === false) {
+ return $this->url;
}
- return "";
+ $parts = $this->getUrlParts();
+
+ if (count($parts) < 2 || $parts[1] === "" || $parts[1] === "/") {
+ return "/";
+ }
+
+ return "/" . $this->beautifyQuery($parts[1]);
}
public function setProtocol ($protocol) : self {
diff --git a/tests/HTTP/URLTest.php b/tests/HTTP/URLTest.php
--- a/tests/HTTP/URLTest.php
+++ b/tests/HTTP/URLTest.php
@@ -66,19 +66,25 @@
/**
* @dataProvider provideURLsAndComponents
*/
- public function testCompose ($expectedUrl, $domain, $protocol, $query) {
+ public function testCompose ($url, $domain, $protocol, $query,
+ $expectedUrl = null) {
$this->assertEquals(
- $expectedUrl,
+ $expectedUrl ?? $url,
URL::compose($protocol, $domain, $query)->__toString()
);
}
public function provideURLsAndComponents () : iterable {
+ // base URL, domain, protocol, query[, expected URL]
+ // When omitted, the expected URL is the base URL.
+
yield ["http://foo/bar", "foo", "http", "/bar"];
yield ["https://xn--dghrefn-mxa.nasqueron.org/", "dæghrefn.nasqueron.org", "https", "/"];
yield ["://foo/bar", "foo", "", "/bar"];
yield ["/bar", "", "", "/bar"];
yield ["http://foo/bar%20quux", "foo", "http", "/bar quux"];
+ yield ["https://foo/", "foo", "https", "/"];
+ yield ["https://foo", "foo", "https", "/", "https://foo/"];
}
}

File Metadata

Mime Type
text/plain
Expires
Sun, Nov 24, 04:15 (4 h, 34 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2259510
Default Alt Text
D2285.diff (2 KB)

Event Timeline