From 8d920f3db97067159fcf6ca497979f8e063986dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Tue, 13 Nov 2018 15:55:09 +0100 Subject: Add the {deflate, boolean()} Websocket command It allows to temporarily disable Websocket compression when it was negotiated. It's ignored otherwise. This can be used as fine-grained control when some frames do not compress well. --- test/handlers/ws_active_commands_h.erl | 4 ++-- test/handlers/ws_deflate_commands_h.erl | 24 +++++++++++++++++++ test/ws_handler_SUITE.erl | 42 ++++++++++++++++++++++++++++++++- 3 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 test/handlers/ws_deflate_commands_h.erl (limited to 'test') diff --git a/test/handlers/ws_active_commands_h.erl b/test/handlers/ws_active_commands_h.erl index 4cdb3b1..1c615e3 100644 --- a/test/handlers/ws_active_commands_h.erl +++ b/test/handlers/ws_active_commands_h.erl @@ -1,5 +1,5 @@ -%% This module takes commands from the x-commands header -%% and returns them in the websocket_init/1 callback. +%% This module starts with active mode disabled +%% and enables it again once a timeout is triggered. -module(ws_active_commands_h). -behavior(cowboy_websocket). diff --git a/test/handlers/ws_deflate_commands_h.erl b/test/handlers/ws_deflate_commands_h.erl new file mode 100644 index 0000000..14236bc --- /dev/null +++ b/test/handlers/ws_deflate_commands_h.erl @@ -0,0 +1,24 @@ +%% This module enables/disables compression +%% every time it echoes a frame. + +-module(ws_deflate_commands_h). +-behavior(cowboy_websocket). + +-export([init/2]). +-export([websocket_handle/2]). +-export([websocket_info/2]). + +init(Req, RunOrHibernate) -> + {cowboy_websocket, Req, + #{deflate => true, hibernate => RunOrHibernate}, + #{compress => true}}. + +websocket_handle(Frame, State=#{deflate := Deflate0, hibernate := run}) -> + Deflate = not Deflate0, + {[Frame, {deflate, Deflate}], State#{deflate => Deflate}}; +websocket_handle(Frame, State=#{deflate := Deflate0, hibernate := hibernate}) -> + Deflate = not Deflate0, + {[Frame, {deflate, Deflate}], State#{deflate => Deflate}, hibernate}. + +websocket_info(_Info, State) -> + {[], State}. diff --git a/test/ws_handler_SUITE.erl b/test/ws_handler_SUITE.erl index 2caacca..6e2bb41 100644 --- a/test/ws_handler_SUITE.erl +++ b/test/ws_handler_SUITE.erl @@ -50,7 +50,8 @@ init_dispatch(Name) -> {"/init", ws_init_commands_h, RunOrHibernate}, {"/handle", ws_handle_commands_h, RunOrHibernate}, {"/info", ws_info_commands_h, RunOrHibernate}, - {"/active", ws_active_commands_h, RunOrHibernate} + {"/active", ws_active_commands_h, RunOrHibernate}, + {"/deflate", ws_deflate_commands_h, RunOrHibernate} ]}]). %% Support functions for testing using Gun. @@ -217,3 +218,42 @@ websocket_active_false(Config) -> {ok, {text, <<"Not received until the handler enables active again.">>}} = receive_ws(ConnPid, StreamRef), ok. + +websocket_deflate_false(Config) -> + doc("The {deflate, false} command temporarily disables compression. " + "The {deflate, true} command reenables it."), + %% We disable context takeover so that the compressed data + %% does not change across all frames. + {ok, Socket, Headers} = ws_SUITE:do_handshake("/deflate", + "Sec-WebSocket-Extensions: permessage-deflate; server_no_context_takeover\r\n", Config), + {_, "permessage-deflate; server_no_context_takeover"} + = lists:keyfind("sec-websocket-extensions", 1, Headers), + %% The handler receives a compressed "Hello" frame and + %% sends back a compressed or uncompressed echo intermittently. + Mask = 16#11223344, + CompressedHello = <<242, 72, 205, 201, 201, 7, 0>>, + MaskedHello = ws_SUITE:do_mask(CompressedHello, Mask, <<>>), + %% First echo is compressed. + ok = gen_tcp:send(Socket, <<1:1, 1:1, 0:2, 1:4, 1:1, 7:7, Mask:32, MaskedHello/binary>>), + {ok, <<1:1, 1:1, 0:2, 1:4, 0:1, 7:7, CompressedHello/binary>>} = gen_tcp:recv(Socket, 0, 6000), + %% Second echo is not compressed when it is received back. + ok = gen_tcp:send(Socket, <<1:1, 1:1, 0:2, 1:4, 1:1, 7:7, Mask:32, MaskedHello/binary>>), + {ok, <<1:1, 0:3, 1:4, 0:1, 5:7, "Hello">>} = gen_tcp:recv(Socket, 0, 6000), + %% Third echo is compressed again. + ok = gen_tcp:send(Socket, <<1:1, 1:1, 0:2, 1:4, 1:1, 7:7, Mask:32, MaskedHello/binary>>), + {ok, <<1:1, 1:1, 0:2, 1:4, 0:1, 7:7, CompressedHello/binary>>} = gen_tcp:recv(Socket, 0, 6000), + %% Client-initiated close. + 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), + ok. + +websocket_deflate_ignore_if_not_negotiated(Config) -> + doc("The {deflate, boolean()} commands are ignored " + "when compression was not negotiated."), + {ok, ConnPid, StreamRef} = gun_open_ws(Config, "/deflate", []), + _ = [begin + gun:ws_send(ConnPid, {text, <<"Hello.">>}), + {ok, {text, <<"Hello.">>}} = receive_ws(ConnPid, StreamRef) + end || _ <- lists:seq(1, 10)], + ok. -- cgit v1.2.3