Uint8List update

This commit is contained in:
Jonathan Rezende 2019-10-09 15:59:52 -03:00 committed by GitHub
parent 20fa8e4177
commit 3434b4cf59
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,5 +1,6 @@
import 'dart:async'; import 'dart:async';
import 'dart:io'; import 'dart:io';
import 'dart:typed_data';
import 'package:dart2_constant/convert.dart'; import 'package:dart2_constant/convert.dart';
import 'package:http_parser/http_parser.dart'; import 'package:http_parser/http_parser.dart';
@ -32,11 +33,11 @@ Future<BodyParseResult> parseBody(HttpRequest request,
/// ///
/// Use [storeOriginalBuffer] to add the original request bytes to the result. /// Use [storeOriginalBuffer] to add the original request bytes to the result.
Future<BodyParseResult> parseBodyFromStream( Future<BodyParseResult> parseBodyFromStream(
Stream<List<int>> data, MediaType contentType, Uri requestUri, Stream<Uint8List> data, MediaType contentType, Uri requestUri,
{bool storeOriginalBuffer: false}) async { {bool storeOriginalBuffer: false}) async {
var result = new _BodyParseResultImpl(); var result = new _BodyParseResultImpl();
Future<List<int>> getBytes() { Future<Uint8List> getBytes() {
return data return data
.fold<BytesBuilder>(new BytesBuilder(copy: false), (a, b) => a..add(b)) .fold<BytesBuilder>(new BytesBuilder(copy: false), (a, b) => a..add(b))
.then((b) => b.takeBytes()); .then((b) => b.takeBytes());
@ -48,19 +49,20 @@ Future<BodyParseResult> parseBodyFromStream(
result.originalBuffer = bytes; result.originalBuffer = bytes;
return utf8.decode(bytes); return utf8.decode(bytes);
}); });
} else } else {
return data.transform(utf8.decoder).join(); return utf8.decoder.bind(data).join();
}
} }
try { try {
if (contentType != null) { if (contentType != null) {
if (contentType.type == 'multipart' && if (contentType.type == 'multipart' &&
contentType.parameters.containsKey('boundary')) { contentType.parameters.containsKey('boundary')) {
Stream<List<int>> stream; Stream<Uint8List> stream;
if (storeOriginalBuffer) { if (storeOriginalBuffer) {
var bytes = result.originalBuffer = await getBytes(); var bytes = result.originalBuffer = await getBytes();
var ctrl = new StreamController<List<int>>() var ctrl = new StreamController<Uint8List>()
..add(bytes) ..add(bytes)
..close(); ..close();
stream = ctrl.stream; stream = ctrl.stream;
@ -68,9 +70,8 @@ Future<BodyParseResult> parseBodyFromStream(
stream = data; stream = data;
} }
var parts = stream var parts = MimeMultipartTransformer(
.transform(new MimeMultipartTransformer( contentType.parameters['boundary']).bind(stream)
contentType.parameters['boundary']))
.map((part) => .map((part) =>
HttpMultipartFormData.parse(part, defaultEncoding: utf8)); HttpMultipartFormData.parse(part, defaultEncoding: utf8));