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 751 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
762 Duration _pingInterval; | 762 Duration _pingInterval; |
763 Timer _pingTimer; | 763 Timer _pingTimer; |
764 _WebSocketConsumer _consumer; | 764 _WebSocketConsumer _consumer; |
765 | 765 |
766 int _outCloseCode; | 766 int _outCloseCode; |
767 String _outCloseReason; | 767 String _outCloseReason; |
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(String url, List<String> protocols) { | 772 static Future<WebSocket> connect( |
773 String url, List<String> protocols, Function onRequest) { | |
773 Uri uri = Uri.parse(url); | 774 Uri uri = Uri.parse(url); |
774 if (uri.scheme != "ws" && uri.scheme != "wss") { | 775 if (uri.scheme != "ws" && uri.scheme != "wss") { |
775 throw new WebSocketException("Unsupported URL scheme '${uri.scheme}'"); | 776 throw new WebSocketException("Unsupported URL scheme '${uri.scheme}'"); |
776 } | 777 } |
777 if (uri.userInfo != "") { | 778 if (uri.userInfo != "") { |
778 throw new WebSocketException("Unsupported user info '${uri.userInfo}'"); | 779 throw new WebSocketException("Unsupported user info '${uri.userInfo}'"); |
779 } | 780 } |
780 | 781 |
781 Random random = new Random(); | 782 Random random = new Random(); |
782 // Generate 16 random bytes. | 783 // Generate 16 random bytes. |
(...skipping 10 matching lines...) Expand all Loading... | |
793 path: uri.path, | 794 path: uri.path, |
794 query: uri.query, | 795 query: uri.query, |
795 fragment: uri.fragment); | 796 fragment: uri.fragment); |
796 return _httpClient.openUrl("GET", uri) | 797 return _httpClient.openUrl("GET", uri) |
797 .then((request) { | 798 .then((request) { |
798 // Setup the initial handshake. | 799 // Setup the initial handshake. |
799 request.headers | 800 request.headers |
800 ..add(HttpHeaders.CONNECTION, "Upgrade") | 801 ..add(HttpHeaders.CONNECTION, "Upgrade") |
801 ..set(HttpHeaders.UPGRADE, "websocket") | 802 ..set(HttpHeaders.UPGRADE, "websocket") |
802 ..set("Sec-WebSocket-Key", nonce) | 803 ..set("Sec-WebSocket-Key", nonce) |
803 ..set("Cache-Control", "no-cache") | |
kustermann
2015/01/09 14:45:12
Why was this removed?
Søren Gjesse
2015/01/12 15:36:34
I thought it was not needed as a proxy should neve
| |
804 ..set("Sec-WebSocket-Version", "13"); | 804 ..set("Sec-WebSocket-Version", "13"); |
805 if (protocols.isNotEmpty) { | 805 if (protocols.isNotEmpty) { |
806 request.headers.add("Sec-WebSocket-Protocol", protocols); | 806 request.headers.add("Sec-WebSocket-Protocol", protocols); |
807 } | 807 } |
808 return request.close(); | 808 if (onRequest != null) { |
809 var result = onRequest(request); | |
810 return result is Future | |
Lasse Reichstein Nielsen
2015/01/12 15:11:56
Parentheses around (result is Future) expression (
Søren Gjesse
2015/01/12 15:36:34
Dropped this code.
| |
811 ? result.then((_) => request.close()) | |
812 : request.close(); | |
813 } else { | |
814 return request.close(); | |
815 } | |
Lasse Reichstein Nielsen
2015/01/12 15:11:56
Rewrite as:
if (onRequest != null) {
var result
Søren Gjesse
2015/01/12 15:36:34
Dropped this code.
| |
809 }) | 816 }) |
810 .then((response) { | 817 .then((response) { |
811 void error(String message) { | 818 void error(String message) { |
812 // Flush data. | 819 // Flush data. |
813 response.detachSocket().then((socket) { | 820 response.detachSocket().then((socket) { |
814 socket.destroy(); | 821 socket.destroy(); |
815 }); | 822 }); |
816 throw new WebSocketException(message); | 823 throw new WebSocketException(message); |
817 } | 824 } |
818 if (response.statusCode != HttpStatus.SWITCHING_PROTOCOLS || | 825 if (response.statusCode != HttpStatus.SWITCHING_PROTOCOLS || |
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1008 (code < WebSocketStatus.NORMAL_CLOSURE || | 1015 (code < WebSocketStatus.NORMAL_CLOSURE || |
1009 code == WebSocketStatus.RESERVED_1004 || | 1016 code == WebSocketStatus.RESERVED_1004 || |
1010 code == WebSocketStatus.NO_STATUS_RECEIVED || | 1017 code == WebSocketStatus.NO_STATUS_RECEIVED || |
1011 code == WebSocketStatus.ABNORMAL_CLOSURE || | 1018 code == WebSocketStatus.ABNORMAL_CLOSURE || |
1012 (code > WebSocketStatus.INTERNAL_SERVER_ERROR && | 1019 (code > WebSocketStatus.INTERNAL_SERVER_ERROR && |
1013 code < WebSocketStatus.RESERVED_1015) || | 1020 code < WebSocketStatus.RESERVED_1015) || |
1014 (code >= WebSocketStatus.RESERVED_1015 && | 1021 (code >= WebSocketStatus.RESERVED_1015 && |
1015 code < 3000)); | 1022 code < 3000)); |
1016 } | 1023 } |
1017 } | 1024 } |
OLD | NEW |