Chromium Code Reviews| Index: pkg/http/lib/src/utils.dart |
| diff --git a/pkg/http/lib/src/utils.dart b/pkg/http/lib/src/utils.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..826fd8c554df77577f9c9885d5c7e56e533e2027 |
| --- /dev/null |
| +++ b/pkg/http/lib/src/utils.dart |
| @@ -0,0 +1,130 @@ |
| +// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +library utils; |
| + |
| +import 'dart:io'; |
| +import 'dart:isolate'; |
| +import 'dart:scalarlist'; |
| +import 'dart:uri'; |
| + |
| +/// Convert a URL query string (or `application/x-www-form-urlencoded` body) |
| +/// into a [Map] from parameter names to values. |
|
Bob Nystrom
2012/10/31 01:17:44
Show an example here and elsewhere. In general, I'
nweiz
2012/10/31 18:20:59
Done.
|
| +Map<String, String> queryToMap(String queryList) { |
| + var map = <String>{}; |
| + for (var pair in queryList.split("&")) { |
| + var split = split1(pair, "="); |
| + if (split.isEmpty) continue; |
| + var key = urlDecode(split[0]); |
| + var value = urlDecode(split.length > 1 ? split[1] : ""); |
| + map[key] = value; |
| + } |
| + return map; |
| +} |
| + |
| +/// Convert a [Map] from parameter names to values to a URL query string. |
| +String mapToQuery(Map<String, String> map) { |
| + var pairs = <List<String>>[]; |
| + map.forEach((key, value) => |
| + pairs.add([encodeUriComponent(key), encodeUriComponent(value)])); |
|
Bob Nystrom
2012/10/31 01:17:44
Nit: Indent +2. If it were me, I would probably le
nweiz
2012/10/31 18:20:59
Done.
|
| + return Strings.join(pairs.map((pair) => "${pair[0]}=${pair[1]}"), "&"); |
| +} |
| + |
| +/// Add all key/value pairs from [source] to [destination], overwriting any |
| +/// pre-existing values. |
| +void mapAddAll(Map destination, Map source) => |
| + source.forEach((key, value) => destination[key] = value); |
|
Bob Nystrom
2012/10/31 01:17:44
+2 here and elsewhere.
nweiz
2012/10/31 18:20:59
As I mentioned elsewhere, I feel like this is more
|
| + |
| +/// Decode a URL-encoded string. Unlike [decodeUriComponent], this includes |
| +/// replacing `+` with ` `. |
| +String urlDecode(String encoded) => |
| + decodeUriComponent(encoded.replaceAll("+", " ")); |
| + |
| +/// Like [String.split], but only splits on the first occurrence of the pattern. |
| +/// This will always return an array of two elements or fewer. |
| +List<String> split1(String toSplit, String pattern) { |
| + if (toSplit.isEmpty) return <String>[]; |
| + |
| + var index = toSplit.indexOf(pattern); |
| + if (index == -1) return [toSplit]; |
| + return [toSplit.substring(0, index), |
| + toSplit.substring(index + pattern.length)]; |
|
Bob Nystrom
2012/10/31 01:17:44
+2.
nweiz
2012/10/31 18:20:59
Changed the formatting in a different way, let me
Bob Nystrom
2012/11/01 19:53:59
+1. I like it.
|
| +} |
| + |
| +/// Return the [Encoding] that corresponds to [charset]. Return |
| +/// [Encoding.ISO_8859_1] if [charset] is null or if no [Encoding] was found |
| +/// that corresponds to [charset]. |
| +Encoding encodingForCharset(String charset) { |
| + if (charset == null) return Encoding.ISO_8859_1; |
| + var encoding = _encodingForCharset(charset); |
| + return encoding == null ? Encoding.ISO_8859_1 : encoding; |
| +} |
| + |
| +/// Return the [Encoding] that corresponds to [charset]. Throw an |
| +/// [UnsupportedError] if no [Encoding] was found that corresponds to [charset]. |
| +/// [charset] may not be null. |
| +Encoding requiredEncodingForCharset(String charset) { |
| + var encoding = _encodingForCharset(charset); |
| + if (encoding != null) return encoding; |
| + throw new UnsupportedError('Unsupported encoding "$charset".'); |
|
Bob Nystrom
2012/10/31 01:17:44
I think this should be ArgumentError or possibly F
nweiz
2012/10/31 18:20:59
Changed to ArgumentError.
|
| +} |
| + |
| +/// Return the [Encoding] that corresponds to [charset]. Return null if no |
| +/// [Encoding] was found that corresponds to [charset]. [charset] may not be |
| +/// null. |
| +Encoding _encodingForCharset(String charset) { |
| + charset = charset.toLowerCase(); |
| + if (charset == 'ascii' || charset == 'us-ascii') return Encoding.ASCII; |
| + if (charset == 'utf-8') return Encoding.UTF_8; |
| + if (charset == 'iso-8859-1') return Encoding.ISO_8859_1; |
| + return null; |
| +} |
| + |
| +/// Convert [bytes] into a [String] according to [encoding]. |
| +String decodeString(List<int> bytes, Encoding encoding) { |
| + // TODO(nweiz): implement this once issue 6284 is fixed. |
| + return new String.fromCharCodes(bytes); |
| +} |
| + |
| +/// Convert [string] into a byte array according to [encoding]. |
| +List<int> encodeString(String string, Encoding encoding) { |
| + // TODO(nweiz): implement this once issue 6284 is fixed. |
| + return string.charCodes; |
| +} |
| + |
| +/// Convert [input] into a [Uint8List]. If [input] is a [ByteArray] or |
| +/// [ByteArrayViewable], this just returns a view on [input]. |
| +Uint8List uint8List(List<int> input) { |
|
Bob Nystrom
2012/10/31 01:17:44
This name isn't very verby. How about "toUint8List
nweiz
2012/10/31 18:20:59
Done.
|
| + if (input is Uint8List) return input; |
| + if (input is ByteArrayViewable) input = input.asByteArray(); |
| + if (input is ByteArray) return new Uint8List.view(input); |
|
Bob Nystrom
2012/10/31 01:17:44
Does the order of these two lines matter? If not,
nweiz
2012/10/31 18:20:59
Yes, the previous line causes "input" to become a
|
| + var output = new Uint8List(input.length); |
| + output.setRange(0, input.length, input); |
| + return output; |
| +} |
| + |
| +/// Buffers all input from an InputStream and returns it as a future. |
| +Future<List<int>> consumeInputStream(InputStream stream) { |
| + var completer = new Completer<List<int>>(); |
| + /// TODO(nweiz): use BufferList when issue 6409 is fixed |
| + var buffer = <int>[]; |
| + stream.onClosed = () => completer.complete(buffer); |
| + stream.onData = () => buffer.addAll(stream.read()); |
| + stream.onError = (e) => completer.completeException(e); |
|
Bob Nystrom
2012/10/31 01:17:44
stream.onError = completer.completeException;
nweiz
2012/10/31 18:20:59
Done.
|
| + return completer.future; |
| +} |
| + |
| +/// Takes all input from [source] and writes it to [sink]. |
| +void pipeInputToInput(InputStream source, ListInputStream sink) { |
|
Bob Nystrom
2012/10/31 01:17:44
This method name seems crazy, but then I guess tho
nweiz
2012/10/31 18:20:59
In the context of this method, "sink" is coneptual
|
| + source.onClosed = () => sink.markEndOfStream(); |
| + source.onData = () => sink.write(source.read()); |
| + // TODO(nweiz): propagate source errors to the sink. See issue 3657. |
| +} |
| + |
| +/// Returns a [Future] that asynchronously completes to `null`. |
| +Future get async { |
| + var completer = new Completer(); |
| + new Timer(0, (_) => completer.complete(null)); |
| + return completer.future; |
| +} |