| Index: runtime/bin/http_impl.dart
|
| diff --git a/runtime/bin/http_impl.dart b/runtime/bin/http_impl.dart
|
| index 69d346df6502f5a3608600b50b5d27a0b319f2a7..b1155fc669fc99ca76cffd8ac877d0094ea6ec6c 100644
|
| --- a/runtime/bin/http_impl.dart
|
| +++ b/runtime/bin/http_impl.dart
|
| @@ -793,10 +793,18 @@ class _HttpConnection extends _HttpConnectionBase {
|
| }
|
|
|
|
|
| +class _RequestHandlerRegistration {
|
| + _RequestHandlerRegistration(RegExp this._pattern, Object this._handler);
|
| + RegExp _pattern;
|
| + Object _handler;
|
| +}
|
| +
|
| +
|
| // HTTP server waiting for socket connections. The connections are
|
| // managed by the server and as requests are received the request.
|
| class _HttpServer implements HttpServer {
|
| - _HttpServer() : _connections = new Set<_HttpConnection>();
|
| + _HttpServer() : _connections = new Set<_HttpConnection>(),
|
| + _handlers = new List<_RequestHandlerRegistration>();
|
|
|
| void listen(String host, int port, [int backlog = 5]) {
|
| listenOn(new ServerSocket(host, port, backlog));
|
| @@ -808,7 +816,7 @@ class _HttpServer implements HttpServer {
|
| // Accept the client connection.
|
| _HttpConnection connection = new _HttpConnection(this);
|
| connection._connectionEstablished(socket);
|
| - connection.onRequestReceived = _onRequest;
|
| + connection.onRequestReceived = _handleRequest;
|
| connection.onClosed = () => _connections.remove(connection);
|
| connection.onError = (e) {
|
| _connections.remove(connection);
|
| @@ -822,6 +830,14 @@ class _HttpServer implements HttpServer {
|
| _closeServer = false;
|
| }
|
|
|
| + addRequestHandler(RegExp pattern, Object handler) {
|
| + _handlers.add(new _RequestHandlerRegistration(pattern, handler));
|
| + }
|
| +
|
| + void set defaultRequestHandler(Object handler) {
|
| + _defaultHandler = handler;
|
| + }
|
| +
|
| void close() {
|
| if (_server !== null && _closeServer) {
|
| _server.close();
|
| @@ -844,14 +860,44 @@ class _HttpServer implements HttpServer {
|
| _onError = callback;
|
| }
|
|
|
| - void set onRequest(void callback(HttpRequest, HttpResponse)) {
|
| - _onRequest = callback;
|
| + void _handleRequest(HttpRequest request, HttpResponse response) {
|
| + for (int i = 0; i < _handlers.length; i++) {
|
| + if (_handlers[i]._pattern.hasMatch(request.path)) {
|
| + var handler = _handlers[i]._handler;
|
| + try {
|
| + if (handler is Function) {
|
| + handler(request, response);
|
| + } else {
|
| + handler.onRequest(request, response);
|
| + }
|
| + } catch (var e) {
|
| + if (_onError != null) {
|
| + _onError(e);
|
| + }
|
| + }
|
| +
|
| + return;
|
| + }
|
| + }
|
| +
|
| + if (_defaultHandler != null) {
|
| + if (_defaultHandler is Function) {
|
| + _defaultHandler(request, response);
|
| + } else {
|
| + _defaultHandler.onRequest(request, response);
|
| + }
|
| + } else {
|
| + response.statusCode = HttpStatus.NOT_FOUND;
|
| + response.outputStream.close();
|
| + }
|
| }
|
|
|
| +
|
| ServerSocket _server; // The server listen socket.
|
| bool _closeServer = false;
|
| Set<_HttpConnection> _connections; // Set of currently connected clients.
|
| - Function _onRequest;
|
| + List<_RequestHandlerRegistration> _handlers;
|
| + Object _defaultHandler;
|
| Function _onError;
|
| }
|
|
|
|
|