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

Unified Diff: pkg/http/lib/src/multipart_request.dart

Issue 218993016: Make MultipartRequest more closely adhere to browsers' behavior. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: code review Created 6 years, 9 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « pkg/http/CHANGELOG.md ('k') | pkg/http/test/multipart_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/http/lib/src/multipart_request.dart
diff --git a/pkg/http/lib/src/multipart_request.dart b/pkg/http/lib/src/multipart_request.dart
index 5bbd5b328497539fa68c4f27e754baf0ed715715..2ccbd92106f787ac9bfa90ed370c8693b3df39b0 100644
--- a/pkg/http/lib/src/multipart_request.dart
+++ b/pkg/http/lib/src/multipart_request.dart
@@ -13,6 +13,8 @@ import 'byte_stream.dart';
import 'multipart_file.dart';
import 'utils.dart';
+final _newlineRegExp = new RegExp(r"\r\n|\r|\n");
+
/// A `multipart/form-data` request. Such a request has both string [fields],
/// which function as normal form fields, and (potentially streamed) binary
/// [files].
@@ -61,13 +63,13 @@ class MultipartRequest extends BaseRequest {
fields.forEach((name, value) {
length += "--".length + _BOUNDARY_LENGTH + "\r\n".length +
- _headerForField(name, value).length +
+ UTF8.encode(_headerForField(name, value)).length +
UTF8.encode(value).length + "\r\n".length;
});
for (var file in _files) {
length += "--".length + _BOUNDARY_LENGTH + "\r\n".length +
- _headerForFile(file).length +
+ UTF8.encode(_headerForFile(file)).length +
file.length + "\r\n".length;
}
@@ -91,8 +93,7 @@ class MultipartRequest extends BaseRequest {
var controller = new StreamController<List<int>>(sync: true);
void writeAscii(String string) {
- assert(isPlainAscii(string));
- controller.add(string.codeUnits);
+ controller.add(UTF8.encode(string));
}
writeUtf8(String string) => controller.add(UTF8.encode(string));
@@ -133,11 +134,8 @@ class MultipartRequest extends BaseRequest {
/// Returns the header string for a field. The return value is guaranteed to
/// contain only ASCII characters.
String _headerForField(String name, String value) {
- // http://tools.ietf.org/html/rfc2388 mandates some complex encodings for
- // field names and file names, but in practice user agents seem to just
- // URL-encode them so we do the same.
var header =
- 'content-disposition: form-data; name="${Uri.encodeFull(name)}"';
+ 'content-disposition: form-data; name="${_browserEncode(name)}"';
if (!isPlainAscii(value)) {
header = '$header\r\ncontent-type: text/plain; charset=utf-8';
}
@@ -148,14 +146,24 @@ class MultipartRequest extends BaseRequest {
/// contain only ASCII characters.
String _headerForFile(MultipartFile file) {
var header = 'content-type: ${file.contentType}\r\n'
- 'content-disposition: form-data; name="${Uri.encodeFull(file.field)}"';
+ 'content-disposition: form-data; name="${_browserEncode(file.field)}"';
if (file.filename != null) {
- header = '$header; filename="${Uri.encodeFull(file.filename)}"';
+ header = '$header; filename="${_browserEncode(file.filename)}"';
}
return '$header\r\n\r\n';
}
+ /// Encode [value] in the same way browsers do.
+ String _browserEncode(String value) {
+ // http://tools.ietf.org/html/rfc2388 mandates some complex encodings for
+ // field names and file names, but in practice user agents seem not to
+ // follow this at all. Instead, they URL-encode `\r`, `\n`, and `\r\n` as
+ // `\r\n`; URL-encode `"`; and do nothing else (even for `%` or non-ASCII
+ // characters). We follow their behavior.
+ return value.replaceAll(_newlineRegExp, "%0D%0A").replaceAll('"', "%22");
+ }
+
/// Returns a randomly-generated multipart boundary string
String _boundaryString() {
var prefix = "dart-http-boundary-";
« no previous file with comments | « pkg/http/CHANGELOG.md ('k') | pkg/http/test/multipart_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698