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

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

Issue 11363094: Add a multipart HTTP request class. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Add test file 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
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:crypto';
7 import 'dart:io'; 8 import 'dart:io';
8 import 'dart:isolate'; 9 import 'dart:isolate';
9 import 'dart:scalarlist'; 10 import 'dart:scalarlist';
10 import 'dart:uri'; 11 import 'dart:uri';
12 import 'dart:utf';
11 13
12 /// 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)
13 /// into a [Map] from parameter names to values. 15 /// into a [Map] from parameter names to values.
14 /// 16 ///
15 /// queryToMap("foo=bar&baz=bang&qux"); 17 /// queryToMap("foo=bar&baz=bang&qux");
16 /// //=> {"foo": "bar", "baz": "bang", "qux": ""} 18 /// //=> {"foo": "bar", "baz": "bang", "qux": ""}
17 Map<String, String> queryToMap(String queryList) { 19 Map<String, String> queryToMap(String queryList) {
18 var map = <String>{}; 20 var map = <String>{};
19 for (var pair in queryList.split("&")) { 21 for (var pair in queryList.split("&")) {
20 var split = split1(pair, "="); 22 var split = split1(pair, "=");
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
61 if (toSplit.isEmpty) return <String>[]; 63 if (toSplit.isEmpty) return <String>[];
62 64
63 var index = toSplit.indexOf(pattern); 65 var index = toSplit.indexOf(pattern);
64 if (index == -1) return [toSplit]; 66 if (index == -1) return [toSplit];
65 return [ 67 return [
66 toSplit.substring(0, index), 68 toSplit.substring(0, index),
67 toSplit.substring(index + pattern.length) 69 toSplit.substring(index + pattern.length)
68 ]; 70 ];
69 } 71 }
70 72
71 /// Returns the [Encoding] that corresponds to [charset]. Returns 73 /// Returns the [Encoding] that corresponds to [charset]. Returns [fallback] if
72 /// [Encoding.ISO_8859_1] if [charset] is null or if no [Encoding] was found 74 /// [charset] is null or if no [Encoding] was found that corresponds to
73 /// that corresponds to [charset]. 75 /// [charset].
74 Encoding encodingForCharset(String charset) { 76 Encoding encodingForCharset(
75 if (charset == null) return Encoding.ISO_8859_1; 77 String charset, [Encoding fallback = Encoding.ISO_8859_1]) {
78 if (charset == null) return fallback;
76 var encoding = _encodingForCharset(charset); 79 var encoding = _encodingForCharset(charset);
77 return encoding == null ? Encoding.ISO_8859_1 : encoding; 80 return encoding == null ? fallback : encoding;
78 } 81 }
79 82
80 /// Returns the [Encoding] that corresponds to [charset]. Throws a [FormatExcept ion] 83 /// Returns the [Encoding] that corresponds to [charset]. Throws a [FormatExcept ion]
Bob Nystrom 2012/11/06 22:00:08 Unrelated, but long line here.
nweiz 2012/11/06 23:15:56 Done.
81 /// if no [Encoding] was found that corresponds to [charset]. [charset] may not 84 /// if no [Encoding] was found that corresponds to [charset]. [charset] may not
82 /// be null. 85 /// be null.
83 Encoding requiredEncodingForCharset(String charset) { 86 Encoding requiredEncodingForCharset(String charset) {
84 var encoding = _encodingForCharset(charset); 87 var encoding = _encodingForCharset(charset);
85 if (encoding != null) return encoding; 88 if (encoding != null) return encoding;
86 throw new FormatException('Unsupported encoding "$charset".'); 89 throw new FormatException('Unsupported encoding "$charset".');
87 } 90 }
88 91
89 /// Returns the [Encoding] that corresponds to [charset]. Returns null if no 92 /// Returns the [Encoding] that corresponds to [charset]. Returns null if no
90 /// [Encoding] was found that corresponds to [charset]. [charset] may not be 93 /// [Encoding] was found that corresponds to [charset]. [charset] may not be
(...skipping 11 matching lines...) Expand all
102 // TODO(nweiz): implement this once issue 6284 is fixed. 105 // TODO(nweiz): implement this once issue 6284 is fixed.
103 return new String.fromCharCodes(bytes); 106 return new String.fromCharCodes(bytes);
104 } 107 }
105 108
106 /// Converts [string] into a byte array according to [encoding]. 109 /// Converts [string] into a byte array according to [encoding].
107 List<int> encodeString(String string, Encoding encoding) { 110 List<int> encodeString(String string, Encoding encoding) {
108 // TODO(nweiz): implement this once issue 6284 is fixed. 111 // TODO(nweiz): implement this once issue 6284 is fixed.
109 return string.charCodes; 112 return string.charCodes;
110 } 113 }
111 114
115 /// A regular expression that matches strings that are composed entirely of
116 /// ASCII-compatible characters.
117 final RegExp _asciiOnly = const RegExp(r"^[\x00-\x7F]+$");
Bob Nystrom 2012/11/06 22:00:08 static const _ASCII_ONLY = const ...
nweiz 2012/11/06 23:15:56 Done.
118
119 /// Returns whether [string] is composed entirely of ASCII-compatible
120 /// characters.
121 bool isPlainAscii(String string) => _asciiOnly.hasMatch(string);
122
112 /// Converts [input] into a [Uint8List]. If [input] is a [ByteArray] or 123 /// Converts [input] into a [Uint8List]. If [input] is a [ByteArray] or
113 /// [ByteArrayViewable], this just returns a view on [input]. 124 /// [ByteArrayViewable], this just returns a view on [input].
114 Uint8List toUint8List(List<int> input) { 125 Uint8List toUint8List(List<int> input) {
115 if (input is Uint8List) return input; 126 if (input is Uint8List) return input;
116 if (input is ByteArrayViewable) input = input.asByteArray(); 127 if (input is ByteArrayViewable) input = input.asByteArray();
117 if (input is ByteArray) return new Uint8List.view(input); 128 if (input is ByteArray) return new Uint8List.view(input);
118 var output = new Uint8List(input.length); 129 var output = new Uint8List(input.length);
119 output.setRange(0, input.length, input); 130 output.setRange(0, input.length, input);
120 return output; 131 return output;
121 } 132 }
122 133
123 /// Buffers all input from an InputStream and returns it as a future. 134 /// Buffers all input from an InputStream and returns it as a future.
124 Future<List<int>> consumeInputStream(InputStream stream) { 135 Future<List<int>> consumeInputStream(InputStream stream) {
125 var completer = new Completer<List<int>>(); 136 var completer = new Completer<List<int>>();
126 /// TODO(nweiz): use BufferList when issue 6409 is fixed 137 /// TODO(nweiz): use BufferList when issue 6409 is fixed
127 var buffer = <int>[]; 138 var buffer = <int>[];
128 stream.onClosed = () => completer.complete(buffer); 139 stream.onClosed = () => completer.complete(buffer);
129 stream.onData = () => buffer.addAll(stream.read()); 140 stream.onData = () => buffer.addAll(stream.read());
130 stream.onError = completer.completeException; 141 stream.onError = completer.completeException;
131 return completer.future; 142 return completer.future;
132 } 143 }
133 144
134 /// Takes all input from [source] and writes it to [sink]. 145 /// Takes all input from [source] and writes it to [sink], then closes [sink].
135 void pipeInputToInput(InputStream source, ListInputStream sink) { 146 void pipeInputToInput(InputStream source, ListInputStream sink) {
136 source.onClosed = () => sink.markEndOfStream(); 147 source.onClosed = () => sink.markEndOfStream();
137 source.onData = () => sink.write(source.read()); 148 source.onData = () => sink.write(source.read());
138 // TODO(nweiz): propagate source errors to the sink. See issue 3657. 149 // TODO(nweiz): propagate source errors to the sink. See issue 3657.
139 } 150 }
140 151
152 /// Takes all input from [source] and writes it to [sink], but does not close
153 /// [sink] when [source] is closed. Returns a [Future] that completes when
154 /// [source] is closed.
155 Future writeInputToInput(InputStream source, ListInputStream sink) {
156 var completer = new Completer();
157 source.onClosed = () => completer.complete(null);
158 source.onData = () => sink.write(source.read());
159 // TODO(nweiz): propagate source errors to the sink. See issue 3657.
160 return completer.future;
161 }
162
141 /// Returns a [Future] that asynchronously completes to `null`. 163 /// Returns a [Future] that asynchronously completes to `null`.
142 Future get async { 164 Future get async {
143 var completer = new Completer(); 165 var completer = new Completer();
144 new Timer(0, (_) => completer.complete(null)); 166 new Timer(0, (_) => completer.complete(null));
145 return completer.future; 167 return completer.future;
146 } 168 }
169
170 /// Runs [fn] for each element in [input] in order, moving to the next element
171 /// only when the [Future] returned by [fn] completes. Returns a [Future] that
172 /// completes when all elements have been processed.
173 ///
174 /// The return values of all [Future]s are discarded. Any errors will cause the
175 /// iteration to stop and will be piped through the return value.
Bob Nystrom 2012/11/06 22:00:08 Add a TODO that Future should support this directl
nweiz 2012/11/06 23:15:56 I'll do you one better and send a patch.
Bob Nystrom 2012/11/07 00:50:26 I love patches!
176 Future forEachFuture(Iterable input, Future fn(element)) {
177 var iterator = input.iterator();
178 Future nextElement(_) {
179 if (!iterator.hasNext) return new Future.immediate(null);
180 return fn(iterator.next()).chain(nextElement);
181 }
182 return nextElement(null);
183 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698