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