From 01f57ad65d7c75fb455f48e354bb3a328c472ce4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Mon, 7 Jan 2013 22:42:16 +0100 Subject: Add optional automatic response body compression This behavior can be enabled with the `compress` protocol option. See the `compress_response` example for more details. All tests are now ran with and without compression for both HTTP and HTTPS. --- src/cowboy_protocol.erl | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'src/cowboy_protocol.erl') diff --git a/src/cowboy_protocol.erl b/src/cowboy_protocol.erl index 48c0b00..0e9982b 100644 --- a/src/cowboy_protocol.erl +++ b/src/cowboy_protocol.erl @@ -17,6 +17,8 @@ %% %% The available options are: %%
+%%
compress
Whether to automatically compress the response +%% body when the conditions are met. Disabled by default.
%%
env
The environment passed and optionally modified %% by middlewares.
%%
max_empty_lines
Max number of empty lines before a request. @@ -64,6 +66,7 @@ socket :: inet:socket(), transport :: module(), middlewares :: [module()], + compress :: boolean(), env :: cowboy_middleware:env(), onrequest :: undefined | onrequest_fun(), onresponse = undefined :: undefined | onresponse_fun(), @@ -99,6 +102,7 @@ get_value(Key, Opts, Default) -> %% @private -spec init(pid(), inet:socket(), module(), any()) -> ok. init(ListenerPid, Socket, Transport, Opts) -> + Compress = get_value(compress, Opts, false), MaxEmptyLines = get_value(max_empty_lines, Opts, 5), MaxHeaderNameLength = get_value(max_header_name_length, Opts, 64), MaxHeaderValueLength = get_value(max_header_value_length, Opts, 4096), @@ -112,7 +116,7 @@ init(ListenerPid, Socket, Transport, Opts) -> Timeout = get_value(timeout, Opts, 5000), ok = ranch:accept_ack(ListenerPid), wait_request(<<>>, #state{socket=Socket, transport=Transport, - middlewares=Middlewares, env=Env, + middlewares=Middlewares, compress=Compress, env=Env, max_empty_lines=MaxEmptyLines, max_keepalive=MaxKeepalive, max_request_line_length=MaxRequestLineLength, max_header_name_length=MaxHeaderNameLength, @@ -457,11 +461,11 @@ parse_host(<< C, Rest/bits >>, Acc) -> request(Buffer, State=#state{socket=Socket, transport=Transport, req_keepalive=ReqKeepalive, max_keepalive=MaxKeepalive, - onresponse=OnResponse}, + compress=Compress, onresponse=OnResponse}, Method, Path, Query, Fragment, Version, Headers, Host, Port) -> Req = cowboy_req:new(Socket, Transport, Method, Path, Query, Fragment, Version, Headers, Host, Port, Buffer, ReqKeepalive < MaxKeepalive, - OnResponse), + Compress, OnResponse), onrequest(Req, State). %% Call the global onrequest callback. The callback can send a reply, @@ -546,13 +550,13 @@ error_terminate(Code, Req, State) -> %% Only send an error reply if there is no resp_sent message. -spec error_terminate(cowboy_http:status(), #state{}) -> ok. error_terminate(Code, State=#state{socket=Socket, transport=Transport, - onresponse=OnResponse}) -> + compress=Compress, onresponse=OnResponse}) -> receive {cowboy_req, resp_sent} -> ok after 0 -> _ = cowboy_req:reply(Code, cowboy_req:new(Socket, Transport, <<"GET">>, <<>>, <<>>, <<>>, {1, 1}, [], <<>>, undefined, - <<>>, false, OnResponse)), + <<>>, false, Compress, OnResponse)), ok end, terminate(State). -- cgit v1.2.3