@@ -14,53 +14,51 @@ import 'utils.dart';
14
14
15
15
final _newlineRegExp = RegExp (r"\r\n|\r|\n" );
16
16
17
- /// A `multipart/form-data` request. Such a request has both string [fields] ,
18
- /// which function as normal form fields, and (potentially streamed) binary
19
- /// [files] .
17
+ /// A `multipart/form-data` request.
18
+ ///
19
+ /// Such a request has both string [fields] , which function as normal form
20
+ /// fields, and (potentially streamed) binary [files] .
20
21
///
21
22
/// This request automatically sets the Content-Type header to
22
23
/// `multipart/form-data` . This value will override any value set by the user.
23
24
///
24
- /// var uri = Uri.parse("https://example.com/create");
25
- /// var request = new http.MultipartRequest("POST", uri);
26
- /// request.fields['user'] = '[email protected] ';
27
- /// request.files.add(new http.MultipartFile.fromPath(
28
- /// 'package',
29
- /// 'build/package.tar.gz',
30
- /// contentType: new MediaType('application', 'x-tar'));
25
+ /// var uri = Uri.parse('https://example.com/create');
26
+ /// var request = http.MultipartRequest('POST', uri)
27
+ /// ..fields['user'] = '[email protected] '
28
+ /// ..files.add(http.MultipartFile.fromPath(
29
+ /// 'package', 'build/package.tar.gz',
30
+ /// contentType: MediaType('application', 'x-tar')));
31
31
/// var response = await request.send();
32
32
/// if (response.statusCode == 200) print('Uploaded!');
33
33
class MultipartRequest extends BaseRequest {
34
34
/// The total length of the multipart boundaries used when building the
35
- /// request body. According to http://tools.ietf.org/html/rfc1341.html, this
36
- /// can't be longer than 70.
37
- static const int _BOUNDARY_LENGTH = 70 ;
35
+ /// request body.
36
+ ///
37
+ /// According to http://tools.ietf.org/html/rfc1341.html, this can't be longer
38
+ /// than 70.
39
+ static const int _boundaryLength = 70 ;
38
40
39
41
static final Random _random = Random ();
40
42
41
43
/// The form fields to send for this request.
42
- final Map <String , String > fields ;
44
+ final fields = < String , String > {} ;
43
45
44
- /// The private version of [files] .
45
- final List <MultipartFile > _files;
46
+ final _files = < MultipartFile > [];
46
47
47
- /// Creates a new [MultipartRequest] .
48
- MultipartRequest (String method, Uri url)
49
- : fields = {},
50
- _files = < MultipartFile > [],
51
- super (method, url);
48
+ MultipartRequest (String method, Uri url) : super (method, url);
52
49
53
50
/// The list of files to upload for this request.
54
51
List <MultipartFile > get files => _files;
55
52
56
- /// The total length of the request body, in bytes. This is calculated from
57
- /// [fields] and [files] and cannot be set manually.
53
+ /// The total length of the request body, in bytes.
54
+ ///
55
+ /// This is calculated from [fields] and [files] and cannot be set manually.
58
56
int get contentLength {
59
57
var length = 0 ;
60
58
61
59
fields.forEach ((name, value) {
62
60
length += "--" .length +
63
- _BOUNDARY_LENGTH +
61
+ _boundaryLength +
64
62
"\r\n " .length +
65
63
utf8.encode (_headerForField (name, value)).length +
66
64
utf8.encode (value).length +
@@ -69,14 +67,14 @@ class MultipartRequest extends BaseRequest {
69
67
70
68
for (var file in _files) {
71
69
length += "--" .length +
72
- _BOUNDARY_LENGTH +
70
+ _boundaryLength +
73
71
"\r\n " .length +
74
72
utf8.encode (_headerForFile (file)).length +
75
73
file.length +
76
74
"\r\n " .length;
77
75
}
78
76
79
- return length + "--" .length + _BOUNDARY_LENGTH + "--\r\n " .length;
77
+ return length + "--" .length + _boundaryLength + "--\r\n " .length;
80
78
}
81
79
82
80
set contentLength (int value) {
@@ -123,8 +121,9 @@ class MultipartRequest extends BaseRequest {
123
121
return ByteStream (controller.stream);
124
122
}
125
123
126
- /// Returns the header string for a field. The return value is guaranteed to
127
- /// contain only ASCII characters.
124
+ /// Returns the header string for a field.
125
+ ///
126
+ /// The return value is guaranteed to contain only ASCII characters.
128
127
String _headerForField (String name, String value) {
129
128
var header =
130
129
'content-disposition: form-data; name="${_browserEncode (name )}"' ;
@@ -136,8 +135,9 @@ class MultipartRequest extends BaseRequest {
136
135
return '$header \r\n\r\n ' ;
137
136
}
138
137
139
- /// Returns the header string for a file. The return value is guaranteed to
140
- /// contain only ASCII characters.
138
+ /// Returns the header string for a file.
139
+ ///
140
+ /// The return value is guaranteed to contain only ASCII characters.
141
141
String _headerForFile (MultipartFile file) {
142
142
var header = 'content-type: ${file .contentType }\r\n '
143
143
'content-disposition: form-data; name="${_browserEncode (file .field )}"' ;
@@ -162,7 +162,7 @@ class MultipartRequest extends BaseRequest {
162
162
String _boundaryString () {
163
163
var prefix = "dart-http-boundary-" ;
164
164
var list = List <int >.generate (
165
- _BOUNDARY_LENGTH - prefix.length,
165
+ _boundaryLength - prefix.length,
166
166
(index) =>
167
167
BOUNDARY_CHARACTERS [_random.nextInt (BOUNDARY_CHARACTERS .length)],
168
168
growable: false );
0 commit comments