OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 part of dart.io; | 5 part of dart.io; |
6 | 6 |
7 const int _OUTGOING_BUFFER_SIZE = 8 * 1024; | 7 const int _OUTGOING_BUFFER_SIZE = 8 * 1024; |
8 | 8 |
9 class _HttpIncoming extends Stream<List<int>> { | 9 class _HttpIncoming extends Stream<List<int>> { |
10 final int _transferLength; | 10 final int _transferLength; |
(...skipping 1974 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1985 return proxyCfg; | 1985 return proxyCfg; |
1986 } | 1986 } |
1987 } | 1987 } |
1988 return "DIRECT"; | 1988 return "DIRECT"; |
1989 } | 1989 } |
1990 | 1990 |
1991 static Map<String, String> _platformEnvironmentCache = Platform.environment; | 1991 static Map<String, String> _platformEnvironmentCache = Platform.environment; |
1992 } | 1992 } |
1993 | 1993 |
1994 | 1994 |
1995 class _HttpConnection extends LinkedListEntry<_HttpConnection> { | 1995 class _HttpConnection |
| 1996 extends LinkedListEntry<_HttpConnection> with _ServiceObject { |
1996 static const _ACTIVE = 0; | 1997 static const _ACTIVE = 0; |
1997 static const _IDLE = 1; | 1998 static const _IDLE = 1; |
1998 static const _CLOSING = 2; | 1999 static const _CLOSING = 2; |
1999 static const _DETACHED = 3; | 2000 static const _DETACHED = 3; |
2000 | 2001 |
2001 final Socket _socket; | 2002 // Use HashMap, as we don't need to keep order. |
| 2003 static Map<int, _HttpConnection> _connections = |
| 2004 new HashMap<int, _HttpConnection>(); |
| 2005 |
| 2006 final _socket; |
2002 final _HttpServer _httpServer; | 2007 final _HttpServer _httpServer; |
2003 final _HttpParser _httpParser; | 2008 final _HttpParser _httpParser; |
2004 int _state = _IDLE; | 2009 int _state = _IDLE; |
2005 StreamSubscription _subscription; | 2010 StreamSubscription _subscription; |
2006 Timer _idleTimer; | 2011 Timer _idleTimer; |
2007 bool _idleMark = false; | 2012 bool _idleMark = false; |
2008 Future _streamFuture; | 2013 Future _streamFuture; |
2009 | 2014 |
2010 _HttpConnection(this._socket, this._httpServer) | 2015 _HttpConnection(this._socket, this._httpServer) |
2011 : _httpParser = new _HttpParser.requestParser() { | 2016 : _httpParser = new _HttpParser.requestParser() { |
| 2017 try { _socket._owner = this; } catch (_) { print(_); } |
| 2018 _connections[_serviceId] = this; |
2012 _httpParser.listenToStream(_socket); | 2019 _httpParser.listenToStream(_socket); |
2013 _subscription = _httpParser.listen( | 2020 _subscription = _httpParser.listen( |
2014 (incoming) { | 2021 (incoming) { |
2015 _httpServer._markActive(this); | 2022 _httpServer._markActive(this); |
2016 // If the incoming was closed, close the connection. | 2023 // If the incoming was closed, close the connection. |
2017 incoming.dataDone.then((closing) { | 2024 incoming.dataDone.then((closing) { |
2018 if (closing) destroy(); | 2025 if (closing) destroy(); |
2019 }); | 2026 }); |
2020 // Only handle one incoming request at the time. Keep the | 2027 // Only handle one incoming request at the time. Keep the |
2021 // stream paused until the request has been send. | 2028 // stream paused until the request has been send. |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2067 _idleMark = true; | 2074 _idleMark = true; |
2068 } | 2075 } |
2069 | 2076 |
2070 bool get isMarkedIdle => _idleMark; | 2077 bool get isMarkedIdle => _idleMark; |
2071 | 2078 |
2072 void destroy() { | 2079 void destroy() { |
2073 if (_state == _CLOSING || _state == _DETACHED) return; | 2080 if (_state == _CLOSING || _state == _DETACHED) return; |
2074 _state = _CLOSING; | 2081 _state = _CLOSING; |
2075 _socket.destroy(); | 2082 _socket.destroy(); |
2076 _httpServer._connectionClosed(this); | 2083 _httpServer._connectionClosed(this); |
| 2084 _connections.remove(_serviceId); |
2077 } | 2085 } |
2078 | 2086 |
2079 Future<Socket> detachSocket() { | 2087 Future<Socket> detachSocket() { |
2080 _state = _DETACHED; | 2088 _state = _DETACHED; |
2081 // Remove connection from server. | 2089 // Remove connection from server. |
2082 _httpServer._connectionClosed(this); | 2090 _httpServer._connectionClosed(this); |
2083 | 2091 |
2084 _HttpDetachedIncoming detachedIncoming = _httpParser.detachIncoming(); | 2092 _HttpDetachedIncoming detachedIncoming = _httpParser.detachIncoming(); |
2085 | 2093 |
2086 return _streamFuture.then((_) { | 2094 return _streamFuture.then((_) { |
| 2095 _connections.remove(_serviceId); |
2087 return new _DetachedSocket(_socket, detachedIncoming); | 2096 return new _DetachedSocket(_socket, detachedIncoming); |
2088 }); | 2097 }); |
2089 } | 2098 } |
2090 | 2099 |
2091 HttpConnectionInfo get connectionInfo => _HttpConnectionInfo.create(_socket); | 2100 HttpConnectionInfo get connectionInfo => _HttpConnectionInfo.create(_socket); |
2092 | 2101 |
2093 bool get _isActive => _state == _ACTIVE; | 2102 bool get _isActive => _state == _ACTIVE; |
2094 bool get _isIdle => _state == _IDLE; | 2103 bool get _isIdle => _state == _IDLE; |
2095 bool get _isClosing => _state == _CLOSING; | 2104 bool get _isClosing => _state == _CLOSING; |
2096 bool get _isDetached => _state == _DETACHED; | 2105 bool get _isDetached => _state == _DETACHED; |
| 2106 |
| 2107 String get _serviceTypePath => 'io/http/serverconnections'; |
| 2108 String get _serviceTypeName => 'HttpServerConnection'; |
| 2109 |
| 2110 Map _toJSON(bool ref) { |
| 2111 var name = "${_socket.address.host}:${_socket.port} <-> " |
| 2112 "${_socket.remoteAddress.host}:${_socket.remotePort}"; |
| 2113 var r = { |
| 2114 'id': _servicePath, |
| 2115 'type': _serviceType(ref), |
| 2116 'name': name, |
| 2117 'user_name': name, |
| 2118 }; |
| 2119 if (ref) { |
| 2120 return r; |
| 2121 } |
| 2122 r['server'] = _httpServer._toJSON(true); |
| 2123 try { |
| 2124 r['socket'] = _socket._toJSON(true); |
| 2125 } catch (_) { |
| 2126 r['socket'] = { |
| 2127 'id': _servicePath, |
| 2128 'type': '@Socket', |
| 2129 'name': 'UserSocket', |
| 2130 'user_name': 'UserSocket', |
| 2131 }; |
| 2132 } |
| 2133 switch (_state) { |
| 2134 case _ACTIVE: r['state'] = "Active"; break; |
| 2135 case _IDLE: r['state'] = "Idle"; break; |
| 2136 case _CLOSING: r['state'] = "Closing"; break; |
| 2137 case _DETACHED: r['state'] = "Detached"; break; |
| 2138 default: r['state'] = 'Unknown'; break; |
| 2139 } |
| 2140 return r; |
| 2141 } |
2097 } | 2142 } |
2098 | 2143 |
2099 | 2144 |
2100 // HTTP server waiting for socket connections. | 2145 // HTTP server waiting for socket connections. |
2101 class _HttpServer | 2146 class _HttpServer |
2102 extends Stream<HttpRequest> with _ServiceObject | 2147 extends Stream<HttpRequest> with _ServiceObject |
2103 implements HttpServer { | 2148 implements HttpServer { |
2104 // Use default Map so we keep order. | 2149 // Use default Map so we keep order. |
2105 static Map<int, _HttpServer> _servers = new Map<int, _HttpServer>(); | 2150 static Map<int, _HttpServer> _servers = new Map<int, _HttpServer>(); |
2106 | 2151 |
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2316 } catch (_) { | 2361 } catch (_) { |
2317 r['socket'] = { | 2362 r['socket'] = { |
2318 'id': _servicePath, | 2363 'id': _servicePath, |
2319 'type': '@Socket', | 2364 'type': '@Socket', |
2320 'name': 'UserSocket', | 2365 'name': 'UserSocket', |
2321 'user_name': 'UserSocket', | 2366 'user_name': 'UserSocket', |
2322 }; | 2367 }; |
2323 } | 2368 } |
2324 r['port'] = port; | 2369 r['port'] = port; |
2325 r['address'] = address.host; | 2370 r['address'] = address.host; |
2326 r['active'] = _activeConnections.length; | 2371 r['active'] = _activeConnections.map((c) => c._toJSON(true)).toList(); |
2327 r['idle'] = _idleConnections.length; | 2372 r['idle'] = _idleConnections.map((c) => c._toJSON(true)).toList(); |
2328 r['closed'] = closed; | 2373 r['closed'] = closed; |
2329 return r; | 2374 return r; |
2330 } | 2375 } |
2331 | 2376 |
2332 _HttpSessionManager _sessionManagerInstance; | 2377 _HttpSessionManager _sessionManagerInstance; |
2333 | 2378 |
2334 // Indicated if the http server has been closed. | 2379 // Indicated if the http server has been closed. |
2335 bool closed = false; | 2380 bool closed = false; |
2336 | 2381 |
2337 // The server listen socket. Untyped as it can be both ServerSocket and | 2382 // The server listen socket. Untyped as it can be both ServerSocket and |
(...skipping 415 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2753 const _RedirectInfo(this.statusCode, this.method, this.location); | 2798 const _RedirectInfo(this.statusCode, this.method, this.location); |
2754 } | 2799 } |
2755 | 2800 |
2756 String _getHttpVersion() { | 2801 String _getHttpVersion() { |
2757 var version = Platform.version; | 2802 var version = Platform.version; |
2758 // Only include major and minor version numbers. | 2803 // Only include major and minor version numbers. |
2759 int index = version.indexOf('.', version.indexOf('.') + 1); | 2804 int index = version.indexOf('.', version.indexOf('.') + 1); |
2760 version = version.substring(0, index); | 2805 version = version.substring(0, index); |
2761 return 'Dart/$version (dart:io)'; | 2806 return 'Dart/$version (dart:io)'; |
2762 } | 2807 } |
OLD | NEW |