OLD | NEW |
---|---|
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library dart.uri; | 5 library dart.uri; |
6 | 6 |
7 import 'dart:math'; | 7 import 'dart:math'; |
8 import 'dart:utf'; | 8 import 'dart:utf'; |
9 | 9 |
10 part 'encode_decode.dart'; | 10 part 'encode_decode.dart'; |
11 part 'helpers.dart'; | 11 part 'helpers.dart'; |
12 | 12 |
13 /** | 13 /** |
14 * A parsed URI, inspired by Closure's [URI][] class. Implements [RFC-3986][]. | 14 * A parsed URI, inspired by Closure's [URI][] class. Implements [RFC-3986][]. |
15 * The domain component can either be a hostname, a IPv4 address or an IPv6 | |
16 * address, contained in '[' and ']', following [RFC-2732][]. If the domain | |
17 * component contains a ':', the String returned from [toString] will have | |
18 * '[' and ']' around the part. | |
floitsch
2013/05/01 08:53:30
which part?
Anders Johnsen
2013/05/01 09:26:14
Done.
| |
15 * [uri]: http://closure-library.googlecode.com/svn/docs/class_goog_Uri.html | 19 * [uri]: http://closure-library.googlecode.com/svn/docs/class_goog_Uri.html |
floitsch
2013/05/01 08:53:30
URI ?
Anders Johnsen
2013/05/01 09:26:14
Done.
| |
16 * [RFC-3986]: http://tools.ietf.org/html/rfc3986#section-4.3) | 20 * [RFC-3986]: http://tools.ietf.org/html/rfc3986#section-4.3) |
21 * [RFC-2732]: http://www.ietf.org/rfc/rfc2732.txt | |
17 */ | 22 */ |
18 class Uri { | 23 class Uri { |
19 final String scheme; | 24 final String scheme; |
20 final String userInfo; | 25 final String userInfo; |
21 final String domain; | 26 final String domain; |
22 final int port; | 27 final int port; |
23 final String path; | 28 final String path; |
24 final String query; | 29 final String query; |
25 final String fragment; | 30 final String fragment; |
26 | 31 |
27 /** | 32 /** |
28 * Deprecated. Please use [parse] instead. | 33 * Deprecated. Please use [parse] instead. |
floitsch
2013/05/01 08:53:30
Now that I see it: yes. Please get rid of it.
Anders Johnsen
2013/05/01 09:26:14
Done.
| |
29 */ | 34 */ |
30 Uri.fromString(String uri) : this._fromMatch(_splitRe.firstMatch(uri)); | 35 Uri.fromString(String uri) : this._fromMatch(_splitRe.firstMatch(uri)); |
31 | 36 |
32 static Uri parse(String uri) => new Uri._fromMatch(_splitRe.firstMatch(uri)); | 37 static Uri parse(String uri) => new Uri._fromMatch(_splitRe.firstMatch(uri)); |
33 | 38 |
34 Uri._fromMatch(Match m) : | 39 Uri._fromMatch(Match m) : |
35 this.fromComponents(scheme: _emptyIfNull(m[_COMPONENT_SCHEME]), | 40 this.fromComponents(scheme: _emptyIfNull(m[_COMPONENT_SCHEME]), |
36 userInfo: _emptyIfNull(m[_COMPONENT_USER_INFO]), | 41 userInfo: _emptyIfNull(m[_COMPONENT_USER_INFO]), |
37 domain: _emptyIfNull(m[_COMPONENT_DOMAIN]), | 42 domain: _eitherOf( |
43 m[_COMPONENT_DOMAIN], m[_COMPONENT_DOMAIN_IPV6]), | |
38 port: _parseIntOrZero(m[_COMPONENT_PORT]), | 44 port: _parseIntOrZero(m[_COMPONENT_PORT]), |
39 path: _emptyIfNull(m[_COMPONENT_PATH]), | 45 path: _emptyIfNull(m[_COMPONENT_PATH]), |
40 query: _emptyIfNull(m[_COMPONENT_QUERY_DATA]), | 46 query: _emptyIfNull(m[_COMPONENT_QUERY_DATA]), |
41 fragment: _emptyIfNull(m[_COMPONENT_FRAGMENT])); | 47 fragment: _emptyIfNull(m[_COMPONENT_FRAGMENT])); |
42 | 48 |
43 const Uri.fromComponents({this.scheme: "", | 49 const Uri.fromComponents({this.scheme: "", |
44 this.userInfo: "", | 50 this.userInfo: "", |
45 this.domain: "", | 51 this.domain: "", |
46 this.port: 0, | 52 this.port: 0, |
47 this.path: "", | 53 this.path: "", |
48 this.query: "", | 54 this.query: "", |
49 this.fragment: ""}); | 55 this.fragment: ""}); |
50 | 56 |
51 Uri(String uri) : this.fromString(uri); | 57 Uri(String uri) : this.fromString(uri); |
52 | 58 |
53 static String _emptyIfNull(String val) => val != null ? val : ''; | 59 static String _emptyIfNull(String val) => val != null ? val : ''; |
54 | 60 |
55 static int _parseIntOrZero(String val) { | 61 static int _parseIntOrZero(String val) { |
56 if (val != null && val != '') { | 62 if (val != null && val != '') { |
57 return int.parse(val); | 63 return int.parse(val); |
58 } else { | 64 } else { |
59 return 0; | 65 return 0; |
60 } | 66 } |
61 } | 67 } |
62 | 68 |
69 static String _eitherOf(String val1, String val2) { | |
70 if (val1 != null) return val1; | |
71 if (val2 != null) return val2; | |
72 return ''; | |
73 } | |
74 | |
63 // NOTE: This code was ported from: closure-library/closure/goog/uri/utils.js | 75 // NOTE: This code was ported from: closure-library/closure/goog/uri/utils.js |
64 static final RegExp _splitRe = new RegExp( | 76 static final RegExp _splitRe = new RegExp( |
65 '^' | 77 '^' |
66 '(?:' | 78 '(?:' |
67 '([^:/?#.]+)' // scheme - ignore special characters | 79 '([^:/?#.]+)' // scheme - ignore special characters |
68 // used by other URL parts such as :, | 80 // used by other URL parts such as :, |
69 // ?, /, #, and . | 81 // ?, /, #, and . |
70 ':)?' | 82 ':)?' |
71 '(?://' | 83 '(?://' |
72 '(?:([^/?#]*)@)?' // userInfo | 84 '(?:([^/?#]*)@)?' // userInfo |
73 '([\\w\\d\\-\\u0100-\\uffff.%]*)' | 85 '(?:' |
86 '([\\w\\d\\-\\u0100-\\uffff.%]*)|' | |
floitsch
2013/05/01 08:53:30
I would prefer raw strings and not \\
Move the "|"
Anders Johnsen
2013/05/01 09:26:14
Done.
| |
74 // domain - restrict to letters, | 87 // domain - restrict to letters, |
75 // digits, dashes, dots, percent | 88 // digits, dashes, dots, percent |
76 // escapes, and unicode characters. | 89 // escapes, and unicode characters. |
90 '\\[([A-Fa-f0-9:.]*)\\])' | |
floitsch
2013/05/01 08:53:30
Shouldn't you restrict the number of possible digi
Anders Johnsen
2013/05/01 09:26:14
We don't really do validations like that atm, but
| |
91 // IPv6 domain - restrict to hex, | |
92 // dot and colon. | |
77 '(?::([0-9]+))?' // port | 93 '(?::([0-9]+))?' // port |
78 ')?' | 94 ')?' |
79 '([^?#]+)?' // path | 95 '([^?#\\[]+)?' // path |
80 '(?:\\?([^#]*))?' // query | 96 '(?:\\?([^#]*))?' // query |
81 '(?:#(.*))?' // fragment | 97 '(?:#(.*))?' // fragment |
82 '\$'); | 98 '\$'); |
83 | 99 |
84 static const _COMPONENT_SCHEME = 1; | 100 static const _COMPONENT_SCHEME = 1; |
85 static const _COMPONENT_USER_INFO = 2; | 101 static const _COMPONENT_USER_INFO = 2; |
86 static const _COMPONENT_DOMAIN = 3; | 102 static const _COMPONENT_DOMAIN = 3; |
87 static const _COMPONENT_PORT = 4; | 103 static const _COMPONENT_DOMAIN_IPV6 = 4; |
88 static const _COMPONENT_PATH = 5; | 104 static const _COMPONENT_PORT = 5; |
89 static const _COMPONENT_QUERY_DATA = 6; | 105 static const _COMPONENT_PATH = 6; |
90 static const _COMPONENT_FRAGMENT = 7; | 106 static const _COMPONENT_QUERY_DATA = 7; |
107 static const _COMPONENT_FRAGMENT = 8; | |
91 | 108 |
92 /** | 109 /** |
93 * Returns `true` if the URI is absolute. | 110 * Returns `true` if the URI is absolute. |
94 */ | 111 */ |
95 bool get isAbsolute { | 112 bool get isAbsolute { |
96 if ("" == scheme) return false; | 113 if ("" == scheme) return false; |
97 if ("" != fragment) return false; | 114 if ("" != fragment) return false; |
98 return true; | 115 return true; |
99 | 116 |
100 /* absolute-URI = scheme ":" hier-part [ "?" query ] | 117 /* absolute-URI = scheme ":" hier-part [ "?" query ] |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
215 } | 232 } |
216 return sb.toString(); | 233 return sb.toString(); |
217 } | 234 } |
218 | 235 |
219 String toString() { | 236 String toString() { |
220 StringBuffer sb = new StringBuffer(); | 237 StringBuffer sb = new StringBuffer(); |
221 _addIfNonEmpty(sb, scheme, scheme, ':'); | 238 _addIfNonEmpty(sb, scheme, scheme, ':'); |
222 if (hasAuthority || (scheme == "file")) { | 239 if (hasAuthority || (scheme == "file")) { |
223 sb.write("//"); | 240 sb.write("//"); |
224 _addIfNonEmpty(sb, userInfo, userInfo, "@"); | 241 _addIfNonEmpty(sb, userInfo, userInfo, "@"); |
225 sb.write(domain == null ? "null" : domain); | 242 sb.write(domain == null ? "null" : |
243 domain.contains(':') ? '[$domain]' : domain); | |
226 if (port != 0) { | 244 if (port != 0) { |
227 sb.write(":"); | 245 sb.write(":"); |
228 sb.write(port.toString()); | 246 sb.write(port.toString()); |
229 } | 247 } |
230 } | 248 } |
231 sb.write(path == null ? "null" : path); | 249 sb.write(path == null ? "null" : path); |
232 _addIfNonEmpty(sb, query, "?", query); | 250 _addIfNonEmpty(sb, query, "?", query); |
233 _addIfNonEmpty(sb, fragment, "#", fragment); | 251 _addIfNonEmpty(sb, fragment, "#", fragment); |
234 return sb.toString(); | 252 return sb.toString(); |
235 } | 253 } |
(...skipping 20 matching lines...) Expand all Loading... | |
256 } | 274 } |
257 | 275 |
258 static void _addIfNonEmpty(StringBuffer sb, String test, | 276 static void _addIfNonEmpty(StringBuffer sb, String test, |
259 String first, String second) { | 277 String first, String second) { |
260 if ("" != test) { | 278 if ("" != test) { |
261 sb.write(first == null ? "null" : first); | 279 sb.write(first == null ? "null" : first); |
262 sb.write(second == null ? "null" : second); | 280 sb.write(second == null ? "null" : second); |
263 } | 281 } |
264 } | 282 } |
265 } | 283 } |
OLD | NEW |