-
Notifications
You must be signed in to change notification settings - Fork 89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for running optimized native code for websocket mask #394
Merged
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than the
put_new
elsewhere, is there a reason not to useKeyword.get
with a default ofBandit.PrimitiveOps.WebSocket
here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought we don't want to couple the Extractor module with websocket specifics (even though it is only used for websockets as of now).
But we can either do a
Keyword.get/3
as you suggested or add an argument to the new/3 so it looks likedef new(frame_parser, primitive_ops_module, opts)
. LMK what you prefer.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point about extractor reuse!
It's a required thing, so I think a separate arg to new/3 makes the most sense
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated in 028e890. Also do you think I should drop
_module
suffix fromprimitive_ops_module
? 🤔