OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 multipart_request; | 5 library multipart_request; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:io'; | 8 import 'dart:io'; |
9 import 'dart:math'; | 9 import 'dart:math'; |
10 import 'dart:utf'; | 10 import 'dart:utf'; |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
82 | 82 |
83 /// Freezes all mutable fields and returns a single-subscription [ByteStream] | 83 /// Freezes all mutable fields and returns a single-subscription [ByteStream] |
84 /// that will emit the request body. | 84 /// that will emit the request body. |
85 ByteStream finalize() { | 85 ByteStream finalize() { |
86 // TODO(nweiz): freeze fields and files | 86 // TODO(nweiz): freeze fields and files |
87 var boundary = _boundaryString(_BOUNDARY_LENGTH); | 87 var boundary = _boundaryString(_BOUNDARY_LENGTH); |
88 headers['content-type'] = 'multipart/form-data; boundary="$boundary"'; | 88 headers['content-type'] = 'multipart/form-data; boundary="$boundary"'; |
89 headers['content-transfer-encoding'] = 'binary'; | 89 headers['content-transfer-encoding'] = 'binary'; |
90 super.finalize(); | 90 super.finalize(); |
91 | 91 |
92 var controller = new StreamController<List<int>>(); | 92 var controller = new StreamController<List<int>>(sync: true); |
93 | 93 |
94 void writeAscii(String string) { | 94 void writeAscii(String string) { |
95 assert(isPlainAscii(string)); | 95 assert(isPlainAscii(string)); |
96 controller.add(string.codeUnits); | 96 controller.add(string.codeUnits); |
97 } | 97 } |
98 | 98 |
99 writeUtf8(String string) => controller.add(encodeUtf8(string)); | 99 writeUtf8(String string) => controller.add(encodeUtf8(string)); |
100 writeLine() => controller.add([13, 10]); // \r\n | 100 writeLine() => controller.add([13, 10]); // \r\n |
101 | 101 |
102 fields.forEach((name, value) { | 102 fields.forEach((name, value) { |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 String _boundaryString(int length) { | 162 String _boundaryString(int length) { |
163 var prefix = "dart-http-boundary-"; | 163 var prefix = "dart-http-boundary-"; |
164 var list = new List<int>(length - prefix.length); | 164 var list = new List<int>(length - prefix.length); |
165 for (var i = 0; i < list.length; i++) { | 165 for (var i = 0; i < list.length; i++) { |
166 list[i] = _BOUNDARY_CHARACTERS[ | 166 list[i] = _BOUNDARY_CHARACTERS[ |
167 _random.nextInt(_BOUNDARY_CHARACTERS.length)]; | 167 _random.nextInt(_BOUNDARY_CHARACTERS.length)]; |
168 } | 168 } |
169 return "$prefix${new String.fromCharCodes(list)}"; | 169 return "$prefix${new String.fromCharCodes(list)}"; |
170 } | 170 } |
171 } | 171 } |
OLD | NEW |