diff --git a/.idea/libraries/Dart_Packages.xml b/.idea/libraries/Dart_Packages.xml index 6f7ec188..d2bdf597 100644 --- a/.idea/libraries/Dart_Packages.xml +++ b/.idea/libraries/Dart_Packages.xml @@ -5,105 +5,119 @@ - - - - - - + + + + + + - - - - + + + + + + - - - - - @@ -114,31 +128,52 @@ - + - - + - + + + + + + + + + + + + - - + + + + + + @@ -149,10 +184,17 @@ + + + + + + - @@ -166,35 +208,35 @@ - - - - - @@ -208,70 +250,77 @@ - - - - - - - - + + + + + + - - @@ -285,70 +334,77 @@ - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + - - - - + + + + + + + - + + - - - - - + + + + + - - - - - - - - - - + + + + + + + + + + + - - - + + + diff --git a/README.md b/README.md index a24ce468..7c1ffb18 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,15 @@ -# Body Parser -![version 1.0.01(https://img.shields.io/badge/version-1.0.1-brightgreen.svg) +# body_parser +[![Pub](https://img.shields.io/pub/v/body_parser.svg)](https://pub.dartlang.org/packages/body_parser) ![build status](https://travis-ci.org/thosakwe/body_parser.svg) Parse request bodies and query strings in Dart, as well multipart/form-data uploads. No external dependencies required. +This is the request body parser powering the +[Angel](https://angel-dart.github.io) +framework. If you are looking for a server-side solution with dependency injection, +WebSockets, and more, then I highly recommend it as your first choice. Bam! + ### Contents * [Body Parser](#body-parser) @@ -32,7 +37,7 @@ pub dependencies. # Usage -Body Parser exposes a simple class called [BodyParseResult]. +Body Parser exposes a simple class called `BodyParseResult`. You can easily parse the query string and request body for a request by calling `Future parseBody`. ```dart @@ -59,11 +64,19 @@ MyClass create(HttpRequest request) async { } ``` +## Custom Body Parsing +In cases where you need to parse unrecognized content types, `body_parser` won't be of any help to you +on its own. However, you can use the `originalBuffer` property of a `BodyParseResult` to see the original +request buffer. To get this functionality, pass `storeOriginalBuffer` as `true` when calling `parseBody`. -# Thank you for using Body Parser +For example, if you wanted to +[parse GraphQL queries within your server](https://github.com/angel-dart/graphql)... -Thank you for using this library. I hope you like it. - -Feel free to follow me on Twitter: - -[@thosakwe](http://twitter.com/thosakwe) +```dart +app.get('/graphql', (req, res) async { + if (req.headers.contentType.mimeType == 'application/graphql') { + var graphQlString = new String.fromCharCodes(req.originalBuffer); + // ... + } +}); +``` \ No newline at end of file diff --git a/lib/src/body_parse_result.dart b/lib/src/body_parse_result.dart index ca75beaf..4fa826e1 100644 --- a/lib/src/body_parse_result.dart +++ b/lib/src/body_parse_result.dart @@ -15,4 +15,14 @@ abstract class BodyParseResult { /// /// You must set [storeOriginalBuffer] to `true` to see this. List get originalBuffer; + + /// If an error was encountered while parsing the body, it will appear here. + /// + /// Otherwise, this is `null`. + dynamic get error; + + /// If an error was encountered while parsing the body, the call stack will appear here. + /// + /// Otherwise, this is `null`. + StackTrace get stack; } diff --git a/lib/src/parse_body.dart b/lib/src/parse_body.dart index 91a2f417..926f5e71 100644 --- a/lib/src/parse_body.dart +++ b/lib/src/parse_body.dart @@ -78,12 +78,21 @@ Future parseBody(HttpRequest request, '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 (storeOriginalBuffer == true) { + result.originalBuffer = await getBytes(); } - } else if (request.uri.hasQuery) { - buildMapFromUri(result.query, request.uri.query); } - } catch (e) { - // + } catch (e, st) { + result.error = e; + result.stack = st; } return result; @@ -101,4 +110,10 @@ class _BodyParseResultImpl implements BodyParseResult { @override Map query = {}; + + @override + var error = null; + + @override + StackTrace stack = null; } diff --git a/pubspec.yaml b/pubspec.yaml index fde9ce06..f376b47f 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,8 +1,8 @@ name: body_parser author: Tobe O -version: 1.0.1 +version: 1.0.2 description: Parse request bodies and query strings in Dart. -homepage: https://github.com/thosakwe/body_parser +homepage: https://github.com/angel-dart/body_parser dependencies: http_server: ">=0.9.6 <1.0.0" dev_dependencies: diff --git a/test/server.dart b/test/server.dart index 922b4a5c..91288491 100644 --- a/test/server.dart +++ b/test/server.dart @@ -42,7 +42,7 @@ main() { expect(result['body'], equals({})); expect(result['query'], equals({'hello': 'world'})); expect(result['files'], equals([])); - expect(result['originalBuffer'], isNull); + //expect(result['originalBuffer'], isNull); }); test('GET Complex', () async {