OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 import 'dart:async'; | 5 import 'dart:async'; |
| 6 import 'dart:convert'; |
6 | 7 |
7 import 'package:async/async.dart'; | 8 import 'package:async/async.dart'; |
8 import 'package:crypto/crypto.dart'; | 9 import 'package:crypto/crypto.dart'; |
9 import 'package:stream_channel/stream_channel.dart'; | 10 import 'package:stream_channel/stream_channel.dart'; |
10 | 11 |
11 import '../copy/web_socket_impl.dart'; | 12 import '../copy/web_socket_impl.dart'; |
12 | 13 |
13 /// This class is deprecated. | 14 /// This class is deprecated. |
14 /// | 15 /// |
15 /// Use the [`web_socket_channel`][web_socket_channel] package instead. | 16 /// Use the [`web_socket_channel`][web_socket_channel] package instead. |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 WebSocketSink get sink => new WebSocketSink._(_webSocket); | 62 WebSocketSink get sink => new WebSocketSink._(_webSocket); |
62 | 63 |
63 /// Signs a `Sec-WebSocket-Key` header sent by a WebSocket client as part of | 64 /// Signs a `Sec-WebSocket-Key` header sent by a WebSocket client as part of |
64 /// the [initial handshake]. | 65 /// the [initial handshake]. |
65 /// | 66 /// |
66 /// The return value should be sent back to the client in a | 67 /// The return value should be sent back to the client in a |
67 /// `Sec-WebSocket-Accept` header. | 68 /// `Sec-WebSocket-Accept` header. |
68 /// | 69 /// |
69 /// [initial handshake]: https://tools.ietf.org/html/rfc6455#section-4.2.2 | 70 /// [initial handshake]: https://tools.ietf.org/html/rfc6455#section-4.2.2 |
70 static String signKey(String key) { | 71 static String signKey(String key) { |
71 var hash = new SHA1(); | |
72 // We use [codeUnits] here rather than UTF-8-decoding the string because | 72 // We use [codeUnits] here rather than UTF-8-decoding the string because |
73 // [key] is expected to be base64 encoded, and so will be pure ASCII. | 73 // [key] is expected to be base64 encoded, and so will be pure ASCII. |
74 hash.add((key + webSocketGUID).codeUnits); | 74 return BASE64.encode(sha1.convert((key + webSocketGUID).codeUnits).bytes); |
75 return CryptoUtils.bytesToBase64(hash.close()); | |
76 } | 75 } |
77 | 76 |
78 /// Creates a new WebSocket handling messaging across an existing socket. | 77 /// Creates a new WebSocket handling messaging across an existing socket. |
79 /// | 78 /// |
80 /// Because this is HTTP-API-agnostic, the initial [WebSocket handshake][] | 79 /// Because this is HTTP-API-agnostic, the initial [WebSocket handshake][] |
81 /// must have already been completed on the socket before this is called. | 80 /// must have already been completed on the socket before this is called. |
82 /// | 81 /// |
83 /// If [stream] is also a [StreamSink] (for example, if it's a "dart:io" | 82 /// If [stream] is also a [StreamSink] (for example, if it's a "dart:io" |
84 /// `Socket`), it will be used for both sending and receiving data. Otherwise, | 83 /// `Socket`), it will be used for both sending and receiving data. Otherwise, |
85 /// it will be used for receiving data and [sink] will be used for sending it. | 84 /// it will be used for receiving data and [sink] will be used for sending it. |
(...skipping 27 matching lines...) Expand all Loading... |
113 /// | 112 /// |
114 /// [closeCode] and [closeReason] are the [close code][] and [reason][] sent | 113 /// [closeCode] and [closeReason] are the [close code][] and [reason][] sent |
115 /// to the remote peer, respectively. If they are omitted, the peer will see | 114 /// to the remote peer, respectively. If they are omitted, the peer will see |
116 /// a "no status received" code with no reason. | 115 /// a "no status received" code with no reason. |
117 /// | 116 /// |
118 /// [close code]: https://tools.ietf.org/html/rfc6455#section-7.1.5 | 117 /// [close code]: https://tools.ietf.org/html/rfc6455#section-7.1.5 |
119 /// [reason]: https://tools.ietf.org/html/rfc6455#section-7.1.6 | 118 /// [reason]: https://tools.ietf.org/html/rfc6455#section-7.1.6 |
120 Future close([int closeCode, String closeReason]) => | 119 Future close([int closeCode, String closeReason]) => |
121 _webSocket.close(closeCode, closeReason); | 120 _webSocket.close(closeCode, closeReason); |
122 } | 121 } |
OLD | NEW |