diff --git a/lib/src/http/controller.dart b/lib/src/http/controller.dart index 6f795afc..85d8b17d 100644 --- a/lib/src/http/controller.dart +++ b/lib/src/http/controller.dart @@ -52,33 +52,29 @@ class Controller { ResponseContext res) async { List args = []; - try { - // Load parameters, and execute - for (int i = 0; i < methodMirror.parameters.length; i++) { - ParameterMirror parameter = methodMirror.parameters[i]; - if (parameter.type.reflectedType == RequestContext) - args.add(req); - else if (parameter.type.reflectedType == ResponseContext) - args.add(res); - else { - String name = MirrorSystem.getName(parameter.simpleName); - var arg = req.params[name]; + // Load parameters, and execute + for (int i = 0; i < methodMirror.parameters.length; i++) { + ParameterMirror parameter = methodMirror.parameters[i]; + if (parameter.type.reflectedType == RequestContext) + args.add(req); + else if (parameter.type.reflectedType == ResponseContext) + args.add(res); + else { + String name = MirrorSystem.getName(parameter.simpleName); + var arg = req.params[name]; - if (arg == null && - !exposeMirror.reflectee.allowNull.contain(name)) { - throw new AngelHttpException.BadRequest(); - } - - args.add(arg); + if (arg == null && + !exposeMirror.reflectee.allowNull.contain(name)) { + throw new AngelHttpException.BadRequest(); } - } - return await instanceMirror - .invoke(key, args) - .reflectee; - } catch (e) { - throw new AngelHttpException(e); + args.add(arg); + } } + + return await instanceMirror + .invoke(key, args) + .reflectee; }; Route route = new Route( exposeMirror.reflectee.method, diff --git a/lib/src/http/response_context.dart b/lib/src/http/response_context.dart index 42c1a0e4..cfef32a9 100644 --- a/lib/src/http/response_context.dart +++ b/lib/src/http/response_context.dart @@ -83,7 +83,7 @@ class ResponseContext extends Extensible {

Currently redirecting you...


- Click if you are not automatically redirected... + Click here if you are not automatically redirected... diff --git a/lib/src/http/server.dart b/lib/src/http/server.dart index ac4ecbc7..b49329f1 100644 --- a/lib/src/http/server.dart +++ b/lib/src/http/server.dart @@ -12,6 +12,12 @@ typedef Future AngelConfigurer(Angel app); /// A powerful real-time/REST/MVC server class. class Angel extends Routable { + var _beforeProcessed = new StreamController(); + var _afterProcessed = new StreamController(); + + Stream get beforeProcessed => _beforeProcessed.stream; + Stream get afterProcessed => _afterProcessed.stream; + ServerGenerator _serverGenerator = (address, port) async => await HttpServer.bind(address, port); @@ -50,69 +56,72 @@ class Angel extends Routable { await _serverGenerator(address ?? InternetAddress.LOOPBACK_IP_V4, port); this.httpServer = server; - server.listen((HttpRequest request) async { - String req_url = - request.uri.toString().replaceAll("?" + request.uri.query, "").replaceAll(new RegExp(r'\/+$'), ''); - if (req_url.isEmpty) req_url = '/'; - RequestContext req = await RequestContext.from(request, {}, this, null); - ResponseContext res = await ResponseContext.from(request.response, this); - - bool canContinue = true; - - var execHandler = (handler, req) async { - if (canContinue) { - canContinue = await new Future.sync(() async { - return _applyHandler(handler, req, res); - }).catchError((e, [StackTrace stackTrace]) async { - if (e is AngelHttpException) { - // Special handling for AngelHttpExceptions :) - try { - res.status(e.statusCode); - String accept = request.headers.value(HttpHeaders.ACCEPT); - if (accept == "*/*" || - accept.contains("application/json") || - accept.contains("application/javascript")) { - res.json(e.toMap()); - } else { - await _errorHandler(e, req, res); - } - _finalizeResponse(request, res); - } catch (_) {} - } - _onError(e, stackTrace); - canContinue = false; - return false; - }); - } else - return false; - }; - - for (var handler in before) { - await execHandler(handler, req); - } - - for (Route route in routes) { - if (!canContinue) break; - if (route.matcher.hasMatch(req_url) && - (request.method == route.method || route.method == '*')) { - req.params = route.parseParameters(req_url); - req.route = route; - - for (var handler in route.handlers) { - await execHandler(handler, req); - } - } - } - - for (var handler in after) { - await execHandler(handler, req); - } - _finalizeResponse(request, res); - }); + server.listen(handleRequest); return server; } + Future handleRequest(HttpRequest request) async { + _beforeProcessed.add(request); + String req_url = + request.uri.toString().replaceAll("?" + request.uri.query, "").replaceAll(new RegExp(r'\/+$'), ''); + if (req_url.isEmpty) req_url = '/'; + RequestContext req = await RequestContext.from(request, {}, this, null); + ResponseContext res = await ResponseContext.from(request.response, this); + + bool canContinue = true; + + var execHandler = (handler, req) async { + if (canContinue) { + canContinue = await new Future.sync(() async { + return _applyHandler(handler, req, res); + }).catchError((e, [StackTrace stackTrace]) async { + if (e is AngelHttpException) { + // Special handling for AngelHttpExceptions :) + try { + res.status(e.statusCode); + String accept = request.headers.value(HttpHeaders.ACCEPT); + if (accept == "*/*" || + accept.contains("application/json") || + accept.contains("application/javascript")) { + res.json(e.toMap()); + } else { + await _errorHandler(e, req, res); + } + _finalizeResponse(request, res); + } catch (_) {} + } + _onError(e, stackTrace); + canContinue = false; + return false; + }); + } else + return false; + }; + + for (var handler in before) { + await execHandler(handler, req); + } + + for (Route route in routes) { + if (!canContinue) break; + if (route.matcher.hasMatch(req_url) && + (request.method == route.method || route.method == '*')) { + req.params = route.parseParameters(req_url); + req.route = route; + + for (var handler in route.handlers) { + await execHandler(handler, req); + } + } + } + + for (var handler in after) { + await execHandler(handler, req); + } + _finalizeResponse(request, res); + } + Future _applyHandler( handler, RequestContext req, ResponseContext res) async { if (handler is RequestMiddleware) { @@ -162,6 +171,7 @@ class Angel extends Routable { if (!res.willCloseItself) { res.responseData.forEach((blob) => request.response.add(blob)); await request.response.close(); + _afterProcessed.add(request); } } catch (e) { // Remember: This fails silently diff --git a/pubspec.yaml b/pubspec.yaml index 31832234..5fdf2a91 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: angel_framework -version: 1.0.0-dev.8 +version: 1.0.0-dev.13 description: Core libraries for the Angel framework. author: Tobe O homepage: https://github.com/angel-dart/angel_framework