| 1 |
%% This module echoes a request body of to test |
| 2 |
%% the cowboy_decompress_h stream handler. |
| 3 |
|
| 4 |
-module(decompress_h). |
| 5 |
|
| 6 |
-export([init/2]). |
| 7 |
|
| 8 |
init(Req0, State=echo) -> |
| 9 |
case cowboy_req:binding(what, Req0) of |
| 10 |
<<"decompress_disable">> -> |
| 11 |
cowboy_req:cast({set_options, #{decompress_enabled => false}}, Req0); |
| 12 |
<<"decompress_ratio_limit">> -> |
| 13 |
cowboy_req:cast({set_options, #{decompress_ratio_limit => 0.5}}, Req0); |
| 14 |
<<"normal">> -> ok |
| 15 |
end, |
| 16 |
{ok, Body, Req1} = read_body(Req0), |
| 17 |
Req = cowboy_req:reply(200, #{}, Body, Req1), |
| 18 |
{ok, Req, State}; |
| 19 |
init(Req0, State=test) -> |
| 20 |
Req = test(Req0, cowboy_req:binding(what, Req0)), |
| 21 |
{ok, Req, State}. |
| 22 |
|
| 23 |
test(Req, <<"content-encoding">>) -> |
| 24 |
cowboy_req:reply(200, #{}, |
| 25 |
cowboy_req:header(<<"content-encoding">>, Req, <<"undefined">>), |
| 26 |
Req); |
| 27 |
test(Req, <<"content-decoded">>) -> |
| 28 |
cowboy_req:reply(200, #{}, |
| 29 |
io_lib:format("~0p", [maps:get(content_decoded, Req, undefined)]), |
| 30 |
Req); |
| 31 |
test(Req0, <<"disable-in-the-middle">>) -> |
| 32 |
{Status, Data, Req1} = cowboy_req:read_body(Req0, #{length => 1000}), |
| 33 |
cowboy_req:cast({set_options, #{decompress_enabled => false}}, Req1), |
| 34 |
{ok, Body, Req} = do_read_body(Status, Req1, Data), |
| 35 |
cowboy_req:reply(200, #{}, Body, Req); |
| 36 |
test(Req0, <<"enable-in-the-middle">>) -> |
| 37 |
{Status, Data, Req1} = cowboy_req:read_body(Req0, #{length => 1000}), |
| 38 |
cowboy_req:cast({set_options, #{decompress_enabled => true}}, Req1), |
| 39 |
{ok, Body, Req} = do_read_body(Status, Req1, Data), |
| 40 |
cowboy_req:reply(200, #{}, Body, Req); |
| 41 |
test(Req0, <<"header-command">>) -> |
| 42 |
{ok, Body, Req1} = read_body(Req0), |
| 43 |
Req = cowboy_req:stream_reply(200, #{}, Req1), |
| 44 |
cowboy_req:stream_body(Body, fin, Req); |
| 45 |
test(Req0, <<"accept-identity">>) -> |
| 46 |
{ok, Body, Req} = read_body(Req0), |
| 47 |
cowboy_req:reply(200, |
| 48 |
#{<<"accept-encoding">> => <<"identity">>}, |
| 49 |
Body, Req); |
| 50 |
test(Req0, <<"invalid-header">>) -> |
| 51 |
{ok, Body, Req} = read_body(Req0), |
| 52 |
cowboy_req:reply(200, |
| 53 |
#{<<"accept-encoding">> => <<";">>}, |
| 54 |
Body, Req); |
| 55 |
test(Req0, <<"reject-explicit-header">>) -> |
| 56 |
{ok, Body, Req} = read_body(Req0), |
| 57 |
cowboy_req:reply(200, |
| 58 |
#{<<"accept-encoding">> => <<"identity, gzip;q=0">>}, |
| 59 |
Body, Req); |
| 60 |
test(Req0, <<"reject-implicit-header">>) -> |
| 61 |
{ok, Body, Req} = read_body(Req0), |
| 62 |
cowboy_req:reply(200, |
| 63 |
#{<<"accept-encoding">> => <<"identity, *;q=0">>}, |
| 64 |
Body, Req); |
| 65 |
test(Req0, <<"accept-explicit-header">>) -> |
| 66 |
{ok, Body, Req} = read_body(Req0), |
| 67 |
cowboy_req:reply(200, |
| 68 |
#{<<"accept-encoding">> => <<"identity, gzip;q=0.5">>}, |
| 69 |
Body, Req); |
| 70 |
test(Req0, <<"accept-implicit-header">>) -> |
| 71 |
{ok, Body, Req} = read_body(Req0), |
| 72 |
cowboy_req:reply(200, |
| 73 |
#{<<"accept-encoding">> => <<"identity, *;q=0.5">>}, |
| 74 |
Body, Req). |
| 75 |
|
| 76 |
read_body(Req0) -> |
| 77 |
{Status, Data, Req} = cowboy_req:read_body(Req0, #{length => 1000}), |
| 78 |
do_read_body(Status, Req, Data). |
| 79 |
|
| 80 |
do_read_body(more, Req0, Acc) -> |
| 81 |
{Status, Data, Req} = cowboy_req:read_body(Req0), |
| 82 |
do_read_body(Status, Req, << Acc/binary, Data/binary >>); |
| 83 |
do_read_body(ok, Req, Acc) -> |
| 84 |
{ok, Acc, Req}. |