Url

A Unique Resource Locator.

Urls can be parsed (see parseUrl) and implicitly convert to strings.

Constructors

this
this(string url)
Undocumented in source.

Alias This

toString

Implicitly convert Urls to strings.

Members

Functions

isValid
bool isValid()
Undocumented in source. Be warned that the author may not have intended to support it.
opBinary
Url opBinary(string subsequentPath)

The append operator (~).

opCmp
int opCmp(Url other)

Compare two Urls.

opEquals
bool opEquals(Url other)

Equality checks. Ditto

opOpAssign
Url opOpAssign(string subsequentPath)

The append-in-place operator (~=).

toHash
hash_t toHash()
Undocumented in source. Be warned that the author may not have intended to support it.
toHumanReadableString
string toHumanReadableString()

Convert this Url to a string.

toPathAndQueryString
string toPathAndQueryString()

Convert the path and query string of this Url to a string.

toString
string toString()

Convert this Url to a string. The string is properly formatted and usable for, eg, a web request.

Properties

port
ushort port [@property getter]

The port. * This is inferred from the scheme if it isn't present in the Url itself. If the scheme is not known and the port is not present, the port will be given as 0. For some schemes, port will not be sensible -- for instance, file or chrome-extension.

port
ushort port [@property setter]

* Set the port.

Variables

fragment
string fragment;

The fragment. In web documents, this typically refers to an anchor element. For instance, in the Url https://cnn.com/news/story/17774#header2, the fragment is "header2".

host
string host;

The hostname.

pass
string pass;

The password in this Url. Usually absent.

path
string path;

The path.

providedPort
ushort providedPort;

The port that was explicitly provided in the Url.

queryParams
QueryParams queryParams;

The query parameters associated with this Url.

scheme
string scheme;

The Url scheme. For instance, ssh, ftp, or https.

user
string user;

The username in this Url. Usually absent. If present, there will also be a password.

Examples

1 {
2     // Basic.
3     Url url;
4     with (url) {
5         scheme = "https";
6         host = "example.org";
7         path = "/foo/bar";
8         queryParams.add("hello", "world");
9         queryParams.add("gibe", "clay");
10         fragment = "frag";
11     }
12     assert(
13             // Not sure what order it'll come out in.
14             url.toString == "https://example.org/foo/bar?hello=world&gibe=clay#frag" ||
15             url.toString == "https://example.org/foo/bar?gibe=clay&hello=world#frag",
16             url.toString);
17 }
18 {
19     // Passing an array of query values.
20     Url url;
21     with (url) {
22         scheme = "https";
23         host = "example.org";
24         path = "/foo/bar";
25         queryParams.add("hello", "world");
26         queryParams.add("hello", "aether");
27         fragment = "frag";
28     }
29     assert(
30             // Not sure what order it'll come out in.
31             url.toString == "https://example.org/foo/bar?hello=world&hello=aether#frag" ||
32             url.toString == "https://example.org/foo/bar?hello=aether&hello=world#frag",
33             url.toString);
34 }
35 {
36     // Percent encoded.
37     Url url;
38     with (url) {
39         scheme = "https";
40         host = "example.org";
41         path = "/f☃o";
42         queryParams.add("❄", "❀");
43         queryParams.add("[", "]");
44         fragment = "ş";
45     }
46     assert(
47             // Not sure what order it'll come out in.
48             url.toString == "https://example.org/f%E2%98%83o?%E2%9D%84=%E2%9D%80&%5B=%5D#%C5%9F" ||
49             url.toString == "https://example.org/f%E2%98%83o?%5B=%5D&%E2%9D%84=%E2%9D%80#%C5%9F",
50             url.toString);
51 }
52 {
53     // Port, user, pass.
54     Url url;
55     with (url) {
56         scheme = "https";
57         host = "example.org";
58         user = "dhasenan";
59         pass = "itsasecret";
60         port = 17;
61     }
62     assert(
63             url.toString == "https://dhasenan:itsasecret@example.org:17/",
64             url.toString);
65 }
66 {
67     // Query with no path.
68     Url url;
69     with (url) {
70         scheme = "https";
71         host = "example.org";
72         queryParams.add("hi", "bye");
73     }
74     assert(
75             url.toString == "https://example.org/?hi=bye",
76             url.toString);
77 }
// There's an existing path.
auto url = parseUrl("http://example.org/foo");
Url url2;
// No slash? Assume it needs a slash.
assert((url ~ "bar").toString == "http://example.org/foo/bar");
// With slash? Don't add another.
url2 = url ~ "/bar";
assert(url2.toString == "http://example.org/foo/bar", url2.toString);
url ~= "bar";
assert(url.toString == "http://example.org/foo/bar");

