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 = const CompressionOptions(enabled: false) ; | |
Søren Gjesse
2015/06/25 15:41:26
Long line, see https://www.dartlang.org/articles/s
| |
45 | |
46 /** | |
47 * Control whether the client will reuse it's compression instances. | |
48 */ | |
49 final bool clientNoContextTakeover; | |
50 | |
51 /** | |
52 * Control whether the server will reuse it's compression instances. | |
53 */ | |
54 final bool serverNoContextTakeover; | |
55 | |
56 /** | |
57 * Sets the Max Window Bits for the Client. | |
58 */ | |
59 final int clientMaxWindowBits; | |
60 | |
61 /** | |
62 * Sets the Max Window Bits for the Server. | |
63 */ | |
64 final int serverMaxWindowBits; | |
65 | |
66 /** | |
67 * Enables or disables WebSocket compression. | |
68 */ | |
69 final bool enabled; | |
70 | |
71 const CompressionOptions({this.clientNoContextTakeover: false, | |
Søren Gjesse
2015/06/25 15:41:26
Please reformat the arguments, see https://www.dar
| |
72 this.serverNoContextTakeover: false, this.clientMaxWindowBits, | |
73 this.serverMaxWindowBits, this.enabled: true}); | |
74 | |
75 /** | |
76 * Create a Compression Header | |
77 */ | |
78 String _createHeader([List<String> requested]) { | |
79 if (!enabled) { | |
80 return ""; | |
81 } | |
82 | |
83 var header = "permessage-deflate"; | |
84 | |
85 if (requested == null) { | |
86 header += "; client_max_window_bits"; | |
87 } else { | |
88 if (requested.contains("client_max_window_bits")) { | |
89 var myMaxWindowBits = clientMaxWindowBits == null ? 15 : clientMaxWindow Bits; | |
Søren Gjesse
2015/06/25 15:41:26
Long line.
| |
90 header += "; client_max_window_bits=${myMaxWindowBits}"; | |
91 } | |
92 } | |
93 | |
94 if (clientNoContextTakeover && (requested != null | |
Søren Gjesse
2015/06/25 15:41:27
Please look at https://www.dartlang.org/articles/s
| |
95 && requested.contains("client_no_context_takeover"))) { | |
96 header += "; client_no_context_takeover"; | |
97 } | |
98 | |
99 if (serverNoContextTakeover && (requested != null | |
100 && requested.contains("server_no_context_takeover"))) { | |
101 header += "; server_no_context_takeover"; | |
102 } | |
103 | |
104 if (requested != null) { | |
105 var mwb = serverMaxWindowBits == null ? 15 : serverMaxWindowBits; | |
106 header += "; server_max_window_bits=${mwb}"; | |
107 } | |
108 | |
109 return header; | |
110 } | |
111 } | |
112 | |
113 /** | |
27 * The [WebSocketTransformer] provides the ability to upgrade a | 114 * The [WebSocketTransformer] provides the ability to upgrade a |
28 * [HttpRequest] to a [WebSocket] connection. It supports both | 115 * [HttpRequest] to a [WebSocket] connection. It supports both |
29 * upgrading a single [HttpRequest] and upgrading a stream of | 116 * upgrading a single [HttpRequest] and upgrading a stream of |
30 * [HttpRequest]s. | 117 * [HttpRequest]s. |
31 * | 118 * |
32 * To upgrade a single [HttpRequest] use the static [upgrade] method. | 119 * To upgrade a single [HttpRequest] use the static [upgrade] method. |
33 * | 120 * |
34 * HttpServer server; | 121 * HttpServer server; |
35 * server.listen((request) { | 122 * server.listen((request) { |
36 * if (...) { | 123 * if (...) { |
(...skipping 19 matching lines...) Expand all Loading... | |
56 | 143 |
57 /** | 144 /** |
58 * Create a new [WebSocketTransformer]. | 145 * Create a new [WebSocketTransformer]. |
59 * | 146 * |
60 * If [protocolSelector] is provided, [protocolSelector] will be called to | 147 * If [protocolSelector] is provided, [protocolSelector] will be called to |
61 * select what protocol to use, if any were provided by the client. | 148 * select what protocol to use, if any were provided by the client. |
62 * [protocolSelector] is should return either a [String] or a [Future] | 149 * [protocolSelector] is should return either a [String] or a [Future] |
63 * completing with a [String]. The [String] must exist in the list of | 150 * completing with a [String]. The [String] must exist in the list of |
64 * protocols. | 151 * protocols. |
65 */ | 152 */ |
66 factory WebSocketTransformer({protocolSelector(List<String> protocols)}) | 153 factory WebSocketTransformer({protocolSelector(List<String> protocols), |
67 => new _WebSocketTransformerImpl(protocolSelector); | 154 CompressionOptions compression: CompressionOptions.DEFAULT}) |
155 => new _WebSocketTransformerImpl(protocolSelector, compression); | |
68 | 156 |
69 /** | 157 /** |
70 * Upgrades a [HttpRequest] to a [WebSocket] connection. If the | 158 * Upgrades a [HttpRequest] to a [WebSocket] connection. If the |
71 * request is not a valid WebSocket upgrade request an HTTP response | 159 * request is not a valid WebSocket upgrade request an HTTP response |
72 * with status code 500 will be returned. Otherwise the returned | 160 * with status code 500 will be returned. Otherwise the returned |
73 * future will complete with the [WebSocket] when the upgrade pocess | 161 * future will complete with the [WebSocket] when the upgrade pocess |
74 * is complete. | 162 * is complete. |
75 * | 163 * |
76 * If [protocolSelector] is provided, [protocolSelector] will be called to | 164 * If [protocolSelector] is provided, [protocolSelector] will be called to |
77 * select what protocol to use, if any were provided by the client. | 165 * select what protocol to use, if any were provided by the client. |
78 * [protocolSelector] is should return either a [String] or a [Future] | 166 * [protocolSelector] is should return either a [String] or a [Future] |
79 * completing with a [String]. The [String] must exist in the list of | 167 * completing with a [String]. The [String] must exist in the list of |
80 * protocols. | 168 * protocols. |
81 */ | 169 */ |
82 static Future<WebSocket> upgrade(HttpRequest request, | 170 static Future<WebSocket> upgrade(HttpRequest request, |
83 {protocolSelector(List<String> protocols)}) { | 171 {protocolSelector(List<String> protocols), |
84 return _WebSocketTransformerImpl._upgrade(request, protocolSelector); | 172 CompressionOptions compression: CompressionO ptions.DEFAULT}) { |
173 return _WebSocketTransformerImpl._upgrade(request, protocolSelector, compres sion); | |
85 } | 174 } |
86 | 175 |
87 /** | 176 /** |
88 * Checks whether the request is a valid WebSocket upgrade request. | 177 * Checks whether the request is a valid WebSocket upgrade request. |
89 */ | 178 */ |
90 static bool isUpgradeRequest(HttpRequest request) { | 179 static bool isUpgradeRequest(HttpRequest request) { |
91 return _WebSocketTransformerImpl._isUpgradeRequest(request); | 180 return _WebSocketTransformerImpl._isUpgradeRequest(request); |
92 } | 181 } |
93 } | 182 } |
94 | 183 |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
169 * [WebSocketTransformer.upgrade]. To manually upgrade an [HttpRequest], | 258 * [WebSocketTransformer.upgrade]. To manually upgrade an [HttpRequest], |
170 * [HttpRequest.detachSocket] may be called. | 259 * [HttpRequest.detachSocket] may be called. |
171 * | 260 * |
172 * [protocol] should be the protocol negotiated by this handshake, if any. | 261 * [protocol] should be the protocol negotiated by this handshake, if any. |
173 * | 262 * |
174 * [serverSide] must be passed explicitly. If it's `false`, the WebSocket will | 263 * [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 | 264 * 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. | 265 * act as the server and will not mask its messages. |
177 */ | 266 */ |
178 factory WebSocket.fromUpgradedSocket(Socket socket, {String protocol, | 267 factory WebSocket.fromUpgradedSocket(Socket socket, {String protocol, |
179 bool serverSide}) { | 268 bool serverSide, CompressionOptions compression: CompressionOptions.DEFA ULT}) { |
180 if (serverSide == null) { | 269 if (serverSide == null) { |
181 throw new ArgumentError("The serverSide argument must be passed " | 270 throw new ArgumentError("The serverSide argument must be passed " |
182 "explicitly to WebSocket.fromUpgradedSocket."); | 271 "explicitly to WebSocket.fromUpgradedSocket."); |
183 } | 272 } |
184 return new _WebSocketImpl._fromSocket(socket, protocol, serverSide); | 273 return new _WebSocketImpl._fromSocket(socket, protocol, compression, serverS ide); |
185 } | 274 } |
186 | 275 |
187 /** | 276 /** |
188 * Returns the current state of the connection. | 277 * Returns the current state of the connection. |
189 */ | 278 */ |
190 int get readyState; | 279 int get readyState; |
191 | 280 |
192 /** | 281 /** |
193 * The extensions property is initially the empty string. After the | 282 * The extensions property is initially the empty string. After the |
194 * WebSocket connection is established this string reflects the | 283 * WebSocket connection is established this string reflects the |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
237 */ | 326 */ |
238 Future addStream(Stream stream); | 327 Future addStream(Stream stream); |
239 } | 328 } |
240 | 329 |
241 | 330 |
242 class WebSocketException implements IOException { | 331 class WebSocketException implements IOException { |
243 final String message; | 332 final String message; |
244 const WebSocketException([this.message = ""]); | 333 const WebSocketException([this.message = ""]); |
245 String toString() => "WebSocketException: $message"; | 334 String toString() => "WebSocketException: $message"; |
246 } | 335 } |
OLD | NEW |