From fbfec873f6026b858c3604d74d88470ce45f4296 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Thu, 15 Nov 2018 10:11:36 +0100 Subject: Add a compress_buffering option to cowboy_compress_h Also changes the behavior to disable buffering by default, so that the default works in all cases, including server-sent events. --- test/compress_SUITE.erl | 55 ++++++++++++++++++++++++++++++++++++++++++++ test/handlers/compress_h.erl | 8 +++++++ 2 files changed, 63 insertions(+) (limited to 'test') diff --git a/test/compress_SUITE.erl b/test/compress_SUITE.erl index 74be4f9..052d82d 100644 --- a/test/compress_SUITE.erl +++ b/test/compress_SUITE.erl @@ -18,6 +18,7 @@ -import(ct_helper, [config/2]). -import(ct_helper, [doc/1]). +-import(ct_helper, [name/0]). -import(cowboy_test, [gun_open/1]). %% ct. @@ -144,3 +145,57 @@ gzip_stream_reply_content_encoding(Config) -> {_, <<"compress">>} = lists:keyfind(<<"content-encoding">>, 1, Headers), 100000 = iolist_size(Body), ok. + +opts_compress_buffering_false(Config0) -> + doc("Confirm that the compress_buffering option can be set to false, " + "which is the default."), + Fun = case config(ref, Config0) of + https_compress -> init_https; + h2_compress -> init_http2; + _ -> init_http + end, + Config = cowboy_test:Fun(name(), #{ + env => #{dispatch => init_dispatch(Config0)}, + stream_handlers => [cowboy_compress_h, cowboy_stream_h], + compress_buffering => false + }, Config0), + ConnPid = gun_open(Config), + Ref = gun:get(ConnPid, "/stream_reply/delayed", + [{<<"accept-encoding">>, <<"gzip">>}]), + {response, nofin, 200, Headers} = gun:await(ConnPid, Ref), + {_, <<"gzip">>} = lists:keyfind(<<"content-encoding">>, 1, Headers), + Z = zlib:open(), + zlib:inflateInit(Z, 31), + {data, nofin, Data1} = gun:await(ConnPid, Ref, 100), + <<"data: Hello!\r\n\r\n">> = iolist_to_binary(zlib:inflate(Z, Data1)), + timer:sleep(1000), + {data, nofin, Data2} = gun:await(ConnPid, Ref, 100), + <<"data: World!\r\n\r\n">> = iolist_to_binary(zlib:inflate(Z, Data2)), + gun:close(ConnPid), + cowboy:stop_listener(name()). + +opts_compress_buffering_true(Config0) -> + doc("Confirm that the compress_buffering option can be set to true, " + "and that the data received is buffered."), + Fun = case config(ref, Config0) of + https_compress -> init_https; + h2_compress -> init_http2; + _ -> init_http + end, + Config = cowboy_test:Fun(name(), #{ + env => #{dispatch => init_dispatch(Config0)}, + stream_handlers => [cowboy_compress_h, cowboy_stream_h], + compress_buffering => true + }, Config0), + ConnPid = gun_open(Config), + Ref = gun:get(ConnPid, "/stream_reply/delayed", + [{<<"accept-encoding">>, <<"gzip">>}]), + {response, nofin, 200, Headers} = gun:await(ConnPid, Ref), + {_, <<"gzip">>} = lists:keyfind(<<"content-encoding">>, 1, Headers), + Z = zlib:open(), + zlib:inflateInit(Z, 31), + %% The data gets buffered because it is too small. + {data, nofin, Data1} = gun:await(ConnPid, Ref, 100), + <<>> = iolist_to_binary(zlib:inflate(Z, Data1)), + gun:close(ConnPid), + cowboy:stop_listener(name()). diff --git a/test/handlers/compress_h.erl b/test/handlers/compress_h.erl index 1509d82..ffea05f 100644 --- a/test/handlers/compress_h.erl +++ b/test/handlers/compress_h.erl @@ -50,6 +50,14 @@ init(Req0, State=stream_reply) -> cowboy_req:stream_body({sendfile, 0, Size, AppFile}, nofin, Req1), cowboy_req:stream_body(Data, nofin, Req1), cowboy_req:stream_body({sendfile, 0, Size, AppFile}, fin, Req1), + Req1; + <<"delayed">> -> + Req1 = cowboy_req:stream_reply(200, Req0), + cowboy_req:stream_body(<<"data: Hello!\r\n\r\n">>, nofin, Req1), + timer:sleep(1000), + cowboy_req:stream_body(<<"data: World!\r\n\r\n">>, nofin, Req1), + timer:sleep(1000), + cowboy_req:stream_body(<<"data: Closing!\r\n\r\n">>, fin, Req1), Req1 end, {ok, Req, State}. -- cgit v1.2.3