compress_h.erl 3.5 KB

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