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 utils; | 5 library utils; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:uri'; | |
9 import 'dart:isolate'; | 8 import 'dart:isolate'; |
10 import 'dart:crypto'; | 9 import 'dart:crypto'; |
11 | 10 |
12 /// Adds additional query parameters to [url], overwriting the original | 11 /// Adds additional query parameters to [url], overwriting the original |
13 /// parameters if a name conflict occurs. | 12 /// parameters if a name conflict occurs. |
14 Uri addQueryParameters(Uri url, Map<String, String> parameters) { | 13 Uri addQueryParameters(Uri url, Map<String, String> parameters) { |
15 var queryMap = queryToMap(url.query); | 14 var queryMap = queryToMap(url.query); |
16 mapAddAll(queryMap, parameters); | 15 mapAddAll(queryMap, parameters); |
17 return url.resolve("?${mapToQuery(queryMap)}"); | 16 return url.resolve("?${mapToQuery(queryMap)}"); |
18 } | 17 } |
19 | 18 |
20 /// Convert a URL query string (or `application/x-www-form-urlencoded` body) | 19 /// Convert a URL query string (or `application/x-www-form-urlencoded` body) |
21 /// into a [Map] from parameter names to values. | 20 /// into a [Map] from parameter names to values. |
22 Map<String, String> queryToMap(String queryList) { | 21 Map<String, String> queryToMap(String queryList) { |
23 var map = {}; | 22 var map = {}; |
24 for (var pair in queryList.split("&")) { | 23 for (var pair in queryList.split("&")) { |
25 var split = split1(pair, "="); | 24 var split = split1(pair, "="); |
26 if (split.isEmpty) continue; | 25 if (split.isEmpty) continue; |
27 var key = urlDecode(split[0]); | 26 var key = urlDecode(split[0]); |
28 var value = split.length > 1 ? urlDecode(split[1]) : ""; | 27 var value = split.length > 1 ? urlDecode(split[1]) : ""; |
29 map[key] = value; | 28 map[key] = value; |
30 } | 29 } |
31 return map; | 30 return map; |
32 } | 31 } |
33 | 32 |
34 /// Convert a [Map] from parameter names to values to a URL query string. | 33 /// Convert a [Map] from parameter names to values to a URL query string. |
35 String mapToQuery(Map<String, String> map) { | 34 String mapToQuery(Map<String, String> map) { |
36 var pairs = <List<String>>[]; | 35 var pairs = <List<String>>[]; |
37 map.forEach((key, value) { | 36 map.forEach((key, value) { |
38 key = encodeUriComponent(key); | 37 key = Uri.encodeQueryComponent(key); |
39 value = (value == null || value.isEmpty) ? null : encodeUriComponent(value); | 38 value = (value == null || value.isEmpty) |
| 39 ? null |
| 40 : Uri.encodeQueryComponent(value); |
40 pairs.add([key, value]); | 41 pairs.add([key, value]); |
41 }); | 42 }); |
42 return pairs.map((pair) { | 43 return pairs.map((pair) { |
43 if (pair[1] == null) return pair[0]; | 44 if (pair[1] == null) return pair[0]; |
44 return "${pair[0]}=${pair[1]}"; | 45 return "${pair[0]}=${pair[1]}"; |
45 }).join("&"); | 46 }).join("&"); |
46 } | 47 } |
47 | 48 |
48 /// Add all key/value pairs from [source] to [destination], overwriting any | 49 /// Add all key/value pairs from [source] to [destination], overwriting any |
49 /// pre-existing values. | 50 /// pre-existing values. |
50 void mapAddAll(Map destination, Map source) => | 51 void mapAddAll(Map destination, Map source) => |
51 source.forEach((key, value) => destination[key] = value); | 52 source.forEach((key, value) => destination[key] = value); |
52 | 53 |
53 /// Decode a URL-encoded string. Unlike [decodeUriComponent], this includes | 54 /// Decode a URL-encoded string. Unlike [Uri.decodeComponent], this includes |
54 /// replacing `+` with ` `. | 55 /// replacing `+` with ` `. |
55 String urlDecode(String encoded) => | 56 String urlDecode(String encoded) => |
56 decodeUriComponent(encoded.replaceAll("+", " ")); | 57 Uri.decodeComponent(encoded.replaceAll("+", " ")); |
57 | 58 |
58 /// Like [String.split], but only splits on the first occurrence of the pattern. | 59 /// Like [String.split], but only splits on the first occurrence of the pattern. |
59 /// This will always return a list of two elements or fewer. | 60 /// This will always return a list of two elements or fewer. |
60 List<String> split1(String toSplit, String pattern) { | 61 List<String> split1(String toSplit, String pattern) { |
61 if (toSplit.isEmpty) return <String>[]; | 62 if (toSplit.isEmpty) return <String>[]; |
62 | 63 |
63 var index = toSplit.indexOf(pattern); | 64 var index = toSplit.indexOf(pattern); |
64 if (index == -1) return [toSplit]; | 65 if (index == -1) return [toSplit]; |
65 return [toSplit.substring(0, index), | 66 return [toSplit.substring(0, index), |
66 toSplit.substring(index + pattern.length)]; | 67 toSplit.substring(index + pattern.length)]; |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 throw new FormatException('Invalid WWW-Authenticate header: "$header"'); | 109 throw new FormatException('Invalid WWW-Authenticate header: "$header"'); |
109 } | 110 } |
110 | 111 |
111 return new AuthenticateHeader(scheme, parameters); | 112 return new AuthenticateHeader(scheme, parameters); |
112 } | 113 } |
113 } | 114 } |
114 | 115 |
115 /// Returns a [Future] that asynchronously completes to `null`. | 116 /// Returns a [Future] that asynchronously completes to `null`. |
116 Future get async => new Future.delayed(const Duration(milliseconds: 0), | 117 Future get async => new Future.delayed(const Duration(milliseconds: 0), |
117 () => null); | 118 () => null); |
OLD | NEW |