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

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

Issue 19866007: pkg/http tweaks (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: final nits Created 7 years, 5 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 | « pkg/http/lib/src/client.dart ('k') | pkg/http/lib/src/streamed_request.dart » ('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';
9 import 'dart:math'; 8 import 'dart:math';
10 import 'dart:utf'; 9 import 'dart:utf';
11 10
12 import 'base_request.dart'; 11 import 'base_request.dart';
13 import 'byte_stream.dart'; 12 import 'byte_stream.dart';
14 import 'multipart_file.dart'; 13 import 'multipart_file.dart';
15 import 'utils.dart'; 14 import 'utils.dart';
16 15
17 /// 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],
18 /// which function as normal form fields, and (potentially streamed) binary 17 /// which function as normal form fields, and (potentially streamed) binary
(...skipping 10 matching lines...) Expand all
29 /// 'package', 28 /// 'package',
30 /// new File('build/package.tar.gz'), 29 /// new File('build/package.tar.gz'),
31 /// contentType: new ContentType('application', 'x-tar')); 30 /// contentType: new ContentType('application', 'x-tar'));
32 /// request.send().then((response) { 31 /// request.send().then((response) {
33 /// if (response.statusCode == 200) print("Uploaded!"); 32 /// if (response.statusCode == 200) print("Uploaded!");
34 /// }); 33 /// });
35 class MultipartRequest extends BaseRequest { 34 class MultipartRequest extends BaseRequest {
36 /// The total length of the multipart boundaries used when building the 35 /// The total length of the multipart boundaries used when building the
37 /// request body. According to http://tools.ietf.org/html/rfc1341.html, this 36 /// request body. According to http://tools.ietf.org/html/rfc1341.html, this
38 /// can't be longer than 70. 37 /// can't be longer than 70.
39 static final int _BOUNDARY_LENGTH = 70; 38 static const int _BOUNDARY_LENGTH = 70;
40 39
41 static final Random _random = new Random(); 40 static final Random _random = new Random();
42 41
43 /// The form fields to send for this request. 42 /// The form fields to send for this request.
44 final Map<String, String> fields; 43 final Map<String, String> fields;
45 44
46 /// The private version of [files]. 45 /// The private version of [files].
47 final List<MultipartFile> _files; 46 final List<MultipartFile> _files;
48 47
49 /// Creates a new [MultipartRequest]. 48 /// Creates a new [MultipartRequest].
(...skipping 18 matching lines...) Expand all
68 67
69 for (var file in _files) { 68 for (var file in _files) {
70 length += "--".length + _BOUNDARY_LENGTH + "\r\n".length + 69 length += "--".length + _BOUNDARY_LENGTH + "\r\n".length +
71 _headerForFile(file).length + 70 _headerForFile(file).length +
72 file.length + "\r\n".length; 71 file.length + "\r\n".length;
73 } 72 }
74 73
75 return length + "--".length + _BOUNDARY_LENGTH + "--\r\n".length; 74 return length + "--".length + _BOUNDARY_LENGTH + "--\r\n".length;
76 } 75 }
77 76
78 set contentLength(int value) { 77 void set contentLength(int value) {
79 throw new UnsupportedError("Cannot set the contentLength property of " 78 throw new UnsupportedError("Cannot set the contentLength property of "
80 "multipart requests."); 79 "multipart requests.");
81 } 80 }
82 81
83 /// Freezes all mutable fields and returns a single-subscription [ByteStream] 82 /// Freezes all mutable fields and returns a single-subscription [ByteStream]
84 /// that will emit the request body. 83 /// that will emit the request body.
85 ByteStream finalize() { 84 ByteStream finalize() {
86 // TODO(nweiz): freeze fields and files 85 // TODO(nweiz): freeze fields and files
87 var boundary = _boundaryString(_BOUNDARY_LENGTH); 86 var boundary = _boundaryString();
88 headers['content-type'] = 'multipart/form-data; boundary="$boundary"'; 87 headers['content-type'] = 'multipart/form-data; boundary="$boundary"';
89 headers['content-transfer-encoding'] = 'binary'; 88 headers['content-transfer-encoding'] = 'binary';
90 super.finalize(); 89 super.finalize();
91 90
92 var controller = new StreamController<List<int>>(sync: true); 91 var controller = new StreamController<List<int>>(sync: true);
93 92
94 void writeAscii(String string) { 93 void writeAscii(String string) {
95 assert(isPlainAscii(string)); 94 assert(isPlainAscii(string));
96 controller.add(string.codeUnits); 95 controller.add(string.codeUnits);
97 } 96 }
(...skipping 18 matching lines...) Expand all
116 // the stream. See issue 3657. 115 // the stream. See issue 3657.
117 writeAscii('--$boundary--\r\n'); 116 writeAscii('--$boundary--\r\n');
118 controller.close(); 117 controller.close();
119 }); 118 });
120 119
121 return new ByteStream(controller.stream); 120 return new ByteStream(controller.stream);
122 } 121 }
123 122
124 /// All character codes that are valid in multipart boundaries. From 123 /// All character codes that are valid in multipart boundaries. From
125 /// http://tools.ietf.org/html/rfc2046#section-5.1.1. 124 /// http://tools.ietf.org/html/rfc2046#section-5.1.1.
126 static final List<int> _BOUNDARY_CHARACTERS = const <int>[ 125 static const List<int> _BOUNDARY_CHARACTERS = const <int>[
127 39, 40, 41, 43, 95, 44, 45, 46, 47, 58, 61, 63, 48, 49, 50, 51, 52, 53, 54, 126 39, 40, 41, 43, 95, 44, 45, 46, 47, 58, 61, 63, 48, 49, 50, 51, 52, 53, 54,
128 55, 56, 57, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 127 55, 56, 57, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
129 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 97, 98, 99, 100, 101, 102, 103, 128 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 97, 98, 99, 100, 101, 102, 103,
130 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 129 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118,
131 119, 120, 121, 122 130 119, 120, 121, 122
132 ]; 131 ];
133 132
134 /// Returns the header string for a field. The return value is guaranteed to 133 /// Returns the header string for a field. The return value is guaranteed to
135 /// contain only ASCII characters. 134 /// contain only ASCII characters.
136 String _headerForField(String name, String value) { 135 String _headerForField(String name, String value) {
(...skipping 13 matching lines...) Expand all
150 String _headerForFile(MultipartFile file) { 149 String _headerForFile(MultipartFile file) {
151 var header = 'content-type: ${file.contentType}\r\n' 150 var header = 'content-type: ${file.contentType}\r\n'
152 'content-disposition: form-data; name="${Uri.encodeFull(file.field)}"'; 151 'content-disposition: form-data; name="${Uri.encodeFull(file.field)}"';
153 152
154 if (file.filename != null) { 153 if (file.filename != null) {
155 header = '$header; filename="${Uri.encodeFull(file.filename)}"'; 154 header = '$header; filename="${Uri.encodeFull(file.filename)}"';
156 } 155 }
157 return '$header\r\n\r\n'; 156 return '$header\r\n\r\n';
158 } 157 }
159 158
160 /// Returns a randomly-generated multipart boundary string of the given 159 /// Returns a randomly-generated multipart boundary string
161 /// [length]. 160 String _boundaryString() {
162 String _boundaryString(int length) {
163 var prefix = "dart-http-boundary-"; 161 var prefix = "dart-http-boundary-";
164 var list = new List<int>(length - prefix.length); 162 var list = new List<int>.generate(_BOUNDARY_LENGTH - prefix.length,
165 for (var i = 0; i < list.length; i++) { 163 (index) =>
166 list[i] = _BOUNDARY_CHARACTERS[ 164 _BOUNDARY_CHARACTERS[_random.nextInt(_BOUNDARY_CHARACTERS.length)],
167 _random.nextInt(_BOUNDARY_CHARACTERS.length)]; 165 growable: false);
nweiz 2013/07/23 22:19:58 style nit: indent -2
168 }
169 return "$prefix${new String.fromCharCodes(list)}"; 166 return "$prefix${new String.fromCharCodes(list)}";
170 } 167 }
171 } 168 }
OLDNEW
« no previous file with comments | « pkg/http/lib/src/client.dart ('k') | pkg/http/lib/src/streamed_request.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698