Chromium Code Reviews| Index: sdk/lib/io/websocket.dart |
| diff --git a/sdk/lib/io/websocket.dart b/sdk/lib/io/websocket.dart |
| index 599e295c60ac92923dc620303830b986b8522f96..3a65685259c89df5d92999645ca84c3c1fa846d4 100644 |
| --- a/sdk/lib/io/websocket.dart |
| +++ b/sdk/lib/io/websocket.dart |
| @@ -24,6 +24,93 @@ abstract class WebSocketStatus { |
| } |
| /** |
| + * The [CompressionOptions] class allows you to control |
| + * the options of WebSocket compression. |
| + */ |
| +class CompressionOptions { |
| + /** |
| + * Default WebSocket Compression options. |
| + * Compression will be enabled with the following options: |
| + * clientNoContextTakeover: false |
| + * serverNoContextTakeover: false |
| + * clientMaxWindowBits: 15 |
| + * serverMaxWindowBits: 15 |
| + */ |
| + static const CompressionOptions DEFAULT = const CompressionOptions(); |
| + |
| + /** |
| + * Disables WebSocket Compression. |
| + */ |
| + 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
|
| + |
| + /** |
| + * Control whether the client will reuse it's compression instances. |
| + */ |
| + final bool clientNoContextTakeover; |
| + |
| + /** |
| + * Control whether the server will reuse it's compression instances. |
| + */ |
| + final bool serverNoContextTakeover; |
| + |
| + /** |
| + * Sets the Max Window Bits for the Client. |
| + */ |
| + final int clientMaxWindowBits; |
| + |
| + /** |
| + * Sets the Max Window Bits for the Server. |
| + */ |
| + final int serverMaxWindowBits; |
| + |
| + /** |
| + * Enables or disables WebSocket compression. |
| + */ |
| + final bool enabled; |
| + |
| + const CompressionOptions({this.clientNoContextTakeover: false, |
|
Søren Gjesse
2015/06/25 15:41:26
Please reformat the arguments, see https://www.dar
|
| + this.serverNoContextTakeover: false, this.clientMaxWindowBits, |
| + this.serverMaxWindowBits, this.enabled: true}); |
| + |
| + /** |
| + * Create a Compression Header |
| + */ |
| + String _createHeader([List<String> requested]) { |
| + if (!enabled) { |
| + return ""; |
| + } |
| + |
| + var header = "permessage-deflate"; |
| + |
| + if (requested == null) { |
| + header += "; client_max_window_bits"; |
| + } else { |
| + if (requested.contains("client_max_window_bits")) { |
| + var myMaxWindowBits = clientMaxWindowBits == null ? 15 : clientMaxWindowBits; |
|
Søren Gjesse
2015/06/25 15:41:26
Long line.
|
| + header += "; client_max_window_bits=${myMaxWindowBits}"; |
| + } |
| + } |
| + |
| + if (clientNoContextTakeover && (requested != null |
|
Søren Gjesse
2015/06/25 15:41:27
Please look at https://www.dartlang.org/articles/s
|
| + && requested.contains("client_no_context_takeover"))) { |
| + header += "; client_no_context_takeover"; |
| + } |
| + |
| + if (serverNoContextTakeover && (requested != null |
| + && requested.contains("server_no_context_takeover"))) { |
| + header += "; server_no_context_takeover"; |
| + } |
| + |
| + if (requested != null) { |
| + var mwb = serverMaxWindowBits == null ? 15 : serverMaxWindowBits; |
| + header += "; server_max_window_bits=${mwb}"; |
| + } |
| + |
| + return header; |
| + } |
| +} |
| + |
| +/** |
| * The [WebSocketTransformer] provides the ability to upgrade a |
| * [HttpRequest] to a [WebSocket] connection. It supports both |
| * upgrading a single [HttpRequest] and upgrading a stream of |
| @@ -63,8 +150,9 @@ abstract class WebSocketTransformer |
| * completing with a [String]. The [String] must exist in the list of |
| * protocols. |
| */ |
| - factory WebSocketTransformer({protocolSelector(List<String> protocols)}) |
| - => new _WebSocketTransformerImpl(protocolSelector); |
| + factory WebSocketTransformer({protocolSelector(List<String> protocols), |
| + CompressionOptions compression: CompressionOptions.DEFAULT}) |
| + => new _WebSocketTransformerImpl(protocolSelector, compression); |
| /** |
| * Upgrades a [HttpRequest] to a [WebSocket] connection. If the |
| @@ -80,8 +168,9 @@ abstract class WebSocketTransformer |
| * protocols. |
| */ |
| static Future<WebSocket> upgrade(HttpRequest request, |
| - {protocolSelector(List<String> protocols)}) { |
| - return _WebSocketTransformerImpl._upgrade(request, protocolSelector); |
| + {protocolSelector(List<String> protocols), |
| + CompressionOptions compression: CompressionOptions.DEFAULT}) { |
| + return _WebSocketTransformerImpl._upgrade(request, protocolSelector, compression); |
| } |
| /** |
| @@ -176,12 +265,12 @@ abstract class WebSocket implements Stream, StreamSink { |
| * act as the server and will not mask its messages. |
| */ |
| factory WebSocket.fromUpgradedSocket(Socket socket, {String protocol, |
| - bool serverSide}) { |
| + bool serverSide, CompressionOptions compression: CompressionOptions.DEFAULT}) { |
| if (serverSide == null) { |
| throw new ArgumentError("The serverSide argument must be passed " |
| "explicitly to WebSocket.fromUpgradedSocket."); |
| } |
| - return new _WebSocketImpl._fromSocket(socket, protocol, serverSide); |
| + return new _WebSocketImpl._fromSocket(socket, protocol, compression, serverSide); |
| } |
| /** |