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

Side by Side 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: Addressed review comments 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « runtime/bin/http.dart ('k') | samples/chat/chat_server_lib.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 class _HttpHeaders implements HttpHeaders { 5 class _HttpHeaders implements HttpHeaders {
6 _HttpHeaders() : _headers = new Map<String, List<String>>(); 6 _HttpHeaders() : _headers = new Map<String, List<String>>();
7 7
8 List<String> operator[](String name) { 8 List<String> operator[](String name) {
9 name = name.toLowerCase(); 9 name = name.toLowerCase();
10 return _headers[name]; 10 return _headers[name];
(...skipping 775 matching lines...) Expand 10 before | Expand all | Expand 10 after
786 HttpRequest _request; 786 HttpRequest _request;
787 HttpResponse _response; 787 HttpResponse _response;
788 788
789 // Callbacks. 789 // Callbacks.
790 Function onRequestReceived; 790 Function onRequestReceived;
791 Function onClosed; 791 Function onClosed;
792 Function onError; 792 Function onError;
793 } 793 }
794 794
795 795
796 class _RequestHandlerRegistration {
797 _RequestHandlerRegistration(Function this._matcher, Object this._handler);
798 Function _matcher;
799 RequestHandler _handler;
800 }
801
802
803 class _RequestHandlerImpl implements RequestHandler {
804 _RequestHandlerImpl(Function this._handler);
805 void onRequest(HttpRequest request, HttpResponse response) =>
806 _handler(request, response);
807 Function _handler;
808 }
809
810
796 // HTTP server waiting for socket connections. The connections are 811 // HTTP server waiting for socket connections. The connections are
797 // managed by the server and as requests are received the request. 812 // managed by the server and as requests are received the request.
798 class _HttpServer implements HttpServer { 813 class _HttpServer implements HttpServer {
799 _HttpServer() : _connections = new Set<_HttpConnection>(); 814 _HttpServer() : _connections = new Set<_HttpConnection>(),
815 _handlers = new List<_RequestHandlerRegistration>();
800 816
801 void listen(String host, int port, [int backlog = 5]) { 817 void listen(String host, int port, [int backlog = 5]) {
802 listenOn(new ServerSocket(host, port, backlog)); 818 listenOn(new ServerSocket(host, port, backlog));
803 _closeServer = true; 819 _closeServer = true;
804 } 820 }
805 821
806 void listenOn(ServerSocket serverSocket) { 822 void listenOn(ServerSocket serverSocket) {
807 void onConnection(Socket socket) { 823 void onConnection(Socket socket) {
808 // Accept the client connection. 824 // Accept the client connection.
809 _HttpConnection connection = new _HttpConnection(this); 825 _HttpConnection connection = new _HttpConnection(this);
810 connection._connectionEstablished(socket); 826 connection._connectionEstablished(socket);
811 connection.onRequestReceived = _onRequest; 827 connection.onRequestReceived = _handleRequest;
812 connection.onClosed = () => _connections.remove(connection); 828 connection.onClosed = () => _connections.remove(connection);
813 connection.onError = (e) { 829 connection.onError = (e) {
814 _connections.remove(connection); 830 _connections.remove(connection);
815 if (_onError != null) _onError(e); 831 if (_onError != null) _onError(e);
816 }; 832 };
817 connection._connectionEstablished(socket); 833 connection._connectionEstablished(socket);
818 _connections.add(connection); 834 _connections.add(connection);
819 } 835 }
820 serverSocket.onConnection = onConnection; 836 serverSocket.onConnection = onConnection;
821 _server = serverSocket; 837 _server = serverSocket;
822 _closeServer = false; 838 _closeServer = false;
823 } 839 }
824 840
841 addRequestHandler(bool matcher(String path), Object handler) {
842 if (handler is Function) {
843 handler = new _RequestHandlerImpl(handler);
844 }
845 _handlers.add(new _RequestHandlerRegistration(matcher, handler));
846 }
847
848 void set defaultRequestHandler(Object handler) {
849 _defaultHandler = handler;
850 }
851
825 void close() { 852 void close() {
826 if (_server !== null && _closeServer) { 853 if (_server !== null && _closeServer) {
827 _server.close(); 854 _server.close();
828 } 855 }
829 _server = null; 856 _server = null;
830 for (_HttpConnection connection in _connections) { 857 for (_HttpConnection connection in _connections) {
831 connection._close(); 858 connection._close();
832 } 859 }
833 _connections.clear(); 860 _connections.clear();
834 } 861 }
835 862
836 int get port() { 863 int get port() {
837 if (_server === null) { 864 if (_server === null) {
838 throw new HttpException("The HttpServer is not listening on a port."); 865 throw new HttpException("The HttpServer is not listening on a port.");
839 } 866 }
840 return _server.port; 867 return _server.port;
841 } 868 }
842 869
843 void set onError(void callback(Exception e)) { 870 void set onError(void callback(Exception e)) {
844 _onError = callback; 871 _onError = callback;
845 } 872 }
846 873
847 void set onRequest(void callback(HttpRequest, HttpResponse)) { 874 void _handleRequest(HttpRequest request, HttpResponse response) {
848 _onRequest = callback; 875 for (int i = 0; i < _handlers.length; i++) {
876 if (_handlers[i]._matcher(request)) {
877 var handler = _handlers[i]._handler;
878 try {
879 handler.onRequest(request, response);
880 } catch (var e) {
881 if (_onError != null) {
882 _onError(e);
883 }
884 }
885 return;
886 }
887 }
888
889 if (_defaultHandler != null) {
890 if (_defaultHandler is Function) {
891 _defaultHandler(request, response);
892 } else {
893 _defaultHandler.onRequest(request, response);
894 }
895 } else {
896 response.statusCode = HttpStatus.NOT_FOUND;
897 response.outputStream.close();
898 }
849 } 899 }
850 900
901
851 ServerSocket _server; // The server listen socket. 902 ServerSocket _server; // The server listen socket.
852 bool _closeServer = false; 903 bool _closeServer = false;
853 Set<_HttpConnection> _connections; // Set of currently connected clients. 904 Set<_HttpConnection> _connections; // Set of currently connected clients.
854 Function _onRequest; 905 List<_RequestHandlerRegistration> _handlers;
906 Object _defaultHandler;
855 Function _onError; 907 Function _onError;
856 } 908 }
857 909
858 910
859 class _HttpClientRequest 911 class _HttpClientRequest
860 extends _HttpRequestResponseBase implements HttpClientRequest { 912 extends _HttpRequestResponseBase implements HttpClientRequest {
861 static final int START = 0; 913 static final int START = 0;
862 static final int HEADERS_SENT = 1; 914 static final int HEADERS_SENT = 1;
863 static final int DONE = 2; 915 static final int DONE = 2;
864 916
(...skipping 473 matching lines...) Expand 10 before | Expand all | Expand 10 after
1338 sockets.addFirst(socketConn); 1390 sockets.addFirst(socketConn);
1339 socketConn._markReturned(); 1391 socketConn._markReturned();
1340 } 1392 }
1341 1393
1342 Function _onOpen; 1394 Function _onOpen;
1343 Map<String, Queue<_SocketConnection>> _openSockets; 1395 Map<String, Queue<_SocketConnection>> _openSockets;
1344 Set<_SocketConnection> _activeSockets; 1396 Set<_SocketConnection> _activeSockets;
1345 Timer _evictionTimer; 1397 Timer _evictionTimer;
1346 bool _shutdown; // Has this HTTP client been shutdown? 1398 bool _shutdown; // Has this HTTP client been shutdown?
1347 } 1399 }
OLDNEW
« no previous file with comments | « runtime/bin/http.dart ('k') | samples/chat/chat_server_lib.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698