diff --git a/lib/bandit/websocket/frame.ex b/lib/bandit/websocket/frame.ex index a4938003..3c047bb6 100644 --- a/lib/bandit/websocket/frame.ex +++ b/lib/bandit/websocket/frame.ex @@ -96,17 +96,19 @@ defmodule Bandit.WebSocket.Frame do defp mask_and_length(length), do: <<0::1, 127::7, length::64>> # Note that masking is an involution, so we don't need a separate unmask function - def mask(payload, mask, acc \\ <<>>) - - def mask(payload, mask, acc) when is_integer(mask), do: mask(payload, <>, acc) + def mask(payload, mask) do + payload + |> do_mask(<>, []) + |> IO.iodata_to_binary() + end - def mask(<>, <>, acc) do - mask(rest, mask, acc <> <>) + defp do_mask(<>, <> = mask, acc) do + do_mask(rest, mask, [acc, <>]) end - def mask(<>, <>, acc) do - mask(rest, <>, acc <> <>) + defp do_mask(<>, <>, acc) do + do_mask(rest, <>, [acc, <>]) end - def mask(<<>>, _mask, acc), do: acc + defp do_mask(<<>>, _mask, acc), do: acc end