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

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

Issue 11363063: Add an HTTP library that wraps dart:io. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 1 month 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/streamed_response.dart ('k') | pkg/http/pubspec.yaml » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 library utils;
6
7 import 'dart:io';
8 import 'dart:isolate';
9 import 'dart:scalarlist';
10 import 'dart:uri';
11
12 /// Converts a URL query string (or `application/x-www-form-urlencoded` body)
13 /// into a [Map] from parameter names to values.
14 ///
15 /// queryToMap("foo=bar&baz=bang&qux");
16 /// //=> {"foo": "bar", "baz": "bang", "qux": ""}
17 Map<String, String> queryToMap(String queryList) {
18 var map = <String>{};
19 for (var pair in queryList.split("&")) {
20 var split = split1(pair, "=");
21 if (split.isEmpty) continue;
22 var key = urlDecode(split[0]);
23 var value = urlDecode(split.length > 1 ? split[1] : "");
24 map[key] = value;
25 }
26 return map;
27 }
28
29 /// Converts a [Map] from parameter names to values to a URL query string.
30 ///
31 /// mapToQuery({"foo": "bar", "baz": "bang"});
32 /// //=> "foo=bar&baz=bang"
33 String mapToQuery(Map<String, String> map) {
34 var pairs = <List<String>>[];
35 map.forEach((key, value) =>
36 pairs.add([encodeUriComponent(key), encodeUriComponent(value)]));
37 return Strings.join(pairs.map((pair) => "${pair[0]}=${pair[1]}"), "&");
38 }
39
40 /// Adds all key/value pairs from [source] to [destination], overwriting any
41 /// pre-existing values.
42 ///
43 /// var a = {"foo": "bar", "baz": "bang"};
44 /// mapAddAll(a, {"baz": "zap", "qux": "quux"});
45 /// a; //=> {"foo": "bar", "baz": "zap", "qux": "quux"}
46 void mapAddAll(Map destination, Map source) =>
47 source.forEach((key, value) => destination[key] = value);
48
49 /// Decodes a URL-encoded string. Unlike [decodeUriComponent], this includes
50 /// replacing `+` with ` `.
51 String urlDecode(String encoded) =>
52 decodeUriComponent(encoded.replaceAll("+", " "));
53
54 /// Like [String.split], but only splits on the first occurrence of the pattern.
55 /// This will always return an array of two elements or fewer.
56 ///
57 /// split1("foo,bar,baz", ","); //=> ["foo", "bar,baz"]
58 /// split1("foo", ","); //=> ["foo"]
59 /// split1("", ","); //=> []
60 List<String> split1(String toSplit, String pattern) {
61 if (toSplit.isEmpty) return <String>[];
62
63 var index = toSplit.indexOf(pattern);
64 if (index == -1) return [toSplit];
65 return [
66 toSplit.substring(0, index),
67 toSplit.substring(index + pattern.length)
68 ];
69 }
70
71 /// Returns the [Encoding] that corresponds to [charset]. Returns
72 /// [Encoding.ISO_8859_1] if [charset] is null or if no [Encoding] was found
73 /// that corresponds to [charset].
74 Encoding encodingForCharset(String charset) {
75 if (charset == null) return Encoding.ISO_8859_1;
76 var encoding = _encodingForCharset(charset);
77 return encoding == null ? Encoding.ISO_8859_1 : encoding;
78 }
79
80 /// Returns the [Encoding] that corresponds to [charset]. Throws a [FormatExcept ion]
81 /// if no [Encoding] was found that corresponds to [charset]. [charset] may not
82 /// be null.
83 Encoding requiredEncodingForCharset(String charset) {
84 var encoding = _encodingForCharset(charset);
85 if (encoding != null) return encoding;
86 throw new FormatException('Unsupported encoding "$charset".');
87 }
88
89 /// Returns the [Encoding] that corresponds to [charset]. Returns null if no
90 /// [Encoding] was found that corresponds to [charset]. [charset] may not be
91 /// null.
92 Encoding _encodingForCharset(String charset) {
93 charset = charset.toLowerCase();
94 if (charset == 'ascii' || charset == 'us-ascii') return Encoding.ASCII;
95 if (charset == 'utf-8') return Encoding.UTF_8;
96 if (charset == 'iso-8859-1') return Encoding.ISO_8859_1;
97 return null;
98 }
99
100 /// Converts [bytes] into a [String] according to [encoding].
101 String decodeString(List<int> bytes, Encoding encoding) {
102 // TODO(nweiz): implement this once issue 6284 is fixed.
103 return new String.fromCharCodes(bytes);
104 }
105
106 /// Converts [string] into a byte array according to [encoding].
107 List<int> encodeString(String string, Encoding encoding) {
108 // TODO(nweiz): implement this once issue 6284 is fixed.
109 return string.charCodes;
110 }
111
112 /// Converts [input] into a [Uint8List]. If [input] is a [ByteArray] or
113 /// [ByteArrayViewable], this just returns a view on [input].
114 Uint8List toUint8List(List<int> input) {
115 if (input is Uint8List) return input;
116 if (input is ByteArrayViewable) input = input.asByteArray();
117 if (input is ByteArray) return new Uint8List.view(input);
118 var output = new Uint8List(input.length);
119 output.setRange(0, input.length, input);
120 return output;
121 }
122
123 /// Buffers all input from an InputStream and returns it as a future.
124 Future<List<int>> consumeInputStream(InputStream stream) {
125 var completer = new Completer<List<int>>();
126 /// TODO(nweiz): use BufferList when issue 6409 is fixed
127 var buffer = <int>[];
128 stream.onClosed = () => completer.complete(buffer);
129 stream.onData = () => buffer.addAll(stream.read());
130 stream.onError = completer.completeException;
131 return completer.future;
132 }
133
134 /// Takes all input from [source] and writes it to [sink].
135 void pipeInputToInput(InputStream source, ListInputStream sink) {
136 source.onClosed = () => sink.markEndOfStream();
137 source.onData = () => sink.write(source.read());
138 // TODO(nweiz): propagate source errors to the sink. See issue 3657.
139 }
140
141 /// Returns a [Future] that asynchronously completes to `null`.
142 Future get async {
143 var completer = new Completer();
144 new Timer(0, (_) => completer.complete(null));
145 return completer.future;
146 }
OLDNEW
« no previous file with comments | « pkg/http/lib/src/streamed_response.dart ('k') | pkg/http/pubspec.yaml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698