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:async';
import 'dart:convert'; import 'dart:convert';
import 'dart:io'; import 'dart:io';
import 'package:http_parser/http_parser.dart';
import 'package:http_server/http_server.dart'; import 'package:http_server/http_server.dart';
import 'package:mime/mime.dart'; import 'package:mime/mime.dart';
import 'body_parse_result.dart'; import 'body_parse_result.dart';
import 'file_upload_info.dart'; import 'file_upload_info.dart';
import 'map_from_uri.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. /// Grabs data from an incoming request.
/// ///
/// Supports URL-encoded and JSON, as well as multipart/* forms. /// Supports URL-encoded and JSON, as well as multipart/* forms.
@ -15,12 +29,13 @@ import 'map_from_uri.dart';
/// via the *fileUploadName* parameter. :) /// via the *fileUploadName* parameter. :)
/// ///
/// Use [storeOriginalBuffer] to add the original request bytes to the result. /// 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 { {bool storeOriginalBuffer: false}) async {
var result = new _BodyParseResultImpl(); var result = new _BodyParseResultImpl();
Future<List<int>> getBytes() { Future<List<int>> getBytes() {
return request 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());
} }
@ -32,13 +47,13 @@ Future<BodyParseResult> parseBody(HttpRequest request,
return UTF8.decode(bytes); return UTF8.decode(bytes);
}); });
} else } else
return request.transform(UTF8.decoder).join(); return data.transform(UTF8.decoder).join();
} }
try { try {
if (request.headers.contentType != null) { if (contentType != null) {
if (request.headers.contentType.primaryType == 'multipart' && if (contentType.type == 'multipart' &&
request.headers.contentType.parameters.containsKey('boundary')) { contentType.parameters.containsKey('boundary')) {
Stream<List<int>> stream; Stream<List<int>> stream;
if (storeOriginalBuffer) { if (storeOriginalBuffer) {
@ -48,12 +63,12 @@ Future<BodyParseResult> parseBody(HttpRequest request,
..close(); ..close();
stream = ctrl.stream; stream = ctrl.stream;
} else { } else {
stream = request; stream = data;
} }
var parts = stream var parts = stream
.transform(new MimeMultipartTransformer( .transform(new MimeMultipartTransformer(
request.headers.contentType.parameters['boundary'])) contentType.parameters['boundary']))
.map((part) => .map((part) =>
HttpMultipartFormData.parse(part, defaultEncoding: UTF8)); HttpMultipartFormData.parse(part, defaultEncoding: UTF8));
@ -76,19 +91,17 @@ Future<BodyParseResult> parseBody(HttpRequest request,
'${part.contentDisposition.parameters["name"]}=$text'); '${part.contentDisposition.parameters["name"]}=$text');
} }
} }
} else if (request.headers.contentType.mimeType == } else if (contentType.mimeType == ContentType.JSON.mimeType) {
ContentType.JSON.mimeType) {
result.body.addAll(JSON.decode(await getBody())); result.body.addAll(JSON.decode(await getBody()));
} else if (request.headers.contentType.mimeType == } else if (contentType.mimeType == 'application/x-www-form-urlencoded') {
'application/x-www-form-urlencoded') {
String body = await getBody(); String body = await getBody();
buildMapFromUri(result.body, body); buildMapFromUri(result.body, body);
} else if (storeOriginalBuffer == true) { } else if (storeOriginalBuffer == true) {
result.originalBuffer = await getBytes(); result.originalBuffer = await getBytes();
} }
} else { } else {
if (request.uri.hasQuery) { if (requestUri.hasQuery) {
buildMapFromUri(result.query, request.uri.query); buildMapFromUri(result.query, requestUri.query);
} }
if (storeOriginalBuffer == true) { if (storeOriginalBuffer == true) {

View file

@ -1,10 +1,12 @@
name: body_parser name: body_parser
author: Tobe O <thosakwe@gmail.com> author: Tobe O <thosakwe@gmail.com>
version: 1.0.3 version: 1.1.0
description: Parse request bodies and query strings in Dart. 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 homepage: https://github.com/angel-dart/body_parser
dependencies: dependencies:
http_parser: ">=3.1.1 <4.0.0"
http_server: ">=0.9.6 <1.0.0" http_server: ">=0.9.6 <1.0.0"
mime: ">=0.9.3 <1.0.0"
dev_dependencies: dev_dependencies:
http: ">=0.11.3 <0.12.0" http: ">=0.11.3 <0.12.0"
json_god: ">=2.0.0-beta <3.0.0" json_god: ">=2.0.0-beta <3.0.0"