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 // The close queue handles graceful closing of HTTP connections. When | 5 // The close queue handles graceful closing of HTTP connections. When |
6 // a connection is added to the queue it will enter a wait state | 6 // a connection is added to the queue it will enter a wait state |
7 // waiting for all data written and possibly socket shutdown from | 7 // waiting for all data written and possibly socket shutdown from |
8 // peer. | 8 // peer. |
9 class _CloseQueue { | 9 class _CloseQueue { |
10 _CloseQueue() : _q = new Set<_HttpConnectionBase>(); | 10 _CloseQueue() : _q = new Set<_HttpConnectionBase>(); |
(...skipping 907 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
918 | 918 |
919 | 919 |
920 class _RequestHandlerRegistration { | 920 class _RequestHandlerRegistration { |
921 _RequestHandlerRegistration(Function this._matcher, Function this._handler); | 921 _RequestHandlerRegistration(Function this._matcher, Function this._handler); |
922 Function _matcher; | 922 Function _matcher; |
923 Function _handler; | 923 Function _handler; |
924 } | 924 } |
925 | 925 |
926 // HTTP server waiting for socket connections. The connections are | 926 // HTTP server waiting for socket connections. The connections are |
927 // managed by the server and as requests are received the request. | 927 // managed by the server and as requests are received the request. |
928 class _HttpServer implements HttpServer { | 928 // HTTPS connections are also supported, if the _HttpServer.httpsServer |
929 _HttpServer() : _connections = new Set<_HttpConnection>(), | 929 // constructor is used and a certificate name is provided in listen, |
930 _handlers = new List<_RequestHandlerRegistration>(), | 930 // or a SecureServerSocket is provided to listenOn. |
931 _closeQueue = new _CloseQueue(); | 931 class _HttpServer implements HttpServer, HttpsServer { |
Mads Ager (google)
2012/11/29 09:45:24
Similarly here in the implementation. It might be
| |
932 _HttpServer() : this._internal(isSecure: false); | |
932 | 933 |
933 void listen(String host, int port, {int backlog: 128}) { | 934 _HttpServer.httpsServer() : this._internal(isSecure: true); |
934 listenOn(new ServerSocket(host, port, backlog)); | 935 |
936 _HttpServer._internal({ bool isSecure: false }) | |
937 : _secure = isSecure, | |
938 _connections = new Set<_HttpConnection>(), | |
939 _handlers = new List<_RequestHandlerRegistration>(), | |
940 _closeQueue = new _CloseQueue(); | |
941 | |
942 void listen(String host, | |
943 int port, | |
944 {int backlog: 128, | |
945 String certificate_name}) { | |
946 if (_secure) { | |
947 listenOn(new SecureServerSocket(host, port, backlog, certificate_name)); | |
948 } else { | |
949 listenOn(new ServerSocket(host, port, backlog)); | |
950 } | |
935 _closeServer = true; | 951 _closeServer = true; |
936 } | 952 } |
937 | 953 |
938 void listenOn(ServerSocket serverSocket) { | 954 void listenOn(ServerSocket serverSocket) { |
955 if (_secure && serverSocket is! SecureServerSocket) { | |
956 throw new HttpException( | |
957 'HttpsServer.listenOn was called with non-secure server socket'); | |
958 } else if (!_secure && serverSocket is SecureServerSocket) { | |
959 throw new HttpException( | |
960 'HttpServer.listenOn was called with a secure server socket'); | |
961 } | |
939 void onConnection(Socket socket) { | 962 void onConnection(Socket socket) { |
940 // Accept the client connection. | 963 // Accept the client connection. |
941 _HttpConnection connection = new _HttpConnection(this); | 964 _HttpConnection connection = new _HttpConnection(this); |
942 connection._connectionEstablished(socket); | 965 connection._connectionEstablished(socket); |
943 _connections.add(connection); | 966 _connections.add(connection); |
944 connection.onRequestReceived = _handleRequest; | 967 connection.onRequestReceived = _handleRequest; |
945 connection.onClosed = () => _connections.remove(connection); | 968 connection.onClosed = () => _connections.remove(connection); |
946 connection.onDetach = () => _connections.remove(connection); | 969 connection.onDetach = () => _connections.remove(connection); |
947 connection.onError = (e) { | 970 connection.onError = (e) { |
948 _connections.remove(connection); | 971 _connections.remove(connection); |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1044 } else { | 1067 } else { |
1045 assert(result._isClosing); | 1068 assert(result._isClosing); |
1046 result.closing++; | 1069 result.closing++; |
1047 } | 1070 } |
1048 }); | 1071 }); |
1049 return result; | 1072 return result; |
1050 } | 1073 } |
1051 | 1074 |
1052 ServerSocket _server; // The server listen socket. | 1075 ServerSocket _server; // The server listen socket. |
1053 bool _closeServer = false; | 1076 bool _closeServer = false; |
1077 bool _secure; | |
1054 Set<_HttpConnection> _connections; // Set of currently connected clients. | 1078 Set<_HttpConnection> _connections; // Set of currently connected clients. |
1055 List<_RequestHandlerRegistration> _handlers; | 1079 List<_RequestHandlerRegistration> _handlers; |
1056 Object _defaultHandler; | 1080 Object _defaultHandler; |
1057 Function _onError; | 1081 Function _onError; |
1058 _CloseQueue _closeQueue; | 1082 _CloseQueue _closeQueue; |
1059 _HttpSessionManager _sessionManagerInstance; | 1083 _HttpSessionManager _sessionManagerInstance; |
1060 } | 1084 } |
1061 | 1085 |
1062 | 1086 |
1063 class _HttpClientRequest | 1087 class _HttpClientRequest |
(...skipping 1061 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2125 | 2149 |
2126 | 2150 |
2127 class _RedirectInfo implements RedirectInfo { | 2151 class _RedirectInfo implements RedirectInfo { |
2128 const _RedirectInfo(int this.statusCode, | 2152 const _RedirectInfo(int this.statusCode, |
2129 String this.method, | 2153 String this.method, |
2130 Uri this.location); | 2154 Uri this.location); |
2131 final int statusCode; | 2155 final int statusCode; |
2132 final String method; | 2156 final String method; |
2133 final Uri location; | 2157 final Uri location; |
2134 } | 2158 } |
OLD | NEW |