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