-
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 PrimitiveOps behaviour and default impl
- Loading branch information
Showing
8 changed files
with
136 additions
and
104 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,10 @@ | ||
defmodule Bandit.PrimitiveOps do | ||
@moduledoc """ | ||
Primitive operations behaviour | ||
""" | ||
|
||
@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() | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
defmodule Bandit.PrimitiveOps.Default do | ||
@moduledoc """ | ||
Default implementation of `Bandit.PrimitiveOps` | ||
""" | ||
|
||
@behaviour Bandit.PrimitiveOps | ||
|
||
# Note that masking is an involution, so we don't need a separate unmask function | ||
@impl Bandit.PrimitiveOps | ||
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.