Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions guides/websocket_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ hackney:ws_close(Conn).
| `protocols` | `[]` | Sec-WebSocket-Protocol values |
| `active` | `false` | Active mode: false, true, once |
| `ssl_options` | `[]` | SSL options for wss:// |
| `max_frame_size` | 16 MiB | Maximum frame payload (bytes) |
| `max_message_size` | 64 MiB | Maximum message size across fragments (bytes) |

## Sending Messages

Expand Down
17 changes: 16 additions & 1 deletion src/hackney.erl
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,9 @@ resume_stream(ConnPid) when is_pid(ConnPid) ->
%% <li>recv_timeout: Receive timeout in ms (default infinity)</li>
%% <li>connect_options: Options passed to transport connect</li>
%% <li>ssl_options: Additional SSL options</li>
%% <li>max_frame_size: Maximum frame payload in bytes (default 16 MiB)</li>
%% <li>max_message_size: Maximum message size in bytes across fragments
%% (default 64 MiB)</li>
%% </ul>
%%
%% Returns `{ok, WsPid}' on success, where WsPid is the hackney_ws process.
Expand Down Expand Up @@ -941,7 +944,7 @@ ws_connect(URL, Options) when is_binary(URL) orelse is_list(URL) ->
ProxyConfig = get_ws_proxy_config(Scheme, Host, Options),

%% Build connection options
WsOpts = #{
WsOpts0 = #{
host => Host,
port => Port,
transport => Transport,
Expand All @@ -956,6 +959,18 @@ ws_connect(URL, Options) when is_binary(URL) orelse is_list(URL) ->
proxy => ProxyConfig
},

%% Frame and message limits keep the hackney_ws defaults unless given
WsOpts = lists:foldl(
fun(Key, Acc) ->
case proplists:lookup(Key, Options) of
{Key, Value} -> Acc#{Key => Value};
none -> Acc
end
end,
WsOpts0,
[max_frame_size, max_message_size]
),

%% Start WebSocket process and connect
case hackney_ws:start_link(WsOpts) of
{ok, WsPid} ->
Expand Down
7 changes: 7 additions & 0 deletions test/hackney_ws_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ ws_integration_test_() ->
fun() -> test_handshake_header_injection(Port) end},
{"GHSA-q8jg: oversized frame rejected",
fun() -> test_max_frame_size(Port) end},
{"ws_connect passes max_frame_size",
fun() -> test_ws_connect_max_frame_size(WsUrl) end},
{"Send and receive text message",
fun() -> test_text_message(WsUrl) end},
{"Send and receive binary message",
Expand Down Expand Up @@ -146,6 +148,11 @@ test_max_frame_size(Port) ->
ok = hackney_ws:send(Ws, {text, <<"large:5000">>}),
?assertMatch({error, {frame_too_big, _}}, hackney_ws:recv(Ws, 5000)).

test_ws_connect_max_frame_size(WsUrl) ->
{ok, Ws} = hackney:ws_connect(WsUrl, [{max_frame_size, 100}]),
ok = hackney:ws_send(Ws, {text, <<"large:5000">>}),
?assertMatch({error, {frame_too_big, _}}, hackney:ws_recv(Ws, 5000)).

test_connect_disconnect(WsUrl) ->
{ok, Ws} = hackney:ws_connect(WsUrl),
?assert(is_pid(Ws)),
Expand Down