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

Introduce the req_filter Websocket option

This option allows customizing the compacting of the Req object
when using Websocket. By default it will keep most public fields
excluding headers of course, since those can be large.
This commit is contained in:
Loïc Hoguin 2017-05-28 19:04:16 +02:00
parent 8cb125dbb7
commit 5f421f93bc
No known key found for this signature in database
GPG key ID: 71366FF21851DF03
4 changed files with 88 additions and 8 deletions

View file

@ -79,6 +79,7 @@ init_dispatch() ->
{text, <<"won't be received">>}]}
]},
{"/ws_subprotocol", ws_subprotocol, []},
{"/terminate", ws_terminate_h, []},
{"/ws_timeout_hibernate", ws_timeout_hibernate, []},
{"/ws_timeout_cancel", ws_timeout_cancel, []}
]}
@ -355,6 +356,39 @@ ws_subprotocol(Config) ->
{_, "foo"} = lists:keyfind("sec-websocket-protocol", 1, Headers),
ok.
ws_terminate(Config) ->
doc("The Req object is kept in a more compact form by default."),
{ok, Socket, _} = do_handshake("/terminate",
"x-test-pid: " ++ pid_to_list(self()) ++ "\r\n", Config),
%% Send a close frame.
ok = gen_tcp:send(Socket, << 1:1, 0:3, 8:4, 1:1, 0:7, 0:32 >>),
{ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000),
{error, closed} = gen_tcp:recv(Socket, 0, 6000),
%% Confirm terminate/3 was called with a compacted Req.
receive {terminate, _, Req} ->
true = maps:is_key(path, Req),
false = maps:is_key(headers, Req),
ok
after 1000 ->
error(timeout)
end.
ws_terminate_fun(Config) ->
doc("A function can be given to filter the Req object."),
{ok, Socket, _} = do_handshake("/terminate?req_filter",
"x-test-pid: " ++ pid_to_list(self()) ++ "\r\n", Config),
%% Send a close frame.
ok = gen_tcp:send(Socket, << 1:1, 0:3, 8:4, 1:1, 0:7, 0:32 >>),
{ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000),
{error, closed} = gen_tcp:recv(Socket, 0, 6000),
%% Confirm terminate/3 was called with a compacted Req.
receive {terminate, _, Req} ->
filtered = Req,
ok
after 1000 ->
error(timeout)
end.
ws_text_fragments(Config) ->
doc("Client sends fragmented text frames."),
{ok, Socket, _} = do_handshake("/ws_echo", Config),