Skip to content

Commit

Permalink
Move into websocket options
Browse files Browse the repository at this point in the history
  • Loading branch information
alisinabh committed Nov 15, 2024
1 parent 8dc4348 commit 053ee99
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 60 deletions.
4 changes: 2 additions & 2 deletions lib/bandit.ex
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,9 @@ defmodule Bandit do

@top_level_keys ~w(plug scheme port ip keyfile certfile otp_app cipher_suite display_plug startup_log thousand_island_options http_options http_1_options http_2_options websocket_options)a
@http_keys ~w(compress deflate_options log_exceptions_with_status_codes log_protocol_errors log_client_closures)a
@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 primitive_ops_module)a
@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
@http_2_keys ~w(enabled max_header_block_size max_requests default_local_settings)a
@websocket_keys ~w(enabled max_frame_size validate_text_frames compress)a
@websocket_keys ~w(enabled max_frame_size validate_text_frames compress primitive_ops_module)a
@thousand_island_keys ThousandIsland.ServerConfig.__struct__()
|> Map.from_struct()
|> Map.keys()
Expand Down
2 changes: 1 addition & 1 deletion lib/bandit/extractor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ defmodule Bandit.Extractor do
@spec new(module(), Keyword.t()) :: t()
def new(frame_parser, opts) do
max_frame_size = Keyword.get(opts, :max_frame_size, 0)
primitive_ops_module = Keyword.get(opts, :primitive_ops_module) || Bandit.PrimitiveOps.Default
primitive_ops_module = Keyword.fetch!(opts, :primitive_ops_module)

%__MODULE__{
max_frame_size: max_frame_size,
Expand Down
10 changes: 0 additions & 10 deletions lib/bandit/primitive_ops.ex

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
defmodule Bandit.PrimitiveOps.Default do
defmodule Bandit.PrimitiveOps.WebSocket do
@moduledoc """
Default implementation of `Bandit.PrimitiveOps`
WebSocket primitive operations behaviour and default implementation
"""

@behaviour Bandit.PrimitiveOps
@doc """
WebSocket masking according to [RFC6455§5.3](https://www.rfc-editor.org/rfc/rfc6455#section-5.3)
"""
@callback ws_mask(payload :: binary(), mask :: integer()) :: binary()

@behaviour __MODULE__

# Note that masking is an involution, so we don't need a separate unmask function
@impl Bandit.PrimitiveOps
@impl true
def ws_mask(payload, mask)
when is_binary(payload) and is_integer(mask) and mask >= 0x00000000 and mask <= 0xFFFFFFFF do
ws_mask(<<>>, payload, mask)
Expand Down
2 changes: 1 addition & 1 deletion lib/bandit/websocket/handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ defmodule Bandit.WebSocket.Handler do
connection_opts =
state.opts.websocket
|> Keyword.merge(connection_opts)
|> Keyword.put(:primitive_ops_module, Keyword.get(state.opts.http_1, :primitive_ops_module))
|> Keyword.put_new(:primitive_ops_module, Bandit.PrimitiveOps.WebSocket)

state =
state
Expand Down
Loading

0 comments on commit 053ee99

Please sign in to comment.