// Path already ends with a slash; don't add another.
url = parseUrl("http://example.org/foo/");
assert((url ~ "bar").toString == "http://example.org/foo/bar");
// Still don't add one even if you're appending with a slash.
assert((url ~ "/bar").toString == "http://example.org/foo/bar");
url ~= "/bar";
assert(url.toString == "http://example.org/foo/bar");

// No path.
url = parseUrl("http://example.org");
assert((url ~ "bar").toString == "http://example.org/bar");
assert((url ~ "/bar").toString == "http://example.org/bar");
url ~= "bar";
assert(url.toString == "http://example.org/bar");

// Path is just a slash.
url = parseUrl("http://example.org/");
assert((url ~ "bar").toString == "http://example.org/bar");
assert((url ~ "/bar").toString == "http://example.org/bar");
url ~= "bar";
assert(url.toString == "http://example.org/bar", url.toString);

// No path, just fragment.
url = "ircs://irc.freenode.com/#d".parseUrl;
assert(url.toString == "ircs://irc.freenode.com/#d", url.toString);

Parse the input string as a Url.

Throws: UrlException if the string was in an incorrect format.

1 {
2     // Infer scheme
3     auto u1 = parseUrl("example.org");
4     assert(u1.scheme == "http");
5     assert(u1.host == "example.org");
6     assert(u1.path == "");
7     assert(u1.port == 80);
8     assert(u1.providedPort == 0);
9     assert(u1.fragment == "");
10 }
11 {
12     // Simple host and scheme
13     auto u1 = parseUrl("https://example.org");
14     assert(u1.scheme == "https");
15     assert(u1.host == "example.org");
16     assert(u1.path == "");
17     assert(u1.port == 443);
18     assert(u1.providedPort == 0);
19 }
20 {
21     // With path
22     auto u1 = parseUrl("https://example.org/foo/bar");
23     assert(u1.scheme == "https");
24     assert(u1.host == "example.org");
25     assert(u1.path == "/foo/bar", "expected /foo/bar but got " ~ u1.path);
26     assert(u1.port == 443);
27     assert(u1.providedPort == 0);
28 }
29 {
30     // With explicit port
31     auto u1 = parseUrl("https://example.org:1021/foo/bar");
32     assert(u1.scheme == "https");
33     assert(u1.host == "example.org");
34     assert(u1.path == "/foo/bar", "expected /foo/bar but got " ~ u1.path);
35     assert(u1.port == 1021);
36     assert(u1.providedPort == 1021);
37 }
38 {
39     // With user
40     auto u1 = parseUrl("https://bob:secret@example.org/foo/bar");
41     assert(u1.scheme == "https");
42     assert(u1.host == "example.org");
43     assert(u1.path == "/foo/bar");
44     assert(u1.port == 443);
45     assert(u1.user == "bob");
46     assert(u1.pass == "secret");
47 }
48 {
49     // With user, Url-encoded
50     auto u1 = parseUrl("https://bob%21:secret%21%3F@example.org/foo/bar");
51     assert(u1.scheme == "https");
52     assert(u1.host == "example.org");
53     assert(u1.path == "/foo/bar");
54     assert(u1.port == 443);
55     assert(u1.user == "bob!");
56     assert(u1.pass == "secret!?");
57 }
58 {
59     // With user and port and path
60     auto u1 = parseUrl("https://bob:secret@example.org:2210/foo/bar");
61     assert(u1.scheme == "https");
62     assert(u1.host == "example.org");
63     assert(u1.path == "/foo/bar");
64     assert(u1.port == 2210);
65     assert(u1.user == "bob");
66     assert(u1.pass == "secret");
67     assert(u1.fragment == "");
68 }
69 {
70     // With query string
71     auto u1 = parseUrl("https://example.org/?login=true");
72     assert(u1.scheme == "https");
73     assert(u1.host == "example.org");
74     assert(u1.path == "/", "expected path: / actual path: " ~ u1.path);
75     assert(u1.queryParams["login"].front == "true");
76     assert(u1.fragment == "");
77 }
78 {
79     // With query string and fragment
80     auto u1 = parseUrl("https://example.org/?login=true#justkidding");
81     assert(u1.scheme == "https");
82     assert(u1.host == "example.org");
83     assert(u1.path == "/", "expected path: / actual path: " ~ u1.path);
84     assert(u1.queryParams["login"].front == "true");
85     assert(u1.fragment == "justkidding");
86 }
87 {
88     // With Url-encoded values
89     auto u1 = parseUrl("https://example.org/%E2%98%83?%E2%9D%84=%3D#%5E");
90     assert(u1.scheme == "https");
91     assert(u1.host == "example.org");
92     assert(u1.path == "/☃", "expected path: /☃ actual path: " ~ u1.path);
93     assert(u1.queryParams["❄"].front == "=");
94     assert(u1.fragment == "^");
95 }

Meta