0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-14 12:20:24 +00:00

Websocket: Allow setting the max_frame_size option dynamically

This can be used to limit the maximum frame size before
some authentication or other validation is completed.
This commit is contained in:
Loïc Hoguin 2025-01-16 14:40:37 +01:00
parent 818b448ae9
commit 81de580aee
No known key found for this signature in database
GPG key ID: 8A9DF795F6FED764
4 changed files with 65 additions and 14 deletions

View file

@ -11,10 +11,21 @@ init(Req, RunOrHibernate) ->
{cowboy_websocket, Req, RunOrHibernate,
#{idle_timeout => infinity}}.
websocket_handle(Frame={text, <<"idle_timeout_short">>}, State=run) ->
{[{set_options, #{idle_timeout => 500}}, Frame], State};
websocket_handle(Frame={text, <<"idle_timeout_short">>}, State=hibernate) ->
{[{set_options, #{idle_timeout => 500}}, Frame], State, hibernate}.
%% Set the idle_timeout option dynamically.
websocket_handle({text, <<"idle_timeout_short">>}, State=run) ->
{[{set_options, #{idle_timeout => 500}}], State};
websocket_handle({text, <<"idle_timeout_short">>}, State=hibernate) ->
{[{set_options, #{idle_timeout => 500}}], State, hibernate};
%% Set the max_frame_size option dynamically.
websocket_handle({text, <<"max_frame_size_small">>}, State=run) ->
{[{set_options, #{max_frame_size => 1000}}], State};
websocket_handle({text, <<"max_frame_size_small">>}, State=hibernate) ->
{[{set_options, #{max_frame_size => 1000}}], State, hibernate};
%% We just echo binary frames.
websocket_handle(Frame={binary, _}, State=run) ->
{[Frame], State};
websocket_handle(Frame={binary, _}, State=hibernate) ->
{[Frame], State, hibernate}.
websocket_info(_Info, State) ->
{[], State}.