diff --git a/src/hackney_ws.erl b/src/hackney_ws.erl index 275ce2a5..808e0dba 100644 --- a/src/hackney_ws.erl +++ b/src/hackney_ws.erl @@ -930,6 +930,11 @@ handle_active_data(SocketData, #ws_data{buffer = Buffer, owner = Owner, active = end, Frames), %% Handle active mode case Active of + once when Frames =:= [] -> + %% Only part of a frame has arrived: keep reading until + %% a whole frame can be delivered. + _ = set_socket_active(Data2, once), + {keep_state, Data2}; once -> _ = set_socket_active(Data2, false), {keep_state, Data2#ws_data{active = false}}; diff --git a/test/hackney_ws_tests.erl b/test/hackney_ws_tests.erl index 83e5e954..d0de5a8f 100644 --- a/test/hackney_ws_tests.erl +++ b/test/hackney_ws_tests.erl @@ -114,6 +114,8 @@ ws_integration_test_() -> fun() -> test_ping_pong(WsUrl) end}, {"Active mode once", fun() -> test_active_once(WsUrl) end}, + {"Active mode once with a frame split across reads", + fun() -> test_active_once_split_frame(WsUrl) end}, {"Active mode true", fun() -> test_active_true(WsUrl) end} ] @@ -209,6 +211,16 @@ test_ping_pong(WsUrl) -> ?assertMatch({ok, pong}, Result), hackney:ws_close(Ws). +test_active_once_split_frame(WsUrl) -> + {ok, Ws} = hackney:ws_connect(WsUrl, [{active, once}]), + ok = hackney:ws_send(Ws, {text, <<"large:1048576">>}), + receive + {hackney_ws, Ws, {binary, Bin}} -> ?assertEqual(1048576, byte_size(Bin)) + after 3000 -> + ?assert(false) + end, + hackney:ws_close(Ws). + test_active_once(WsUrl) -> {ok, Ws} = hackney:ws_connect(WsUrl, [{active, once}]),