| 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 const String _webSocketGUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; | 5 const String _webSocketGUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; |
| 6 | 6 |
| 7 class _WebSocketMessageType { | 7 class _WebSocketMessageType { |
| 8 static const int NONE = 0; | 8 static const int NONE = 0; |
| 9 static const int BINARY = 1; | 9 static const int BINARY = 1; |
| 10 static const int TEXT = 2; | 10 static const int TEXT = 2; |
| (...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 207 } | 207 } |
| 208 | 208 |
| 209 // Hack - as we always do index++ below. | 209 // Hack - as we always do index++ below. |
| 210 index--; | 210 index--; |
| 211 } | 211 } |
| 212 | 212 |
| 213 // Move to the next byte. | 213 // Move to the next byte. |
| 214 index++; | 214 index++; |
| 215 } | 215 } |
| 216 } catch (e) { | 216 } catch (e) { |
| 217 if (onClosed !== null) onClosed(1002, "Protocol error"); | 217 if (onClosed !== null) onClosed(WebSocketStatus.PROTOCOL_ERROR, |
| 218 "Protocol error"); |
| 218 _state = FAILURE; | 219 _state = FAILURE; |
| 219 } | 220 } |
| 220 } | 221 } |
| 221 | 222 |
| 222 /** | 223 /** |
| 223 * Indicate that the underlying communication channel has been closed. | 224 * Indicate that the underlying communication channel has been closed. |
| 224 */ | 225 */ |
| 225 void closed() { | 226 void closed() { |
| 226 if (_state == START || _state == CLOSED || _state == FAILURE) return; | 227 if (_state == START || _state == CLOSED || _state == FAILURE) return; |
| 227 if (onClosed !== null) onClosed(1006, "Connection closed unexpectedly"); | 228 if (onClosed !== null) onClosed(WebSocketStatus.ABNORMAL_CLOSURE, |
| 229 "Connection closed unexpectedly"); |
| 228 _state = CLOSED; | 230 _state = CLOSED; |
| 229 } | 231 } |
| 230 | 232 |
| 231 void _lengthDone() { | 233 void _lengthDone() { |
| 232 if (_masked) { | 234 if (_masked) { |
| 233 _state = MASK; | 235 _state = MASK; |
| 234 _remainingMaskingKeyBytes = 4; | 236 _remainingMaskingKeyBytes = 4; |
| 235 } else { | 237 } else { |
| 236 _remainingPayloadBytes = _len; | 238 _remainingPayloadBytes = _len; |
| 237 _startPayload(); | 239 _startPayload(); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 273 if (_fin) { | 275 if (_fin) { |
| 274 if (onMessageEnd !== null) onMessageEnd(); | 276 if (onMessageEnd !== null) onMessageEnd(); |
| 275 _currentMessageType = _WebSocketMessageType.NONE; | 277 _currentMessageType = _WebSocketMessageType.NONE; |
| 276 } | 278 } |
| 277 _prepareForNextFrame(); | 279 _prepareForNextFrame(); |
| 278 } | 280 } |
| 279 | 281 |
| 280 void _controlFrameEnd() { | 282 void _controlFrameEnd() { |
| 281 switch (_opcode) { | 283 switch (_opcode) { |
| 282 case _WebSocketOpcode.CLOSE: | 284 case _WebSocketOpcode.CLOSE: |
| 283 int status = 1005; | 285 int status = WebSocketStatus.NO_STATUS_RECEIVED; |
| 284 String reason = ""; | 286 String reason = ""; |
| 285 if (_controlPayload.length > 0) { | 287 if (_controlPayload.length > 0) { |
| 286 if (_controlPayload.length == 1) { | 288 if (_controlPayload.length == 1) { |
| 287 throw new WebSocketException("Protocol error"); | 289 throw new WebSocketException("Protocol error"); |
| 288 } | 290 } |
| 289 status = _controlPayload[0] << 8 | _controlPayload[1]; | 291 status = _controlPayload[0] << 8 | _controlPayload[1]; |
| 290 if (_controlPayload.length > 2) { | 292 if (_controlPayload.length > 2) { |
| 291 var decoder = _StringDecoders.decoder(Encoding.UTF_8); | 293 var decoder = _StringDecoders.decoder(Encoding.UTF_8); |
| 292 decoder.write( | 294 decoder.write( |
| 293 _controlPayload.getRange(2, _controlPayload.length - 2)); | 295 _controlPayload.getRange(2, _controlPayload.length - 2)); |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 375 int read = _socket.readList(data, 0, available); | 377 int read = _socket.readList(data, 0, available); |
| 376 processor.update(data, 0, read); | 378 processor.update(data, 0, read); |
| 377 }; | 379 }; |
| 378 _socket.onClosed = () { | 380 _socket.onClosed = () { |
| 379 processor.closed(); | 381 processor.closed(); |
| 380 if (_closeSent) { | 382 if (_closeSent) { |
| 381 // Got socket close in response to close frame. Don't treat | 383 // Got socket close in response to close frame. Don't treat |
| 382 // that as an error. | 384 // that as an error. |
| 383 if (_closeTimer !== null) _closeTimer.cancel(); | 385 if (_closeTimer !== null) _closeTimer.cancel(); |
| 384 } else { | 386 } else { |
| 385 if (_onClosed !== null) _onClosed(1006, "Unexpected close"); | 387 if (_onClosed !== null) _onClosed(WebSocketStatus.ABNORMAL_CLOSURE, |
| 388 "Unexpected close"); |
| 386 } | 389 } |
| 387 _socket.close(); | 390 _socket.close(); |
| 388 }; | 391 }; |
| 389 } | 392 } |
| 390 | 393 |
| 391 void set onMessage(void callback(Object message)) { | 394 void set onMessage(void callback(Object message)) { |
| 392 _onMessage = callback; | 395 _onMessage = callback; |
| 393 } | 396 } |
| 394 | 397 |
| 395 void set onClosed(void callback(int status, String reason)) { | 398 void set onClosed(void callback(int status, String reason)) { |
| (...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 629 | 632 |
| 630 | 633 |
| 631 class _WebSocketClientConnection | 634 class _WebSocketClientConnection |
| 632 extends _WebSocketConnectionBase implements WebSocketClientConnection { | 635 extends _WebSocketConnectionBase implements WebSocketClientConnection { |
| 633 _WebSocketClientConnection(HttpClientConnection this._conn, | 636 _WebSocketClientConnection(HttpClientConnection this._conn, |
| 634 [List<String> protocols]) { | 637 [List<String> protocols]) { |
| 635 _conn.onRequest = _onHttpClientRequest; | 638 _conn.onRequest = _onHttpClientRequest; |
| 636 _conn.onResponse = _onHttpClientResponse; | 639 _conn.onResponse = _onHttpClientResponse; |
| 637 _conn.onError = (e) { | 640 _conn.onError = (e) { |
| 638 if (_onClosed !== null) { | 641 if (_onClosed !== null) { |
| 639 _onClosed(1006, "$e"); | 642 _onClosed(WebSocketStatus.ABNORMAL_CLOSURE, "$e"); |
| 640 } | 643 } |
| 641 }; | 644 }; |
| 642 | 645 |
| 643 // Generate the nonce now as it is also used to set the hash code. | 646 // Generate the nonce now as it is also used to set the hash code. |
| 644 _generateNonceAndHash(); | 647 _generateNonceAndHash(); |
| 645 } | 648 } |
| 646 | 649 |
| 647 void set onRequest(void callback(HttpClientRequest request)) { | 650 void set onRequest(void callback(HttpClientRequest request)) { |
| 648 _onRequest = callback; | 651 _onRequest = callback; |
| 649 } | 652 } |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 788 }; | 791 }; |
| 789 _wsconn.onClosed = (status, reason) { | 792 _wsconn.onClosed = (status, reason) { |
| 790 _readyState = WebSocket.CLOSED; | 793 _readyState = WebSocket.CLOSED; |
| 791 if (_onclose !== null) { | 794 if (_onclose !== null) { |
| 792 _onclose(new _WebSocketCloseEvent(true, status, reason)); | 795 _onclose(new _WebSocketCloseEvent(true, status, reason)); |
| 793 } | 796 } |
| 794 }; | 797 }; |
| 795 _wsconn.onNoUpgrade = (response) { | 798 _wsconn.onNoUpgrade = (response) { |
| 796 if (_onclose !== null) { | 799 if (_onclose !== null) { |
| 797 _onclose( | 800 _onclose( |
| 798 new _WebSocketCloseEvent(true, 1006, "Connection not upgraded")); | 801 new _WebSocketCloseEvent(true, |
| 802 WebSocketStatus.ABNORMAL_CLOSURE, |
| 803 "Connection not upgraded")); |
| 799 } | 804 } |
| 800 }; | 805 }; |
| 801 } | 806 } |
| 802 | 807 |
| 803 int get readyState => _readyState; | 808 int get readyState => _readyState; |
| 804 int get bufferedAmount => 0; | 809 int get bufferedAmount => 0; |
| 805 | 810 |
| 806 void set onopen(Function callback) { | 811 void set onopen(Function callback) { |
| 807 _onopen = callback; | 812 _onopen = callback; |
| 808 } | 813 } |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 846 | 851 |
| 847 class _WebSocketCloseEvent implements CloseEvent { | 852 class _WebSocketCloseEvent implements CloseEvent { |
| 848 _WebSocketCloseEvent(this._wasClean, this._code, this._reason); | 853 _WebSocketCloseEvent(this._wasClean, this._code, this._reason); |
| 849 bool get wasClean => _wasClean; | 854 bool get wasClean => _wasClean; |
| 850 int get code => _code; | 855 int get code => _code; |
| 851 String get reason => _reason; | 856 String get reason => _reason; |
| 852 bool _wasClean; | 857 bool _wasClean; |
| 853 int _code; | 858 int _code; |
| 854 String _reason; | 859 String _reason; |
| 855 } | 860 } |
| OLD | NEW |