-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for running optimized native code for websocket mask (#394)
* Add PrimitiveOps behaviour and default impl * Move into websocket options * Move primitive_ops_module in Extractor.new/3
- Loading branch information
Showing
7 changed files
with
132 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
defmodule Bandit.PrimitiveOps.WebSocket do | ||
@moduledoc """ | ||
WebSocket primitive operations behaviour and default implementation | ||
""" | ||
|
||
@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 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) | ||
end | ||
|
||
defp ws_mask(acc, <<h::32, rest::binary>>, mask) do | ||
ws_mask(<<acc::binary, (<<Bitwise.bxor(h, mask)::32>>)>>, rest, mask) | ||
end | ||
|
||
for size <- [24, 16, 8] do | ||
defp ws_mask(acc, <<h::unquote(size)>>, mask) do | ||
<<mask::unquote(size), _::binary>> = <<mask::32>> | ||
<<acc::binary, (<<Bitwise.bxor(h, mask)::unquote(size)>>)>> | ||
end | ||
end | ||
|
||
defp ws_mask(acc, <<>>, _mask) do | ||
acc | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.