aboutsummaryrefslogtreecommitdiffstats
path: root/src/cowboy_compress_h.erl
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2018-11-15 18:53:42 +0100
committerLoïc Hoguin <[email protected]>2018-11-15 18:53:42 +0100
commit240da3f2d9bc02611b23c1ea0e7bbe8db9d99cd8 (patch)
tree7c59e4ebd16731bab4fd9ac5deddb55a7762cf1c /src/cowboy_compress_h.erl
parentfbfec873f6026b858c3604d74d88470ce45f4296 (diff)
downloadcowboy-240da3f2d9bc02611b23c1ea0e7bbe8db9d99cd8.tar.gz
cowboy-240da3f2d9bc02611b23c1ea0e7bbe8db9d99cd8.tar.bz2
cowboy-240da3f2d9bc02611b23c1ea0e7bbe8db9d99cd8.zip
Add the set_options stream handler command
The first two options to benefit from this are the cowboy_compress_h options.
Diffstat (limited to 'src/cowboy_compress_h.erl')
-rw-r--r--src/cowboy_compress_h.erl19
1 files changed, 15 insertions, 4 deletions
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