| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 // Global constants. | 5 // Global constants. |
| 6 class Const { | 6 class Const { |
| 7 // Bytes for "HTTP/1.0". | 7 // Bytes for "HTTP/1.0". |
| 8 static final HTTP10 = const [72, 84, 84, 80, 47, 49, 46, 48]; | 8 static final HTTP10 = const [72, 84, 84, 80, 47, 49, 46, 48]; |
| 9 // Bytes for "HTTP/1.1". | 9 // Bytes for "HTTP/1.1". |
| 10 static final HTTP11 = const [72, 84, 84, 80, 47, 49, 46, 49]; | 10 static final HTTP11 = const [72, 84, 84, 80, 47, 49, 46, 49]; |
| (...skipping 910 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 921 } | 921 } |
| 922 | 922 |
| 923 | 923 |
| 924 class HTTPConnectionBase { | 924 class HTTPConnectionBase { |
| 925 static final BUFFER_SIZE = 80; | 925 static final BUFFER_SIZE = 80; |
| 926 | 926 |
| 927 HTTPConnectionBase(Socket this._socket) | 927 HTTPConnectionBase(Socket this._socket) |
| 928 : _sendBuffers = new Queue(), | 928 : _sendBuffers = new Queue(), |
| 929 _httpParser = new HTTPParser() { | 929 _httpParser = new HTTPParser() { |
| 930 // Register handler for socket events. | 930 // Register handler for socket events. |
| 931 _socket.setDataHandler(() => _dataHandler()); | 931 _socket.dataHandler = _dataHandler; |
| 932 _socket.setCloseHandler(() => _closeHandler()); | 932 _socket.closeHandler = _closeHandler; |
| 933 _socket.setErrorHandler(() => _errorHandler()); | 933 _socket.errorHandler = _errorHandler; |
| 934 } | 934 } |
| 935 | 935 |
| 936 // Add data to be send. If the flag keepBuffer is false (the | 936 // Add data to be send. If the flag keepBuffer is false (the |
| 937 // default) the data will be copied to an internal send buffer. If | 937 // default) the data will be copied to an internal send buffer. If |
| 938 // the flag keepBuffer is true the data will be kept until it has | 938 // the flag keepBuffer is true the data will be kept until it has |
| 939 // successfuly been sent. Note: calling this function will not | 939 // successfuly been sent. Note: calling this function will not |
| 940 // initiate sending of the data. | 940 // initiate sending of the data. |
| 941 void _addSendData(List<int> data, | 941 void _addSendData(List<int> data, |
| 942 int offset, | 942 int offset, |
| 943 int count, | 943 int count, |
| (...skipping 29 matching lines...) Expand all Loading... |
| 973 // immediately and no callbacks will occour. | 973 // immediately and no callbacks will occour. |
| 974 bool _startSending(var callback) { | 974 bool _startSending(var callback) { |
| 975 void send() { | 975 void send() { |
| 976 | 976 |
| 977 void continueSending() { | 977 void continueSending() { |
| 978 // Send more data. | 978 // Send more data. |
| 979 send(); | 979 send(); |
| 980 | 980 |
| 981 // If all data has been sent notify the callback. | 981 // If all data has been sent notify the callback. |
| 982 if (_sendBuffers.isEmpty() && callback != null) { | 982 if (_sendBuffers.isEmpty() && callback != null) { |
| 983 _socket.setWriteHandler(null); | 983 _socket.writeHandler = null; |
| 984 callback(); | 984 callback(); |
| 985 } | 985 } |
| 986 } | 986 } |
| 987 | 987 |
| 988 // Send as much data as possible. | 988 // Send as much data as possible. |
| 989 while (!_sendBuffers.isEmpty()) { | 989 while (!_sendBuffers.isEmpty()) { |
| 990 SendBuffer buffer = _sendBuffers.first(); | 990 SendBuffer buffer = _sendBuffers.first(); |
| 991 buffer._write(_socket); | 991 buffer._write(_socket); |
| 992 if (!buffer._isSent) { | 992 if (!buffer._isSent) { |
| 993 _socket.setWriteHandler(continueSending); | 993 _socket.writeHandler = continueSending; |
| 994 break; | 994 break; |
| 995 } else { | 995 } else { |
| 996 _sendBuffers.removeFirst(); | 996 _sendBuffers.removeFirst(); |
| 997 } | 997 } |
| 998 } | 998 } |
| 999 } | 999 } |
| 1000 | 1000 |
| 1001 // Queue the current send buffer now if any. | 1001 // Queue the current send buffer now if any. |
| 1002 if (_currentSendBuffer != null) { | 1002 if (_currentSendBuffer != null) { |
| 1003 _sendBuffers.addLast(_currentSendBuffer); | 1003 _sendBuffers.addLast(_currentSendBuffer); |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1119 _connections.addLast(connection); | 1119 _connections.addLast(connection); |
| 1120 _connectionsCount++; | 1120 _connectionsCount++; |
| 1121 if (_debugTrace) { | 1121 if (_debugTrace) { |
| 1122 print("New connection (total $_connectionsCount connections)"); | 1122 print("New connection (total $_connectionsCount connections)"); |
| 1123 } | 1123 } |
| 1124 } | 1124 } |
| 1125 | 1125 |
| 1126 _connections = new Queue<HTTPConnection>(); | 1126 _connections = new Queue<HTTPConnection>(); |
| 1127 _connectionsCount = 0; | 1127 _connectionsCount = 0; |
| 1128 _server = new ServerSocket(host, port, 5); | 1128 _server = new ServerSocket(host, port, 5); |
| 1129 _server.setConnectionHandler(connectionHandler); | 1129 _server.connectionHandler = connectionHandler; |
| 1130 } | 1130 } |
| 1131 | 1131 |
| 1132 void close() => _server.close(); | 1132 void close() => _server.close(); |
| 1133 int get port() => _server.port; | 1133 int get port() => _server.port; |
| 1134 | 1134 |
| 1135 ServerSocket _server; // The server listen socket. | 1135 ServerSocket _server; // The server listen socket. |
| 1136 Queue<HTTPConnection> _connections; // Queue of currently connected clients. | 1136 Queue<HTTPConnection> _connections; // Queue of currently connected clients. |
| 1137 int _connectionsCount; | 1137 int _connectionsCount; |
| 1138 bool _debugTrace; | 1138 bool _debugTrace; |
| 1139 } | 1139 } |
| (...skipping 340 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1480 | 1480 |
| 1481 // Return connection. | 1481 // Return connection. |
| 1482 sockets.addFirst(socketConn); | 1482 sockets.addFirst(socketConn); |
| 1483 socketConn._markReturned(); | 1483 socketConn._markReturned(); |
| 1484 } | 1484 } |
| 1485 | 1485 |
| 1486 Map<String, Queue<SocketConnection>> _openSockets; | 1486 Map<String, Queue<SocketConnection>> _openSockets; |
| 1487 Timer _evictionTimer; | 1487 Timer _evictionTimer; |
| 1488 bool _shutdown; // Has this HTTP client been shutdown? | 1488 bool _shutdown; // Has this HTTP client been shutdown? |
| 1489 } | 1489 } |
| OLD | NEW |