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:uri'; | 10 import 'dart:uri'; |
(...skipping 25 matching lines...) Expand all Loading... | |
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 total length of the request body, in bytes. This is calculated from | 44 /// The total length of the request body, in bytes. This is calculated from |
45 /// [fields] and [files] and cannot be set manually. | 45 /// [fields] and [files] and cannot be set manually. |
46 int get contentLength { | 46 int get contentLength { |
Lasse Reichstein Nielsen
2013/04/11 09:29:13
Could you move these gettes below the constructor?
Anders Johnsen
2013/04/11 09:31:26
Done.
Anders Johnsen
2013/04/11 09:31:26
Done.
| |
47 var length = 0; | 47 var length = 0; |
48 | 48 |
49 fields.forEach((name, value) { | 49 fields.forEach((name, value) { |
50 length += "--".length + _BOUNDARY_LENGTH + "\r\n".length + | 50 length += "--".length + _BOUNDARY_LENGTH + "\r\n".length + |
51 _headerForField(name, value).length + | 51 _headerForField(name, value).length + |
52 encodeUtf8(value).length + "\r\n".length; | 52 encodeUtf8(value).length + "\r\n".length; |
53 }); | 53 }); |
54 | 54 |
55 for (var file in _files.collection) { | 55 for (var file in _files) { |
56 length += "--".length + _BOUNDARY_LENGTH + "\r\n".length + | 56 length += "--".length + _BOUNDARY_LENGTH + "\r\n".length + |
57 _headerForFile(file).length + | 57 _headerForFile(file).length + |
58 file.length + "\r\n".length; | 58 file.length + "\r\n".length; |
59 } | 59 } |
60 | 60 |
61 return length + "--".length + _BOUNDARY_LENGTH + "--\r\n".length; | 61 return length + "--".length + _BOUNDARY_LENGTH + "--\r\n".length; |
62 } | 62 } |
63 | 63 |
64 set contentLength(int value) { | 64 set contentLength(int value) { |
65 throw new UnsupportedError("Cannot set the contentLength property of " | 65 throw new UnsupportedError("Cannot set the contentLength property of " |
66 "multipart requests."); | 66 "multipart requests."); |
67 } | 67 } |
68 | 68 |
69 /// The form fields to send for this request. | 69 /// The form fields to send for this request. |
70 final Map<String, String> fields; | 70 final Map<String, String> fields; |
71 | 71 |
72 /// The sink for files to upload for this request. | 72 /// The sink for files to upload for this request. |
73 /// | 73 /// |
74 /// This doesn't need to be closed. When the request is sent, whichever files | 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. | 75 /// are written to this sink at that point will be used. |
76 CollectionSink<MultipartFile> get files => _files; | 76 List<MultipartFile> get files => _files; |
77 | 77 |
78 /// The private version of [files], typed so that the underlying collection is | 78 /// The private version of [files]. |
79 /// accessible. | 79 final List<MultipartFile> _files; |
80 final CollectionSink<MultipartFile> _files; | |
81 | 80 |
82 /// Creates a new [MultipartRequest]. | 81 /// Creates a new [MultipartRequest]. |
83 MultipartRequest(String method, Uri url) | 82 MultipartRequest(String method, Uri url) |
84 : super(method, url), | 83 : super(method, url), |
85 fields = {}, | 84 fields = {}, |
86 _files = new CollectionSink<MultipartFile>(<MultipartFile>[]); | 85 _files = <MultipartFile>[]; |
87 | 86 |
88 /// Freezes all mutable fields and returns a single-subscription [ByteStream] | 87 /// Freezes all mutable fields and returns a single-subscription [ByteStream] |
89 /// that will emit the request body. | 88 /// that will emit the request body. |
90 ByteStream finalize() { | 89 ByteStream finalize() { |
91 // TODO(nweiz): freeze fields and files | 90 // TODO(nweiz): freeze fields and files |
92 var boundary = _boundaryString(_BOUNDARY_LENGTH); | 91 var boundary = _boundaryString(_BOUNDARY_LENGTH); |
93 headers['content-type'] = 'multipart/form-data; boundary="$boundary"'; | 92 headers['content-type'] = 'multipart/form-data; boundary="$boundary"'; |
94 headers['content-transfer-encoding'] = 'binary'; | 93 headers['content-transfer-encoding'] = 'binary'; |
95 super.finalize(); | 94 super.finalize(); |
96 | 95 |
97 var controller = new StreamController<List<int>>(); | 96 var controller = new StreamController<List<int>>(); |
98 | 97 |
99 void writeAscii(String string) { | 98 void writeAscii(String string) { |
100 assert(isPlainAscii(string)); | 99 assert(isPlainAscii(string)); |
101 controller.add(string.codeUnits); | 100 controller.add(string.codeUnits); |
102 } | 101 } |
103 | 102 |
104 writeUtf8(String string) => controller.add(encodeUtf8(string)); | 103 writeUtf8(String string) => controller.add(encodeUtf8(string)); |
105 writeLine() => controller.add([13, 10]); // \r\n | 104 writeLine() => controller.add([13, 10]); // \r\n |
106 | 105 |
107 fields.forEach((name, value) { | 106 fields.forEach((name, value) { |
108 writeAscii('--$boundary\r\n'); | 107 writeAscii('--$boundary\r\n'); |
109 writeAscii(_headerForField(name, value)); | 108 writeAscii(_headerForField(name, value)); |
110 writeUtf8(value); | 109 writeUtf8(value); |
111 writeLine(); | 110 writeLine(); |
112 }); | 111 }); |
113 | 112 |
114 Future.forEach(_files.collection, (file) { | 113 Future.forEach(_files, (file) { |
115 writeAscii('--$boundary\r\n'); | 114 writeAscii('--$boundary\r\n'); |
116 writeAscii(_headerForFile(file)); | 115 writeAscii(_headerForFile(file)); |
117 return writeStreamToSink(file.finalize(), controller) | 116 return writeStreamToSink(file.finalize(), controller) |
118 .then((_) => writeLine()); | 117 .then((_) => writeLine()); |
119 }).then((_) { | 118 }).then((_) { |
120 // TODO(nweiz): pass any errors propagated through this future on to | 119 // TODO(nweiz): pass any errors propagated through this future on to |
121 // the stream. See issue 3657. | 120 // the stream. See issue 3657. |
122 writeAscii('--$boundary--\r\n'); | 121 writeAscii('--$boundary--\r\n'); |
123 controller.close(); | 122 controller.close(); |
124 }); | 123 }); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
166 String _boundaryString(int length) { | 165 String _boundaryString(int length) { |
167 var prefix = "dart-http-boundary-"; | 166 var prefix = "dart-http-boundary-"; |
168 var list = new List<int>(length - prefix.length); | 167 var list = new List<int>(length - prefix.length); |
169 for (var i = 0; i < list.length; i++) { | 168 for (var i = 0; i < list.length; i++) { |
170 list[i] = _BOUNDARY_CHARACTERS[ | 169 list[i] = _BOUNDARY_CHARACTERS[ |
171 _random.nextInt(_BOUNDARY_CHARACTERS.length)]; | 170 _random.nextInt(_BOUNDARY_CHARACTERS.length)]; |
172 } | 171 } |
173 return "$prefix${new String.fromCharCodes(list)}"; | 172 return "$prefix${new String.fromCharCodes(list)}"; |
174 } | 173 } |
175 } | 174 } |
OLD | NEW |