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 String _webSocketGUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; | 7 const String _webSocketGUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; |
8 | 8 |
9 class _WebSocketMessageType { | 9 class _WebSocketMessageType { |
10 static const int NONE = 0; | 10 static const int NONE = 0; |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
166 } | 166 } |
167 } | 167 } |
168 | 168 |
169 if (_isControlFrame()) { | 169 if (_isControlFrame()) { |
170 if (payload > 0) { | 170 if (payload > 0) { |
171 // Allocate a buffer for collecting the control frame | 171 // Allocate a buffer for collecting the control frame |
172 // payload if any. | 172 // payload if any. |
173 if (_controlPayload == null) { | 173 if (_controlPayload == null) { |
174 _controlPayload = new List<int>(); | 174 _controlPayload = new List<int>(); |
175 } | 175 } |
176 _controlPayload.addAll(buffer.getRange(index, payload)); | 176 _controlPayload.addAll(buffer.sublist(index, index + payload)); |
177 index += payload; | 177 index += payload; |
178 } | 178 } |
179 | 179 |
180 if (_remainingPayloadBytes == 0) { | 180 if (_remainingPayloadBytes == 0) { |
181 _controlFrameEnd(sink); | 181 _controlFrameEnd(sink); |
182 } | 182 } |
183 } else { | 183 } else { |
184 switch (_currentMessageType) { | 184 switch (_currentMessageType) { |
185 case _WebSocketMessageType.NONE: | 185 case _WebSocketMessageType.NONE: |
186 throw new WebSocketException("Protocol error"); | 186 throw new WebSocketException("Protocol error"); |
187 | 187 |
188 case _WebSocketMessageType.TEXT: | 188 case _WebSocketMessageType.TEXT: |
189 _buffer.write(_decodeString(buffer.getRange(index, payload))); | 189 _buffer.write(_decodeString( |
| 190 buffer.sublist(index, index + payload))); |
190 index += payload; | 191 index += payload; |
191 if (_remainingPayloadBytes == 0) { | 192 if (_remainingPayloadBytes == 0) { |
192 _messageFrameEnd(sink); | 193 _messageFrameEnd(sink); |
193 } | 194 } |
194 break; | 195 break; |
195 | 196 |
196 case _WebSocketMessageType.BINARY: | 197 case _WebSocketMessageType.BINARY: |
197 _buffer.write(buffer.getRange(index, payload)); | 198 _buffer.write(buffer.sublist(index, index + payload)); |
198 index += payload; | 199 index += payload; |
199 if (_remainingPayloadBytes == 0) { | 200 if (_remainingPayloadBytes == 0) { |
200 _messageFrameEnd(sink); | 201 _messageFrameEnd(sink); |
201 } | 202 } |
202 break; | 203 break; |
203 | 204 |
204 default: | 205 default: |
205 throw new WebSocketException("Protocol error"); | 206 throw new WebSocketException("Protocol error"); |
206 } | 207 } |
207 } | 208 } |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
288 if (_controlPayload.length > 0) { | 289 if (_controlPayload.length > 0) { |
289 if (_controlPayload.length == 1) { | 290 if (_controlPayload.length == 1) { |
290 throw new WebSocketException("Protocol error"); | 291 throw new WebSocketException("Protocol error"); |
291 } | 292 } |
292 closeCode = _controlPayload[0] << 8 | _controlPayload[1]; | 293 closeCode = _controlPayload[0] << 8 | _controlPayload[1]; |
293 if (closeCode == WebSocketStatus.NO_STATUS_RECEIVED) { | 294 if (closeCode == WebSocketStatus.NO_STATUS_RECEIVED) { |
294 throw new WebSocketException("Protocol error"); | 295 throw new WebSocketException("Protocol error"); |
295 } | 296 } |
296 if (_controlPayload.length > 2) { | 297 if (_controlPayload.length > 2) { |
297 closeReason = _decodeString( | 298 closeReason = _decodeString( |
298 _controlPayload.getRange(2, _controlPayload.length - 2)); | 299 _controlPayload.sublist(2)); |
299 } | 300 } |
300 } | 301 } |
301 _state = CLOSED; | 302 _state = CLOSED; |
302 sink.close(); | 303 sink.close(); |
303 break; | 304 break; |
304 | 305 |
305 case _WebSocketOpcode.PING: | 306 case _WebSocketOpcode.PING: |
306 // TODO(ajohnsen): Handle ping. | 307 // TODO(ajohnsen): Handle ping. |
307 break; | 308 break; |
308 | 309 |
(...skipping 354 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
663 if (data != null) { | 664 if (data != null) { |
664 _socket.writeBytes(data); | 665 _socket.writeBytes(data); |
665 } | 666 } |
666 } catch (_) { | 667 } catch (_) { |
667 // The socket can be closed before _socket.done have a chance | 668 // The socket can be closed before _socket.done have a chance |
668 // to complete. | 669 // to complete. |
669 _writeClosed = true; | 670 _writeClosed = true; |
670 } | 671 } |
671 } | 672 } |
672 } | 673 } |
OLD | NEW |