RequestNavigator class
RequestNavigator wires together Route matching, Filtering of HttpRequests
and Handler calling. It manages url addresses and bind them handlers.
class RequestNavigator {
final Stream<HttpRequest> _incoming;
final Router _router;
/**
* Maps route names to [Stream]
*/
final Map<String, StreamController<RequestHandlerParameters>> _streams = {};
/**
* If no route is matched.
*/
StreamController<RequestHandlerParameters> _defaultStreamController = null;
/**
* Creates new RequestNavigator listening on [_incoming] and routing via [_router].
*/
RequestNavigator(this._incoming, this._router){
this._incoming.listen(processHttpRequest);
}
StreamController _createStreamControllerWithHandler(dynamic handler){
var streamController = new StreamController<RequestHandlerParameters>();
streamController.stream.listen((p) => handler(p.req, p.url_params));
return streamController;
}
/**
* Registers [Handler] for a [Route] and adds listener to the stream which
* is also returned.
*/
void registerHandler(String routeName, dynamic handler){
if(_streams.containsKey(routeName)){
throw new ArgumentError("Cannot register handler as route name '$routeName' already in use in RequestNavigator.");
}
_streams[routeName] = _createStreamControllerWithHandler(handler);
}
/**
* When [Router] matches nothing then [handler] will be called (through the returned [Stream]).
*/
void registerDefaultHandler(dynamic handler){
if(_defaultStreamController != null){
throw new ArgumentError("Default route already set.");
_defaultStreamController.close();
}
_defaultStreamController = _createStreamControllerWithHandler(handler);
}
/**
* [HttpRequest.uri.path] is matched agains [Router],
* whole [HttpRequest] is filtered through [Filter] and if passes all then
* [HttpRequest] is inserted to the correspondig [Stream].
*/
void processHttpRequest(HttpRequest req){
var matchInfo = _router.match(req.uri.path);
if(matchInfo != null){
if(_streams.containsKey(matchInfo[0])){
_streams[matchInfo[0]].add(new RequestHandlerParameters(req, matchInfo[1]));
}
else{
throw new ArgumentError("Stream not found for '${matchInfo[0]}'");
}
}
else{
_defaultStreamController.add(new RequestHandlerParameters(req, {}));
}
}
}
Constructors
new RequestNavigator(Stream<HttpRequest> _incoming, Router _router) #
Creates new RequestNavigator listening on _incoming and routing via _router.
RequestNavigator(this._incoming, this._router){
this._incoming.listen(processHttpRequest);
}
Methods
void processHttpRequest(HttpRequest req) #
HttpRequest.uri.path is matched agains Router,
whole HttpRequest is filtered through Filter and if passes all then
HttpRequest is inserted to the correspondig Stream.
void processHttpRequest(HttpRequest req){
var matchInfo = _router.match(req.uri.path);
if(matchInfo != null){
if(_streams.containsKey(matchInfo[0])){
_streams[matchInfo[0]].add(new RequestHandlerParameters(req, matchInfo[1]));
}
else{
throw new ArgumentError("Stream not found for '${matchInfo[0]}'");
}
}
else{
_defaultStreamController.add(new RequestHandlerParameters(req, {}));
}
}
void registerDefaultHandler(handler) #
When Router matches nothing then handler will be called (through the returned Stream).
void registerDefaultHandler(dynamic handler){
if(_defaultStreamController != null){
throw new ArgumentError("Default route already set.");
_defaultStreamController.close();
}
_defaultStreamController = _createStreamControllerWithHandler(handler);
}
void registerHandler(String routeName, handler) #
Registers Handler for a Route and adds listener to the stream which
is also returned.
void registerHandler(String routeName, dynamic handler){
if(_streams.containsKey(routeName)){
throw new ArgumentError("Cannot register handler as route name '$routeName' already in use in RequestNavigator.");
}
_streams[routeName] = _createStreamControllerWithHandler(handler);
}