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 /** | 7 /** |
8 * WebSocket status codes used when closing a WebSocket connection. | 8 * WebSocket status codes used when closing a WebSocket connection. |
9 */ | 9 */ |
10 abstract class WebSocketStatus { | 10 abstract class WebSocketStatus { |
11 static const int NORMAL_CLOSURE = 1000; | 11 static const int NORMAL_CLOSURE = 1000; |
12 static const int GOING_AWAY = 1001; | 12 static const int GOING_AWAY = 1001; |
13 static const int PROTOCOL_ERROR = 1002; | 13 static const int PROTOCOL_ERROR = 1002; |
14 static const int UNSUPPORTED_DATA = 1003; | 14 static const int UNSUPPORTED_DATA = 1003; |
15 static const int RESERVED_1004 = 1004; | 15 static const int RESERVED_1004 = 1004; |
16 static const int NO_STATUS_RECEIVED = 1005; | 16 static const int NO_STATUS_RECEIVED = 1005; |
17 static const int ABNORMAL_CLOSURE = 1006; | 17 static const int ABNORMAL_CLOSURE = 1006; |
18 static const int INVALID_FRAME_PAYLOAD_DATA = 1007; | 18 static const int INVALID_FRAME_PAYLOAD_DATA = 1007; |
19 static const int POLICY_VIOLATION = 1008; | 19 static const int POLICY_VIOLATION = 1008; |
20 static const int MESSAGE_TOO_BIG = 1009; | 20 static const int MESSAGE_TOO_BIG = 1009; |
21 static const int MISSING_MANDATORY_EXTENSION = 1010; | 21 static const int MISSING_MANDATORY_EXTENSION = 1010; |
22 static const int INTERNAL_SERVER_ERROR = 1011; | 22 static const int INTERNAL_SERVER_ERROR = 1011; |
23 static const int RESERVED_1015 = 1015; | 23 static const int RESERVED_1015 = 1015; |
24 } | 24 } |
25 | 25 |
26 /** | 26 /** |
27 * The [CompressionOptions] class allows you to control | |
28 * the options of WebSocket compression. | |
29 */ | |
30 class CompressionOptions { | |
31 /** | |
32 * Default WebSocket Compression options. | |
33 * Compression will be enabled with the following options: | |
34 * clientNoContextTakeover: false | |
35 * serverNoContextTakeover: false | |
36 * clientMaxWindowBits: 15 | |
37 * serverMaxWindowBits: 15 | |
38 */ | |
39 static const CompressionOptions DEFAULT = const CompressionOptions(); | |
40 | |
41 /** | |
42 * Disables WebSocket Compression. | |
43 */ | |
44 static const CompressionOptions OFF = | |
45 const CompressionOptions(enabled: false); | |
46 | |
47 /** | |
48 * Control whether the client will reuse it's compression instances. | |
49 */ | |
50 final bool clientNoContextTakeover; | |
51 | |
52 /** | |
53 * Control whether the server will reuse it's compression instances. | |
54 */ | |
55 final bool serverNoContextTakeover; | |
56 | |
57 /** | |
58 * Sets the Max Window Bits for the Client. | |
59 */ | |
60 final int clientMaxWindowBits; | |
61 | |
62 /** | |
63 * Sets the Max Window Bits for the Server. | |
64 */ | |
65 final int serverMaxWindowBits; | |
66 | |
67 /** | |
68 * Enables or disables WebSocket compression. | |
69 */ | |
70 final bool enabled; | |
71 | |
72 const CompressionOptions( | |
73 {this.clientNoContextTakeover: false, | |
74 this.serverNoContextTakeover: false, | |
75 this.clientMaxWindowBits, | |
76 this.serverMaxWindowBits, | |
77 this.enabled: true}); | |
78 | |
79 /** | |
80 * Create a Compression Header | |
Søren Gjesse
2015/08/24 08:21:50
Please add more documentation here on what request
| |
81 */ | |
82 List _createHeader([List<String> requested]) { | |
Søren Gjesse
2015/08/24 08:21:50
Pass HeaderValue here and use its parameters (see
| |
83 if (!enabled) { | |
84 return ["", 0]; | |
85 } | |
86 | |
87 var info = new List(2); | |
88 | |
89 var header = _WebSocketImpl.PER_MESSAGE_DEFLATE; | |
90 | |
91 if (clientNoContextTakeover && | |
92 (requested != null && | |
93 requested.contains("client_no_context_takeover"))) { | |
Søren Gjesse
2015/08/24 08:21:50
Please make the values "server_no_context_takeover
| |
94 header += "; client_no_context_takeover"; | |
95 } | |
96 | |
97 if (serverNoContextTakeover && | |
98 (requested != null && | |
99 requested.contains("server_no_context_takeover"))) { | |
100 header += "; server_no_context_takeover"; | |
101 } | |
102 | |
103 if (requested != null && | |
104 requested.any((x) => x.startsWith("server_max_window_bits="))) { | |
Søren Gjesse
2015/08/24 08:21:50
This check is fragile, there can be WS on both sid
| |
105 var part = requested | |
106 .firstWhere((x) => x.startsWith("server_max_window_bits=")) | |
107 .substring(23); | |
108 var mwb = serverMaxWindowBits == null | |
109 ? int.parse(part, | |
Søren Gjesse
2015/08/24 08:21:50
According to https://tools.ietf.org/html/draft-iet
| |
110 onError: (source) => _WebSocketImpl.DEFAULT_WINDOW_BITS) | |
111 : serverMaxWindowBits; | |
Søren Gjesse
2015/08/24 08:21:50
You also need to check for the allowed range 8-15.
| |
112 header += "; server_max_window_bits=${mwb}"; | |
113 info[1] = mwb; | |
114 } else { | |
115 info[1] = _WebSocketImpl.DEFAULT_WINDOW_BITS; | |
116 } | |
117 | |
118 if (requested == null) { | |
119 header += "; client_max_window_bits"; | |
120 } else { | |
121 if (requested.contains("client_max_window_bits")) { | |
122 var myMaxWindowBits = info[1]; | |
123 header += "; client_max_window_bits=${myMaxWindowBits}"; | |
124 } | |
125 } | |
126 | |
127 info[0] = header; | |
128 | |
129 return info; | |
130 } | |
131 } | |
132 | |
133 /** | |
27 * The [WebSocketTransformer] provides the ability to upgrade a | 134 * The [WebSocketTransformer] provides the ability to upgrade a |
28 * [HttpRequest] to a [WebSocket] connection. It supports both | 135 * [HttpRequest] to a [WebSocket] connection. It supports both |
29 * upgrading a single [HttpRequest] and upgrading a stream of | 136 * upgrading a single [HttpRequest] and upgrading a stream of |
30 * [HttpRequest]s. | 137 * [HttpRequest]s. |
31 * | 138 * |
32 * To upgrade a single [HttpRequest] use the static [upgrade] method. | 139 * To upgrade a single [HttpRequest] use the static [upgrade] method. |
33 * | 140 * |
34 * HttpServer server; | 141 * HttpServer server; |
35 * server.listen((request) { | 142 * server.listen((request) { |
36 * if (...) { | 143 * if (...) { |
37 * WebSocketTransformer.upgrade(request).then((websocket) { | 144 * WebSocketTransformer.upgrade(request).then((websocket) { |
38 * ... | 145 * ... |
39 * }); | 146 * }); |
40 * } else { | 147 * } else { |
41 * // Do normal HTTP request processing. | 148 * // Do normal HTTP request processing. |
42 * } | 149 * } |
43 * }); | 150 * }); |
44 * | 151 * |
45 * To transform a stream of [HttpRequest] events as it implements a | 152 * To transform a stream of [HttpRequest] events as it implements a |
46 * stream transformer that transforms a stream of HttpRequest into a | 153 * stream transformer that transforms a stream of HttpRequest into a |
47 * stream of WebSockets by upgrading each HttpRequest from the HTTP or | 154 * stream of WebSockets by upgrading each HttpRequest from the HTTP or |
48 * HTTPS server, to the WebSocket protocol. | 155 * HTTPS server, to the WebSocket protocol. |
49 * | 156 * |
50 * server.transform(new WebSocketTransformer()).listen((webSocket) => ...); | 157 * server.transform(new WebSocketTransformer()).listen((webSocket) => ...); |
51 * | 158 * |
52 * This transformer strives to implement WebSockets as specified by RFC6455. | 159 * This transformer strives to implement WebSockets as specified by RFC6455. |
53 */ | 160 */ |
54 abstract class WebSocketTransformer | 161 abstract class WebSocketTransformer |
55 implements StreamTransformer<HttpRequest, WebSocket> { | 162 implements StreamTransformer<HttpRequest, WebSocket> { |
56 | |
57 /** | 163 /** |
58 * Create a new [WebSocketTransformer]. | 164 * Create a new [WebSocketTransformer]. |
59 * | 165 * |
60 * If [protocolSelector] is provided, [protocolSelector] will be called to | 166 * If [protocolSelector] is provided, [protocolSelector] will be called to |
61 * select what protocol to use, if any were provided by the client. | 167 * select what protocol to use, if any were provided by the client. |
62 * [protocolSelector] is should return either a [String] or a [Future] | 168 * [protocolSelector] is should return either a [String] or a [Future] |
63 * completing with a [String]. The [String] must exist in the list of | 169 * completing with a [String]. The [String] must exist in the list of |
64 * protocols. | 170 * protocols. |
65 */ | 171 */ |
66 factory WebSocketTransformer({protocolSelector(List<String> protocols)}) | 172 factory WebSocketTransformer( |
67 => new _WebSocketTransformerImpl(protocolSelector); | 173 {protocolSelector(List<String> protocols), |
174 CompressionOptions compression: CompressionOptions.DEFAULT}) => | |
175 new _WebSocketTransformerImpl(protocolSelector, compression); | |
68 | 176 |
69 /** | 177 /** |
70 * Upgrades a [HttpRequest] to a [WebSocket] connection. If the | 178 * Upgrades a [HttpRequest] to a [WebSocket] connection. If the |
71 * request is not a valid WebSocket upgrade request an HTTP response | 179 * request is not a valid WebSocket upgrade request an HTTP response |
72 * with status code 500 will be returned. Otherwise the returned | 180 * with status code 500 will be returned. Otherwise the returned |
73 * future will complete with the [WebSocket] when the upgrade pocess | 181 * future will complete with the [WebSocket] when the upgrade pocess |
74 * is complete. | 182 * is complete. |
75 * | 183 * |
76 * If [protocolSelector] is provided, [protocolSelector] will be called to | 184 * If [protocolSelector] is provided, [protocolSelector] will be called to |
77 * select what protocol to use, if any were provided by the client. | 185 * select what protocol to use, if any were provided by the client. |
78 * [protocolSelector] is should return either a [String] or a [Future] | 186 * [protocolSelector] is should return either a [String] or a [Future] |
79 * completing with a [String]. The [String] must exist in the list of | 187 * completing with a [String]. The [String] must exist in the list of |
80 * protocols. | 188 * protocols. |
81 */ | 189 */ |
82 static Future<WebSocket> upgrade(HttpRequest request, | 190 static Future<WebSocket> upgrade(HttpRequest request, |
83 {protocolSelector(List<String> protocols)}) { | 191 {protocolSelector(List<String> protocols), |
84 return _WebSocketTransformerImpl._upgrade(request, protocolSelector); | 192 CompressionOptions compression: CompressionOptions.DEFAULT}) { |
193 return _WebSocketTransformerImpl._upgrade( | |
194 request, protocolSelector, compression); | |
85 } | 195 } |
86 | 196 |
87 /** | 197 /** |
88 * Checks whether the request is a valid WebSocket upgrade request. | 198 * Checks whether the request is a valid WebSocket upgrade request. |
89 */ | 199 */ |
90 static bool isUpgradeRequest(HttpRequest request) { | 200 static bool isUpgradeRequest(HttpRequest request) { |
91 return _WebSocketTransformerImpl._isUpgradeRequest(request); | 201 return _WebSocketTransformerImpl._isUpgradeRequest(request); |
92 } | 202 } |
93 } | 203 } |
94 | 204 |
95 | |
96 /** | 205 /** |
97 * A two-way HTTP communication object for client or server applications. | 206 * A two-way HTTP communication object for client or server applications. |
98 * | 207 * |
99 * The stream exposes the messages received. A text message will be of type | 208 * The stream exposes the messages received. A text message will be of type |
100 * [:String:] and a binary message will be of type [:List<int>:]. | 209 * [:String:] and a binary message will be of type [:List<int>:]. |
101 */ | 210 */ |
102 abstract class WebSocket implements Stream, StreamSink { | 211 abstract class WebSocket implements Stream, StreamSink { |
103 /** | 212 /** |
104 * Possible states of the connection. | 213 * Possible states of the connection. |
105 */ | 214 */ |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
145 * - `sec-websocket-protocol` | 254 * - `sec-websocket-protocol` |
146 * - `sec-websocket-version` | 255 * - `sec-websocket-version` |
147 * - `upgrade` | 256 * - `upgrade` |
148 * | 257 * |
149 * If any of these are passed in the `headers` map they will be ignored. | 258 * If any of these are passed in the `headers` map they will be ignored. |
150 * | 259 * |
151 * If the `url` contains user information this will be passed as basic | 260 * If the `url` contains user information this will be passed as basic |
152 * authentication when setting up the connection. | 261 * authentication when setting up the connection. |
153 */ | 262 */ |
154 static Future<WebSocket> connect(String url, | 263 static Future<WebSocket> connect(String url, |
155 {Iterable<String> protocols, | 264 {Iterable<String> protocols, |
156 Map<String, dynamic> headers}) => | 265 Map<String, dynamic> headers, |
157 _WebSocketImpl.connect(url, protocols, headers); | 266 CompressionOptions compression: CompressionOptions.DEFAULT}) => |
267 _WebSocketImpl.connect(url, protocols, headers, compression: compression); | |
158 | 268 |
159 @Deprecated('This constructor will be removed in Dart 2.0. Use `implements`' | 269 @Deprecated('This constructor will be removed in Dart 2.0. Use `implements`' |
160 ' instead of `extends` if implementing this abstract class.') | 270 ' instead of `extends` if implementing this abstract class.') |
161 WebSocket(); | 271 WebSocket(); |
162 | 272 |
163 /** | 273 /** |
164 * Creates a WebSocket from an already-upgraded socket. | 274 * Creates a WebSocket from an already-upgraded socket. |
165 * | 275 * |
166 * The initial WebSocket handshake must have occurred prior to this call. A | 276 * The initial WebSocket handshake must have occurred prior to this call. A |
167 * WebSocket client can automatically perform the handshake using | 277 * WebSocket client can automatically perform the handshake using |
168 * [WebSocket.connect], while a server can do so using | 278 * [WebSocket.connect], while a server can do so using |
169 * [WebSocketTransformer.upgrade]. To manually upgrade an [HttpRequest], | 279 * [WebSocketTransformer.upgrade]. To manually upgrade an [HttpRequest], |
170 * [HttpRequest.detachSocket] may be called. | 280 * [HttpRequest.detachSocket] may be called. |
171 * | 281 * |
172 * [protocol] should be the protocol negotiated by this handshake, if any. | 282 * [protocol] should be the protocol negotiated by this handshake, if any. |
173 * | 283 * |
174 * [serverSide] must be passed explicitly. If it's `false`, the WebSocket will | 284 * [serverSide] must be passed explicitly. If it's `false`, the WebSocket will |
175 * act as the client and mask the messages it sends. If it's `true`, it will | 285 * act as the client and mask the messages it sends. If it's `true`, it will |
176 * act as the server and will not mask its messages. | 286 * act as the server and will not mask its messages. |
177 */ | 287 */ |
178 factory WebSocket.fromUpgradedSocket(Socket socket, {String protocol, | 288 factory WebSocket.fromUpgradedSocket(Socket socket, |
179 bool serverSide}) { | 289 {String protocol, |
290 bool serverSide, | |
291 CompressionOptions compression: CompressionOptions.DEFAULT}) { | |
180 if (serverSide == null) { | 292 if (serverSide == null) { |
181 throw new ArgumentError("The serverSide argument must be passed " | 293 throw new ArgumentError("The serverSide argument must be passed " |
182 "explicitly to WebSocket.fromUpgradedSocket."); | 294 "explicitly to WebSocket.fromUpgradedSocket."); |
183 } | 295 } |
184 return new _WebSocketImpl._fromSocket(socket, protocol, serverSide); | 296 return new _WebSocketImpl._fromSocket( |
297 socket, protocol, compression, serverSide); | |
185 } | 298 } |
186 | 299 |
187 /** | 300 /** |
188 * Returns the current state of the connection. | 301 * Returns the current state of the connection. |
189 */ | 302 */ |
190 int get readyState; | 303 int get readyState; |
191 | 304 |
192 /** | 305 /** |
193 * The extensions property is initially the empty string. After the | 306 * The extensions property is initially the empty string. After the |
194 * WebSocket connection is established this string reflects the | 307 * WebSocket connection is established this string reflects the |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
231 void add(data); | 344 void add(data); |
232 | 345 |
233 /** | 346 /** |
234 * Sends data from a stream on WebSocket connection. Each data event from | 347 * Sends data from a stream on WebSocket connection. Each data event from |
235 * [stream] will be send as a single WebSocket frame. The data from [stream] | 348 * [stream] will be send as a single WebSocket frame. The data from [stream] |
236 * must be either [:String:]s, or [:List<int>:]s holding bytes. | 349 * must be either [:String:]s, or [:List<int>:]s holding bytes. |
237 */ | 350 */ |
238 Future addStream(Stream stream); | 351 Future addStream(Stream stream); |
239 } | 352 } |
240 | 353 |
241 | |
242 class WebSocketException implements IOException { | 354 class WebSocketException implements IOException { |
243 final String message; | 355 final String message; |
356 | |
244 const WebSocketException([this.message = ""]); | 357 const WebSocketException([this.message = ""]); |
358 | |
245 String toString() => "WebSocketException: $message"; | 359 String toString() => "WebSocketException: $message"; |
246 } | 360 } |
OLD | NEW |