Index: lib/server.dart |
diff --git a/lib/server.dart b/lib/server.dart |
index 6f30b77272a5cfe5befb5d9439cb5de467feac6a..7b682cc4290ca98b2818fb0b6d3abe63daa7cb6d 100644 |
--- a/lib/server.dart |
+++ b/lib/server.dart |
@@ -32,6 +32,10 @@ typedef Future<bool> Filter(HttpRequest request); |
* subsequent filters and request handlers are not run. This way a filter can |
* prevent further processing, like needed for authentication. |
* |
+ * Requests not matched by a call to [serve] are sent to the [defaultStream]. |
+ * If there's no subscriber to the defaultStream then a 404 is sent to the |
+ * response. |
+ * |
* Example: |
* import 'package:route/server.dart'; |
* import 'package:route/pattern.dart'; |
@@ -41,19 +45,30 @@ typedef Future<bool> Filter(HttpRequest request); |
* router.filter(matchesAny(['/foo', '/bar']), authFilter); |
* router.serve('/foo').listen(fooHandler); |
* router.serve('/bar').listen(barHandler); |
+ * router.defaultStream.listen(send404); |
* }); |
*/ |
class Router { |
- final Stream<HttpRequest> incoming; |
- final Map<Pattern, StreamController> _controllers = new LinkedHashMap(); |
- final Map<Pattern, Filter> _filters = new LinkedHashMap(); |
- |
- Router(this.incoming) { |
- incoming.listen(_handleRequest); |
+ final Stream<HttpRequest> _incoming; |
+ |
+ final Map<Pattern, StreamController> _controllers = |
+ new LinkedHashMap<Pattern, StreamController>(); |
+ |
+ final Map<Pattern, Filter> _filters = new LinkedHashMap<Pattern, Filter>(); |
+ |
+ final StreamController<HttpRequest> _defaultController = |
+ new StreamController<HttpRequest>(); |
+ |
+ /** |
+ * Create a new Router that listens to the [incoming] stream, usually an |
+ * instance of [HttpServer]. |
+ */ |
+ Router(Stream<HttpRequest> incoming) : _incoming = incoming { |
+ _incoming.listen(_handleRequest); |
} |
/** |
- * Request whose URI matches [url] are sent the the stream created by this\ |
+ * Request whose URI matches [url] are sent the the stream created by this |
butlermatt
2013/03/07 13:21:52
double 'the'
|
* method, and not sent to any other serve streams. |
*/ |
Stream<HttpRequest> serve(Pattern url) { |
@@ -74,6 +89,8 @@ class Router { |
_filters[url] = filter; |
} |
+ Stream<HttpRequest> get defaultStream => _defaultController.stream; |
+ |
void _handleRequest(HttpRequest req) { |
bool cont = true; |
doWhile(_filters.keys, (Pattern pattern) { |
@@ -95,7 +112,11 @@ class Router { |
} |
} |
if (!handled) { |
- send404(req); |
+ if (_defaultController.hasSubscribers) { |
+ _defaultController.add(req); |
+ } else { |
+ send404(req); |
+ } |
} |
} |
}); |