Chromium Code Reviews| 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 // Matches _WebSocketOpcode. | 9 // Matches _WebSocketOpcode. |
| 10 class _WebSocketMessageType { | 10 class _WebSocketMessageType { |
| (...skipping 757 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 768 Timer _closeTimer; | 768 Timer _closeTimer; |
| 769 | 769 |
| 770 static final HttpClient _httpClient = new HttpClient(); | 770 static final HttpClient _httpClient = new HttpClient(); |
| 771 | 771 |
| 772 static Future<WebSocket> connect( | 772 static Future<WebSocket> connect( |
| 773 String url, Iterable<String> protocols, Map<String, dynamic> headers) { | 773 String url, Iterable<String> protocols, Map<String, dynamic> headers) { |
| 774 Uri uri = Uri.parse(url); | 774 Uri uri = Uri.parse(url); |
| 775 if (uri.scheme != "ws" && uri.scheme != "wss") { | 775 if (uri.scheme != "ws" && uri.scheme != "wss") { |
| 776 throw new WebSocketException("Unsupported URL scheme '${uri.scheme}'"); | 776 throw new WebSocketException("Unsupported URL scheme '${uri.scheme}'"); |
| 777 } | 777 } |
| 778 if (uri.userInfo != "") { | |
| 779 throw new WebSocketException("Unsupported user info '${uri.userInfo}'"); | |
| 780 } | |
| 781 | 778 |
| 782 Random random = new Random(); | 779 Random random = new Random(); |
| 783 // Generate 16 random bytes. | 780 // Generate 16 random bytes. |
| 784 Uint8List nonceData = new Uint8List(16); | 781 Uint8List nonceData = new Uint8List(16); |
| 785 for (int i = 0; i < 16; i++) { | 782 for (int i = 0; i < 16; i++) { |
| 786 nonceData[i] = random.nextInt(256); | 783 nonceData[i] = random.nextInt(256); |
| 787 } | 784 } |
| 788 String nonce = _CryptoUtils.bytesToBase64(nonceData); | 785 String nonce = _CryptoUtils.bytesToBase64(nonceData); |
| 789 | 786 |
| 790 uri = new Uri(scheme: uri.scheme == "wss" ? "https" : "http", | 787 uri = new Uri(scheme: uri.scheme == "wss" ? "https" : "http", |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 802 // Setup the initial handshake. | 799 // Setup the initial handshake. |
| 803 request.headers | 800 request.headers |
| 804 ..set(HttpHeaders.CONNECTION, "Upgrade") | 801 ..set(HttpHeaders.CONNECTION, "Upgrade") |
| 805 ..set(HttpHeaders.UPGRADE, "websocket") | 802 ..set(HttpHeaders.UPGRADE, "websocket") |
| 806 ..set("Sec-WebSocket-Key", nonce) | 803 ..set("Sec-WebSocket-Key", nonce) |
| 807 ..set("Cache-Control", "no-cache") | 804 ..set("Cache-Control", "no-cache") |
| 808 ..set("Sec-WebSocket-Version", "13"); | 805 ..set("Sec-WebSocket-Version", "13"); |
| 809 if (protocols != null) { | 806 if (protocols != null) { |
| 810 request.headers.add("Sec-WebSocket-Protocol", protocols.toList()); | 807 request.headers.add("Sec-WebSocket-Protocol", protocols.toList()); |
| 811 } | 808 } |
| 809 if (uri.userInfo != null && !uri.userInfo.isEmpty) { | |
| 810 // If the URL contains user information use that for basic | |
| 811 // authorization. | |
| 812 String auth = | |
| 813 _CryptoUtils.bytesToBase64(UTF8.encode(uri.userInfo)); | |
| 814 request.headers.set(HttpHeaders.AUTHORIZATION, "Basic $auth"); | |
| 815 } | |
|
kustermann
2015/02/06 13:13:46
Maybe move this before the `if (headers != null) {
Søren Gjesse
2015/02/09 10:38:59
Good point, done.
| |
| 812 return request.close(); | 816 return request.close(); |
| 813 }) | 817 }) |
| 814 .then((response) { | 818 .then((response) { |
| 815 void error(String message) { | 819 void error(String message) { |
| 816 // Flush data. | 820 // Flush data. |
| 817 response.detachSocket().then((socket) { | 821 response.detachSocket().then((socket) { |
| 818 socket.destroy(); | 822 socket.destroy(); |
| 819 }); | 823 }); |
| 820 throw new WebSocketException(message); | 824 throw new WebSocketException(message); |
| 821 } | 825 } |
| (...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1012 (code < WebSocketStatus.NORMAL_CLOSURE || | 1016 (code < WebSocketStatus.NORMAL_CLOSURE || |
| 1013 code == WebSocketStatus.RESERVED_1004 || | 1017 code == WebSocketStatus.RESERVED_1004 || |
| 1014 code == WebSocketStatus.NO_STATUS_RECEIVED || | 1018 code == WebSocketStatus.NO_STATUS_RECEIVED || |
| 1015 code == WebSocketStatus.ABNORMAL_CLOSURE || | 1019 code == WebSocketStatus.ABNORMAL_CLOSURE || |
| 1016 (code > WebSocketStatus.INTERNAL_SERVER_ERROR && | 1020 (code > WebSocketStatus.INTERNAL_SERVER_ERROR && |
| 1017 code < WebSocketStatus.RESERVED_1015) || | 1021 code < WebSocketStatus.RESERVED_1015) || |
| 1018 (code >= WebSocketStatus.RESERVED_1015 && | 1022 (code >= WebSocketStatus.RESERVED_1015 && |
| 1019 code < 3000)); | 1023 code < 3000)); |
| 1020 } | 1024 } |
| 1021 } | 1025 } |
| OLD | NEW |