From c2ee694cc35002ed905ba303c12bfce401dd8fd1 Mon Sep 17 00:00:00 2001 From: Tobe O Date: Mon, 20 Aug 2018 00:09:54 -0400 Subject: [PATCH] Update query parsing --- .idea/workspace.xml | 466 +++++++++++++++--------------- CHANGELOG.md | 3 +- lib/src/core/metadata.dart | 2 +- lib/src/core/request_context.dart | 57 +--- lib/src/core/service.dart | 134 +++++---- test/routing_test.dart | 2 +- 6 files changed, 324 insertions(+), 340 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 564643a6..92c30624 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -3,7 +3,11 @@ - + + + + + @@ -26,13 +30,67 @@ - - + + - - + + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -41,8 +99,8 @@ - - + + @@ -50,48 +108,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -104,11 +120,6 @@ - redirectTo - finalizers.t - `Iterable` - instance - == as Map<String, dynamic> result as Map<String, dynamic> random_strin @@ -132,11 +143,15 @@ package:dart:io deprecated parse - io. io + io. + lazyP + storeO + origina + originalBuffer + lazyQue - isClosed useStream streaming !isOpen @@ -166,6 +181,7 @@ dart:io json.decode rawRequest. + rawResponse. C:\Users\thosa\Source\Angel\framework\lib @@ -189,8 +205,6 @@ @@ -658,35 +674,7 @@ - - - - 1528442409476 - - - 1528670046042 - - - 1528672631391 - - - 1529523884120 - 1529724578292 @@ -1003,7 +991,35 @@ - @@ -1039,7 +1055,7 @@ - @@ -1059,7 +1075,7 @@ - + @@ -1077,7 +1093,7 @@ - + @@ -1102,10 +1118,6 @@ @@ -1135,23 +1151,6 @@ - - - - - - - - - - - - - - - - - @@ -1169,16 +1168,6 @@ - - - - - - - - - - @@ -1196,20 +1185,6 @@ - - - - - - - - - - - - - - @@ -1251,16 +1226,6 @@ - - - - - - - - - - @@ -1295,13 +1260,6 @@ - - - - - - - @@ -1380,13 +1338,6 @@ - - - - - - - @@ -1451,16 +1402,6 @@ - - - - - - - - - - @@ -1478,46 +1419,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -1528,16 +1429,131 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/CHANGELOG.md b/CHANGELOG.md index db310248..83a80483 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,4 +26,5 @@ gone. with `Angel.eagerParseRequestBodies`. * `Angel.storeOriginalBuffer` -> `Angel.storeRawRequestBuffers`. * The methods `lazyBody`, `lazyFiles`, and `lazyOriginalBuffer` on `ResponseContext` were all -replaced with `parseBody`, `parseUploadedFiles`, and `parseRawRequestBuffer`, respectively. \ No newline at end of file +replaced with `parseBody`, `parseUploadedFiles`, and `parseRawRequestBuffer`, respectively. +* Removed the synchronous equivalents of the above methods (`body`, `files`, and `originalBuffer`). \ No newline at end of file diff --git a/lib/src/core/metadata.dart b/lib/src/core/metadata.dart index 0fd38063..df549b85 100644 --- a/lib/src/core/metadata.dart +++ b/lib/src/core/metadata.dart @@ -91,7 +91,7 @@ class Parameter { return req.headers.value(header) ?? defaultValue; if (session?.isNotEmpty == true) return req.session[session] ?? defaultValue; - if (query?.isNotEmpty == true) return req.query[query] ?? defaultValue; + if (query?.isNotEmpty == true) return req.uri.queryParameters[query] ?? defaultValue; return defaultValue; } } diff --git a/lib/src/core/request_context.dart b/lib/src/core/request_context.dart index dd0930b7..639ad73b 100644 --- a/lib/src/core/request_context.dart +++ b/lib/src/core/request_context.dart @@ -59,70 +59,15 @@ abstract class RequestContext { /// The original HTTP verb sent to the server. String get originalMethod; - StateError _unparsed(String type, String caps) => new StateError( - 'Cannot get the $type of an unparsed request. Use lazy${caps}() instead.'); - - /// All post data submitted to the server. - /// - /// If you are lazy-parsing request bodies, but have not manually [parse]d this one, - /// then an error will be thrown. - /// - /// **If you are writing a plug-in, use [parseBody] instead.** - Map get body { - if (_body == null) - throw _unparsed('body', 'Body'); - else - return _body.body; - } - /// The content type of an incoming request. MediaType get contentType; - /// Any and all files sent to the server with this request. - /// - /// If you are lazy-parsing request bodies, but have not manually [parse]d this one, - /// then an error will be thrown. - /// - /// **If you are writing a plug-in, use [parseUploadedFiles] instead.** - List get files { - if (_body == null) - throw _unparsed('query', 'Files'); - else - return _body.files; - } - - /// The original body bytes sent with this request. May be empty. - /// - /// If you are lazy-parsing request bodies, but have not manually [parse]d this one, - /// then an error will be thrown. - /// - /// **If you are writing a plug-in, use [parseRawRequestBuffer] instead.** - List get originalBuffer { - if (_body == null) - throw _unparsed('original buffer', 'OriginalBuffer'); - else - return _body.originalBuffer ?? []; - } - /// The URL parameters extracted from the request URI. Map params = {}; /// The requested path. String get path; - /// The parsed request query string. - /// - /// If you are lazy-parsing request bodies, but have not manually [parse]d this one, - /// then [uri].query will be returned. - /// - /// **If you are writing a plug-in, consider using [lazyQuery] instead.** - Map get query { - if (_body == null) - return _provisionalQuery ??= new Map.from(uri.queryParameters); - else - return _body.query; - } - /// The remote address requesting this resource. InternetAddress get remoteAddress; @@ -223,7 +168,7 @@ abstract class RequestContext { /// Retrieves the request body if it has already been parsed, or lazy-parses it before returning the query. /// /// If [forceParse] is not `true`, then [uri].query will be returned, and no parsing will be performed. - Future> lazyQuery({bool forceParse: false}) { + Future> parseQuery({bool forceParse: false}) { if (_body == null && forceParse != true) return new Future.value(uri.queryParameters); else diff --git a/lib/src/core/service.dart b/lib/src/core/service.dart index 455c891b..363ed2d1 100644 --- a/lib/src/core/service.dart +++ b/lib/src/core/service.dart @@ -160,11 +160,13 @@ class Service extends Routable { Middleware indexMiddleware = getAnnotation(service.index, Middleware); get('/', (RequestContext req, res) { - return this.index(mergeMap([ - {'query': req.query}, - restProvider, - req.serviceParams - ])); + return req.parseQuery().then((query) { + return this.index(mergeMap([ + {'query': query}, + restProvider, + req.serviceParams + ])); + }); }, middleware: [] ..addAll(handlers) @@ -172,18 +174,20 @@ class Service extends Routable { Middleware createMiddleware = getAnnotation(service.create, Middleware); post('/', (RequestContext req, ResponseContext res) { - return req.parseBody().then((body) { - return this - .create( - body, - mergeMap([ - {'query': req.query}, - restProvider, - req.serviceParams - ])) - .then((r) { - res.statusCode = 201; - return r; + return req.parseQuery().then((query) { + return req.parseBody().then((body) { + return this + .create( + body, + mergeMap([ + {'query': query}, + restProvider, + req.serviceParams + ])) + .then((r) { + res.statusCode = 201; + return r; + }); }); }); }, @@ -194,15 +198,17 @@ class Service extends Routable { Middleware readMiddleware = getAnnotation(service.read, Middleware); - get( - '/:id', - (RequestContext req, res) => this.read( + get('/:id', (RequestContext req, res) { + return req.parseQuery().then((query) { + return this.read( parseId(req.params['id']), mergeMap([ - {'query': req.query}, + {'query': query}, restProvider, req.serviceParams - ])), + ])); + }); + }, middleware: [] ..addAll(handlers) ..addAll((readMiddleware == null) ? [] : readMiddleware.handlers)); @@ -210,14 +216,18 @@ class Service extends Routable { Middleware modifyMiddleware = getAnnotation(service.modify, Middleware); patch( '/:id', - (RequestContext req, res) => req.parseBody().then((body) => this.modify( - parseId(req.params['id']), - body, - mergeMap([ - {'query': req.query}, - restProvider, - req.serviceParams - ]))), + (RequestContext req, res) => req.parseBody().then((body) { + return req.parseQuery().then((query) { + return this.modify( + parseId(req.params['id']), + body, + mergeMap([ + {'query': query}, + restProvider, + req.serviceParams + ])); + }); + }), middleware: [] ..addAll(handlers) ..addAll( @@ -226,56 +236,68 @@ class Service extends Routable { Middleware updateMiddleware = getAnnotation(service.update, Middleware); post( '/:id', - (RequestContext req, res) => req.parseBody().then((body) => this.update( - parseId(req.params['id']), - body, - mergeMap([ - {'query': req.query}, - restProvider, - req.serviceParams - ]))), + (RequestContext req, res) => req.parseBody().then((body) { + return req.parseQuery().then((query) { + return this.update( + parseId(req.params['id']), + body, + mergeMap([ + {'query': query}, + restProvider, + req.serviceParams + ])); + }); + }), middleware: [] ..addAll(handlers) ..addAll( (updateMiddleware == null) ? [] : updateMiddleware.handlers)); put( '/:id', - (RequestContext req, res) => req.parseBody().then((body) => this.update( - parseId(req.params['id']), - body, - mergeMap([ - {'query': req.query}, - restProvider, - req.serviceParams - ]))), + (RequestContext req, res) => req.parseBody().then((body) { + return req.parseQuery().then((query) { + return this.update( + parseId(req.params['id']), + body, + mergeMap([ + {'query': query}, + restProvider, + req.serviceParams + ])); + }); + }), middleware: [] ..addAll(handlers) ..addAll( (updateMiddleware == null) ? [] : updateMiddleware.handlers)); Middleware removeMiddleware = getAnnotation(service.remove, Middleware); - delete( - '/', - (RequestContext req, res) => this.remove( + delete('/', (RequestContext req, res) { + return req.parseQuery().then((query) { + return this.remove( null, mergeMap([ - {'query': req.query}, + {'query': query}, restProvider, req.serviceParams - ])), + ])); + }); + }, middleware: [] ..addAll(handlers) ..addAll( (removeMiddleware == null) ? [] : removeMiddleware.handlers)); - delete( - '/:id', - (RequestContext req, res) => this.remove( + delete('/:id', (RequestContext req, res) { + return req.parseQuery().then((query) { + return this.remove( parseId(req.params['id']), mergeMap([ - {'query': req.query}, + {'query': query}, restProvider, req.serviceParams - ])), + ])); + }); + }, middleware: [] ..addAll(handlers) ..addAll( diff --git a/test/routing_test.dart b/test/routing_test.dart index e7b4a281..4573fe15 100644 --- a/test/routing_test.dart +++ b/test/routing_test.dart @@ -81,7 +81,7 @@ main() { res.redirectTo('Named routes', {'name': 'tests'}); }); app.get('/log', (RequestContext req, res) async { - print("Query: ${req.query}"); + print("Query: ${await req.parseQuery()}"); return "Logged"; });