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 part of dart.core; | 5 part of dart.core; |
6 | 6 |
7 /** | 7 /** |
8 * A parsed URI, as specified by RFC-3986, http://tools.ietf.org/html/rfc3986. | 8 * A parsed URI, as specified by RFC-3986, http://tools.ietf.org/html/rfc3986. |
9 */ | 9 */ |
10 class Uri { | 10 class Uri { |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 this(scheme: _emptyIfNull(m[_COMPONENT_SCHEME]), | 101 this(scheme: _emptyIfNull(m[_COMPONENT_SCHEME]), |
102 userInfo: _emptyIfNull(m[_COMPONENT_USER_INFO]), | 102 userInfo: _emptyIfNull(m[_COMPONENT_USER_INFO]), |
103 host: _eitherOf( | 103 host: _eitherOf( |
104 m[_COMPONENT_HOST], m[_COMPONENT_HOST_IPV6]), | 104 m[_COMPONENT_HOST], m[_COMPONENT_HOST_IPV6]), |
105 port: _parseIntOrZero(m[_COMPONENT_PORT]), | 105 port: _parseIntOrZero(m[_COMPONENT_PORT]), |
106 path: _emptyIfNull(m[_COMPONENT_PATH]), | 106 path: _emptyIfNull(m[_COMPONENT_PATH]), |
107 query: _emptyIfNull(m[_COMPONENT_QUERY_DATA]), | 107 query: _emptyIfNull(m[_COMPONENT_QUERY_DATA]), |
108 fragment: _emptyIfNull(m[_COMPONENT_FRAGMENT])); | 108 fragment: _emptyIfNull(m[_COMPONENT_FRAGMENT])); |
109 | 109 |
110 /** | 110 /** |
111 * Create a new URI from its components. | 111 * Creates a new URI from its components. |
112 * | 112 * |
113 * Each component is set through a named argument. Any number of | 113 * Each component is set through a named argument. Any number of |
114 * components can be provided. The default value for the components | 114 * components can be provided. The default value for the components |
115 * not provided is the empry string, except for [port] which has a | 115 * not provided is the empry string, except for [port] which has a |
116 * default value of 0. The [path] and [query] components can be set | 116 * default value of 0. The [path] and [query] components can be set |
117 * using two different named arguments. | 117 * using two different named arguments. |
118 * | 118 * |
119 * The scheme component is set through [scheme]. The scheme is | 119 * The scheme component is set through [scheme]. The scheme is |
120 * normalized to all lowercase letters. | 120 * normalized to all lowercase letters. |
121 * | 121 * |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
171 } else if (scheme == "https" && port == 443) { | 171 } else if (scheme == "https" && port == 443) { |
172 _port = 0; | 172 _port = 0; |
173 } else { | 173 } else { |
174 _port = port; | 174 _port = port; |
175 } | 175 } |
176 // Fill the path. | 176 // Fill the path. |
177 _path = _makePath(path, pathSegments); | 177 _path = _makePath(path, pathSegments); |
178 } | 178 } |
179 | 179 |
180 /** | 180 /** |
| 181 * Creates a new `http` URI from authority, path and query. |
| 182 * |
| 183 * Examples: |
| 184 * |
| 185 * // Create the URI http://example.org/path?q=abc. |
| 186 * new Uri.http("google.com", "/search", { "q" : "dart" });http://example.
org/path?q=abc. |
| 187 * new Uri.http("user:pass@localhost:8080, ""); // http://user:pass@local
host:8080/ |
| 188 * new Uri.http("example.org, "a b"); // http://example.org/a%20b |
| 189 * new Uri.http("example.org, "/a%2F"); // http://example.org/a%25%2F |
| 190 * |
| 191 * The `scheme` is always set to `http`. |
| 192 * |
| 193 * The `userInfo`, `host` and `port` components are set from the |
| 194 * [authority] argument. |
| 195 * |
| 196 * The `path` component is set from the [unencodedPath] |
| 197 * argument. The path passed must not be encoded as this constructor |
| 198 * encodes the path. |
| 199 * |
| 200 * The `query` component is set from the optional [queryParameters] |
| 201 * argument. |
| 202 */ |
| 203 factory Uri.http(String authority, |
| 204 String unencodedPath, |
| 205 [Map<String, String> queryParameters]) { |
| 206 return _makeHttpUri("http", authority, unencodedPath, queryParameters); |
| 207 } |
| 208 |
| 209 /** |
| 210 * Creates a new `https` URI from authority, path and query. |
| 211 * |
| 212 * This constructor is the same as [Uri.http] except for the scheme |
| 213 * which is set to `https`. |
| 214 */ |
| 215 factory Uri.https(String authority, |
| 216 String unencodedPath, |
| 217 [Map<String, String> queryParameters]) { |
| 218 return _makeHttpUri("https", authority, unencodedPath, queryParameters); |
| 219 } |
| 220 |
| 221 static Uri _makeHttpUri(String scheme, |
| 222 String authority, |
| 223 String unencodedPath, |
| 224 Map<String, String> queryParameters) { |
| 225 var userInfo = ""; |
| 226 var host = ""; |
| 227 var port = 0; |
| 228 |
| 229 var hostStart = 0; |
| 230 // Split off the user info. |
| 231 bool hasUserInfo = false; |
| 232 for (int i = 0; i < authority.length; i++) { |
| 233 if (authority.codeUnitAt(i) == _AT_SIGN) { |
| 234 hasUserInfo = true; |
| 235 userInfo = authority.substring(0, i); |
| 236 hostStart = i + 1; |
| 237 break; |
| 238 } |
| 239 } |
| 240 // Split host and port. |
| 241 bool hasPort = false; |
| 242 for (int i = hostStart; i < authority.length; i++) { |
| 243 if (authority.codeUnitAt(i) == _COLON) { |
| 244 hasPort = true; |
| 245 host = authority.substring(hostStart, i); |
| 246 if (!host.isEmpty) { |
| 247 var portString = authority.substring(i + 1); |
| 248 if (portString.isNotEmpty) port = int.parse(portString); |
| 249 } |
| 250 break; |
| 251 } |
| 252 } |
| 253 if (!hasPort) { |
| 254 host = hasUserInfo ? authority.substring(hostStart) : authority; |
| 255 } |
| 256 |
| 257 return new Uri(scheme: scheme, |
| 258 userInfo: userInfo, |
| 259 host: host, |
| 260 port: port, |
| 261 pathSegments: unencodedPath.split("/"), |
| 262 queryParameters: queryParameters); |
| 263 } |
| 264 |
| 265 /** |
181 * Returns the URI path split into its segments. Each of the | 266 * Returns the URI path split into its segments. Each of the |
182 * segments in the returned list have been decoded. If the path is | 267 * segments in the returned list have been decoded. If the path is |
183 * empty the empty list will be returned. A leading slash `/` does | 268 * empty the empty list will be returned. A leading slash `/` does |
184 * not affect the segments returned. | 269 * not affect the segments returned. |
185 * | 270 * |
186 * The returned list is unmodifiable and will throw [UnsupportedError] on any | 271 * The returned list is unmodifiable and will throw [UnsupportedError] on any |
187 * calls that would mutate it. | 272 * calls that would mutate it. |
188 */ | 273 */ |
189 List<String> get pathSegments { | 274 List<String> get pathSegments { |
190 if (_pathSegments == null) { | 275 if (_pathSegments == null) { |
(...skipping 548 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
739 }); | 824 }); |
740 } | 825 } |
741 | 826 |
742 // Frequently used character codes. | 827 // Frequently used character codes. |
743 static const int _PERCENT = 0x25; | 828 static const int _PERCENT = 0x25; |
744 static const int _PLUS = 0x2B; | 829 static const int _PLUS = 0x2B; |
745 static const int _SLASH = 0x2F; | 830 static const int _SLASH = 0x2F; |
746 static const int _ZERO = 0x30; | 831 static const int _ZERO = 0x30; |
747 static const int _NINE = 0x39; | 832 static const int _NINE = 0x39; |
748 static const int _COLON = 0x3A; | 833 static const int _COLON = 0x3A; |
| 834 static const int _AT_SIGN = 0x40; |
749 static const int _UPPER_CASE_A = 0x41; | 835 static const int _UPPER_CASE_A = 0x41; |
750 static const int _UPPER_CASE_F = 0x46; | 836 static const int _UPPER_CASE_F = 0x46; |
751 static const int _LOWER_CASE_A = 0x61; | 837 static const int _LOWER_CASE_A = 0x61; |
752 static const int _LOWER_CASE_F = 0x66; | 838 static const int _LOWER_CASE_F = 0x66; |
753 | 839 |
754 /** | 840 /** |
755 * This is the internal implementation of JavaScript's encodeURI function. | 841 * This is the internal implementation of JavaScript's encodeURI function. |
756 * It encodes all characters in the string [text] except for those | 842 * It encodes all characters in the string [text] except for those |
757 * that appear in [canonicalTable], and returns the escaped string. | 843 * that appear in [canonicalTable], and returns the escaped string. |
758 */ | 844 */ |
(...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1034 void clear() { | 1120 void clear() { |
1035 throw new UnsupportedError("Cannot modify an unmodifiable map"); | 1121 throw new UnsupportedError("Cannot modify an unmodifiable map"); |
1036 } | 1122 } |
1037 void forEach(void f(K key, V value)) => _map.forEach(f); | 1123 void forEach(void f(K key, V value)) => _map.forEach(f); |
1038 Iterable<K> get keys => _map.keys; | 1124 Iterable<K> get keys => _map.keys; |
1039 Iterable<V> get values => _map.values; | 1125 Iterable<V> get values => _map.values; |
1040 int get length => _map.length; | 1126 int get length => _map.length; |
1041 bool get isEmpty => _map.isEmpty; | 1127 bool get isEmpty => _map.isEmpty; |
1042 bool get isNotEmpty => _map.isNotEmpty; | 1128 bool get isNotEmpty => _map.isNotEmpty; |
1043 } | 1129 } |
OLD | NEW |