compress_h.erl 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. <<"over-threshold">> ->
  12. cowboy_req:reply(200, #{}, lists:duplicate(200, $a), Req0);
  13. <<"content-encoding">> ->
  14. cowboy_req:reply(200, #{<<"content-encoding">> => <<"compress">>},
  15. lists:duplicate(100000, $a), Req0);
  16. <<"sendfile">> ->
  17. AppFile = code:where_is_file("cowboy.app"),
  18. Size = filelib:file_size(AppFile),
  19. cowboy_req:reply(200, #{}, {sendfile, 0, Size, AppFile}, Req0);
  20. <<"set_options_threshold0">> ->
  21. %% @todo This should be replaced by a cowboy_req:cast/cowboy_stream:cast.
  22. #{pid := Pid, streamid := StreamID} = Req0,
  23. Pid ! {{Pid, StreamID}, {set_options, #{compress_threshold => 0}}},
  24. cowboy_req:reply(200, #{}, lists:duplicate(100, $a), Req0)
  25. end,
  26. {ok, Req, State};
  27. init(Req0, State=stream_reply) ->
  28. Req = case cowboy_req:binding(what, Req0) of
  29. <<"large">> ->
  30. stream_reply(#{}, Req0);
  31. <<"content-encoding">> ->
  32. stream_reply(#{<<"content-encoding">> => <<"compress">>}, Req0);
  33. <<"sendfile">> ->
  34. Data = lists:duplicate(10000, $a),
  35. AppFile = code:where_is_file("cowboy.app"),
  36. Size = filelib:file_size(AppFile),
  37. Req1 = cowboy_req:stream_reply(200, Req0),
  38. %% We send a few files interspersed into other data.
  39. cowboy_req:stream_body(Data, nofin, Req1),
  40. cowboy_req:stream_body({sendfile, 0, Size, AppFile}, nofin, Req1),
  41. cowboy_req:stream_body(Data, nofin, Req1),
  42. cowboy_req:stream_body({sendfile, 0, Size, AppFile}, nofin, Req1),
  43. cowboy_req:stream_body(Data, fin, Req1),
  44. Req1;
  45. <<"sendfile_fin">> ->
  46. Data = lists:duplicate(10000, $a),
  47. AppFile = code:where_is_file("cowboy.app"),
  48. Size = filelib:file_size(AppFile),
  49. Req1 = cowboy_req:stream_reply(200, Req0),
  50. %% We send a few files interspersed into other data.
  51. cowboy_req:stream_body(Data, nofin, Req1),
  52. cowboy_req:stream_body({sendfile, 0, Size, AppFile}, nofin, Req1),
  53. cowboy_req:stream_body(Data, nofin, Req1),
  54. cowboy_req:stream_body({sendfile, 0, Size, AppFile}, fin, Req1),
  55. Req1;
  56. <<"delayed">> ->
  57. stream_delayed(Req0);
  58. <<"set_options_buffering_false">> ->
  59. %% @todo This should be replaced by a cowboy_req:cast/cowboy_stream:cast.
  60. #{pid := Pid, streamid := StreamID} = Req0,
  61. Pid ! {{Pid, StreamID}, {set_options, #{compress_buffering => false}}},
  62. stream_delayed(Req0);
  63. <<"set_options_buffering_true">> ->
  64. %% @todo This should be replaced by a cowboy_req:cast/cowboy_stream:cast.
  65. #{pid := Pid, streamid := StreamID} = Req0,
  66. Pid ! {{Pid, StreamID}, {set_options, #{compress_buffering => true}}},
  67. stream_delayed(Req0)
  68. end,
  69. {ok, Req, State}.
  70. stream_reply(Headers, Req0) ->
  71. Data = lists:duplicate(10000, $a),
  72. Req = cowboy_req:stream_reply(200, Headers, Req0),
  73. _ = [cowboy_req:stream_body(Data, nofin, Req) || _ <- lists:seq(1,9)],
  74. cowboy_req:stream_body(Data, fin, Req),
  75. Req.
  76. stream_delayed(Req0) ->
  77. Req = cowboy_req:stream_reply(200, Req0),
  78. cowboy_req:stream_body(<<"data: Hello!\r\n\r\n">>, nofin, Req),
  79. timer:sleep(1000),
  80. cowboy_req:stream_body(<<"data: World!\r\n\r\n">>, nofin, Req),
  81. timer:sleep(1000),
  82. cowboy_req:stream_body(<<"data: Closing!\r\n\r\n">>, fin, Req),
  83. Req.