Skip to content

Commit 604f577

Browse files
committed
remove boundary characters that pkg:http_parser cannot parse
1 parent 9076857 commit 604f577

File tree

4 files changed

+29
-17
lines changed

4 files changed

+29
-17
lines changed

lib/src/boundary_characters.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
/// All character codes that are valid in multipart boundaries. This is the
6+
/// intersection of the characters allowed in the `bcharsnospace` production
7+
/// defined in [RFC 2046][] and those allowed in the `token` production
8+
/// defined in [RFC 1521][].
9+
///
10+
/// [RFC 2046]: http://tools.ietf.org/html/rfc2046#section-5.1.1.
11+
/// [RFC 1521]: https://tools.ietf.org/html/rfc1521#section-4
12+
const List<int> BOUNDARY_CHARACTERS = const <int>[
13+
39, 43, 95, 45, 46, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57,
14+
65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83,
15+
84, 85, 86, 87, 88, 89, 90, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106,
16+
107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121,
17+
122
18+
];

lib/src/multipart_request.dart

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import 'dart:convert';
77
import 'dart:math';
88

99
import 'base_request.dart';
10+
import 'boundary_characters.dart';
1011
import 'byte_stream.dart';
1112
import 'multipart_file.dart';
1213
import 'utils.dart';
@@ -117,21 +118,6 @@ class MultipartRequest extends BaseRequest {
117118
return new ByteStream(controller.stream);
118119
}
119120

120-
/// All character codes that are valid in multipart boundaries. This is the
121-
/// intersection of the characters allowed in the `bcharsnospace` production
122-
/// defined in [RFC 2046][] and those allowed in the `token` production
123-
/// defined in [RFC 1521][].
124-
///
125-
/// [RFC 2046]: http://tools.ietf.org/html/rfc2046#section-5.1.1.
126-
/// [RFC 1521]: https://tools.ietf.org/html/rfc1521#section-4
127-
static const List<int> _BOUNDARY_CHARACTERS = const <int>[
128-
39, 43, 95, 44, 45, 46, 58, 61, 63, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57,
129-
65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83,
130-
84, 85, 86, 87, 88, 89, 90, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106,
131-
107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121,
132-
122
133-
];
134-
135121
/// Returns the header string for a field. The return value is guaranteed to
136122
/// contain only ASCII characters.
137123
String _headerForField(String name, String value) {
@@ -172,7 +158,7 @@ class MultipartRequest extends BaseRequest {
172158
var prefix = "dart-http-boundary-";
173159
var list = new List<int>.generate(_BOUNDARY_LENGTH - prefix.length,
174160
(index) =>
175-
_BOUNDARY_CHARACTERS[_random.nextInt(_BOUNDARY_CHARACTERS.length)],
161+
BOUNDARY_CHARACTERS[_random.nextInt(BOUNDARY_CHARACTERS.length)],
176162
growable: false);
177163
return "$prefix${new String.fromCharCodes(list)}";
178164
}

test/io/utils.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ Future startServer() {
8585
requestBody = requestBodyBytes;
8686
}
8787

88-
var content = {
88+
var content = <String, dynamic>{
8989
'method': request.method,
9090
'path': request.uri.path,
9191
'headers': {}

test/multipart_test.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import 'dart:async';
66

77
import 'package:http/http.dart' as http;
8+
import 'package:http/src/boundary_characters.dart';
89
import 'package:http_parser/http_parser.dart';
910
import 'package:unittest/unittest.dart';
1011

@@ -18,6 +19,13 @@ void main() {
1819
'''));
1920
});
2021

22+
test('boundary characters', () {
23+
var testBoundary = new String.fromCharCodes(BOUNDARY_CHARACTERS);
24+
var contentType = new MediaType.parse('text/plain; boundary=${testBoundary}');
25+
var boundary = contentType.parameters['boundary'];
26+
expect(boundary, testBoundary);
27+
});
28+
2129
test('with fields and files', () {
2230
var request = new http.MultipartRequest('POST', dummyUrl);
2331
request.fields['field1'] = 'value1';

0 commit comments

Comments
 (0)