diff --git a/.idea/runConfigurations/main_dart.xml b/.idea/runConfigurations/main_dart.xml new file mode 100644 index 00000000..750f7262 --- /dev/null +++ b/.idea/runConfigurations/main_dart.xml @@ -0,0 +1,7 @@ + + + + \ No newline at end of file diff --git a/.idea/runConfigurations/server_dart.xml b/.idea/runConfigurations/server_dart.xml deleted file mode 100644 index 2171ad5d..00000000 --- a/.idea/runConfigurations/server_dart.xml +++ /dev/null @@ -1,7 +0,0 @@ - - - - \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 00000000..8d27178a --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,2 @@ +# 1.1.0 +* Add `parseBodyFromStream` \ No newline at end of file diff --git a/example/server.dart b/example/main.dart similarity index 100% rename from example/server.dart rename to example/main.dart diff --git a/lib/src/parse_body.dart b/lib/src/parse_body.dart index 5b830502..cca4456e 100644 --- a/lib/src/parse_body.dart +++ b/lib/src/parse_body.dart @@ -1,12 +1,26 @@ import 'dart:async'; import 'dart:convert'; import 'dart:io'; +import 'package:http_parser/http_parser.dart'; import 'package:http_server/http_server.dart'; import 'package:mime/mime.dart'; import 'body_parse_result.dart'; import 'file_upload_info.dart'; import 'map_from_uri.dart'; +/// Forwards to [parseBodyFromStream]. +@deprecated +Future parseBody(HttpRequest request, + {bool storeOriginalBuffer: false}) { + return parseBodyFromStream( + request, + request.headers.contentType != null + ? new MediaType.parse(request.headers.contentType.toString()) + : null, + request.uri, + storeOriginalBuffer: storeOriginalBuffer); +} + /// Grabs data from an incoming request. /// /// Supports URL-encoded and JSON, as well as multipart/* forms. @@ -15,12 +29,13 @@ import 'map_from_uri.dart'; /// via the *fileUploadName* parameter. :) /// /// Use [storeOriginalBuffer] to add the original request bytes to the result. -Future parseBody(HttpRequest request, +Future parseBodyFromStream( + Stream> data, MediaType contentType, Uri requestUri, {bool storeOriginalBuffer: false}) async { var result = new _BodyParseResultImpl(); Future> getBytes() { - return request + return data .fold(new BytesBuilder(copy: false), (a, b) => a..add(b)) .then((b) => b.takeBytes()); } @@ -32,13 +47,13 @@ Future parseBody(HttpRequest request, return UTF8.decode(bytes); }); } else - return request.transform(UTF8.decoder).join(); + return data.transform(UTF8.decoder).join(); } try { - if (request.headers.contentType != null) { - if (request.headers.contentType.primaryType == 'multipart' && - request.headers.contentType.parameters.containsKey('boundary')) { + if (contentType != null) { + if (contentType.type == 'multipart' && + contentType.parameters.containsKey('boundary')) { Stream> stream; if (storeOriginalBuffer) { @@ -48,12 +63,12 @@ Future parseBody(HttpRequest request, ..close(); stream = ctrl.stream; } else { - stream = request; + stream = data; } var parts = stream .transform(new MimeMultipartTransformer( - request.headers.contentType.parameters['boundary'])) + contentType.parameters['boundary'])) .map((part) => HttpMultipartFormData.parse(part, defaultEncoding: UTF8)); @@ -76,19 +91,17 @@ Future parseBody(HttpRequest request, '${part.contentDisposition.parameters["name"]}=$text'); } } - } else if (request.headers.contentType.mimeType == - ContentType.JSON.mimeType) { + } else if (contentType.mimeType == ContentType.JSON.mimeType) { result.body.addAll(JSON.decode(await getBody())); - } else if (request.headers.contentType.mimeType == - 'application/x-www-form-urlencoded') { + } else if (contentType.mimeType == 'application/x-www-form-urlencoded') { String body = await getBody(); buildMapFromUri(result.body, body); } else if (storeOriginalBuffer == true) { result.originalBuffer = await getBytes(); } } else { - if (request.uri.hasQuery) { - buildMapFromUri(result.query, request.uri.query); + if (requestUri.hasQuery) { + buildMapFromUri(result.query, requestUri.query); } if (storeOriginalBuffer == true) { diff --git a/pubspec.yaml b/pubspec.yaml index dd230230..424d74ab 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,10 +1,12 @@ name: body_parser author: Tobe O -version: 1.0.3 -description: Parse request bodies and query strings in Dart. +version: 1.1.0 +description: Parse request bodies and query strings in Dart. Supports JSON, URL-encoded, and multi-part bodies. homepage: https://github.com/angel-dart/body_parser dependencies: + http_parser: ">=3.1.1 <4.0.0" http_server: ">=0.9.6 <1.0.0" + mime: ">=0.9.3 <1.0.0" dev_dependencies: http: ">=0.11.3 <0.12.0" json_god: ">=2.0.0-beta <3.0.0"