| 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 final String _webSocketGUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; | 5 final String _webSocketGUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; |
| 6 | 6 |
| 7 class _WebSocketMessageType { | 7 class _WebSocketMessageType { |
| 8 static final int NONE = 0; | 8 static final int NONE = 0; |
| 9 static final int BINARY = 1; | 9 static final int BINARY = 1; |
| 10 static final int TEXT = 2; | 10 static final int TEXT = 2; |
| (...skipping 568 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 579 response.statusCode = HttpStatus.BAD_REQUEST; | 579 response.statusCode = HttpStatus.BAD_REQUEST; |
| 580 response.outputStream.close(); | 580 response.outputStream.close(); |
| 581 return; | 581 return; |
| 582 } | 582 } |
| 583 | 583 |
| 584 // Send the upgrade response. | 584 // Send the upgrade response. |
| 585 response.statusCode = HttpStatus.SWITCHING_PROTOCOLS; | 585 response.statusCode = HttpStatus.SWITCHING_PROTOCOLS; |
| 586 response.headers.add(HttpHeaders.CONNECTION, "Upgrade"); | 586 response.headers.add(HttpHeaders.CONNECTION, "Upgrade"); |
| 587 response.headers.add(HttpHeaders.UPGRADE, "websocket"); | 587 response.headers.add(HttpHeaders.UPGRADE, "websocket"); |
| 588 String key = request.headers.value("Sec-WebSocket-Key"); | 588 String key = request.headers.value("Sec-WebSocket-Key"); |
| 589 String accept = | 589 SHA1 sha1 = new SHA1(); |
| 590 _Base64._encode(_Sha1._hash("$key$_webSocketGUID".charCodes())); | 590 sha1.update("$key$_webSocketGUID".charCodes()); |
| 591 String accept = _Base64._encode(sha1.digest()); |
| 591 response.headers.add("Sec-WebSocket-Accept", accept); | 592 response.headers.add("Sec-WebSocket-Accept", accept); |
| 592 response.contentLength = 0; | 593 response.contentLength = 0; |
| 593 | 594 |
| 594 // Upgrade the connection and get the underlying socket. | 595 // Upgrade the connection and get the underlying socket. |
| 595 WebSocketConnection conn = | 596 WebSocketConnection conn = |
| 596 new _WebSocketConnection(response.detachSocket()); | 597 new _WebSocketConnection(response.detachSocket()); |
| 597 if (_onOpen != null) _onOpen(conn); | 598 if (_onOpen != null) _onOpen(conn); |
| 598 } | 599 } |
| 599 | 600 |
| 600 void set onOpen(callback(WebSocketConnection connection)) { | 601 void set onOpen(callback(WebSocketConnection connection)) { |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 722 }); | 723 }); |
| 723 if (!isUpgrade) return false; | 724 if (!isUpgrade) return false; |
| 724 String upgrade = response.headers.value(HttpHeaders.UPGRADE); | 725 String upgrade = response.headers.value(HttpHeaders.UPGRADE); |
| 725 if (upgrade == null || upgrade.toLowerCase() != "websocket") { | 726 if (upgrade == null || upgrade.toLowerCase() != "websocket") { |
| 726 return false; | 727 return false; |
| 727 } | 728 } |
| 728 String accept = response.headers.value("Sec-WebSocket-Accept"); | 729 String accept = response.headers.value("Sec-WebSocket-Accept"); |
| 729 if (accept == null) { | 730 if (accept == null) { |
| 730 return false; | 731 return false; |
| 731 } | 732 } |
| 732 List<int> expectedAccept = | 733 SHA1 sha1 = new SHA1(); |
| 733 _Sha1._hash("$_nonce$_webSocketGUID".charCodes()); | 734 sha1.update("$_nonce$_webSocketGUID".charCodes()); |
| 735 List<int> expectedAccept = sha1.digest(); |
| 734 List<int> receivedAccept = _Base64._decode(accept); | 736 List<int> receivedAccept = _Base64._decode(accept); |
| 735 if (expectedAccept.length != receivedAccept.length) return false; | 737 if (expectedAccept.length != receivedAccept.length) return false; |
| 736 for (int i = 0; i < expectedAccept.length; i++) { | 738 for (int i = 0; i < expectedAccept.length; i++) { |
| 737 if (expectedAccept[i] != receivedAccept[i]) return false; | 739 if (expectedAccept[i] != receivedAccept[i]) return false; |
| 738 } | 740 } |
| 739 return true; | 741 return true; |
| 740 } | 742 } |
| 741 | 743 |
| 742 Function _onRequest; | 744 Function _onRequest; |
| 743 Function _onOpen; | 745 Function _onOpen; |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 847 | 849 |
| 848 class _WebSocketCloseEvent implements CloseEvent { | 850 class _WebSocketCloseEvent implements CloseEvent { |
| 849 _WebSocketCloseEvent(this._wasClean, this._code, this._reason); | 851 _WebSocketCloseEvent(this._wasClean, this._code, this._reason); |
| 850 bool get wasClean() => _wasClean; | 852 bool get wasClean() => _wasClean; |
| 851 int get code() => _code; | 853 int get code() => _code; |
| 852 String get reason() => _reason; | 854 String get reason() => _reason; |
| 853 bool _wasClean; | 855 bool _wasClean; |
| 854 int _code; | 856 int _code; |
| 855 String _reason; | 857 String _reason; |
| 856 } | 858 } |
| OLD | NEW |