Skip to content

Commit 4a24bda

Browse files
proximamtrudel
andauthored
Configurable deflate options for websockets (#540)
* Configurable deflate options for websockets * Update deflate_options documentation for clarity --------- Co-authored-by: Mat Trudel <[email protected]>
1 parent cc2a3f1 commit 4a24bda

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

lib/bandit.ex

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,16 +192,20 @@ defmodule Bandit do
192192
upgrade requests still need to set the `compress: true` option in `connection_opts` on
193193
a per-upgrade basis for compression to be negotiated (see 'WebSocket Support' section below
194194
for details). Defaults to `true`
195+
* `deflate_options`: A keyword list of options to set on the deflate library when using the
196+
per-message deflate extension. A complete list can be found at `t:deflate_options/0`.
197+
`window_bits` is currently ignored and left to negotiation.
195198
"""
196199
@type websocket_options :: [
197200
{:enabled, boolean()}
198201
| {:max_frame_size, pos_integer()}
199202
| {:validate_text_frames, boolean()}
200203
| {:compress, boolean()}
204+
| {:deflate_options, deflate_options()}
201205
]
202206

203207
@typedoc """
204-
Options to configure the deflate library used for HTTP compression
208+
Options to configure the deflate library used for HTTP and WebSocket compression
205209
"""
206210
@type deflate_options :: [
207211
{:level, :zlib.zlevel()}
@@ -234,7 +238,7 @@ defmodule Bandit do
234238
@http_keys ~w(compress deflate_options log_exceptions_with_status_codes log_protocol_errors log_client_closures)a
235239
@http_1_keys ~w(enabled max_request_line_length max_header_length max_header_count max_requests clear_process_dict gc_every_n_keepalive_requests log_unknown_messages)a
236240
@http_2_keys ~w(enabled max_header_block_size max_requests max_reset_stream_rate default_local_settings)a
237-
@websocket_keys ~w(enabled max_frame_size validate_text_frames compress primitive_ops_module)a
241+
@websocket_keys ~w(enabled max_frame_size validate_text_frames compress deflate_options primitive_ops_module)a
238242
@thousand_island_keys ThousandIsland.ServerConfig.__struct__()
239243
|> Map.from_struct()
240244
|> Map.keys()

lib/bandit/websocket/permessage_deflate.ex

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,16 @@ defmodule Bandit.WebSocket.PerMessageDeflate do
8787
inflate_context = :zlib.open()
8888
:ok = :zlib.inflateInit(inflate_context, fix_bits(-instance.client_max_window_bits))
8989
deflate_context = :zlib.open()
90+
deflate_opts = Keyword.get(opts, :deflate_options, [])
9091

9192
:ok =
9293
:zlib.deflateInit(
9394
deflate_context,
94-
Keyword.get(opts, :level, :default),
95+
Keyword.get(deflate_opts, :level, :default),
9596
:deflated,
9697
fix_bits(-instance.server_max_window_bits),
97-
Keyword.get(opts, :mem_level, 8),
98-
Keyword.get(opts, :strategy, :default)
98+
Keyword.get(deflate_opts, :mem_level, 8),
99+
Keyword.get(deflate_opts, :strategy, :default)
99100
)
100101

101102
%{instance | inflate_context: inflate_context, deflate_context: deflate_context}

0 commit comments

Comments
 (0)