Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1434)

Unified Diff: runtime/bin/http_impl.dart

Issue 10119005: Add HTTP handler registration to the HTTP server (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Revert test edit Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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;
Anders Johnsen 2012/04/18 12:19:09 The type could be either a Function or a RequestHa
Søren Gjesse 2012/04/18 13:26:56 Good point. Used RequestHandler and added the clas
+}
+
+
// 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)) {
Anders Johnsen 2012/04/18 12:19:09 What if we have multiple matches? Ignore? Should b
Søren Gjesse 2012/04/18 13:26:56 The documentation in http.dart says that the first
+ 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);
+ }
+ }
+
Mads Ager (google) 2012/04/18 12:22:08 Remove blank line?
Søren Gjesse 2012/04/18 13:26:56 Done.
+ 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;
}

Powered by Google App Engine
This is Rietveld 408576698