| 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 * 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 domain part. |
| 19 * [URI]: http://closure-library.googlecode.com/svn/docs/class_goog_Uri.html |
| 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 /** | |
| 28 * Deprecated. Please use [parse] instead. | |
| 29 */ | |
| 30 Uri.fromString(String uri) : this._fromMatch(_splitRe.firstMatch(uri)); | |
| 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 factory Uri(String uri) => parse(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 r'([\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 '|' |
| 86 // TODO(ajohnsen): Only allow a max number of parts? |
| 87 r'\[([A-Fa-f0-9:.]*)\])' |
| 88 // IPv6 domain - restrict to hex, |
| 89 // dot and colon. |
| 77 '(?::([0-9]+))?' // port | 90 '(?::([0-9]+))?' // port |
| 78 ')?' | 91 ')?' |
| 79 '([^?#]+)?' // path | 92 r'([^?#[]+)?' // path |
| 80 '(?:\\?([^#]*))?' // query | 93 r'(?:\?([^#]*))?' // query |
| 81 '(?:#(.*))?' // fragment | 94 '(?:#(.*))?' // fragment |
| 82 '\$'); | 95 r'$'); |
| 83 | 96 |
| 84 static const _COMPONENT_SCHEME = 1; | 97 static const _COMPONENT_SCHEME = 1; |
| 85 static const _COMPONENT_USER_INFO = 2; | 98 static const _COMPONENT_USER_INFO = 2; |
| 86 static const _COMPONENT_DOMAIN = 3; | 99 static const _COMPONENT_DOMAIN = 3; |
| 87 static const _COMPONENT_PORT = 4; | 100 static const _COMPONENT_DOMAIN_IPV6 = 4; |
| 88 static const _COMPONENT_PATH = 5; | 101 static const _COMPONENT_PORT = 5; |
| 89 static const _COMPONENT_QUERY_DATA = 6; | 102 static const _COMPONENT_PATH = 6; |
| 90 static const _COMPONENT_FRAGMENT = 7; | 103 static const _COMPONENT_QUERY_DATA = 7; |
| 104 static const _COMPONENT_FRAGMENT = 8; |
| 91 | 105 |
| 92 /** | 106 /** |
| 93 * Returns `true` if the URI is absolute. | 107 * Returns `true` if the URI is absolute. |
| 94 */ | 108 */ |
| 95 bool get isAbsolute { | 109 bool get isAbsolute { |
| 96 if ("" == scheme) return false; | 110 if ("" == scheme) return false; |
| 97 if ("" != fragment) return false; | 111 if ("" != fragment) return false; |
| 98 return true; | 112 return true; |
| 99 | 113 |
| 100 /* absolute-URI = scheme ":" hier-part [ "?" query ] | 114 /* absolute-URI = scheme ":" hier-part [ "?" query ] |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 215 } | 229 } |
| 216 return sb.toString(); | 230 return sb.toString(); |
| 217 } | 231 } |
| 218 | 232 |
| 219 String toString() { | 233 String toString() { |
| 220 StringBuffer sb = new StringBuffer(); | 234 StringBuffer sb = new StringBuffer(); |
| 221 _addIfNonEmpty(sb, scheme, scheme, ':'); | 235 _addIfNonEmpty(sb, scheme, scheme, ':'); |
| 222 if (hasAuthority || (scheme == "file")) { | 236 if (hasAuthority || (scheme == "file")) { |
| 223 sb.write("//"); | 237 sb.write("//"); |
| 224 _addIfNonEmpty(sb, userInfo, userInfo, "@"); | 238 _addIfNonEmpty(sb, userInfo, userInfo, "@"); |
| 225 sb.write(domain == null ? "null" : domain); | 239 sb.write(domain == null ? "null" : |
| 240 domain.contains(':') ? '[$domain]' : domain); |
| 226 if (port != 0) { | 241 if (port != 0) { |
| 227 sb.write(":"); | 242 sb.write(":"); |
| 228 sb.write(port.toString()); | 243 sb.write(port.toString()); |
| 229 } | 244 } |
| 230 } | 245 } |
| 231 sb.write(path == null ? "null" : path); | 246 sb.write(path == null ? "null" : path); |
| 232 _addIfNonEmpty(sb, query, "?", query); | 247 _addIfNonEmpty(sb, query, "?", query); |
| 233 _addIfNonEmpty(sb, fragment, "#", fragment); | 248 _addIfNonEmpty(sb, fragment, "#", fragment); |
| 234 return sb.toString(); | 249 return sb.toString(); |
| 235 } | 250 } |
| (...skipping 20 matching lines...) Expand all Loading... |
| 256 } | 271 } |
| 257 | 272 |
| 258 static void _addIfNonEmpty(StringBuffer sb, String test, | 273 static void _addIfNonEmpty(StringBuffer sb, String test, |
| 259 String first, String second) { | 274 String first, String second) { |
| 260 if ("" != test) { | 275 if ("" != test) { |
| 261 sb.write(first == null ? "null" : first); | 276 sb.write(first == null ? "null" : first); |
| 262 sb.write(second == null ? "null" : second); | 277 sb.write(second == null ? "null" : second); |
| 263 } | 278 } |
| 264 } | 279 } |
| 265 } | 280 } |
| OLD | NEW |