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 }
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.
Punycode is not supported.