Skip to content

Commit c027b9a

Browse files
committed
allow close frames to have no close code or reason
1 parent a8f1085 commit c027b9a

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ The format is based on [Keep a
66
Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to
77
[Semantic Versioning](https://semver.org/spec/v2.0.0.html).
88

9+
## 0.1.1 - 2021-07-01
10+
11+
### Fixed
12+
13+
- Close frame codes and reasons are now nillable instead of defaulted
14+
- The WebSocket spec does not require that a code and reason be included
15+
for all close frames
16+
917
## 0.1.0 - 2021-06-30
1018

1119
### Added

lib/mint/web_socket.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ defmodule Mint.WebSocket do
135135
136136
* `:ping` - shorthand for `{:ping, ""}`
137137
* `:pong` - shorthand for `{:pong, ""}`
138-
* `:close` - shorthand for `{:close, 1_000, ""}`
138+
* `:close` - shorthand for `{:close, nil, nil}`
139139
140140
These may be passed to `encode/2`.
141141
@@ -192,7 +192,7 @@ defmodule Mint.WebSocket do
192192
| {:binary, binary()}
193193
| {:ping, binary()}
194194
| {:pong, binary()}
195-
| {:close, code :: non_neg_integer(), reason :: binary()}
195+
| {:close, code :: non_neg_integer() | nil, reason :: binary() | nil}
196196

197197
@doc """
198198
Requests that a connection be upgraded to the WebSocket protocol

lib/mint/web_socket/frame.ex

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ defmodule Mint.WebSocket.Frame do
9292
>>
9393
end
9494

95+
defp payload(close(code: nil, reason: nil)) do
96+
<<>>
97+
end
98+
9599
defp payload(close(code: code, reason: reason)) do
96100
code = code || 1_000
97101
reason = reason || ""
@@ -381,17 +385,14 @@ defmodule Mint.WebSocket.Frame do
381385
def translate(pong(data: data)), do: {:pong, data}
382386

383387
def translate(:close) do
384-
translate({:close, 1_000, ""})
388+
translate({:close, nil, nil})
385389
end
386390

387-
def translate({:close, code, reason})
388-
when is_integer(code) and is_binary(reason) do
391+
def translate({:close, code, reason}) do
389392
close(mask: new_mask(), code: code, reason: reason, data: <<>>)
390393
end
391394

392395
def translate(close(code: code, reason: reason)) do
393-
code = code || 1_000
394-
reason = reason || ""
395396
{:close, code, reason}
396397
end
397398

0 commit comments

Comments
 (0)