compress_h.erl 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. cowboy_req:cast({set_options, #{compress_threshold => 0}}, Req0),
  25. cowboy_req:reply(200, #{}, lists:duplicate(100, $a), Req0)
  26. end,
  27. {ok, Req, State};
  28. init(Req0, State=stream_reply) ->
  29. Req = case cowboy_req:binding(what, Req0) of
  30. <<"large">> ->
  31. stream_reply(#{}, Req0);
  32. <<"content-encoding">> ->
  33. stream_reply(#{<<"content-encoding">> => <<"compress">>}, Req0);
  34. <<"sendfile">> ->
  35. Data = lists:duplicate(10000, $a),
  36. AppFile = code:where_is_file("cowboy.app"),
  37. Size = filelib:file_size(AppFile),
  38. Req1 = cowboy_req:stream_reply(200, Req0),
  39. %% We send a few files interspersed into other data.
  40. cowboy_req:stream_body(Data, nofin, Req1),
  41. cowboy_req:stream_body({sendfile, 0, Size, AppFile}, nofin, Req1),
  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, fin, Req1),
  45. Req1;
  46. <<"sendfile_fin">> ->
  47. Data = lists:duplicate(10000, $a),
  48. AppFile = code:where_is_file("cowboy.app"),
  49. Size = filelib:file_size(AppFile),
  50. Req1 = cowboy_req:stream_reply(200, Req0),
  51. %% We send a few files interspersed into other data.
  52. cowboy_req:stream_body(Data, nofin, Req1),
  53. cowboy_req:stream_body({sendfile, 0, Size, AppFile}, nofin, Req1),
  54. cowboy_req:stream_body(Data, nofin, Req1),
  55. cowboy_req:stream_body({sendfile, 0, Size, AppFile}, fin, Req1),
  56. Req1;
  57. <<"delayed">> ->
  58. stream_delayed(Req0);
  59. <<"set_options_buffering_false">> ->
  60. cowboy_req:cast({set_options, #{compress_buffering => false}}, Req0),
  61. stream_delayed(Req0);
  62. <<"set_options_buffering_true">> ->
  63. cowboy_req:cast({set_options, #{compress_buffering => true}}, Req0),
  64. stream_delayed(Req0)
  65. end,
  66. {ok, Req, State}.
  67. stream_reply(Headers, Req0) ->
  68. Data = lists:duplicate(10000, $a),
  69. Req = cowboy_req:stream_reply(200, Headers, Req0),
  70. _ = [cowboy_req:stream_body(Data, nofin, Req) || _ <- lists:seq(1,9)],
  71. cowboy_req:stream_body(Data, fin, Req),
  72. Req.
  73. stream_delayed(Req0) ->
  74. Req = cowboy_req:stream_reply(200, Req0),
  75. cowboy_req:stream_body(<<"data: Hello!\r\n\r\n">>, nofin, Req),
  76. timer:sleep(1000),
  77. cowboy_req:stream_body(<<"data: World!\r\n\r\n">>, nofin, Req),
  78. timer:sleep(1000),
  79. cowboy_req:stream_body(<<"data: Closing!\r\n\r\n">>, fin, Req),
  80. Req.