This commit is contained in:
Tobe O 2018-03-04 17:32:14 -05:00
parent 2f9cf3e45e
commit d691c652f2
6 changed files with 40 additions and 23 deletions

View file

@ -0,0 +1,7 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="main.dart" type="DartCommandLineRunConfigurationType" factoryName="Dart Command Line Application" singleton="true" nameIsGenerated="true">
<option name="filePath" value="$PROJECT_DIR$/example/main.dart" />
<option name="workingDirectory" value="$PROJECT_DIR$" />
<method />
</configuration>
</component>

View file

@ -1,7 +0,0 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="server.dart" type="DartCommandLineRunConfigurationType" factoryName="Dart Command Line Application" singleton="true" nameIsGenerated="true">
<option name="filePath" value="$PROJECT_DIR$/example/server.dart" />
<option name="workingDirectory" value="$PROJECT_DIR$" />
<method />
</configuration>
</component>

2
CHANGELOG.md Normal file
View file

@ -0,0 +1,2 @@
# 1.1.0
* Add `parseBodyFromStream`

View file

@ -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<BodyParseResult> 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<BodyParseResult> parseBody(HttpRequest request,
Future<BodyParseResult> parseBodyFromStream(
Stream<List<int>> data, MediaType contentType, Uri requestUri,
{bool storeOriginalBuffer: false}) async {
var result = new _BodyParseResultImpl();
Future<List<int>> getBytes() {
return request
return data
.fold<BytesBuilder>(new BytesBuilder(copy: false), (a, b) => a..add(b))
.then((b) => b.takeBytes());
}
@ -32,13 +47,13 @@ Future<BodyParseResult> 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<List<int>> stream;
if (storeOriginalBuffer) {
@ -48,12 +63,12 @@ Future<BodyParseResult> 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<BodyParseResult> 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) {

View file

@ -1,10 +1,12 @@
name: body_parser
author: Tobe O <thosakwe@gmail.com>
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"