aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2018-11-15 10:11:36 +0100
committerLoïc Hoguin <[email protected]>2018-11-15 10:11:36 +0100
commitfbfec873f6026b858c3604d74d88470ce45f4296 (patch)
tree021fd12f5458d659e15a4bc7638b4dfd62a9df21 /src
parent292039362a6125dfd0a163d5b0a49b800bf80b11 (diff)
downloadcowboy-fbfec873f6026b858c3604d74d88470ce45f4296.tar.gz
cowboy-fbfec873f6026b858c3604d74d88470ce45f4296.tar.bz2
cowboy-fbfec873f6026b858c3604d74d88470ce45f4296.zip
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.
Diffstat (limited to 'src')
-rw-r--r--src/cowboy_compress_h.erl20
-rw-r--r--src/cowboy_http.erl1
-rw-r--r--src/cowboy_http2.erl1
3 files changed, 16 insertions, 6 deletions
diff --git a/src/cowboy_compress_h.erl b/src/cowboy_compress_h.erl
index ed81db6..bd3df42 100644
--- a/src/cowboy_compress_h.erl
+++ b/src/cowboy_compress_h.erl
@@ -25,7 +25,8 @@
next :: any(),
threshold :: non_neg_integer() | undefined,
compress = undefined :: undefined | gzip,
- deflate = undefined :: undefined | zlib:zstream()
+ deflate = undefined :: undefined | zlib:zstream(),
+ deflate_flush = sync :: none | sync
}).
-spec init(cowboy_stream:streamid(), cowboy_req:req(), cowboy:opts())
@@ -33,8 +34,14 @@
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,
{Commands0, Next} = cowboy_stream:init(StreamID, Req, Opts),
- fold(Commands0, State0#state{next=Next, threshold=CompressThreshold}).
+ fold(Commands0, State0#state{next=Next,
+ threshold=CompressThreshold,
+ deflate_flush=DeflateFlush}).
-spec data(cowboy_stream:streamid(), cowboy_stream:fin(), cowboy_req:resp_body(), State)
-> {cowboy_stream:commands(), State} when State::#state{}.
@@ -176,9 +183,10 @@ gzip_headers({headers, Status, Headers0}, State) ->
%% includes a checksum at the end of the stream. We have
%% to read the file in memory, making this not suitable for
%% large files.
-gzip_data({data, nofin, Sendfile={sendfile, _, _, _}}, State=#state{deflate=Z}) ->
+gzip_data({data, nofin, Sendfile={sendfile, _, _, _}},
+ State=#state{deflate=Z, deflate_flush=Flush}) ->
{ok, Data0} = read_file(Sendfile),
- Data = zlib:deflate(Z, Data0),
+ Data = zlib:deflate(Z, Data0, Flush),
{{data, nofin, Data}, State};
gzip_data({data, fin, Sendfile={sendfile, _, _, _}}, State=#state{deflate=Z}) ->
{ok, Data0} = read_file(Sendfile),
@@ -186,8 +194,8 @@ gzip_data({data, fin, Sendfile={sendfile, _, _, _}}, State=#state{deflate=Z}) ->
zlib:deflateEnd(Z),
zlib:close(Z),
{{data, fin, Data}, State#state{deflate=undefined}};
-gzip_data({data, nofin, Data0}, State=#state{deflate=Z}) ->
- Data = zlib:deflate(Z, Data0),
+gzip_data({data, nofin, Data0}, State=#state{deflate=Z, deflate_flush=Flush}) ->
+ Data = zlib:deflate(Z, Data0, Flush),
{{data, nofin, Data}, State};
gzip_data({data, fin, Data0}, State=#state{deflate=Z}) ->
Data = zlib:deflate(Z, Data0, finish),
diff --git a/src/cowboy_http.erl b/src/cowboy_http.erl
index 56a4af8..9a10790 100644
--- a/src/cowboy_http.erl
+++ b/src/cowboy_http.erl
@@ -25,6 +25,7 @@
-export([system_code_change/4]).
-type opts() :: #{
+ compress_buffering => boolean(),
compress_threshold => non_neg_integer(),
connection_type => worker | supervisor,
env => cowboy_middleware:env(),
diff --git a/src/cowboy_http2.erl b/src/cowboy_http2.erl
index fc1a3a0..3397241 100644
--- a/src/cowboy_http2.erl
+++ b/src/cowboy_http2.erl
@@ -27,6 +27,7 @@
-export([system_code_change/4]).
-type opts() :: #{
+ compress_buffering => boolean(),
compress_threshold => non_neg_integer(),
connection_type => worker | supervisor,
enable_connect_protocol => boolean(),