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 776 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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 { | 796 class _RequestHandlerRegistration { |
797 _RequestHandlerRegistration(Function this._matcher, Object this._handler); | 797 _RequestHandlerRegistration(Function this._matcher, Function this._handler); |
798 Function _matcher; | 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; | 799 Function _handler; |
808 } | 800 } |
809 | 801 |
810 | 802 |
811 // HTTP server waiting for socket connections. The connections are | 803 // HTTP server waiting for socket connections. The connections are |
812 // managed by the server and as requests are received the request. | 804 // managed by the server and as requests are received the request. |
813 class _HttpServer implements HttpServer { | 805 class _HttpServer implements HttpServer { |
814 _HttpServer() : _connections = new Set<_HttpConnection>(), | 806 _HttpServer() : _connections = new Set<_HttpConnection>(), |
815 _handlers = new List<_RequestHandlerRegistration>(); | 807 _handlers = new List<_RequestHandlerRegistration>(); |
816 | 808 |
(...skipping 14 matching lines...) Expand all Loading... | |
831 if (_onError != null) _onError(e); | 823 if (_onError != null) _onError(e); |
832 }; | 824 }; |
833 connection._connectionEstablished(socket); | 825 connection._connectionEstablished(socket); |
834 _connections.add(connection); | 826 _connections.add(connection); |
835 } | 827 } |
836 serverSocket.onConnection = onConnection; | 828 serverSocket.onConnection = onConnection; |
837 _server = serverSocket; | 829 _server = serverSocket; |
838 _closeServer = false; | 830 _closeServer = false; |
839 } | 831 } |
840 | 832 |
841 addRequestHandler(bool matcher(String path), Object handler) { | 833 addRequestHandler(bool matcher(String path), |
842 if (handler is Function) { | 834 void handler(HttpRequest request, HttpResponse response)) { |
843 handler = new _RequestHandlerImpl(handler); | |
844 } | |
845 _handlers.add(new _RequestHandlerRegistration(matcher, handler)); | 835 _handlers.add(new _RequestHandlerRegistration(matcher, handler)); |
846 } | 836 } |
847 | 837 |
848 void set defaultRequestHandler(Object handler) { | 838 void set defaultRequestHandler( |
839 void handler(HttpRequest request, HttpResponse response)) { | |
849 _defaultHandler = handler; | 840 _defaultHandler = handler; |
850 } | 841 } |
851 | 842 |
852 void close() { | 843 void close() { |
853 if (_server !== null && _closeServer) { | 844 if (_server !== null && _closeServer) { |
854 _server.close(); | 845 _server.close(); |
855 } | 846 } |
856 _server = null; | 847 _server = null; |
857 for (_HttpConnection connection in _connections) { | 848 for (_HttpConnection connection in _connections) { |
858 connection._close(); | 849 connection._close(); |
859 } | 850 } |
860 _connections.clear(); | 851 _connections.clear(); |
861 } | 852 } |
862 | 853 |
863 int get port() { | 854 int get port() { |
864 if (_server === null) { | 855 if (_server === null) { |
865 throw new HttpException("The HttpServer is not listening on a port."); | 856 throw new HttpException("The HttpServer is not listening on a port."); |
866 } | 857 } |
867 return _server.port; | 858 return _server.port; |
868 } | 859 } |
869 | 860 |
870 void set onError(void callback(Exception e)) { | 861 void set onError(void callback(Exception e)) { |
871 _onError = callback; | 862 _onError = callback; |
872 } | 863 } |
873 | 864 |
874 void _handleRequest(HttpRequest request, HttpResponse response) { | 865 void _handleRequest(HttpRequest request, HttpResponse response) { |
875 for (int i = 0; i < _handlers.length; i++) { | 866 for (int i = 0; i < _handlers.length; i++) { |
876 if (_handlers[i]._matcher(request)) { | 867 if (_handlers[i]._matcher(request)) { |
877 var handler = _handlers[i]._handler; | 868 Function handler = _handlers[i]._handler; |
878 try { | 869 try { |
879 handler.onRequest(request, response); | 870 handler(request, response); |
880 } catch (var e) { | 871 } catch (var e) { |
Anders Johnsen
2012/04/25 08:46:23
We should consider doing something with the Stackt
Søren Gjesse
2012/04/25 08:54:41
Yes, that is a general issue with error callbacks
| |
881 if (_onError != null) { | 872 if (_onError != null) { |
882 _onError(e); | 873 _onError(e); |
883 } | 874 } |
884 } | 875 } |
885 return; | 876 return; |
886 } | 877 } |
887 } | 878 } |
888 | 879 |
889 if (_defaultHandler != null) { | 880 if (_defaultHandler != null) { |
Anders Johnsen
2012/04/25 08:46:23
!==, as you wrote, we support taking classes using
Søren Gjesse
2012/04/25 08:54:41
We only support functions now.
| |
890 if (_defaultHandler is Function) { | 881 _defaultHandler(request, response); |
891 _defaultHandler(request, response); | |
892 } else { | |
893 _defaultHandler.onRequest(request, response); | |
894 } | |
895 } else { | 882 } else { |
896 response.statusCode = HttpStatus.NOT_FOUND; | 883 response.statusCode = HttpStatus.NOT_FOUND; |
897 response.outputStream.close(); | 884 response.outputStream.close(); |
898 } | 885 } |
899 } | 886 } |
900 | 887 |
901 | 888 |
902 ServerSocket _server; // The server listen socket. | 889 ServerSocket _server; // The server listen socket. |
903 bool _closeServer = false; | 890 bool _closeServer = false; |
904 Set<_HttpConnection> _connections; // Set of currently connected clients. | 891 Set<_HttpConnection> _connections; // Set of currently connected clients. |
(...skipping 487 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1392 _activeSockets.remove(socketConn); | 1379 _activeSockets.remove(socketConn); |
1393 sockets.addFirst(socketConn); | 1380 sockets.addFirst(socketConn); |
1394 } | 1381 } |
1395 | 1382 |
1396 Function _onOpen; | 1383 Function _onOpen; |
1397 Map<String, Queue<_SocketConnection>> _openSockets; | 1384 Map<String, Queue<_SocketConnection>> _openSockets; |
1398 Set<_SocketConnection> _activeSockets; | 1385 Set<_SocketConnection> _activeSockets; |
1399 Timer _evictionTimer; | 1386 Timer _evictionTimer; |
1400 bool _shutdown; // Has this HTTP client been shutdown? | 1387 bool _shutdown; // Has this HTTP client been shutdown? |
1401 } | 1388 } |
OLD | NEW |