| OLD | NEW |
| 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 Loading... |
| 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(RegExp this._pattern, Object this._handler); |
| 798 RegExp _pattern; |
| 799 Object _handler; |
| 800 } |
| 801 |
| 802 |
| 796 // HTTP server waiting for socket connections. The connections are | 803 // HTTP server waiting for socket connections. The connections are |
| 797 // managed by the server and as requests are received the request. | 804 // managed by the server and as requests are received the request. |
| 798 class _HttpServer implements HttpServer { | 805 class _HttpServer implements HttpServer { |
| 799 _HttpServer() : _connections = new Set<_HttpConnection>(); | 806 _HttpServer() : _connections = new Set<_HttpConnection>(), |
| 807 _handlers = new List<_RequestHandlerRegistration>(); |
| 800 | 808 |
| 801 void listen(String host, int port, [int backlog = 5]) { | 809 void listen(String host, int port, [int backlog = 5]) { |
| 802 listenOn(new ServerSocket(host, port, backlog)); | 810 listenOn(new ServerSocket(host, port, backlog)); |
| 803 _closeServer = true; | 811 _closeServer = true; |
| 804 } | 812 } |
| 805 | 813 |
| 806 void listenOn(ServerSocket serverSocket) { | 814 void listenOn(ServerSocket serverSocket) { |
| 807 void onConnection(Socket socket) { | 815 void onConnection(Socket socket) { |
| 808 // Accept the client connection. | 816 // Accept the client connection. |
| 809 _HttpConnection connection = new _HttpConnection(this); | 817 _HttpConnection connection = new _HttpConnection(this); |
| 810 connection._connectionEstablished(socket); | 818 connection._connectionEstablished(socket); |
| 811 connection.onRequestReceived = _onRequest; | 819 connection.onRequestReceived = _handleRequest; |
| 812 connection.onClosed = () => _connections.remove(connection); | 820 connection.onClosed = () => _connections.remove(connection); |
| 813 connection.onError = (e) { | 821 connection.onError = (e) { |
| 814 _connections.remove(connection); | 822 _connections.remove(connection); |
| 815 if (_onError != null) _onError(e); | 823 if (_onError != null) _onError(e); |
| 816 }; | 824 }; |
| 817 connection._connectionEstablished(socket); | 825 connection._connectionEstablished(socket); |
| 818 _connections.add(connection); | 826 _connections.add(connection); |
| 819 } | 827 } |
| 820 serverSocket.onConnection = onConnection; | 828 serverSocket.onConnection = onConnection; |
| 821 _server = serverSocket; | 829 _server = serverSocket; |
| 822 _closeServer = false; | 830 _closeServer = false; |
| 823 } | 831 } |
| 824 | 832 |
| 833 addRequestHandler(RegExp pattern, Object handler) { |
| 834 _handlers.add(new _RequestHandlerRegistration(pattern, handler)); |
| 835 } |
| 836 |
| 837 void set defaultRequestHandler(Object handler) { |
| 838 _defaultHandler = handler; |
| 839 } |
| 840 |
| 825 void close() { | 841 void close() { |
| 826 if (_server !== null && _closeServer) { | 842 if (_server !== null && _closeServer) { |
| 827 _server.close(); | 843 _server.close(); |
| 828 } | 844 } |
| 829 _server = null; | 845 _server = null; |
| 830 for (_HttpConnection connection in _connections) { | 846 for (_HttpConnection connection in _connections) { |
| 831 connection._close(); | 847 connection._close(); |
| 832 } | 848 } |
| 833 _connections.clear(); | 849 _connections.clear(); |
| 834 } | 850 } |
| 835 | 851 |
| 836 int get port() { | 852 int get port() { |
| 837 if (_server === null) { | 853 if (_server === null) { |
| 838 throw new HttpException("The HttpServer is not listening on a port."); | 854 throw new HttpException("The HttpServer is not listening on a port."); |
| 839 } | 855 } |
| 840 return _server.port; | 856 return _server.port; |
| 841 } | 857 } |
| 842 | 858 |
| 843 void set onError(void callback(Exception e)) { | 859 void set onError(void callback(Exception e)) { |
| 844 _onError = callback; | 860 _onError = callback; |
| 845 } | 861 } |
| 846 | 862 |
| 847 void set onRequest(void callback(HttpRequest, HttpResponse)) { | 863 void _handleRequest(HttpRequest request, HttpResponse response) { |
| 848 _onRequest = callback; | 864 for (int i = 0; i < _handlers.length; i++) { |
| 865 if (_handlers[i]._pattern.hasMatch(request.path)) { |
| 866 var handler = _handlers[i]._handler; |
| 867 try { |
| 868 if (handler is Function) { |
| 869 handler(request, response); |
| 870 } else { |
| 871 handler.onRequest(request, response); |
| 872 } |
| 873 } catch (var e) { |
| 874 if (_onError != null) { |
| 875 _onError(e); |
| 876 } |
| 877 } |
| 878 |
| 879 return; |
| 880 } |
| 881 } |
| 882 |
| 883 if (_defaultHandler != null) { |
| 884 if (_defaultHandler is Function) { |
| 885 _defaultHandler(request, response); |
| 886 } else { |
| 887 _defaultHandler.onRequest(request, response); |
| 888 } |
| 889 } else { |
| 890 response.statusCode = HttpStatus.NOT_FOUND; |
| 891 response.outputStream.close(); |
| 892 } |
| 849 } | 893 } |
| 850 | 894 |
| 895 |
| 851 ServerSocket _server; // The server listen socket. | 896 ServerSocket _server; // The server listen socket. |
| 852 bool _closeServer = false; | 897 bool _closeServer = false; |
| 853 Set<_HttpConnection> _connections; // Set of currently connected clients. | 898 Set<_HttpConnection> _connections; // Set of currently connected clients. |
| 854 Function _onRequest; | 899 List<_RequestHandlerRegistration> _handlers; |
| 900 Object _defaultHandler; |
| 855 Function _onError; | 901 Function _onError; |
| 856 } | 902 } |
| 857 | 903 |
| 858 | 904 |
| 859 class _HttpClientRequest | 905 class _HttpClientRequest |
| 860 extends _HttpRequestResponseBase implements HttpClientRequest { | 906 extends _HttpRequestResponseBase implements HttpClientRequest { |
| 861 static final int START = 0; | 907 static final int START = 0; |
| 862 static final int HEADERS_SENT = 1; | 908 static final int HEADERS_SENT = 1; |
| 863 static final int DONE = 2; | 909 static final int DONE = 2; |
| 864 | 910 |
| (...skipping 473 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1338 sockets.addFirst(socketConn); | 1384 sockets.addFirst(socketConn); |
| 1339 socketConn._markReturned(); | 1385 socketConn._markReturned(); |
| 1340 } | 1386 } |
| 1341 | 1387 |
| 1342 Function _onOpen; | 1388 Function _onOpen; |
| 1343 Map<String, Queue<_SocketConnection>> _openSockets; | 1389 Map<String, Queue<_SocketConnection>> _openSockets; |
| 1344 Set<_SocketConnection> _activeSockets; | 1390 Set<_SocketConnection> _activeSockets; |
| 1345 Timer _evictionTimer; | 1391 Timer _evictionTimer; |
| 1346 bool _shutdown; // Has this HTTP client been shutdown? | 1392 bool _shutdown; // Has this HTTP client been shutdown? |
| 1347 } | 1393 } |
| OLD | NEW |