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

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

Issue 14051005: Remove deprecated CollectionSink and Stream.pipeInto. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 8 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 | « no previous file | sdk/lib/async/async_sources.gypi » ('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) 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:uri'; 10 import 'dart:uri';
(...skipping 23 matching lines...) Expand all
34 /// if (response.statusCode == 200) print("Uploaded!"); 34 /// if (response.statusCode == 200) print("Uploaded!");
35 /// }); 35 /// });
36 class MultipartRequest extends BaseRequest { 36 class MultipartRequest extends BaseRequest {
37 /// The total length of the multipart boundaries used when building the 37 /// The total length of the multipart boundaries used when building the
38 /// request body. According to http://tools.ietf.org/html/rfc1341.html, this 38 /// request body. According to http://tools.ietf.org/html/rfc1341.html, this
39 /// can't be longer than 70. 39 /// can't be longer than 70.
40 static final int _BOUNDARY_LENGTH = 70; 40 static final int _BOUNDARY_LENGTH = 70;
41 41
42 static final Random _random = new Random(); 42 static final Random _random = new Random();
43 43
44 /// The form fields to send for this request.
45 final Map<String, String> fields;
46
47 /// The private version of [files].
48 final List<MultipartFile> _files;
49
50 /// Creates a new [MultipartRequest].
51 MultipartRequest(String method, Uri url)
52 : super(method, url),
53 fields = {},
54 _files = <MultipartFile>[];
55
56 /// The list of files to upload for this request.
57 List<MultipartFile> get files => _files;
58
44 /// The total length of the request body, in bytes. This is calculated from 59 /// The total length of the request body, in bytes. This is calculated from
45 /// [fields] and [files] and cannot be set manually. 60 /// [fields] and [files] and cannot be set manually.
46 int get contentLength { 61 int get contentLength {
47 var length = 0; 62 var length = 0;
48 63
49 fields.forEach((name, value) { 64 fields.forEach((name, value) {
50 length += "--".length + _BOUNDARY_LENGTH + "\r\n".length + 65 length += "--".length + _BOUNDARY_LENGTH + "\r\n".length +
51 _headerForField(name, value).length + 66 _headerForField(name, value).length +
52 encodeUtf8(value).length + "\r\n".length; 67 encodeUtf8(value).length + "\r\n".length;
53 }); 68 });
54 69
55 for (var file in _files.collection) { 70 for (var file in _files) {
56 length += "--".length + _BOUNDARY_LENGTH + "\r\n".length + 71 length += "--".length + _BOUNDARY_LENGTH + "\r\n".length +
57 _headerForFile(file).length + 72 _headerForFile(file).length +
58 file.length + "\r\n".length; 73 file.length + "\r\n".length;
59 } 74 }
60 75
61 return length + "--".length + _BOUNDARY_LENGTH + "--\r\n".length; 76 return length + "--".length + _BOUNDARY_LENGTH + "--\r\n".length;
62 } 77 }
63 78
64 set contentLength(int value) { 79 set contentLength(int value) {
65 throw new UnsupportedError("Cannot set the contentLength property of " 80 throw new UnsupportedError("Cannot set the contentLength property of "
66 "multipart requests."); 81 "multipart requests.");
67 } 82 }
68 83
69 /// The form fields to send for this request.
70 final Map<String, String> fields;
71
72 /// The sink for files to upload for this request.
73 ///
74 /// This doesn't need to be closed. When the request is sent, whichever files
75 /// are written to this sink at that point will be used.
76 CollectionSink<MultipartFile> get files => _files;
77
78 /// The private version of [files], typed so that the underlying collection is
79 /// accessible.
80 final CollectionSink<MultipartFile> _files;
81
82 /// Creates a new [MultipartRequest].
83 MultipartRequest(String method, Uri url)
84 : super(method, url),
85 fields = {},
86 _files = new CollectionSink<MultipartFile>(<MultipartFile>[]);
87
88 /// Freezes all mutable fields and returns a single-subscription [ByteStream] 84 /// Freezes all mutable fields and returns a single-subscription [ByteStream]
89 /// that will emit the request body. 85 /// that will emit the request body.
90 ByteStream finalize() { 86 ByteStream finalize() {
91 // TODO(nweiz): freeze fields and files 87 // TODO(nweiz): freeze fields and files
92 var boundary = _boundaryString(_BOUNDARY_LENGTH); 88 var boundary = _boundaryString(_BOUNDARY_LENGTH);
93 headers['content-type'] = 'multipart/form-data; boundary="$boundary"'; 89 headers['content-type'] = 'multipart/form-data; boundary="$boundary"';
94 headers['content-transfer-encoding'] = 'binary'; 90 headers['content-transfer-encoding'] = 'binary';
95 super.finalize(); 91 super.finalize();
96 92
97 var controller = new StreamController<List<int>>(); 93 var controller = new StreamController<List<int>>();
98 94
99 void writeAscii(String string) { 95 void writeAscii(String string) {
100 assert(isPlainAscii(string)); 96 assert(isPlainAscii(string));
101 controller.add(string.codeUnits); 97 controller.add(string.codeUnits);
102 } 98 }
103 99
104 writeUtf8(String string) => controller.add(encodeUtf8(string)); 100 writeUtf8(String string) => controller.add(encodeUtf8(string));
105 writeLine() => controller.add([13, 10]); // \r\n 101 writeLine() => controller.add([13, 10]); // \r\n
106 102
107 fields.forEach((name, value) { 103 fields.forEach((name, value) {
108 writeAscii('--$boundary\r\n'); 104 writeAscii('--$boundary\r\n');
109 writeAscii(_headerForField(name, value)); 105 writeAscii(_headerForField(name, value));
110 writeUtf8(value); 106 writeUtf8(value);
111 writeLine(); 107 writeLine();
112 }); 108 });
113 109
114 Future.forEach(_files.collection, (file) { 110 Future.forEach(_files, (file) {
115 writeAscii('--$boundary\r\n'); 111 writeAscii('--$boundary\r\n');
116 writeAscii(_headerForFile(file)); 112 writeAscii(_headerForFile(file));
117 return writeStreamToSink(file.finalize(), controller) 113 return writeStreamToSink(file.finalize(), controller)
118 .then((_) => writeLine()); 114 .then((_) => writeLine());
119 }).then((_) { 115 }).then((_) {
120 // TODO(nweiz): pass any errors propagated through this future on to 116 // TODO(nweiz): pass any errors propagated through this future on to
121 // the stream. See issue 3657. 117 // the stream. See issue 3657.
122 writeAscii('--$boundary--\r\n'); 118 writeAscii('--$boundary--\r\n');
123 controller.close(); 119 controller.close();
124 }); 120 });
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
166 String _boundaryString(int length) { 162 String _boundaryString(int length) {
167 var prefix = "dart-http-boundary-"; 163 var prefix = "dart-http-boundary-";
168 var list = new List<int>(length - prefix.length); 164 var list = new List<int>(length - prefix.length);
169 for (var i = 0; i < list.length; i++) { 165 for (var i = 0; i < list.length; i++) {
170 list[i] = _BOUNDARY_CHARACTERS[ 166 list[i] = _BOUNDARY_CHARACTERS[
171 _random.nextInt(_BOUNDARY_CHARACTERS.length)]; 167 _random.nextInt(_BOUNDARY_CHARACTERS.length)];
172 } 168 }
173 return "$prefix${new String.fromCharCodes(list)}"; 169 return "$prefix${new String.fromCharCodes(list)}";
174 } 170 }
175 } 171 }
OLDNEW
« no previous file with comments | « no previous file | sdk/lib/async/async_sources.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698