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 * [uri]: http://closure-library.googlecode.com/svn/docs/class_goog_Uri.html | 15 * [uri]: http://closure-library.googlecode.com/svn/docs/class_goog_Uri.html |
16 * [RFC-3986]: http://tools.ietf.org/html/rfc3986#section-4.3) | 16 * [RFC-3986]: http://tools.ietf.org/html/rfc3986#section-4.3) |
Søren Gjesse
2013/05/01 07:46:58
Add some more documentation on domain hostname/IPv
Anders Johnsen
2013/05/01 08:07:27
Done.
| |
17 */ | 17 */ |
18 class Uri { | 18 class Uri { |
19 final String scheme; | 19 final String scheme; |
20 final String userInfo; | 20 final String userInfo; |
21 final String domain; | 21 final String domain; |
22 final int port; | 22 final int port; |
23 final String path; | 23 final String path; |
24 final String query; | 24 final String query; |
25 final String fragment; | 25 final String fragment; |
26 | 26 |
27 /** | 27 /** |
28 * Deprecated. Please use [parse] instead. | 28 * Deprecated. Please use [parse] instead. |
Søren Gjesse
2013/05/01 07:46:58
Remove this while at it?
Anders Johnsen
2013/05/01 08:07:27
I'll wait a bit.
| |
29 */ | 29 */ |
30 Uri.fromString(String uri) : this._fromMatch(_splitRe.firstMatch(uri)); | 30 Uri.fromString(String uri) : this._fromMatch(_splitRe.firstMatch(uri)); |
31 | 31 |
32 static Uri parse(String uri) => new Uri._fromMatch(_splitRe.firstMatch(uri)); | 32 static Uri parse(String uri) => new Uri._fromMatch(_splitRe.firstMatch(uri)); |
33 | 33 |
34 Uri._fromMatch(Match m) : | 34 Uri._fromMatch(Match m) : |
35 this.fromComponents(scheme: _emptyIfNull(m[_COMPONENT_SCHEME]), | 35 this.fromComponents(scheme: _emptyIfNull(m[_COMPONENT_SCHEME]), |
36 userInfo: _emptyIfNull(m[_COMPONENT_USER_INFO]), | 36 userInfo: _emptyIfNull(m[_COMPONENT_USER_INFO]), |
37 domain: _emptyIfNull(m[_COMPONENT_DOMAIN]), | 37 domain: _eitherOf( |
38 m[_COMPONENT_DOMAIN], m[_COMPONENT_DOMAIN_IPV6]), | |
38 port: _parseIntOrZero(m[_COMPONENT_PORT]), | 39 port: _parseIntOrZero(m[_COMPONENT_PORT]), |
39 path: _emptyIfNull(m[_COMPONENT_PATH]), | 40 path: _emptyIfNull(m[_COMPONENT_PATH]), |
40 query: _emptyIfNull(m[_COMPONENT_QUERY_DATA]), | 41 query: _emptyIfNull(m[_COMPONENT_QUERY_DATA]), |
41 fragment: _emptyIfNull(m[_COMPONENT_FRAGMENT])); | 42 fragment: _emptyIfNull(m[_COMPONENT_FRAGMENT])); |
42 | 43 |
43 const Uri.fromComponents({this.scheme: "", | 44 const Uri.fromComponents({this.scheme: "", |
44 this.userInfo: "", | 45 this.userInfo: "", |
45 this.domain: "", | 46 this.domain: "", |
46 this.port: 0, | 47 this.port: 0, |
47 this.path: "", | 48 this.path: "", |
48 this.query: "", | 49 this.query: "", |
49 this.fragment: ""}); | 50 this.fragment: ""}); |
50 | 51 |
51 Uri(String uri) : this.fromString(uri); | 52 Uri(String uri) : this.fromString(uri); |
52 | 53 |
53 static String _emptyIfNull(String val) => val != null ? val : ''; | 54 static String _emptyIfNull(String val) => val != null ? val : ''; |
54 | 55 |
55 static int _parseIntOrZero(String val) { | 56 static int _parseIntOrZero(String val) { |
56 if (val != null && val != '') { | 57 if (val != null && val != '') { |
57 return int.parse(val); | 58 return int.parse(val); |
58 } else { | 59 } else { |
59 return 0; | 60 return 0; |
60 } | 61 } |
61 } | 62 } |
62 | 63 |
64 static String _eitherOf(String val1, String val2) { | |
65 if (val1 != null) return val1; | |
66 if (val2 != null) return val2; | |
67 return ''; | |
68 } | |
69 | |
63 // NOTE: This code was ported from: closure-library/closure/goog/uri/utils.js | 70 // NOTE: This code was ported from: closure-library/closure/goog/uri/utils.js |
64 static final RegExp _splitRe = new RegExp( | 71 static final RegExp _splitRe = new RegExp( |
65 '^' | 72 '^' |
66 '(?:' | 73 '(?:' |
67 '([^:/?#.]+)' // scheme - ignore special characters | 74 '([^:/?#.]+)' // scheme - ignore special characters |
68 // used by other URL parts such as :, | 75 // used by other URL parts such as :, |
69 // ?, /, #, and . | 76 // ?, /, #, and . |
70 ':)?' | 77 ':)?' |
71 '(?://' | 78 '(?://' |
72 '(?:([^/?#]*)@)?' // userInfo | 79 '(?:([^/?#]*)@)?' // userInfo |
73 '([\\w\\d\\-\\u0100-\\uffff.%]*)' | 80 '(?:' |
81 '([\\w\\d\\-\\u0100-\\uffff.%]*)|' | |
74 // domain - restrict to letters, | 82 // domain - restrict to letters, |
75 // digits, dashes, dots, percent | 83 // digits, dashes, dots, percent |
76 // escapes, and unicode characters. | 84 // escapes, and unicode characters. |
85 '\\[([A-Fa-f0-9:.]*)\\])' | |
86 // IPv6 domain - restrict to hex, | |
87 // dot and colon. | |
77 '(?::([0-9]+))?' // port | 88 '(?::([0-9]+))?' // port |
78 ')?' | 89 ')?' |
79 '([^?#]+)?' // path | 90 '([^?#\\[]+)?' // path |
80 '(?:\\?([^#]*))?' // query | 91 '(?:\\?([^#]*))?' // query |
81 '(?:#(.*))?' // fragment | 92 '(?:#(.*))?' // fragment |
82 '\$'); | 93 '\$'); |
83 | 94 |
84 static const _COMPONENT_SCHEME = 1; | 95 static const _COMPONENT_SCHEME = 1; |
85 static const _COMPONENT_USER_INFO = 2; | 96 static const _COMPONENT_USER_INFO = 2; |
86 static const _COMPONENT_DOMAIN = 3; | 97 static const _COMPONENT_DOMAIN = 3; |
87 static const _COMPONENT_PORT = 4; | 98 static const _COMPONENT_DOMAIN_IPV6 = 4; |
88 static const _COMPONENT_PATH = 5; | 99 static const _COMPONENT_PORT = 5; |
89 static const _COMPONENT_QUERY_DATA = 6; | 100 static const _COMPONENT_PATH = 6; |
90 static const _COMPONENT_FRAGMENT = 7; | 101 static const _COMPONENT_QUERY_DATA = 7; |
102 static const _COMPONENT_FRAGMENT = 8; | |
91 | 103 |
92 /** | 104 /** |
93 * Returns `true` if the URI is absolute. | 105 * Returns `true` if the URI is absolute. |
94 */ | 106 */ |
95 bool get isAbsolute { | 107 bool get isAbsolute { |
96 if ("" == scheme) return false; | 108 if ("" == scheme) return false; |
97 if ("" != fragment) return false; | 109 if ("" != fragment) return false; |
98 return true; | 110 return true; |
99 | 111 |
100 /* absolute-URI = scheme ":" hier-part [ "?" query ] | 112 /* absolute-URI = scheme ":" hier-part [ "?" query ] |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
215 } | 227 } |
216 return sb.toString(); | 228 return sb.toString(); |
217 } | 229 } |
218 | 230 |
219 String toString() { | 231 String toString() { |
220 StringBuffer sb = new StringBuffer(); | 232 StringBuffer sb = new StringBuffer(); |
221 _addIfNonEmpty(sb, scheme, scheme, ':'); | 233 _addIfNonEmpty(sb, scheme, scheme, ':'); |
222 if (hasAuthority || (scheme == "file")) { | 234 if (hasAuthority || (scheme == "file")) { |
223 sb.write("//"); | 235 sb.write("//"); |
224 _addIfNonEmpty(sb, userInfo, userInfo, "@"); | 236 _addIfNonEmpty(sb, userInfo, userInfo, "@"); |
225 sb.write(domain == null ? "null" : domain); | 237 sb.write(domain == null ? "null" : |
238 domain.contains(':') ? '[$domain]' : domain); | |
226 if (port != 0) { | 239 if (port != 0) { |
227 sb.write(":"); | 240 sb.write(":"); |
228 sb.write(port.toString()); | 241 sb.write(port.toString()); |
229 } | 242 } |
230 } | 243 } |
231 sb.write(path == null ? "null" : path); | 244 sb.write(path == null ? "null" : path); |
232 _addIfNonEmpty(sb, query, "?", query); | 245 _addIfNonEmpty(sb, query, "?", query); |
233 _addIfNonEmpty(sb, fragment, "#", fragment); | 246 _addIfNonEmpty(sb, fragment, "#", fragment); |
234 return sb.toString(); | 247 return sb.toString(); |
235 } | 248 } |
(...skipping 20 matching lines...) Expand all Loading... | |
256 } | 269 } |
257 | 270 |
258 static void _addIfNonEmpty(StringBuffer sb, String test, | 271 static void _addIfNonEmpty(StringBuffer sb, String test, |
259 String first, String second) { | 272 String first, String second) { |
260 if ("" != test) { | 273 if ("" != test) { |
261 sb.write(first == null ? "null" : first); | 274 sb.write(first == null ? "null" : first); |
262 sb.write(second == null ? "null" : second); | 275 sb.write(second == null ? "null" : second); |
263 } | 276 } |
264 } | 277 } |
265 } | 278 } |
OLD | NEW |