Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(544)

Side by Side Diff: pkg/http/lib/src/utils.dart

Issue 23450003: Add encoding tests to pkg/http. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « pkg/http/lib/src/request.dart ('k') | pkg/http/test/multipart_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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:convert'; 8 import 'dart:convert';
9 import 'dart:io'; 9 import 'dart:io';
10 import 'dart:typed_data'; 10 import 'dart:typed_data';
11 11
12 import 'byte_stream.dart'; 12 import 'byte_stream.dart';
13 13
14 /// Converts a URL query string (or `application/x-www-form-urlencoded` body) 14 /// Converts a URL query string (or `application/x-www-form-urlencoded` body)
15 /// into a [Map] from parameter names to values. 15 /// into a [Map] from parameter names to values.
16 /// 16 ///
17 /// queryToMap("foo=bar&baz=bang&qux"); 17 /// queryToMap("foo=bar&baz=bang&qux");
18 /// //=> {"foo": "bar", "baz": "bang", "qux": ""} 18 /// //=> {"foo": "bar", "baz": "bang", "qux": ""}
19 Map<String, String> queryToMap(String queryList) { 19 Map<String, String> queryToMap(String queryList, {Encoding encoding}) {
20 var map = {}; 20 var map = {};
21 for (var pair in queryList.split("&")) { 21 for (var pair in queryList.split("&")) {
22 var split = split1(pair, "="); 22 var split = split1(pair, "=");
23 if (split.isEmpty) continue; 23 if (split.isEmpty) continue;
24 var key = urlDecode(split[0]); 24 var key = Uri.decodeQueryComponent(split[0], decode: encoding.decode);
25 var value = urlDecode(split.length > 1 ? split[1] : ""); 25 var value = Uri.decodeQueryComponent(split.length > 1 ? split[1] : "",
26 decode: encoding.decode);
26 map[key] = value; 27 map[key] = value;
27 } 28 }
28 return map; 29 return map;
29 } 30 }
30 31
31 /// Converts a [Map] from parameter names to values to a URL query string. 32 /// Converts a [Map] from parameter names to values to a URL query string.
32 /// 33 ///
33 /// mapToQuery({"foo": "bar", "baz": "bang"}); 34 /// mapToQuery({"foo": "bar", "baz": "bang"});
34 /// //=> "foo=bar&baz=bang" 35 /// //=> "foo=bar&baz=bang"
35 String mapToQuery(Map<String, String> map) { 36 String mapToQuery(Map<String, String> map, {Encoding encoding}) {
36 var pairs = <List<String>>[]; 37 var pairs = <List<String>>[];
37 map.forEach((key, value) => 38 map.forEach((key, value) =>
38 pairs.add([Uri.encodeQueryComponent(key), 39 pairs.add([urlEncode(key, encoding: encoding),
39 Uri.encodeQueryComponent(value)])); 40 urlEncode(value, encoding: encoding)]));
40 return pairs.map((pair) => "${pair[0]}=${pair[1]}").join("&"); 41 return pairs.map((pair) => "${pair[0]}=${pair[1]}").join("&");
41 } 42 }
42 43
43 /// Decodes a URL-encoded string. Unlike [Uri.decodeComponent], this includes 44 // TODO(nweiz): get rid of this when issue 12780 is fixed.
44 /// replacing `+` with ` `. 45 /// URL-encodes [source] using [encoding].
45 String urlDecode(String encoded) => 46 String urlEncode(String source, {Encoding encoding}) {
46 Uri.decodeComponent(encoded.replaceAll("+", " ")); 47 if (encoding == null) encoding = UTF8;
48 return encoding.encode(source).map((byte) {
49 // Convert spaces to +, like encodeQueryComponent.
50 if (byte == 0x20) return '+';
51 // Pass through digits.
52 if ((byte >= 0x30 && byte < 0x3A) ||
53 // Pass through uppercase letters.
54 (byte >= 0x41 && byte < 0x5B) ||
55 // Pass through lowercase letters.
56 (byte >= 0x61 && byte < 0x7B) ||
57 // Pass through `-._~`.
58 (byte == 0x2D || byte == 0x2E || byte == 0x5F || byte == 0x7E)) {
59 return new String.fromCharCode(byte);
60 }
61 return '%' + byte.toRadixString(16).toUpperCase();
62 }).join();
63 }
47 64
48 /// Like [String.split], but only splits on the first occurrence of the pattern. 65 /// Like [String.split], but only splits on the first occurrence of the pattern.
49 /// This will always return an array of two elements or fewer. 66 /// This will always return an array of two elements or fewer.
50 /// 67 ///
51 /// split1("foo,bar,baz", ","); //=> ["foo", "bar,baz"] 68 /// split1("foo,bar,baz", ","); //=> ["foo", "bar,baz"]
52 /// split1("foo", ","); //=> ["foo"] 69 /// split1("foo", ","); //=> ["foo"]
53 /// split1("", ","); //=> [] 70 /// split1("", ","); //=> []
54 List<String> split1(String toSplit, String pattern) { 71 List<String> split1(String toSplit, String pattern) {
55 if (toSplit.isEmpty) return <String>[]; 72 if (toSplit.isEmpty) return <String>[];
56 73
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after
213 /// The return values of all [Future]s are discarded. Any errors will cause the 230 /// The return values of all [Future]s are discarded. Any errors will cause the
214 /// iteration to stop and will be piped through the return value. 231 /// iteration to stop and will be piped through the return value.
215 Future forEachFuture(Iterable input, Future fn(element)) { 232 Future forEachFuture(Iterable input, Future fn(element)) {
216 var iterator = input.iterator; 233 var iterator = input.iterator;
217 Future nextElement(_) { 234 Future nextElement(_) {
218 if (!iterator.moveNext()) return new Future.value(); 235 if (!iterator.moveNext()) return new Future.value();
219 return fn(iterator.current).then(nextElement); 236 return fn(iterator.current).then(nextElement);
220 } 237 }
221 return nextElement(null); 238 return nextElement(null);
222 } 239 }
OLDNEW
« no previous file with comments | « pkg/http/lib/src/request.dart ('k') | pkg/http/test/multipart_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698