cowboy_compress_h.erl 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. %% Copyright (c) 2017, Loïc Hoguin <essen@ninenines.eu>
  2. %%
  3. %% Permission to use, copy, modify, and/or distribute this software for any
  4. %% purpose with or without fee is hereby granted, provided that the above
  5. %% copyright notice and this permission notice appear in all copies.
  6. %%
  7. %% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. %% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. %% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  10. %% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. %% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  12. %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  13. %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. -module(cowboy_compress_h).
  15. -behavior(cowboy_stream).
  16. -export([init/3]).
  17. -export([data/4]).
  18. -export([info/3]).
  19. -export([terminate/3]).
  20. -export([early_error/5]).
  21. -record(state, {
  22. next :: any(),
  23. threshold :: non_neg_integer() | undefined,
  24. compress = undefined :: undefined | gzip,
  25. deflate = undefined :: undefined | zlib:zstream(),
  26. deflate_flush = sync :: none | sync
  27. }).
  28. -spec init(cowboy_stream:streamid(), cowboy_req:req(), cowboy:opts())
  29. -> {cowboy_stream:commands(), #state{}}.
  30. init(StreamID, Req, Opts) ->
  31. State0 = check_req(Req),
  32. CompressThreshold = maps:get(compress_threshold, Opts, 300),
  33. DeflateFlush = buffering_to_zflush(maps:get(compress_buffering, Opts, false)),
  34. {Commands0, Next} = cowboy_stream:init(StreamID, Req, Opts),
  35. fold(Commands0, State0#state{next=Next,
  36. threshold=CompressThreshold,
  37. deflate_flush=DeflateFlush}).
  38. -spec data(cowboy_stream:streamid(), cowboy_stream:fin(), cowboy_req:resp_body(), State)
  39. -> {cowboy_stream:commands(), State} when State::#state{}.
  40. data(StreamID, IsFin, Data, State0=#state{next=Next0}) ->
  41. {Commands0, Next} = cowboy_stream:data(StreamID, IsFin, Data, Next0),
  42. fold(Commands0, State0#state{next=Next}).
  43. -spec info(cowboy_stream:streamid(), any(), State)
  44. -> {cowboy_stream:commands(), State} when State::#state{}.
  45. info(StreamID, Info, State0=#state{next=Next0}) ->
  46. {Commands0, Next} = cowboy_stream:info(StreamID, Info, Next0),
  47. fold(Commands0, State0#state{next=Next}).
  48. -spec terminate(cowboy_stream:streamid(), cowboy_stream:reason(), #state{}) -> any().
  49. terminate(StreamID, Reason, #state{next=Next, deflate=Z}) ->
  50. %% Clean the zlib:stream() in case something went wrong.
  51. %% In the normal scenario the stream is already closed.
  52. case Z of
  53. undefined -> ok;
  54. _ -> zlib:close(Z)
  55. end,
  56. cowboy_stream:terminate(StreamID, Reason, Next).
  57. -spec early_error(cowboy_stream:streamid(), cowboy_stream:reason(),
  58. cowboy_stream:partial_req(), Resp, cowboy:opts()) -> Resp
  59. when Resp::cowboy_stream:resp_command().
  60. early_error(StreamID, Reason, PartialReq, Resp, Opts) ->
  61. cowboy_stream:early_error(StreamID, Reason, PartialReq, Resp, Opts).
  62. %% Internal.
  63. %% Check if the client supports decoding of gzip responses.
  64. check_req(Req) ->
  65. %% @todo Probably shouldn't unconditionally crash on failure.
  66. case cowboy_req:parse_header(<<"accept-encoding">>, Req) of
  67. %% Client doesn't support any compression algorithm.
  68. undefined ->
  69. #state{compress=undefined};
  70. Encodings ->
  71. %% We only support gzip so look for it specifically.
  72. %% @todo A recipient SHOULD consider "x-gzip" to be
  73. %% equivalent to "gzip". (RFC7230 4.2.3)
  74. case [E || E={<<"gzip">>, Q} <- Encodings, Q =/= 0] of
  75. [] ->
  76. #state{compress=undefined};
  77. _ ->
  78. #state{compress=gzip}
  79. end
  80. end.
  81. %% Do not compress responses that contain the content-encoding header.
  82. check_resp_headers(#{<<"content-encoding">> := _}, State) ->
  83. State#state{compress=undefined};
  84. check_resp_headers(_, State) ->
  85. State.
  86. fold(Commands, State=#state{compress=undefined}) ->
  87. {Commands, State};
  88. fold(Commands, State) ->
  89. fold(Commands, State, []).
  90. fold([], State, Acc) ->
  91. {lists:reverse(Acc), State};
  92. %% We do not compress full sendfile bodies.
  93. fold([Response={response, _, _, {sendfile, _, _, _}}|Tail], State, Acc) ->
  94. fold(Tail, State, [Response|Acc]);
  95. %% We compress full responses directly, unless they are lower than
  96. %% the configured threshold or we find we are not able to by looking at the headers.
  97. fold([Response0={response, _, Headers, Body}|Tail],
  98. State0=#state{threshold=CompressThreshold}, Acc) ->
  99. case check_resp_headers(Headers, State0) of
  100. State=#state{compress=undefined} ->
  101. fold(Tail, State, [Response0|Acc]);
  102. State1 ->
  103. BodyLength = iolist_size(Body),
  104. if
  105. BodyLength =< CompressThreshold ->
  106. fold(Tail, State1, [Response0|Acc]);
  107. true ->
  108. {Response, State} = gzip_response(Response0, State1),
  109. fold(Tail, State, [Response|Acc])
  110. end
  111. end;
  112. %% Check headers and initiate compression...
  113. fold([Response0={headers, _, Headers}|Tail], State0, Acc) ->
  114. case check_resp_headers(Headers, State0) of
  115. State=#state{compress=undefined} ->
  116. fold(Tail, State, [Response0|Acc]);
  117. State1 ->
  118. {Response, State} = gzip_headers(Response0, State1),
  119. fold(Tail, State, [Response|Acc])
  120. end;
  121. %% then compress each data commands individually.
  122. fold([Data0={data, _, _}|Tail], State0=#state{compress=gzip}, Acc) ->
  123. {Data, State} = gzip_data(Data0, State0),
  124. fold(Tail, State, [Data|Acc]);
  125. %% When trailers are sent we need to end the compression.
  126. %% This results in an extra data command being sent.
  127. fold([Trailers={trailers, _}|Tail], State0=#state{compress=gzip}, Acc) ->
  128. {{data, fin, Data}, State} = gzip_data({data, fin, <<>>}, State0),
  129. fold(Tail, State, [Trailers, {data, nofin, Data}|Acc]);
  130. %% All the options from this handler can be updated for the current stream.
  131. fold([{set_options, Opts}|Tail], State=#state{
  132. threshold=CompressThreshold0, deflate_flush=DeflateFlush0}, Acc) ->
  133. CompressThreshold = maps:get(compress_threshold, Opts, CompressThreshold0),
  134. DeflateFlush = case Opts of
  135. #{compress_buffering := CompressBuffering} ->
  136. buffering_to_zflush(CompressBuffering);
  137. _ ->
  138. DeflateFlush0
  139. end,
  140. fold(Tail, State#state{threshold=CompressThreshold, deflate_flush=DeflateFlush}, Acc);
  141. %% Otherwise, we have an unrelated command or compression is disabled.
  142. fold([Command|Tail], State, Acc) ->
  143. fold(Tail, State, [Command|Acc]).
  144. buffering_to_zflush(true) -> none;
  145. buffering_to_zflush(false) -> sync.
  146. gzip_response({response, Status, Headers, Body}, State) ->
  147. %% We can't call zlib:gzip/1 because it does an
  148. %% iolist_to_binary(GzBody) at the end to return
  149. %% a binary(). Therefore the code here is largely
  150. %% a duplicate of the code of that function.
  151. Z = zlib:open(),
  152. GzBody = try
  153. %% 31 = 16+?MAX_WBITS from zlib.erl
  154. %% @todo It might be good to allow them to be configured?
  155. zlib:deflateInit(Z, default, deflated, 31, 8, default),
  156. Gz = zlib:deflate(Z, Body, finish),
  157. zlib:deflateEnd(Z),
  158. Gz
  159. after
  160. zlib:close(Z)
  161. end,
  162. {{response, Status, Headers#{
  163. <<"content-length">> => integer_to_binary(iolist_size(GzBody)),
  164. <<"content-encoding">> => <<"gzip">>
  165. }, GzBody}, State}.
  166. gzip_headers({headers, Status, Headers0}, State) ->
  167. Z = zlib:open(),
  168. %% We use the same arguments as when compressing the body fully.
  169. %% @todo It might be good to allow them to be configured?
  170. zlib:deflateInit(Z, default, deflated, 31, 8, default),
  171. Headers = maps:remove(<<"content-length">>, Headers0),
  172. {{headers, Status, Headers#{
  173. <<"content-encoding">> => <<"gzip">>
  174. }}, State#state{deflate=Z}}.
  175. %% It is not possible to combine zlib and the sendfile
  176. %% syscall as far as I can tell, because the zlib format
  177. %% includes a checksum at the end of the stream. We have
  178. %% to read the file in memory, making this not suitable for
  179. %% large files.
  180. gzip_data({data, nofin, Sendfile={sendfile, _, _, _}},
  181. State=#state{deflate=Z, deflate_flush=Flush}) ->
  182. {ok, Data0} = read_file(Sendfile),
  183. Data = zlib:deflate(Z, Data0, Flush),
  184. {{data, nofin, Data}, State};
  185. gzip_data({data, fin, Sendfile={sendfile, _, _, _}}, State=#state{deflate=Z}) ->
  186. {ok, Data0} = read_file(Sendfile),
  187. Data = zlib:deflate(Z, Data0, finish),
  188. zlib:deflateEnd(Z),
  189. zlib:close(Z),
  190. {{data, fin, Data}, State#state{deflate=undefined}};
  191. gzip_data({data, nofin, Data0}, State=#state{deflate=Z, deflate_flush=Flush}) ->
  192. Data = zlib:deflate(Z, Data0, Flush),
  193. {{data, nofin, Data}, State};
  194. gzip_data({data, fin, Data0}, State=#state{deflate=Z}) ->
  195. Data = zlib:deflate(Z, Data0, finish),
  196. zlib:deflateEnd(Z),
  197. zlib:close(Z),
  198. {{data, fin, Data}, State#state{deflate=undefined}}.
  199. read_file({sendfile, Offset, Bytes, Path}) ->
  200. {ok, IoDevice} = file:open(Path, [read, raw, binary]),
  201. try
  202. _ = case Offset of
  203. 0 -> ok;
  204. _ -> file:position(IoDevice, {bof, Offset})
  205. end,
  206. file:read(IoDevice, Bytes)
  207. after
  208. file:close(IoDevice)
  209. end.