Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(312)

Side by Side Diff: lib/src/channel.dart

Issue 1734773002: Copy the web socket stuff from http_parser. (Closed) Base URL: git@github.com:dart-lang/web_socket_channel.git@master
Patch Set: Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « codereview.settings ('k') | lib/src/copy/bytes_builder.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 import 'dart:async';
6
7 import 'package:async/async.dart';
8 import 'package:crypto/crypto.dart';
9 import 'package:stream_channel/stream_channel.dart';
10
11 import 'copy/web_socket_impl.dart';
12
13 /// A [StreamChannel] that communicates over a WebSocket.
14 ///
15 /// This is implemented by classes that use `dart:io` and `dart:html`. The [new
16 /// WebSocketChannel] constructor can also be used on any platform to connect to
17 /// use the WebSocket protocol over a pre-existing channel.
18 ///
19 /// All implementations emit [WebSocketChannelException]s. These exceptions wrap
20 /// the native exception types where possible.
21 class WebSocketChannel extends StreamChannelMixin {
22 /// The underlying web socket.
23 ///
24 /// This is essentially a copy of `dart:io`'s WebSocket implementation, with
25 /// the IO-specific pieces factored out.
26 final WebSocketImpl _webSocket;
27
28 /// The interval for sending ping signals.
29 ///
30 /// If a ping message is not answered by a pong message from the peer, the
31 /// `WebSocket` is assumed disconnected and the connection is closed with a
32 /// [WebSocketStatus.GOING_AWAY] close code. When a ping signal is sent, the
33 /// pong message must be received within [pingInterval].
34 ///
35 /// There are never two outstanding pings at any given time, and the next ping
36 /// timer starts when the pong is received.
37 ///
38 /// By default, the [pingInterval] is `null`, indicating that ping messages
39 /// are disabled. Some implementations may not support setting it.
40 Duration get pingInterval => _webSocket.pingInterval;
41 set pingInterval(Duration value) => _webSocket.pingInterval = value;
42
43 /// The subprotocol selected by the server.
44 ///
45 /// For a client socket, this is initially `null`. After the WebSocket
46 /// connection is established the value is set to the subprotocol selected by
47 /// the server. If no subprotocol is negotiated the value will remain `null`.
48 String get protocol => _webSocket.protocol;
49
50 /// The [close code][] set when the WebSocket connection is closed.
51 ///
52 /// [close code]: https://tools.ietf.org/html/rfc6455#section-7.1.5
53 ///
54 /// Before the connection has been closed, this will be `null`.
55 int get closeCode => _webSocket.closeCode;
56
57 /// The [close reason][] set when the WebSocket connection is closed.
58 ///
59 /// [close reason]: https://tools.ietf.org/html/rfc6455#section-7.1.6
60 ///
61 /// Before the connection has been closed, this will be `null`.
62 String get closeReason => _webSocket.closeReason;
63
64 Stream get stream => new StreamView(_webSocket);
65
66 /// The sink for sending values to the other endpoint.
67 ///
68 /// This supports additional arguments to [WebSocketSink.close] that provide
69 /// the remote endpoint reasons for closing the connection.
70 WebSocketSink get sink => new WebSocketSink._(_webSocket);
71
72 /// Signs a `Sec-WebSocket-Key` header sent by a WebSocket client as part of
73 /// the [initial handshake].
74 ///
75 /// The return value should be sent back to the client in a
76 /// `Sec-WebSocket-Accept` header.
77 ///
78 /// [initial handshake]: https://tools.ietf.org/html/rfc6455#section-4.2.2
79 static String signKey(String key) {
80 var hash = new SHA1();
81 // We use [codeUnits] here rather than UTF-8-decoding the string because
82 // [key] is expected to be base64 encoded, and so will be pure ASCII.
83 hash.add((key + webSocketGUID).codeUnits);
84 return CryptoUtils.bytesToBase64(hash.close());
85 }
86
87 /// Creates a new WebSocket handling messaging across an existing [channel].
88 ///
89 /// This is a cross-platform constructor; it doesn't use either `dart:io` or
90 /// `dart:html`. It's also HTTP-API-agnostic, which means that the initial
91 /// [WebSocket handshake][] must have already been completed on the socket
92 /// before this is called.
93 ///
94 /// [protocol] should be the protocol negotiated by this handshake, if any.
95 ///
96 /// If this is a WebSocket server, [serverSide] should be `true` (the
97 /// default); if it's a client, [serverSide] should be `false`.
98 ///
99 /// [WebSocket handshake]: https://tools.ietf.org/html/rfc6455#section-4
100 WebSocketChannel(StreamChannel<List<int>> channel,
101 {String protocol, bool serverSide: true})
102 : _webSocket = new WebSocketImpl.fromSocket(
103 channel.stream, channel.sink, protocol, serverSide);
104 }
105
106 /// The sink exposed by a [WebSocketChannel].
107 ///
108 /// This is like a normal [StreamSink], except that it supports extra arguments
109 /// to [close].
110 class WebSocketSink extends DelegatingStreamSink {
111 final WebSocketImpl _webSocket;
112
113 WebSocketSink._(WebSocketImpl webSocket)
114 : super(webSocket),
115 _webSocket = webSocket;
116
117 /// Closes the web socket connection.
118 ///
119 /// [closeCode] and [closeReason] are the [close code][] and [reason][] sent
120 /// to the remote peer, respectively. If they are omitted, the peer will see
121 /// a "no status received" code with no reason.
122 ///
123 /// [close code]: https://tools.ietf.org/html/rfc6455#section-7.1.5
124 /// [reason]: https://tools.ietf.org/html/rfc6455#section-7.1.6
125 Future close([int closeCode, String closeReason]) =>
126 _webSocket.close(closeCode, closeReason);
127 }
OLDNEW
« no previous file with comments | « codereview.settings ('k') | lib/src/copy/bytes_builder.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698