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:convert'; |
8 import 'dart:math'; | 9 import 'dart:math'; |
9 import 'dart:utf'; | |
10 | 10 |
11 import 'base_request.dart'; | 11 import 'base_request.dart'; |
12 import 'byte_stream.dart'; | 12 import 'byte_stream.dart'; |
13 import 'multipart_file.dart'; | 13 import 'multipart_file.dart'; |
14 import 'utils.dart'; | 14 import 'utils.dart'; |
15 | 15 |
16 /// A `multipart/form-data` request. Such a request has both string [fields], | 16 /// A `multipart/form-data` request. Such a request has both string [fields], |
17 /// which function as normal form fields, and (potentially streamed) binary | 17 /// which function as normal form fields, and (potentially streamed) binary |
18 /// [files]. | 18 /// [files]. |
19 /// | 19 /// |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
55 List<MultipartFile> get files => _files; | 55 List<MultipartFile> get files => _files; |
56 | 56 |
57 /// The total length of the request body, in bytes. This is calculated from | 57 /// The total length of the request body, in bytes. This is calculated from |
58 /// [fields] and [files] and cannot be set manually. | 58 /// [fields] and [files] and cannot be set manually. |
59 int get contentLength { | 59 int get contentLength { |
60 var length = 0; | 60 var length = 0; |
61 | 61 |
62 fields.forEach((name, value) { | 62 fields.forEach((name, value) { |
63 length += "--".length + _BOUNDARY_LENGTH + "\r\n".length + | 63 length += "--".length + _BOUNDARY_LENGTH + "\r\n".length + |
64 _headerForField(name, value).length + | 64 _headerForField(name, value).length + |
65 encodeUtf8(value).length + "\r\n".length; | 65 UTF8.encode(value).length + "\r\n".length; |
66 }); | 66 }); |
67 | 67 |
68 for (var file in _files) { | 68 for (var file in _files) { |
69 length += "--".length + _BOUNDARY_LENGTH + "\r\n".length + | 69 length += "--".length + _BOUNDARY_LENGTH + "\r\n".length + |
70 _headerForFile(file).length + | 70 _headerForFile(file).length + |
71 file.length + "\r\n".length; | 71 file.length + "\r\n".length; |
72 } | 72 } |
73 | 73 |
74 return length + "--".length + _BOUNDARY_LENGTH + "--\r\n".length; | 74 return length + "--".length + _BOUNDARY_LENGTH + "--\r\n".length; |
75 } | 75 } |
(...skipping 12 matching lines...) Expand all Loading... |
88 headers['content-transfer-encoding'] = 'binary'; | 88 headers['content-transfer-encoding'] = 'binary'; |
89 super.finalize(); | 89 super.finalize(); |
90 | 90 |
91 var controller = new StreamController<List<int>>(sync: true); | 91 var controller = new StreamController<List<int>>(sync: true); |
92 | 92 |
93 void writeAscii(String string) { | 93 void writeAscii(String string) { |
94 assert(isPlainAscii(string)); | 94 assert(isPlainAscii(string)); |
95 controller.add(string.codeUnits); | 95 controller.add(string.codeUnits); |
96 } | 96 } |
97 | 97 |
98 writeUtf8(String string) => controller.add(encodeUtf8(string)); | 98 writeUtf8(String string) => controller.add(UTF8.encode(string)); |
99 writeLine() => controller.add([13, 10]); // \r\n | 99 writeLine() => controller.add([13, 10]); // \r\n |
100 | 100 |
101 fields.forEach((name, value) { | 101 fields.forEach((name, value) { |
102 writeAscii('--$boundary\r\n'); | 102 writeAscii('--$boundary\r\n'); |
103 writeAscii(_headerForField(name, value)); | 103 writeAscii(_headerForField(name, value)); |
104 writeUtf8(value); | 104 writeUtf8(value); |
105 writeLine(); | 105 writeLine(); |
106 }); | 106 }); |
107 | 107 |
108 Future.forEach(_files, (file) { | 108 Future.forEach(_files, (file) { |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 /// Returns a randomly-generated multipart boundary string | 159 /// Returns a randomly-generated multipart boundary string |
160 String _boundaryString() { | 160 String _boundaryString() { |
161 var prefix = "dart-http-boundary-"; | 161 var prefix = "dart-http-boundary-"; |
162 var list = new List<int>.generate(_BOUNDARY_LENGTH - prefix.length, | 162 var list = new List<int>.generate(_BOUNDARY_LENGTH - prefix.length, |
163 (index) => | 163 (index) => |
164 _BOUNDARY_CHARACTERS[_random.nextInt(_BOUNDARY_CHARACTERS.length)], | 164 _BOUNDARY_CHARACTERS[_random.nextInt(_BOUNDARY_CHARACTERS.length)], |
165 growable: false); | 165 growable: false); |
166 return "$prefix${new String.fromCharCodes(list)}"; | 166 return "$prefix${new String.fromCharCodes(list)}"; |
167 } | 167 } |
168 } | 168 } |
OLD | NEW |