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 /// Generic utility functions. Stuff that should possibly be in core. | 5 /// Generic utility functions. Stuff that should possibly be in core. |
6 library utils; | 6 library utils; |
7 | 7 |
8 import 'dart:async'; | 8 import 'dart:async'; |
9 import 'dart:crypto'; | 9 import 'dart:crypto'; |
10 import 'dart:isolate'; | 10 import 'dart:isolate'; |
(...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
303 key = encodeUriComponent(key); | 303 key = encodeUriComponent(key); |
304 value = (value == null || value.isEmpty) ? null : encodeUriComponent(value); | 304 value = (value == null || value.isEmpty) ? null : encodeUriComponent(value); |
305 pairs.add([key, value]); | 305 pairs.add([key, value]); |
306 }); | 306 }); |
307 return pairs.map((pair) { | 307 return pairs.map((pair) { |
308 if (pair[1] == null) return pair[0]; | 308 if (pair[1] == null) return pair[0]; |
309 return "${pair[0]}=${pair[1]}"; | 309 return "${pair[0]}=${pair[1]}"; |
310 }).join("&"); | 310 }).join("&"); |
311 } | 311 } |
312 | 312 |
| 313 // TODO(nweiz): remove this when issue 9068 has been fixed. |
| 314 /// Whether [uri1] and [uri2] are equal. This consider HTTP URIs to default to |
| 315 /// port 80, and HTTPs URIs to default to port 443. |
| 316 bool urisEqual(Uri uri1, Uri uri2) => |
| 317 canonicalizeUri(uri1) == canonicalizeUri(uri2); |
| 318 |
| 319 /// Return [uri] with redundant port information removed. |
| 320 Uri canonicalizeUri(Uri uri) { |
| 321 var sansPort = new Uri.fromComponents( |
| 322 scheme: uri.scheme, userInfo: uri.userInfo, domain: uri.domain, |
| 323 path: uri.path, query: uri.query, fragment: uri.fragment); |
| 324 if (uri.scheme == 'http' && uri.port == 80) return sansPort; |
| 325 if (uri.scheme == 'https' && uri.port == 443) return sansPort; |
| 326 return uri; |
| 327 } |
| 328 |
313 /// Add all key/value pairs from [source] to [destination], overwriting any | 329 /// Add all key/value pairs from [source] to [destination], overwriting any |
314 /// pre-existing values. | 330 /// pre-existing values. |
315 void mapAddAll(Map destination, Map source) => | 331 void mapAddAll(Map destination, Map source) => |
316 source.forEach((key, value) => destination[key] = value); | 332 source.forEach((key, value) => destination[key] = value); |
317 | 333 |
318 /// Decodes a URL-encoded string. Unlike [decodeUriComponent], this includes | 334 /// Decodes a URL-encoded string. Unlike [decodeUriComponent], this includes |
319 /// replacing `+` with ` `. | 335 /// replacing `+` with ` `. |
320 String urlDecode(String encoded) => | 336 String urlDecode(String encoded) => |
321 decodeUriComponent(encoded.replaceAll("+", " ")); | 337 decodeUriComponent(encoded.replaceAll("+", " ")); |
OLD | NEW |