tryParseURL

Parse a URL from a string.

This attempts to parse a wide range of URLs as people might actually type them. Some mistakes may be made. However, any URL in a correct format will be parsed correctly.

@safe
bool
tryParseURL
(
string value
,
out URL url
)

Examples

1 {
2 	// Basic.
3 	URL url;
4 	with (url) {
5 		scheme = "https";
6 		host = "example.org";
7 		path = "/foo/bar";
8 		query["hello"] = "world";
9 		query["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 	// Percent encoded.
20 	URL url;
21 	with (url) {
22 		scheme = "https";
23 		host = "example.org";
24 		path = "/f☃o";
25 		query["❄"] = "❀";
26 		query["["] = "]";
27 		fragment = "ş";
28 	}
29 	assert(
30 			// Not sure what order it'll come out in.
31 			url.toString == "https://example.org/f%E2%98%83o?%E2%9D%84=%E2%9D%80&%5B=%5D#%C5%9F" ||
32 			url.toString == "https://example.org/f%E2%98%83o?%5B=%5D&%E2%9D%84=%E2%9D%80#%C5%9F",
33 			url.toString);
34 }
35 {
36 	// Port, user, pass.
37 	URL url;
38 	with (url) {
39 		scheme = "https";
40 		host = "example.org";
41 		user = "dhasenan";
42 		pass = "itsasecret";
43 		port = 17;
44 	}
45 	assert(
46 			url.toString == "https://dhasenan:itsasecret@example.org:17/",
47 			url.toString);
48 }
49 {
50 	// Query with no path.
51 	URL url;
52 	with (url) {
53 		scheme = "https";
54 		host = "example.org";
55 		query["hi"] = "bye";
56 	}
57 	assert(
58 			url.toString == "https://example.org/?hi=bye",
59 			url.toString);
60 }
// There's an existing path.
auto url = parseURL("http://example.org/foo");
// No slash? Assume it needs a slash.
assert((url ~ "bar").toString == "http://example.org/foo/bar");
// With slash? Don't add another.
assert((url ~ "/bar").toString == "http://example.org/foo/bar");
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);

Meta