From 16fa8d8c160de65d118a6bfe45c5ca23e4b59509 Mon Sep 17 00:00:00 2001 From: Aliaksandr Zhuhrou Date: Thu, 7 Jun 2012 19:33:24 +0300 Subject: [PATCH 01/17] alter for compatibility with log4dart. --- core/CrimsonLib.dart | 2 +- handlers/HandlersLib.dart | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/core/CrimsonLib.dart b/core/CrimsonLib.dart index a237464..9429d81 100644 --- a/core/CrimsonLib.dart +++ b/core/CrimsonLib.dart @@ -6,7 +6,7 @@ #library("crimson:core"); -#import('../../log4dart/LogLib.dart'); +#import('../../log4dart/Lib.dart'); #import("dart:io"); #import("dart:uri"); #import("dart:isolate", prefix:"isolate"); diff --git a/handlers/HandlersLib.dart b/handlers/HandlersLib.dart index cad8852..9e0501a 100644 --- a/handlers/HandlersLib.dart +++ b/handlers/HandlersLib.dart @@ -1,6 +1,6 @@ #library("crimson:handlers"); #import("../core/CrimsonLib.dart"); -#import('../../log4dart/LogLib.dart'); +#import('../../log4dart/Lib.dart'); #import('../../dart-crypto-lib/src/md5.dart'); #import("dart:io"); From 9060e3f0d51163ca0f5eb3199edbb2253c68fc8b Mon Sep 17 00:00:00 2001 From: Aliaksandr Zhuhrou Date: Thu, 7 Jun 2012 21:12:04 +0300 Subject: [PATCH 02/17] - sync with latest build of dart especially dart:io --- handlers/.children | 2 +- handlers/HandlersLib.dart | 2 +- handlers/endpoints/staticFile.dart | 22 ++++++++++++---------- handlers/filters/cookieSession.dart | 2 +- 4 files changed, 15 insertions(+), 13 deletions(-) diff --git a/handlers/.children b/handlers/.children index c4c037d..3113ffa 100644 --- a/handlers/.children +++ b/handlers/.children @@ -1 +1 @@ -handlers.dart +HandlersLib.dart diff --git a/handlers/HandlersLib.dart b/handlers/HandlersLib.dart index 9e0501a..25e57ad 100644 --- a/handlers/HandlersLib.dart +++ b/handlers/HandlersLib.dart @@ -1,7 +1,7 @@ #library("crimson:handlers"); #import("../core/CrimsonLib.dart"); #import('../../log4dart/Lib.dart'); -#import('../../dart-crypto-lib/src/md5.dart'); +#import("dart:crypto"); #import("dart:io"); #source("endpoints/favicon.dart"); diff --git a/handlers/endpoints/staticFile.dart b/handlers/endpoints/staticFile.dart index 4aa4ce5..b9d8e9b 100644 --- a/handlers/endpoints/staticFile.dart +++ b/handlers/endpoints/staticFile.dart @@ -46,19 +46,14 @@ class StaticFile implements CrimsonEndpoint { _loadFromPath(String path, success(List data), onNotFound(), fail(exception)) { File file = new File(path); - //file.fullPath((String fullPath) => print(fullPath)); + Future existFuture = file.exists(); - file.onError = (Exception error) { - logger.debug("${path} doesn't exist: ${error}"); - fail(error); - }; - - logger.debug("trying to open file: ${path}"); - file.exists((bool exists) { + existFuture.then((exists) { logger.debug("in exists callback: ${path}, ${exists}"); if (exists) { logger.debug("${path} exists, so reading"); - file.readAsBytes( (List buffer) { + Future> bytesFuture = file.readAsBytes(); + bytesFuture.then((List buffer) { logger.debug("successfully read ${path}"); success(buffer); }); @@ -66,8 +61,15 @@ class StaticFile implements CrimsonEndpoint { else { logger.debug("${path} doesn't exist"); onNotFound(); - } + } }); + + existFuture.handleException((Exception error) { + logger.debug("${path} doesn't exist: ${error}"); + fail(error); + }); + + logger.debug("trying to open file: ${path}"); } final String NAME = "StaticFile"; diff --git a/handlers/filters/cookieSession.dart b/handlers/filters/cookieSession.dart index ac608fd..6d0a683 100644 --- a/handlers/filters/cookieSession.dart +++ b/handlers/filters/cookieSession.dart @@ -121,7 +121,7 @@ class CookieSession implements CrimsonFilter { //generate a new ID. //this is a toy - don't use for real! - var md5 = new Md5(); + var md5 = new MD5(); String s = (Math.random() * Clock.now()).toInt().toString(); md5.update(s.charCodes()); var hash = md5.digest(); From fc2308869dec2f84cc34cc6cf7a422811c5b0156 Mon Sep 17 00:00:00 2001 From: Aliaksandr Zhuhrou Date: Fri, 8 Jun 2012 00:08:51 +0300 Subject: [PATCH 03/17] - add controller route support. --- handlers/HandlersLib.dart | 3 +- handlers/endpoints/controllerRoute.dart | 61 +++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 handlers/endpoints/controllerRoute.dart diff --git a/handlers/HandlersLib.dart b/handlers/HandlersLib.dart index 25e57ad..0e80dec 100644 --- a/handlers/HandlersLib.dart +++ b/handlers/HandlersLib.dart @@ -7,4 +7,5 @@ #source("endpoints/favicon.dart"); #source("endpoints/staticFile.dart"); #source("filters/cookieSession.dart"); -#source("endpoints/route.dart"); \ No newline at end of file +#source("endpoints/route.dart"); +#source("endpoints/controllerRoute.dart"); \ No newline at end of file diff --git a/handlers/endpoints/controllerRoute.dart b/handlers/endpoints/controllerRoute.dart new file mode 100644 index 0000000..f000b66 --- /dev/null +++ b/handlers/endpoints/controllerRoute.dart @@ -0,0 +1,61 @@ +class ControllerRoute implements CrimsonEndpoint { + var _name; + var _path; + var _controller; + var logger; + CrimsonHttpServer server; + + /** Contruct route that forwards all matched requests to a given controller. */ + ControllerRoute(String this._path, AppController controller) { + _name = "ROUTE:${_path}"; + logger = LoggerFactory.getLogger(_name); + _controller = controller; + } + + Future handle(HttpRequest req, HttpResponse res, CrimsonData data) { + logger.debug("Request:${req.method}:${req.path} - Handler:${this._path}"); + bool isMatched = false; + if (req.path == this._path) { + isMatched = true; + } else { + //TODO: Add regex matching + } + + if (isMatched) { + logger.debug("Routable handler for request: ${_name}"); + Completer completer = new Completer(); + + Future handlerComplete = _controller.handler(req,res,data); + + if (handlerComplete != null) { + logger.debug("handling"); + handlerComplete.then((completeData) => onSuccess(res, completer, data)); + handlerComplete.handleException((error) => completer.completeException(error)); + } + else { + onSuccess(res, completer,data); + } + + return completer.future; + } + else { + return null; + } + } + + onSuccess(res, completer, data) { + data["SUCCESS"] = true; + res.outputStream.close(); + completer.complete(data); + } + + String get NAME() => _name; + +} + +interface AppController { + + /** A base method that handles a http requests. */ + Future handler(HttpRequest, HttpResponse, CrimsonData); + +} \ No newline at end of file From 6bec415bdf1b48ccc1ea25f3b07e5533de399de2 Mon Sep 17 00:00:00 2001 From: Aliaksandr Zhuhrou Date: Fri, 8 Jun 2012 11:53:29 +0300 Subject: [PATCH 04/17] Change how ControllerRoute matches the incoming requests. --- handlers/endpoints/controllerRoute.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/handlers/endpoints/controllerRoute.dart b/handlers/endpoints/controllerRoute.dart index f000b66..8608ec8 100644 --- a/handlers/endpoints/controllerRoute.dart +++ b/handlers/endpoints/controllerRoute.dart @@ -15,7 +15,7 @@ class ControllerRoute implements CrimsonEndpoint { Future handle(HttpRequest req, HttpResponse res, CrimsonData data) { logger.debug("Request:${req.method}:${req.path} - Handler:${this._path}"); bool isMatched = false; - if (req.path == this._path) { + if (req.path.startsWith(this._path)) { isMatched = true; } else { //TODO: Add regex matching From 70ef31b9aebf57e057202b7f16c7cd3135af4dd8 Mon Sep 17 00:00:00 2001 From: Aliaksandr Zhuhrou Date: Sat, 9 Jun 2012 13:31:53 +0300 Subject: [PATCH 05/17] Attempt to migrate to dart package management. --- core/CrimsonLib.dart | 2 +- handlers/HandlersLib.dart | 2 +- handlers/endpoints/controllerRoute.dart | 8 +++++--- pubspec | 3 +++ 4 files changed, 10 insertions(+), 5 deletions(-) create mode 100644 pubspec diff --git a/core/CrimsonLib.dart b/core/CrimsonLib.dart index 9429d81..27fb64a 100644 --- a/core/CrimsonLib.dart +++ b/core/CrimsonLib.dart @@ -6,7 +6,7 @@ #library("crimson:core"); -#import('../../log4dart/Lib.dart'); +#import('package:log4dart/Lib.dart'); #import("dart:io"); #import("dart:uri"); #import("dart:isolate", prefix:"isolate"); diff --git a/handlers/HandlersLib.dart b/handlers/HandlersLib.dart index 0e80dec..6fdfde4 100644 --- a/handlers/HandlersLib.dart +++ b/handlers/HandlersLib.dart @@ -1,6 +1,6 @@ #library("crimson:handlers"); #import("../core/CrimsonLib.dart"); -#import('../../log4dart/Lib.dart'); +#import("package:log4dart/Lib.dart"); #import("dart:crypto"); #import("dart:io"); diff --git a/handlers/endpoints/controllerRoute.dart b/handlers/endpoints/controllerRoute.dart index 8608ec8..dcca64b 100644 --- a/handlers/endpoints/controllerRoute.dart +++ b/handlers/endpoints/controllerRoute.dart @@ -9,7 +9,8 @@ class ControllerRoute implements CrimsonEndpoint { ControllerRoute(String this._path, AppController controller) { _name = "ROUTE:${_path}"; logger = LoggerFactory.getLogger(_name); - _controller = controller; + _controller = controller; + controller.route = _path; } Future handle(HttpRequest req, HttpResponse res, CrimsonData data) { @@ -56,6 +57,7 @@ class ControllerRoute implements CrimsonEndpoint { interface AppController { /** A base method that handles a http requests. */ - Future handler(HttpRequest, HttpResponse, CrimsonData); - + Future handler(HttpRequest, HttpResponse, CrimsonData); + /** Path to the controller a knowledge about given route path. */ + String set route(String path); } \ No newline at end of file diff --git a/pubspec b/pubspec new file mode 100644 index 0000000..93c3154 --- /dev/null +++ b/pubspec @@ -0,0 +1,3 @@ +dependencies: + log4dart: + git: git://github.com/zhygr/log4dart.git \ No newline at end of file From 0b10f42b3c18cf797cf17bc8bffd3b2de87ea781 Mon Sep 17 00:00:00 2001 From: Aliaksandr Zhuhrou Date: Sun, 10 Jun 2012 00:41:44 +0300 Subject: [PATCH 06/17] correct pubspec and add yaml extension. --- pubspec | 3 --- pubspec.yaml | 3 +++ 2 files changed, 3 insertions(+), 3 deletions(-) delete mode 100644 pubspec create mode 100644 pubspec.yaml diff --git a/pubspec b/pubspec deleted file mode 100644 index 93c3154..0000000 --- a/pubspec +++ /dev/null @@ -1,3 +0,0 @@ -dependencies: - log4dart: - git: git://github.com/zhygr/log4dart.git \ No newline at end of file diff --git a/pubspec.yaml b/pubspec.yaml new file mode 100644 index 0000000..c32542f --- /dev/null +++ b/pubspec.yaml @@ -0,0 +1,3 @@ +dependencies: + log4dart: + git: git://github.com/zhygr/log4dart.git \ No newline at end of file From 2c1a1be00ce88e4e63c2f3a7e337cc707b55d487 Mon Sep 17 00:00:00 2001 From: Aliaksandr Zhuhrou Date: Mon, 11 Jun 2012 16:24:33 +0300 Subject: [PATCH 07/17] revert back before dart team implement a pub support for windows --- core/CrimsonLib.dart | 2 +- handlers/HandlersLib.dart | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/core/CrimsonLib.dart b/core/CrimsonLib.dart index 27fb64a..9429d81 100644 --- a/core/CrimsonLib.dart +++ b/core/CrimsonLib.dart @@ -6,7 +6,7 @@ #library("crimson:core"); -#import('package:log4dart/Lib.dart'); +#import('../../log4dart/Lib.dart'); #import("dart:io"); #import("dart:uri"); #import("dart:isolate", prefix:"isolate"); diff --git a/handlers/HandlersLib.dart b/handlers/HandlersLib.dart index 6fdfde4..3cf5fdf 100644 --- a/handlers/HandlersLib.dart +++ b/handlers/HandlersLib.dart @@ -1,6 +1,6 @@ #library("crimson:handlers"); #import("../core/CrimsonLib.dart"); -#import("package:log4dart/Lib.dart"); +#import("../../log4dart/Lib.dart"); #import("dart:crypto"); #import("dart:io"); From 2416d3411b29c2db881f74d7221cb375a3550520 Mon Sep 17 00:00:00 2001 From: Aliaksandr Zhuhrou Date: Mon, 11 Jun 2012 23:09:37 +0300 Subject: [PATCH 08/17] add CrimsonData implementation in order to avoid the errors in type checking mode. --- core/crimson.dart | 108 +++++++++++++++++++++++- core/crimsonModule.dart | 99 +--------------------- handlers/endpoints/controllerRoute.dart | 3 +- 3 files changed, 112 insertions(+), 98 deletions(-) diff --git a/core/crimson.dart b/core/crimson.dart index 5558976..f07a5e2 100644 --- a/core/crimson.dart +++ b/core/crimson.dart @@ -153,7 +153,113 @@ interface CrimsonEndpoint extends CrimsonHandler { } -interface CrimsonData extends Map { +interface CrimsonData extends Map + default _CrimsonData { + /** Constructor with default implementation */ + CrimsonData(); + +} + +/** An default implementation */ +class _CrimsonData implements CrimsonData { + /** a delegate that implements a given methods */ + Map _delegate; + + _CrimsonData() { + _delegate = new Map(); + } + + /** + * Returns whether this map contains the given [value]. + */ + bool containsValue(V value) { + return _delegate.containsValue(value); + } + + /** + * Returns whether this map contains the given [key]. + */ + bool containsKey(K key) { + return _delegate.containsKey(key); + } + + /** + * Returns the value for the given [key] or null if [key] is not + * in the map. Because null values are supported, one should either + * use containsKey to distinguish between an absent key and a null + * value, or use the [putIfAbsent] method. + */ + V operator [](K key) { + return _delegate[key]; + } + + /** + * Associates the [key] with the given [value]. + */ + void operator []=(K key, V value) { + _delegate[key] = value; + } + + /** + * If [key] is not associated to a value, calls [ifAbsent] and + * updates the map by mapping [key] to the value returned by + * [ifAbsent]. Returns the value in the map. + */ + V putIfAbsent(K key, V ifAbsent()) { + _delegate.putIfAbsent(key, ifAbsent); + } + + /** + * Removes the association for the given [key]. Returns the value for + * [key] in the map or null if [key] is not in the map. Note that values + * can be null and a returned null value does not always imply that the + * key is absent. + */ + V remove(K key) { + _delegate.remove(key); + } + + /** + * Removes all pairs from the map. + */ + void clear() { + _delegate.clear(); + } + + /** + * Applies [f] to each {key, value} pair of the map. + */ + void forEach(void f(K key, V value)) { + _delegate.forEach(f); + } + + /** + * Returns a collection containing all the keys in the map. + */ + Collection getKeys() { + return _delegate.getKeys(); + } + + /** + * Returns a collection containing all the values in the map. + */ + Collection getValues() { + return _delegate.getValues(); + } + + /** + * The number of {key, value} pairs in the map. + */ + int get length() { + return _delegate.length; + } + + /** + * Returns true if there is no {key, value} pair in the map. + */ + bool isEmpty() { + return _delegate.isEmpty(); + } } diff --git a/core/crimsonModule.dart b/core/crimsonModule.dart index fe4a6b2..9e3542f 100644 --- a/core/crimsonModule.dart +++ b/core/crimsonModule.dart @@ -14,7 +14,7 @@ class CrimsonModule { Future handle(HttpRequest req, HttpResponse res) { logger.debug("New requst passed to module"); - var data = new Map(); + CrimsonData data = new CrimsonData(); Completer completer = new Completer(); @@ -48,7 +48,6 @@ class CrimsonModule { catch (var ex, var stack) { print(ex); print(stack); - //res.outputStream.close(); } }); @@ -56,8 +55,7 @@ class CrimsonModule { else { logger.debug("handler returned null - trying next"); handleNext(); //recurse - } - + } } else { @@ -74,96 +72,5 @@ class CrimsonModule { return completer.future; } - -// CrimsonHandlerList _filters; -// CrimsonHandlerList _endpoints; -// -// ///TODO: Aggregate the filters and endpoints into handlers. -///// Contains the complete list of filters which implement [CrimsonFilter] -// CrimsonHandlerList get filters() => _filters; -// -// /// Contains the complete list of handlers which implement [CrimsonEndpoint] -// CrimsonHandlerList get endpoints() => _endpoints; -// -// /// Process all the [filters] in the list. -// /// If a filter generates an error, then it is logged, but we still contiune. -// _processFilters(HttpRequest req, HttpResponse res, void onAllComplete()) { -// Iterator filterIterator = filters.iterator(); -// CrimsonFilter filter = null; -// -// //closure to allow chaining async handlers -// next([CrimsonHttpException error]) { -// if (error != null) { -// print("in filter error handler: ${error}"); -// logger.error(error.toString()); -// } -// -// if (filterIterator.hasNext()) { -// filter = filterIterator.next(); -// try { -// filter.handle(req, res, (var err) => next(err)); -// } -// catch (Exception ex, var stack){ -// //call next, passing in the exception details so that we can log it. -// next(new CrimsonHttpException(HttpStatus.INTERNAL_SERVER_ERROR, ex, stack)); -// } -// } -// else { -// //call the onAllComplete handler when we've processed all the filters -// onAllComplete(); -// } -// -// } -// -// //start the chain running by calling next(); -// next(); -// } -// -// /// Process all the [endpoints] in the list -// /// If an endpoint generates and error, it is logged, and we fail with a 500 -// _processEndpoints(HttpRequest req, HttpResponse res) { -// Iterator endpointIterator = endpoints.iterator(); -// CrimsonEndpoint endpoint = null; -// -// //recursive closure to allow chaining async handlers -// next([CrimsonHttpException error = null]) { -// -// if (error != null) { -// //if there is an error, then END (no more recursing - note the else... -// logger.error(error.toString()); -// _crimsonErrorHandler(error,req,res); -// } -// else if (endpointIterator.hasNext()) { -// endpoint = endpointIterator.next(); -// //call the handler, passing in this function to allow recursing. -// try { -// endpoint.handle(req, res, -// (var err) => next(err), -// success() => res.outputStream.close()); //second closure represents success -// } -// catch (Exception ex, var stack) { -// next(new CrimsonHttpException(HttpStatus.INTERNAL_SERVER_ERROR, ex, stack)); -// } -// } else { -// //if we've got here, and there are no errors -// //then we've not found a matching endpoint, so return a 404 error. -// _crimsonErrorHandler(new CrimsonHttpException(HttpStatus.NOT_FOUND, "Not found"), req, res); -// } -// -// } -// -// //start the chain running by calling next(); -// next(); -// } -// -// handleRequest(req,res) { -// HttpRequest request = req; -// logger.info("${req.method}: ${req.uri}"); -// -// -// _processFilters(request,res,onAllComplete() { -// _processEndpoints(req,res); -// }); -// } - + } diff --git a/handlers/endpoints/controllerRoute.dart b/handlers/endpoints/controllerRoute.dart index dcca64b..2ca19b0 100644 --- a/handlers/endpoints/controllerRoute.dart +++ b/handlers/endpoints/controllerRoute.dart @@ -1,7 +1,7 @@ class ControllerRoute implements CrimsonEndpoint { var _name; var _path; - var _controller; + AppController _controller; var logger; CrimsonHttpServer server; @@ -45,6 +45,7 @@ class ControllerRoute implements CrimsonEndpoint { } onSuccess(res, completer, data) { + print("we successfully processed a route: ${_controller.route}"); data["SUCCESS"] = true; res.outputStream.close(); completer.complete(data); From d0b9b28d47c8d81ffd2215f6c26189e49f0b8711 Mon Sep 17 00:00:00 2001 From: Aliaksandr Zhuhrou Date: Tue, 12 Jun 2012 10:57:27 +0300 Subject: [PATCH 09/17] add static annotations. --- handlers/endpoints/controllerRoute.dart | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/handlers/endpoints/controllerRoute.dart b/handlers/endpoints/controllerRoute.dart index 2ca19b0..90bd8f3 100644 --- a/handlers/endpoints/controllerRoute.dart +++ b/handlers/endpoints/controllerRoute.dart @@ -1,6 +1,6 @@ class ControllerRoute implements CrimsonEndpoint { - var _name; - var _path; + String _name; + String _path; AppController _controller; var logger; CrimsonHttpServer server; @@ -60,5 +60,7 @@ interface AppController { /** A base method that handles a http requests. */ Future handler(HttpRequest, HttpResponse, CrimsonData); /** Path to the controller a knowledge about given route path. */ - String set route(String path); + void set route(String path); + + String get route(); } \ No newline at end of file From 0fc258c00d7e7dd96cf046f690acaa995cbc4bc8 Mon Sep 17 00:00:00 2001 From: Aliaksandr Zhuhrou Date: Fri, 22 Jun 2012 22:46:08 +0300 Subject: [PATCH 10/17] refactor code that using + operator for string concatenation. --- handlers/endpoints/controllerRoute.dart | 3 --- handlers/endpoints/staticFile.dart | 2 +- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/handlers/endpoints/controllerRoute.dart b/handlers/endpoints/controllerRoute.dart index 90bd8f3..3dfcbd0 100644 --- a/handlers/endpoints/controllerRoute.dart +++ b/handlers/endpoints/controllerRoute.dart @@ -18,10 +18,7 @@ class ControllerRoute implements CrimsonEndpoint { bool isMatched = false; if (req.path.startsWith(this._path)) { isMatched = true; - } else { - //TODO: Add regex matching } - if (isMatched) { logger.debug("Routable handler for request: ${_name}"); Completer completer = new Completer(); diff --git a/handlers/endpoints/staticFile.dart b/handlers/endpoints/staticFile.dart index b9d8e9b..2dbc007 100644 --- a/handlers/endpoints/staticFile.dart +++ b/handlers/endpoints/staticFile.dart @@ -18,7 +18,7 @@ class StaticFile implements CrimsonEndpoint { Future handle(HttpRequest request, HttpResponse response, CrimsonData data) { Completer completer = new Completer(); - String fileToLoad = this.rootPath + request.uri; + String fileToLoad = "${this.rootPath}${request.uri}"; onSuccess(List filedata) { logger.debug("Read file: ${fileToLoad}"); From 85570f6c49951bc78b266bef63e2beb286273629 Mon Sep 17 00:00:00 2001 From: Aliaksandr Zhuhrou Date: Mon, 16 Jul 2012 00:06:26 +0300 Subject: [PATCH 11/17] alter logging statements. --- core/crimsonModule.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/crimsonModule.dart b/core/crimsonModule.dart index 9e3542f..4f7a335 100644 --- a/core/crimsonModule.dart +++ b/core/crimsonModule.dart @@ -23,7 +23,7 @@ class CrimsonModule { if (data["SUCCESS"] != true) { if (handlerIterator.hasNext()) { CrimsonHandler handler = handlerIterator.next(); - print("trying handler: ${handler.NAME}"); + logger.debug("trying handler: ${handler.NAME}"); Future onHandled = handler.handle(req,res,data); //it is valid for a handler to return null, when they are not even From 2df831c030b1774d6128dd5efcd24ff032daadd3 Mon Sep 17 00:00:00 2001 From: Aliaksandr Zhuhrou Date: Mon, 16 Jul 2012 00:52:34 +0300 Subject: [PATCH 12/17] correct async error handling. --- core/crimsonHttpServer.dart | 1 + core/crimsonModule.dart | 2 +- handlers/endpoints/controllerRoute.dart | 5 ++++- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/core/crimsonHttpServer.dart b/core/crimsonHttpServer.dart index 1b1c5ef..9a1b3c4 100644 --- a/core/crimsonHttpServer.dart +++ b/core/crimsonHttpServer.dart @@ -63,6 +63,7 @@ class _CrimsonHttpServer implements CrimsonHttpServer { handled.handleException((error) { res.outputStream.close(); _httpErrorHandler(error); + return true; //this consider a given error to be handled. }); } catch (var ex, var stack) { diff --git a/core/crimsonModule.dart b/core/crimsonModule.dart index 4f7a335..4951975 100644 --- a/core/crimsonModule.dart +++ b/core/crimsonModule.dart @@ -49,7 +49,7 @@ class CrimsonModule { print(ex); print(stack); } - + return true; }); } else { diff --git a/handlers/endpoints/controllerRoute.dart b/handlers/endpoints/controllerRoute.dart index 3dfcbd0..35e4c43 100644 --- a/handlers/endpoints/controllerRoute.dart +++ b/handlers/endpoints/controllerRoute.dart @@ -28,7 +28,10 @@ class ControllerRoute implements CrimsonEndpoint { if (handlerComplete != null) { logger.debug("handling"); handlerComplete.then((completeData) => onSuccess(res, completer, data)); - handlerComplete.handleException((error) => completer.completeException(error)); + handlerComplete.handleException((error) { + completer.completeException(error); + return true; + }); } else { onSuccess(res, completer,data); From 87b137361adbf0d5543d766cef7daae163bc4615 Mon Sep 17 00:00:00 2001 From: Aliaksandr Zhuhrou Date: Wed, 18 Jul 2012 00:13:32 +0300 Subject: [PATCH 13/17] refine async request handling. --- core/crimsonHttpServer.dart | 39 ++++++++----------------- core/crimsonModule.dart | 8 ++--- handlers/endpoints/controllerRoute.dart | 2 -- handlers/endpoints/staticFile.dart | 1 + 4 files changed, 17 insertions(+), 33 deletions(-) diff --git a/core/crimsonHttpServer.dart b/core/crimsonHttpServer.dart index 9a1b3c4..4b80476 100644 --- a/core/crimsonHttpServer.dart +++ b/core/crimsonHttpServer.dart @@ -2,8 +2,7 @@ class _CrimsonHttpServer implements CrimsonHttpServer { /// Contains the [Logger] - Logger logger; - + Logger logger; /// Constructor _CrimsonHttpServer([HttpServer httpServer]) : @@ -26,9 +25,7 @@ class _CrimsonHttpServer implements CrimsonHttpServer { // start the server listening _httpServer.listen(host, port); print("Listening on ${host}:${port}"); - } - - + } /// This is the core of method of [CrimsonHttpServer] /// It first loops through each of the [filters] in turn, calling handle @@ -58,7 +55,12 @@ class _CrimsonHttpServer implements CrimsonHttpServer { try { Future handled = module.handle(req,res); handled.then((result) { - res.outputStream.close(); + try { + res.outputStream.close(); + } + catch (var ex, var stack) { + print("${ex}, ${stack}"); + } }); handled.handleException((error) { res.outputStream.close(); @@ -74,30 +76,13 @@ class _CrimsonHttpServer implements CrimsonHttpServer { logger.debug("404, not found"); res.statusCode = HttpStatus.NOT_FOUND; res.outputStream.close(); - } - - } - + } + } _httpErrorHandler(Exception error) { + print("we invocked http error handler"); this.logger.error(error.toString()); - //CrimsonHttpException ex = new CrimsonHttpException(HttpStatus.INTERNAL_SERVER_ERROR, error); - //_crimsonErrorHandler(ex, null, null); - } - -// _crimsonErrorHandler(CrimsonHttpException error, HttpRequest req, HttpResponse res) { -// res.statusCode = error.status; -// res.setHeader("Content-Type", "text/plain"); -// res.outputStream.writeString("CrimsonHttp: Error" -// "${error}" -// "Method: ${req.method}: ${req.uri}"); -// res.outputStream.close(); -// //TODO: If an error handler filter has been registered, then use that. -// //Might be better to have an errorHandler collection in the same -// //way that we have filters and endpoints. -// } - - + } LinkedHashMap get modules() => _modules; diff --git a/core/crimsonModule.dart b/core/crimsonModule.dart index 4951975..14f1397 100644 --- a/core/crimsonModule.dart +++ b/core/crimsonModule.dart @@ -32,12 +32,13 @@ class CrimsonModule { // for index.html. In this case, just go to the next // handler/ if (onHandled != null) { - onHandled.then((result) { logger.debug("handler handled."); if (result["SUCCESS"] != true) { logger.debug("handler handled = false."); handleNext(); //recurse + } else { + completer.complete("handled"); } }); onHandled.handleException((error) { @@ -55,8 +56,7 @@ class CrimsonModule { else { logger.debug("handler returned null - trying next"); handleNext(); //recurse - } - + } } else { completer.complete("all handled"); @@ -64,7 +64,7 @@ class CrimsonModule { } else { logger.info("Success=true"); - res.outputStream.close(); + completer.complete("handled"); } } diff --git a/handlers/endpoints/controllerRoute.dart b/handlers/endpoints/controllerRoute.dart index 35e4c43..508cfb2 100644 --- a/handlers/endpoints/controllerRoute.dart +++ b/handlers/endpoints/controllerRoute.dart @@ -22,7 +22,6 @@ class ControllerRoute implements CrimsonEndpoint { if (isMatched) { logger.debug("Routable handler for request: ${_name}"); Completer completer = new Completer(); - Future handlerComplete = _controller.handler(req,res,data); if (handlerComplete != null) { @@ -47,7 +46,6 @@ class ControllerRoute implements CrimsonEndpoint { onSuccess(res, completer, data) { print("we successfully processed a route: ${_controller.route}"); data["SUCCESS"] = true; - res.outputStream.close(); completer.complete(data); } diff --git a/handlers/endpoints/staticFile.dart b/handlers/endpoints/staticFile.dart index 2dbc007..9206f8a 100644 --- a/handlers/endpoints/staticFile.dart +++ b/handlers/endpoints/staticFile.dart @@ -67,6 +67,7 @@ class StaticFile implements CrimsonEndpoint { existFuture.handleException((Exception error) { logger.debug("${path} doesn't exist: ${error}"); fail(error); + return true; }); logger.debug("trying to open file: ${path}"); From 9cbd451b6b4c5efbb279771750caafa7e399bec6 Mon Sep 17 00:00:00 2001 From: Aliaksandr Zhuhrou Date: Thu, 16 Aug 2012 16:08:12 +0300 Subject: [PATCH 14/17] start to implement new design. preparing mock variant of home page --- handlers/endpoints/controllerRoute.dart | 1 + 1 file changed, 1 insertion(+) diff --git a/handlers/endpoints/controllerRoute.dart b/handlers/endpoints/controllerRoute.dart index 508cfb2..7882052 100644 --- a/handlers/endpoints/controllerRoute.dart +++ b/handlers/endpoints/controllerRoute.dart @@ -28,6 +28,7 @@ class ControllerRoute implements CrimsonEndpoint { logger.debug("handling"); handlerComplete.then((completeData) => onSuccess(res, completer, data)); handlerComplete.handleException((error) { + print("have error in controller: ${error}"); completer.completeException(error); return true; }); From 35cfca90da05a756083721014921a0f9245fee18 Mon Sep 17 00:00:00 2001 From: Aliaksandr Zhuhrou Date: Mon, 27 Aug 2012 14:07:23 +0300 Subject: [PATCH 15/17] - migrate to new getter syntax. --- core/crimson.dart | 4 +--- core/crimsonHttpServer.dart | 2 +- core/crimsonModule.dart | 2 +- core/crimsonPrivate.dart | 2 +- core/crimson_impl.dart | 2 +- handlers/endpoints/controllerRoute.dart | 2 +- handlers/endpoints/route.dart | 2 +- 7 files changed, 7 insertions(+), 9 deletions(-) diff --git a/core/crimson.dart b/core/crimson.dart index f07a5e2..bbc33c0 100644 --- a/core/crimson.dart +++ b/core/crimson.dart @@ -250,9 +250,7 @@ class _CrimsonData implements CrimsonData { /** * The number of {key, value} pairs in the map. */ - int get length() { - return _delegate.length; - } + int get length => _delegate.length; /** * Returns true if there is no {key, value} pair in the map. diff --git a/core/crimsonHttpServer.dart b/core/crimsonHttpServer.dart index 4b80476..7a8edc8 100644 --- a/core/crimsonHttpServer.dart +++ b/core/crimsonHttpServer.dart @@ -84,7 +84,7 @@ class _CrimsonHttpServer implements CrimsonHttpServer { this.logger.error(error.toString()); } - LinkedHashMap get modules() => _modules; + LinkedHashMap get modules => _modules; final HttpServer _httpServer; final LinkedHashMap _modules; diff --git a/core/crimsonModule.dart b/core/crimsonModule.dart index 14f1397..a33d2ba 100644 --- a/core/crimsonModule.dart +++ b/core/crimsonModule.dart @@ -4,7 +4,7 @@ class CrimsonModule { var logger; CrimsonHandlerList _handlers; - CrimsonHandlerList get handlers() => _handlers; + CrimsonHandlerList get handlers => _handlers; CrimsonModule(this._server) { logger = LoggerFactory.getLogger("CrimsonModule"); diff --git a/core/crimsonPrivate.dart b/core/crimsonPrivate.dart index 8c13842..5c05ee0 100644 --- a/core/crimsonPrivate.dart +++ b/core/crimsonPrivate.dart @@ -47,7 +47,7 @@ class _CrimsonHandlerList implements CrimsonHandlerLis Iterator iterator() => _internalMap.getValues().iterator(); // CrimsonHandler operator[](int i) => _internalList[i]; // void operator []=(int index,value) => _internalList[index] = value; -// int get length() => _internalList.length; +// int get length => _internalList.length; // void addLast(CrimsonHandler value) => _internalList.addLast(value); // void sort(int compare(CrimsonHandler, CrimsonHandler)) => _internalList.sort(compare); // int indexOf(E element, [int start]) => _internalList.indexOf(element, start); diff --git a/core/crimson_impl.dart b/core/crimson_impl.dart index da1cbe0..cc705c2 100644 --- a/core/crimson_impl.dart +++ b/core/crimson_impl.dart @@ -45,7 +45,7 @@ class SessionImpl implements Session { Collection getValues() => _internalList.getValues(); - int get length() => _internalList.length; + int get length => _internalList.length; bool isEmpty() => _internalList.isEmpty(); } \ No newline at end of file diff --git a/handlers/endpoints/controllerRoute.dart b/handlers/endpoints/controllerRoute.dart index 7882052..0f2ed11 100644 --- a/handlers/endpoints/controllerRoute.dart +++ b/handlers/endpoints/controllerRoute.dart @@ -50,7 +50,7 @@ class ControllerRoute implements CrimsonEndpoint { completer.complete(data); } - String get NAME() => _name; + String get NAME => _name; } diff --git a/handlers/endpoints/route.dart b/handlers/endpoints/route.dart index 29b7212..2757fc2 100644 --- a/handlers/endpoints/route.dart +++ b/handlers/endpoints/route.dart @@ -71,6 +71,6 @@ class Route implements CrimsonEndpoint { completer.complete(data); } - String get NAME() => _name; + String get NAME => _name; } From ac3dd40cbf8d43f42bcb9785a8e1bdfba30e4542 Mon Sep 17 00:00:00 2001 From: Aliaksandr Zhuhrou Date: Wed, 10 Oct 2012 20:38:58 +0300 Subject: [PATCH 16/17] - migrate to a new try-catch syntax - migrate to a new logger library --- core/CrimsonLib.dart | 2 +- core/crimsonHttpServer.dart | 5 +++-- core/crimsonModule.dart | 2 +- handlers/HandlersLib.dart | 2 +- 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/core/CrimsonLib.dart b/core/CrimsonLib.dart index 9429d81..da83789 100644 --- a/core/CrimsonLib.dart +++ b/core/CrimsonLib.dart @@ -6,7 +6,7 @@ #library("crimson:core"); -#import('../../log4dart/Lib.dart'); +#import('../../log4dart/lib/log4dart.dart'); #import("dart:io"); #import("dart:uri"); #import("dart:isolate", prefix:"isolate"); diff --git a/core/crimsonHttpServer.dart b/core/crimsonHttpServer.dart index 7a8edc8..31b63f5 100644 --- a/core/crimsonHttpServer.dart +++ b/core/crimsonHttpServer.dart @@ -51,6 +51,7 @@ class _CrimsonHttpServer implements CrimsonHttpServer { module = modules["*"]; } + res.persistentConnection = true; //we need this line for nginx proxy. if (module != null) { try { Future handled = module.handle(req,res); @@ -58,7 +59,7 @@ class _CrimsonHttpServer implements CrimsonHttpServer { try { res.outputStream.close(); } - catch (var ex, var stack) { + catch (ex, stack) { print("${ex}, ${stack}"); } }); @@ -68,7 +69,7 @@ class _CrimsonHttpServer implements CrimsonHttpServer { return true; //this consider a given error to be handled. }); } - catch (var ex, var stack) { + catch (ex, stack) { logger.error("${ex}, ${stack}"); } } diff --git a/core/crimsonModule.dart b/core/crimsonModule.dart index a33d2ba..f0a9802 100644 --- a/core/crimsonModule.dart +++ b/core/crimsonModule.dart @@ -46,7 +46,7 @@ class CrimsonModule { try { completer.completeException(error); } - catch (var ex, var stack) { + catch (ex, stack) { print(ex); print(stack); } diff --git a/handlers/HandlersLib.dart b/handlers/HandlersLib.dart index 3cf5fdf..1739e44 100644 --- a/handlers/HandlersLib.dart +++ b/handlers/HandlersLib.dart @@ -1,6 +1,6 @@ #library("crimson:handlers"); #import("../core/CrimsonLib.dart"); -#import("../../log4dart/Lib.dart"); +#import("../../log4dart/lib/log4dart.dart"); #import("dart:crypto"); #import("dart:io"); From f96326cc496b9914a75be5ef742d0380d28b41f3 Mon Sep 17 00:00:00 2001 From: Aliaksandr Zhuhrou Date: Wed, 10 Oct 2012 22:24:13 +0300 Subject: [PATCH 17/17] migrate to Dart SDK version 13387 --- core/CrimsonLib.dart | 24 +++++++++++------------- handlers/HandlersLib.dart | 20 ++++++++++---------- test/crimsonTest.dart | 8 ++++---- 3 files changed, 25 insertions(+), 27 deletions(-) diff --git a/core/CrimsonLib.dart b/core/CrimsonLib.dart index da83789..bdd7f15 100644 --- a/core/CrimsonLib.dart +++ b/core/CrimsonLib.dart @@ -2,18 +2,16 @@ // Use of this source code is governed by a MIT style licence // can be found in the LICENSE file. // website: +library crimson_core; +import '../../log4dart/lib/log4dart.dart'; +import 'dart:io'; +import 'dart:uri'; +import 'dart:isolate' as isolate; -#library("crimson:core"); - -#import('../../log4dart/lib/log4dart.dart'); -#import("dart:io"); -#import("dart:uri"); -#import("dart:isolate", prefix:"isolate"); - -#source('crimson.dart'); -#source('crimson_impl.dart'); -#source('crimson_utils.dart'); -#source('crimsonPrivate.dart'); -#source('crimsonHttpServer.dart'); -#source('crimsonModule.dart'); \ No newline at end of file +part 'crimson.dart'; +part 'crimson_impl.dart'; +part 'crimson_utils.dart'; +part 'crimsonPrivate.dart'; +part 'crimsonHttpServer.dart'; +part 'crimsonModule.dart'; \ No newline at end of file diff --git a/handlers/HandlersLib.dart b/handlers/HandlersLib.dart index 1739e44..9acc21c 100644 --- a/handlers/HandlersLib.dart +++ b/handlers/HandlersLib.dart @@ -1,11 +1,11 @@ -#library("crimson:handlers"); -#import("../core/CrimsonLib.dart"); -#import("../../log4dart/lib/log4dart.dart"); -#import("dart:crypto"); -#import("dart:io"); +library crimson_handlers; +import '../core/CrimsonLib.dart'; +import '../../log4dart/lib/log4dart.dart'; +import 'dart:crypto'; +import 'dart:io'; -#source("endpoints/favicon.dart"); -#source("endpoints/staticFile.dart"); -#source("filters/cookieSession.dart"); -#source("endpoints/route.dart"); -#source("endpoints/controllerRoute.dart"); \ No newline at end of file +part 'endpoints/favicon.dart'; +part 'endpoints/staticFile.dart'; +part 'filters/cookieSession.dart'; +part 'endpoints/route.dart'; +part 'endpoints/controllerRoute.dart'; \ No newline at end of file diff --git a/test/crimsonTest.dart b/test/crimsonTest.dart index 27f5891..57d0b25 100644 --- a/test/crimsonTest.dart +++ b/test/crimsonTest.dart @@ -1,8 +1,8 @@ -#library('crimsonTest'); +library crimsonTest; -#import("../core/CrimsonLib.dart"); -#import("../handlers/HandlersLib.dart"); -#import("dart:io"); +import '../core/CrimsonLib.dart'; +import '../handlers/HandlersLib.dart'; +import 'dart:io'; ///Simple test server main() {