From 240da3f2d9bc02611b23c1ea0e7bbe8db9d99cd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Thu, 15 Nov 2018 18:53:42 +0100 Subject: Add the set_options stream handler command The first two options to benefit from this are the cowboy_compress_h options. --- src/cowboy_compress_h.erl | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) (limited to 'src/cowboy_compress_h.erl') diff --git a/src/cowboy_compress_h.erl b/src/cowboy_compress_h.erl index bd3df42..95c49d3 100644 --- a/src/cowboy_compress_h.erl +++ b/src/cowboy_compress_h.erl @@ -34,10 +34,7 @@ init(StreamID, Req, Opts) -> State0 = check_req(Req), CompressThreshold = maps:get(compress_threshold, Opts, 300), - DeflateFlush = case maps:get(compress_buffering, Opts, false) of - false -> sync; - true -> none - end, + DeflateFlush = buffering_to_zflush(maps:get(compress_buffering, Opts, false)), {Commands0, Next} = cowboy_stream:init(StreamID, Req, Opts), fold(Commands0, State0#state{next=Next, threshold=CompressThreshold, @@ -143,10 +140,24 @@ fold([Data0={data, _, _}|Tail], State0=#state{compress=gzip}, Acc) -> fold([Trailers={trailers, _}|Tail], State0=#state{compress=gzip}, Acc) -> {{data, fin, Data}, State} = gzip_data({data, fin, <<>>}, State0), fold(Tail, State, [Trailers, {data, nofin, Data}|Acc]); +%% All the options from this handler can be updated for the current stream. +fold([{set_options, Opts}|Tail], State=#state{ + threshold=CompressThreshold0, deflate_flush=DeflateFlush0}, Acc) -> + CompressThreshold = maps:get(compress_threshold, Opts, CompressThreshold0), + DeflateFlush = case Opts of + #{compress_buffering := CompressBuffering} -> + buffering_to_zflush(CompressBuffering); + _ -> + DeflateFlush0 + end, + fold(Tail, State#state{threshold=CompressThreshold, deflate_flush=DeflateFlush}, Acc); %% Otherwise, we have an unrelated command or compression is disabled. fold([Command|Tail], State, Acc) -> fold(Tail, State, [Command|Acc]). +buffering_to_zflush(true) -> none; +buffering_to_zflush(false) -> sync. + gzip_response({response, Status, Headers, Body}, State) -> %% We can't call zlib:gzip/1 because it does an %% iolist_to_binary(GzBody) at the end to return -- cgit v1.2.3