-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathframe.ex
493 lines (402 loc) · 14.9 KB
/
frame.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
defmodule Mint.WebSocket.Frame do
@moduledoc false
# Functions and data structures for describing websocket frames.
# https://tools.ietf.org/html/rfc6455#section-5.2
import Record
alias Mint.WebSocket.{Utils, Extension}
alias Mint.WebSocketError
@compile {:inline, apply_mask: 2, apply_mask: 3}
shared = [{:reserved, <<0::size(3)>>}, :mask, :data, :fin?]
defrecord :continuation, shared
defrecord :text, shared
defrecord :binary, shared
# > All control frames MUST have a payload length of 125 bytes or less
# > and MUST NOT be fragmented.
defrecord :close, shared ++ [:code, :reason]
defrecord :ping, shared
defrecord :pong, shared
@typep continuation_frame() ::
record(:continuation,
reserved: <<_::3>>,
mask: binary(),
data: binary(),
fin?: boolean()
)
@type text_frame() ::
record(:text, reserved: <<_::3>>, mask: binary(), data: binary(), fin?: boolean())
@type binary_frame() ::
record(:binary, reserved: <<_::3>>, mask: binary(), data: binary(), fin?: boolean())
@type close_frame() ::
record(:close,
reserved: <<_::3>>,
mask: binary(),
data: binary(),
fin?: boolean(),
code: binary(),
reason: binary()
)
@type ping_frame() ::
record(:ping, reserved: <<_::3>>, mask: binary(), data: binary(), fin?: boolean())
@type pong_frame() ::
record(:pong, reserved: <<_::3>>, mask: binary(), data: binary(), fin?: boolean())
@type frame_record() ::
continuation_frame()
| text_frame()
| binary_frame()
| close_frame()
| ping_frame()
| pong_frame()
defguard is_control(frame)
when is_tuple(frame) and
(elem(frame, 0) == :close or elem(frame, 0) == :ping or elem(frame, 0) == :pong)
defguard is_fin(frame) when elem(frame, 4) == true
# guards frames dealt with in the user-space (not records)
defguardp is_friendly_frame(frame)
when frame in [:ping, :pong, :close] or
(is_tuple(frame) and elem(frame, 0) in [:text, :binary, :ping, :pong] and
is_binary(elem(frame, 1))) or
(is_tuple(frame) and elem(frame, 0) == :close and is_integer(elem(frame, 1)) and
is_binary(elem(frame, 2)))
# https://tools.ietf.org/html/rfc6455#section-7.4.1
@invalid_status_codes [1_004, 1_005, 1_006, 1_016, 1_100, 2_000, 2_999]
# https://tools.ietf.org/html/rfc6455#section-7.4.2
defguardp is_valid_close_code(code)
when code in 1_000..4_999 and code not in @invalid_status_codes
@opcodes %{
# non-control opcodes:
continuation: <<0x0::size(4)>>,
text: <<0x1::size(4)>>,
binary: <<0x2::size(4)>>,
# 0x3-7 reserved for future non-control frames
# control opcodes:
close: <<0x8::size(4)>>,
ping: <<0x9::size(4)>>,
pong: <<0xA::size(4)>>
# 0xB-F reserved for future control frames
}
@reverse_opcodes Map.new(@opcodes, fn {k, v} -> {v, k} end)
@non_control_opcodes [:continuation, :text, :binary]
def opcodes, do: Map.keys(@opcodes)
def new_mask, do: :crypto.strong_rand_bytes(4)
@spec encode(Mint.WebSocket.t(), Mint.WebSocket.shorthand_frame() | Mint.WebSocket.frame()) ::
{:ok, Mint.WebSocket.t(), bitstring()}
| {:error, Mint.WebSocket.t(), WebSocketError.t()}
def encode(websocket, frame) when is_friendly_frame(frame) do
{frame, extensions} =
frame
|> translate()
|> Extension.encode(websocket.extensions)
websocket = put_in(websocket.extensions, extensions)
frame = encode_to_binary(frame)
{:ok, websocket, frame}
catch
:throw, {:mint, reason} -> {:error, websocket, reason}
end
@spec encode_to_binary(frame_record()) :: bitstring()
defp encode_to_binary(frame) do
payload = payload(frame)
mask = mask(frame)
masked? = if mask == nil, do: 0, else: 1
encoded_payload_length = encode_payload_length(elem(frame, 0), byte_size(payload))
<<
encode_fin(frame)::bitstring,
reserved(frame)::bitstring,
encode_opcode(frame)::bitstring,
masked?::size(1),
encoded_payload_length::bitstring,
mask || <<>>::binary,
apply_mask(payload, mask)::bitstring
>>
end
defp payload(close(code: nil, reason: nil)) do
<<>>
end
defp payload(close(code: code, reason: reason)) do
code = code || 1_000
reason = reason || ""
<<code::unsigned-integer-size(8)-unit(2), reason::binary>>
end
for type <- Map.keys(@opcodes) -- [:close] do
defp payload(unquote(type)(data: data)), do: data
end
for type <- Map.keys(@opcodes) do
defp mask(unquote(type)(mask: mask)), do: mask
defp reserved(unquote(type)(reserved: reserved)), do: reserved
end
defp encode_fin(text(fin?: false)), do: <<0b0::size(1)>>
defp encode_fin(binary(fin?: false)), do: <<0b0::size(1)>>
defp encode_fin(continuation(fin?: false)), do: <<0b0::size(1)>>
defp encode_fin(_), do: <<0b1::size(1)>>
defp encode_opcode(frame), do: @opcodes[elem(frame, 0)]
def encode_payload_length(_opcode, length) when length in 0..125 do
<<length::integer-size(7)>>
end
def encode_payload_length(opcode, length)
when length in 126..65_535 and opcode in @non_control_opcodes do
<<126::integer-size(7), length::unsigned-integer-size(8)-unit(2)>>
end
def encode_payload_length(opcode, length)
when length in 65_535..9_223_372_036_854_775_807 and opcode in @non_control_opcodes do
<<127::integer-size(7), length::unsigned-integer-size(8)-unit(8)>>
end
def encode_payload_length(_opcode, _length) do
throw({:mint, %WebSocketError{reason: :payload_too_large}})
end
# Mask the payload by bytewise XOR-ing the payload bytes against the mask
# bytes (where the mask bytes repeat).
# This is an "involution" function: applying the mask will mask
# the data and applying the mask again will unmask it.
def apply_mask(payload, mask, acc \\ <<>>)
def apply_mask(payload, nil, _acc), do: payload
# n=4 is the happy path
# n=3..1 catches cases where the remaining byte_size/1 of the payload is shorter
# than the mask
# MINOR: We use a literal list instead of `4..1` to avoid compilation warnings on
# Elixir 1.17+ and instead of `4..1//-1` to maintain compatibility with older
# Elixir versions that do not support the range-step syntax.
for n <- [4, 3, 2, 1] do
def apply_mask(
<<part_key::integer-size(8)-unit(unquote(n)), payload_rest::binary>>,
<<mask_key::integer-size(8)-unit(unquote(n)), _::binary>> = mask,
acc
) do
apply_mask(
payload_rest,
mask,
<<acc::binary, :erlang.bxor(mask_key, part_key)::integer-size(8)-unit(unquote(n))>>
)
end
end
def apply_mask(<<>>, _mask, acc), do: acc
@spec decode(Mint.WebSocket.t(), binary()) ::
{:ok, Mint.WebSocket.t(), [Mint.WebSocket.frame() | {:error, term()}]}
| {:error, Mint.WebSocket.t(), any()}
def decode(websocket, data) do
{websocket, frames} = binary_to_frames(websocket, data)
{websocket, frames} =
Enum.reduce(frames, {websocket, []}, fn
{:error, reason}, {websocket, acc} ->
{websocket, [{:error, reason} | acc]}
frame, {websocket, acc} ->
{frame, extensions} = Extension.decode(frame, websocket.extensions)
{put_in(websocket.extensions, extensions), [translate(frame) | acc]}
end)
{:ok, websocket, :lists.reverse(frames)}
catch
{:mint, reason} -> {:error, websocket, reason}
end
defp binary_to_frames(websocket, data) do
case websocket.buffer |> Utils.maybe_concat(data) |> decode_raw(websocket, []) do
{:ok, frames} ->
{websocket, frames} = resolve_fragments(websocket, frames)
{put_in(websocket.buffer, <<>>), frames}
{:buffer, partial, frames} ->
{websocket, frames} = resolve_fragments(websocket, frames)
{put_in(websocket.buffer, partial), frames}
end
end
defp decode_raw(
<<fin::size(1), reserved::bitstring-size(3), opcode::bitstring-size(4), masked::size(1),
payload_and_mask::bitstring>> = data,
websocket,
acc
) do
case decode_payload_and_mask(payload_and_mask, masked == 0b1) do
{:ok, payload, mask, rest} ->
frame = decode_full_frame_binary(opcode, fin, reserved, mask, payload)
decode_raw(rest, websocket, [frame | acc])
{:error, reason} ->
{:ok, :lists.reverse([{:error, reason} | acc])}
:buffer ->
{:buffer, data, :lists.reverse(acc)}
end
end
defp decode_raw(<<>>, _websocket, acc), do: {:ok, :lists.reverse(acc)}
defp decode_raw(partial, _websocket, acc) when is_binary(partial) do
{:buffer, partial, :lists.reverse(acc)}
end
defp decode_payload_and_mask(payload, masked?) do
with {:ok, payload_length, rest} <- decode_payload_length(payload),
{:ok, mask, rest} <- decode_mask(rest, masked?),
<<payload::binary-size(payload_length), more::bitstring>> <- rest do
{:ok, payload, mask, more}
else
partial when is_binary(partial) -> :buffer
:buffer -> :buffer
{:error, reason} -> {:error, reason}
end
end
defp decode_full_frame_binary(opcode, fin, reserved, mask, payload) do
with {:ok, opcode} <- decode_opcode(opcode) do
into_frame(
opcode,
_fin? = fin == 0b1,
reserved,
mask,
apply_mask(payload, mask)
)
end
end
defp decode_opcode(opcode) do
with :error <- Map.fetch(@reverse_opcodes, opcode) do
{:error, {:unsupported_opcode, opcode}}
end
end
defp decode_payload_length(
<<127::integer-size(7), payload_length::unsigned-integer-size(8)-unit(8),
rest::bitstring>>
),
do: {:ok, payload_length, rest}
defp decode_payload_length(<<127::integer-size(7), _rest::bitstring>>), do: :buffer
defp decode_payload_length(
<<126::integer-size(7), payload_length::unsigned-integer-size(8)-unit(2),
rest::bitstring>>
),
do: {:ok, payload_length, rest}
defp decode_payload_length(<<126::integer-size(7), _rest::bitstring>>), do: :buffer
defp decode_payload_length(<<payload_length::integer-size(7), rest::bitstring>>)
when payload_length in 0..125,
do: {:ok, payload_length, rest}
defp decode_payload_length(malformed) do
{:error, {:malformed_payload_length, malformed}}
end
defp decode_mask(payload, masked?)
defp decode_mask(<<mask::binary-size(8)-unit(4), rest::bitstring>>, true) do
{:ok, mask, rest}
end
defp decode_mask(payload, false) do
{:ok, nil, payload}
end
defp decode_mask(payload, _masked?) do
{:error, {:missing_mask, payload}}
end
for data_type <- [:continuation, :text, :binary, :ping, :pong] do
def into_frame(unquote(data_type), fin?, reserved, mask, payload) do
unquote(data_type)(
fin?: fin?,
reserved: reserved,
mask: mask,
data: payload
)
end
end
def into_frame(
:close,
fin?,
reserved,
mask,
<<code::unsigned-integer-size(8)-unit(2), reason::binary>> = payload
)
when byte_size(reason) in 0..123 and is_valid_close_code(code) do
if String.valid?(reason) do
close(reserved: reserved, mask: mask, code: code, reason: reason, fin?: fin?)
else
{:error, {:invalid_close_payload, payload}}
end
end
def into_frame(
:close,
fin?,
reserved,
mask,
<<>>
) do
close(reserved: reserved, mask: mask, code: 1_000, reason: "", fin?: fin?)
end
def into_frame(
:close,
_fin?,
_reserved,
_mask,
payload
) do
{:error, {:invalid_close_payload, payload}}
end
# translate from user-friendly tuple into record defined in this module
# (and the reverse)
@spec translate(Mint.WebSocket.frame() | Mint.WebSocket.shorthand_frame()) :: tuple()
for opcode <- Map.keys(@opcodes) do
def translate(unquote(opcode)(reserved: <<reserved::bitstring>>))
when reserved != <<0::size(3)>> do
{:error, {:malformed_reserved, reserved}}
end
end
def translate({:error, reason}), do: {:error, reason}
def translate({:text, text}) do
text(fin?: true, mask: new_mask(), data: text)
end
def translate(text(fin?: true, data: data)) do
if String.valid?(data) do
{:text, data}
else
{:error, {:invalid_utf8, data}}
end
end
def translate({:binary, binary}) do
binary(fin?: true, mask: new_mask(), data: binary)
end
def translate(binary(fin?: true, data: data)), do: {:binary, data}
def translate(:ping), do: translate({:ping, <<>>})
def translate({:ping, body}) do
ping(mask: new_mask(), data: body)
end
def translate(ping(data: data)), do: {:ping, data}
def translate(:pong), do: translate({:pong, <<>>})
def translate({:pong, body}) do
pong(mask: new_mask(), data: body)
end
def translate(pong(data: data)), do: {:pong, data}
def translate(:close) do
translate({:close, nil, nil})
end
def translate({:close, code, reason}) do
close(mask: new_mask(), code: code, reason: reason, data: <<>>)
end
def translate(close(code: code, reason: reason)) do
{:close, code, reason}
end
def translate(continuation()) do
{:error, :unexpected_continuation}
end
@doc """
Emits frames for any finalized fragments and stores any unfinalized fragments
in the `:fragment` key in the websocket data structure
"""
def resolve_fragments(websocket, frames, acc \\ [])
def resolve_fragments(websocket, [], acc) do
{websocket, :lists.reverse(acc)}
end
def resolve_fragments(websocket, [{:error, reason} | rest], acc) do
resolve_fragments(websocket, rest, [{:error, reason} | acc])
end
def resolve_fragments(websocket, [frame | rest], acc)
when is_control(frame) and is_fin(frame) do
resolve_fragments(websocket, rest, [frame | acc])
end
def resolve_fragments(websocket, [frame | rest], acc) when is_fin(frame) do
frame = combine(websocket.fragment, frame)
put_in(websocket.fragment, nil)
|> resolve_fragments(rest, [frame | acc])
end
def resolve_fragments(websocket, [frame | rest], acc) do
case combine(websocket.fragment, frame) do
{:error, reason} ->
put_in(websocket.fragment, nil)
|> resolve_fragments(rest, [{:error, reason} | acc])
frame ->
put_in(websocket.fragment, frame)
|> resolve_fragments(rest, acc)
end
end
defp combine(nil, continuation(fin?: true)), do: {:error, :insular_continuation}
defp combine(nil, frame), do: frame
for type <- [:continuation, :text, :binary] do
defp combine(
unquote(type)(data: frame_data) = frame,
continuation(data: continuation_data, fin?: fin?)
) do
unquote(type)(frame, data: Utils.maybe_concat(frame_data, continuation_data), fin?: fin?)
end
end
defp combine(a, b), do: {:error, {:cannot_combine_frames, a, b}}
end