Skip to content

Commit 2097129

Browse files
committed
Clean up after content lib
1 parent 6029e43 commit 2097129

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

lib/src/multipart_body.dart

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import 'dart:convert';
77

88
import 'body.dart';
99
import 'multipart_file.dart';
10-
import 'utils.dart';
1110

1211
class MultipartBody implements Body {
1312
/// The contents of the message body.
@@ -26,21 +25,25 @@ class MultipartBody implements Body {
2625
/// Creates a [MultipartBody] from the given [fields] and [files].
2726
///
2827
/// The [boundary] is used to
29-
factory MultipartBody(Map<String, String> fields,
30-
List<MultipartFile> files,
31-
String boundary,) {
28+
factory MultipartBody(
29+
Map<String, String> fields,
30+
List<MultipartFile> files,
31+
String boundary,
32+
) {
3233
var controller = new StreamController<List<int>>(sync: true);
3334
var contentLength = 0;
3435

3536
void writeAscii(String string) {
3637
controller.add(string.codeUnits);
3738
contentLength += string.length;
3839
}
40+
3941
void writeUtf8(String string) {
4042
var encoded = UTF8.encode(string);
4143
controller.add(encoded);
4244
contentLength += encoded.length;
4345
}
46+
4447
void writeLine() {
4548
controller.add([13, 10]); // \r\n
4649
contentLength += 2;
@@ -81,8 +84,8 @@ class MultipartBody implements Body {
8184
Future.forEach(files, (file) {
8285
assert(files[i] == file);
8386
controller.add(fileHeaders[i++]);
84-
return writeStreamToSink(file.read(), controller).then((_) =>
85-
controller.add([13, 10]));
87+
return _writeStreamToSink(file.read(), controller)
88+
.then((_) => controller.add([13, 10]));
8689
}).then((_) {
8790
// TODO(nweiz): pass any errors propagated through this future on to
8891
// the stream. See issue 3657.
@@ -112,7 +115,7 @@ class MultipartBody implements Body {
112115
static String _headerForField(String name, String value) {
113116
var header =
114117
'content-disposition: form-data; name="${_browserEncode(name)}"';
115-
if (!isPlainAscii(value)) {
118+
if (!_asciiOnly.hasMatch(value)) {
116119
header = '$header\r\n'
117120
'content-type: text/plain; charset=utf-8\r\n'
118121
'content-transfer-encoding: binary';
@@ -133,7 +136,8 @@ class MultipartBody implements Body {
133136
return '$header\r\n\r\n';
134137
}
135138

136-
static final _newlineRegExp = new RegExp(r"\r\n|\r|\n");
139+
static final _newlineRegExp = new RegExp(r'\r\n|\r|\n');
140+
static final _asciiOnly = new RegExp(r'^[\x00-\x7F]+$');
137141

138142
/// Encode [value] in the same way browsers do.
139143
static String _browserEncode(String value) {
@@ -144,4 +148,14 @@ class MultipartBody implements Body {
144148
// characters). We follow their behavior.
145149
return value.replaceAll(_newlineRegExp, "%0D%0A").replaceAll('"', "%22");
146150
}
151+
152+
/// Pipes all data and errors from [stream] into [sink]. Completes [Future] once
153+
/// [stream] is done. Unlike [store], [sink] remains open after [stream] is
154+
/// done.
155+
static Future _writeStreamToSink(Stream stream, EventSink sink) {
156+
var completer = new Completer();
157+
stream.listen(sink.add,
158+
onError: sink.addError, onDone: () => completer.complete());
159+
return completer.future;
160+
}
147161
}

lib/src/multipart_file.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import 'package:collection/collection.dart';
1111
import 'package:http_parser/http_parser.dart';
1212
import 'package:path/path.dart' as path;
1313

14-
import 'utils.dart';
14+
import 'content_type.dart';
1515

1616
/// A file to be uploaded as part of a [MultipartRequest]. This doesn't need to
1717
/// correspond to a physical file.
@@ -71,7 +71,7 @@ class MultipartFile {
7171
{String filename, MediaType contentType}) {
7272
contentType =
7373
contentType == null ? new MediaType("text", "plain") : contentType;
74-
var encoding = encodingForCharset(contentType.parameters['charset'], UTF8);
74+
var encoding = encodingForCharset(contentType.parameters['charset']) ?? UTF8;
7575
contentType = contentType.change(parameters: {'charset': encoding.name});
7676

7777
return new MultipartFile.fromBytes(field, encoding.encode(value),

0 commit comments

Comments
 (0)