compress_h.erl 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. %% This module sends a response body of varying sizes to test
  2. %% the cowboy_compress_h stream handler.
  3. -module(compress_h).
  4. -export([init/2]).
  5. init(Req0, State=reply) ->
  6. Req = case cowboy_req:binding(what, Req0) of
  7. <<"small">> ->
  8. cowboy_req:reply(200, #{}, lists:duplicate(100, $a), Req0);
  9. <<"large">> ->
  10. cowboy_req:reply(200, #{}, lists:duplicate(100000, $a), Req0);
  11. <<"content-encoding">> ->
  12. cowboy_req:reply(200, #{<<"content-encoding">> => <<"compress">>},
  13. lists:duplicate(100000, $a), Req0);
  14. <<"sendfile">> ->
  15. AppFile = code:where_is_file("cowboy.app"),
  16. Size = filelib:file_size(AppFile),
  17. cowboy_req:reply(200, #{}, {sendfile, 0, Size, AppFile}, Req0)
  18. end,
  19. {ok, Req, State};
  20. init(Req0, State=stream_reply) ->
  21. Req = case cowboy_req:binding(what, Req0) of
  22. <<"large">> ->
  23. stream_reply(#{}, Req0);
  24. <<"content-encoding">> ->
  25. stream_reply(#{<<"content-encoding">> => <<"compress">>}, Req0)
  26. end,
  27. {ok, Req, State}.
  28. stream_reply(Headers, Req0) ->
  29. Data = lists:duplicate(10000, $a),
  30. Req = cowboy_req:stream_reply(200, Headers, Req0),
  31. _ = [cowboy_req:stream_body(Data, nofin, Req) || _ <- lists:seq(1,9)],
  32. cowboy_req:stream_body(Data, fin, Req),
  33. Req.