diff --git a/guides/websocket_guide.md b/guides/websocket_guide.md
index 4bbd1386..2e3c591a 100644
--- a/guides/websocket_guide.md
+++ b/guides/websocket_guide.md
@@ -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
diff --git a/src/hackney.erl b/src/hackney.erl
index 50293976..6491fe66 100644
--- a/src/hackney.erl
+++ b/src/hackney.erl
@@ -906,6 +906,9 @@ resume_stream(ConnPid) when is_pid(ConnPid) ->
%%
recv_timeout: Receive timeout in ms (default infinity)
%% connect_options: Options passed to transport connect
%% ssl_options: Additional SSL options
+%% max_frame_size: Maximum frame payload in bytes (default 16 MiB)
+%% max_message_size: Maximum message size in bytes across fragments
+%% (default 64 MiB)
%%
%%
%% Returns `{ok, WsPid}' on success, where WsPid is the hackney_ws process.
@@ -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,
@@ -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} ->
diff --git a/test/hackney_ws_tests.erl b/test/hackney_ws_tests.erl
index 83e5e954..5f1b6c7b 100644
--- a/test/hackney_ws_tests.erl
+++ b/test/hackney_ws_tests.erl
@@ -100,6 +100,10 @@ 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},
+ {"ws_connect passes max_message_size",
+ fun() -> test_ws_connect_max_message_size() end},
{"Send and receive text message",
fun() -> test_text_message(WsUrl) end},
{"Send and receive binary message",
@@ -146,6 +150,45 @@ 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_ws_connect_max_message_size() ->
+ {ok, LSock} = gen_tcp:listen(0, [binary, {active, false}, {ip, {127, 0, 0, 1}}]),
+ {ok, Port} = inet:port(LSock),
+ Fragment = binary:copy(<<"x">>, 80),
+ Server = spawn_link(fun() ->
+ {ok, Sock} = gen_tcp:accept(LSock, 5000),
+ Request = recv_request(Sock, <<>>),
+ {match, [Key]} = re:run(Request, <<"(?i)sec-websocket-key: *([^\\r]+)">>,
+ [{capture, all_but_first, binary}]),
+ Accept = base64:encode(crypto:hash(sha, <>)),
+ ok = gen_tcp:send(Sock, [<<"HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\n"
+ "Connection: Upgrade\r\nSec-WebSocket-Accept: ">>, Accept, <<"\r\n\r\n">>,
+ <<0:1, 0:3, 1:4, 0:1, 80:7>>, Fragment,
+ <<0:1, 0:3, 0:4, 0:1, 80:7>>, Fragment]),
+ receive stop -> gen_tcp:close(Sock) end
+ end),
+ try
+ Url = <<"ws://127.0.0.1:", (integer_to_binary(Port))/binary, "/">>,
+ {ok, Ws} = hackney:ws_connect(Url, [{max_message_size, 100}]),
+ ?assertMatch({error, {message_too_big, 160}}, hackney:ws_recv(Ws, 1000))
+ after
+ Server ! stop,
+ gen_tcp:close(LSock)
+ end.
+
+recv_request(Sock, Acc) ->
+ case binary:match(Acc, <<"\r\n\r\n">>) of
+ nomatch ->
+ {ok, Data} = gen_tcp:recv(Sock, 0, 5000),
+ recv_request(Sock, <>);
+ _ ->
+ Acc
+ end.
+
test_connect_disconnect(WsUrl) ->
{ok, Ws} = hackney:ws_connect(WsUrl),
?assert(is_pid(Ws)),