parseURL

Parse the input string as a URL.

@safe
parseURL
(
string value
)

Throws

URLException if the string was in an incorrect format.

Examples

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