| 1 |
%% This module echoes back the value the test is interested in. |
| 2 |
|
| 3 |
-module(echo_h). |
| 4 |
|
| 5 |
-export([init/2]). |
| 6 |
|
| 7 |
init(Req, Opts) -> |
| 8 |
case cowboy_req:binding(arg, Req) of |
| 9 |
undefined -> |
| 10 |
echo(cowboy_req:binding(key, Req), Req, Opts); |
| 11 |
Arg -> |
| 12 |
echo_arg(Arg, Req, Opts) |
| 13 |
end. |
| 14 |
|
| 15 |
echo(<<"read_body">>, Req0, Opts) -> |
| 16 |
case Opts of |
| 17 |
#{crash := true} -> ct_helper:ignore(cowboy_req, read_body, 2); |
| 18 |
_ -> ok |
| 19 |
end, |
| 20 |
{_, Body, Req} = case cowboy_req:path(Req0) of |
| 21 |
<<"/100-continue", _/bits>> -> |
| 22 |
cowboy_req:inform(100, Req0), |
| 23 |
cowboy_req:read_body(Req0); |
| 24 |
<<"/delay", _/bits>> -> |
| 25 |
timer:sleep(500), |
| 26 |
cowboy_req:read_body(Req0); |
| 27 |
<<"/full", _/bits>> -> read_body(Req0, <<>>); |
| 28 |
<<"/auto-sync", _/bits>> -> read_body_auto_sync(Req0, <<>>); |
| 29 |
<<"/auto-async", _/bits>> -> read_body_auto_async(Req0, <<>>); |
| 30 |
<<"/length", _/bits>> -> |
| 31 |
{_, _, Req1} = read_body(Req0, <<>>), |
| 32 |
Length = cowboy_req:body_length(Req1), |
| 33 |
{ok, integer_to_binary(Length), Req1}; |
| 34 |
<<"/opts", _/bits>> -> cowboy_req:read_body(Req0, Opts); |
| 35 |
<<"/spawn", _/bits>> -> |
| 36 |
Parent = self(), |
| 37 |
Pid = spawn_link(fun() -> |
| 38 |
Parent ! {self(), cowboy_req:read_body(Req0)} |
| 39 |
end), |
| 40 |
receive |
| 41 |
{Pid, Msg} -> Msg |
| 42 |
after 5000 -> |
| 43 |
error(timeout) |
| 44 |
end; |
| 45 |
_ -> cowboy_req:read_body(Req0) |
| 46 |
end, |
| 47 |
{ok, cowboy_req:reply(200, #{}, Body, Req), Opts}; |
| 48 |
echo(<<"read_urlencoded_body">>, Req0, Opts) -> |
| 49 |
Path = cowboy_req:path(Req0), |
| 50 |
case {Path, Opts} of |
| 51 |
{<<"/opts", _/bits>>, #{crash := true}} -> ct_helper:ignore(cowboy_req, read_body, 2); |
| 52 |
{_, #{crash := true}} -> ct_helper:ignore(cowboy_req, read_urlencoded_body, 2); |
| 53 |
_ -> ok |
| 54 |
end, |
| 55 |
{ok, Body, Req} = case Path of |
| 56 |
<<"/opts", _/bits>> -> cowboy_req:read_urlencoded_body(Req0, Opts); |
| 57 |
<<"/crash", _/bits>> -> cowboy_req:read_urlencoded_body(Req0, Opts); |
| 58 |
_ -> cowboy_req:read_urlencoded_body(Req0) |
| 59 |
end, |
| 60 |
{ok, cowboy_req:reply(200, #{}, value_to_iodata(Body), Req), Opts}; |
| 61 |
echo(<<"read_and_match_urlencoded_body">>, Req0, Opts) -> |
| 62 |
Path = cowboy_req:path(Req0), |
| 63 |
case {Path, Opts} of |
| 64 |
{<<"/opts", _/bits>>, #{crash := true}} -> ct_helper:ignore(cowboy_req, read_body, 2); |
| 65 |
{_, #{crash := true}} -> ct_helper:ignore(cowboy_req, read_urlencoded_body, 2); |
| 66 |
_ -> ok |
| 67 |
end, |
| 68 |
{ok, Body, Req} = case Path of |
| 69 |
<<"/opts", _/bits>> -> cowboy_req:read_and_match_urlencoded_body([], Req0, Opts); |
| 70 |
<<"/crash", _/bits>> -> cowboy_req:read_and_match_urlencoded_body([], Req0, Opts); |
| 71 |
_ -> cowboy_req:read_and_match_urlencoded_body([], Req0) |
| 72 |
end, |
| 73 |
{ok, cowboy_req:reply(200, #{}, value_to_iodata(Body), Req), Opts}; |
| 74 |
echo(<<"uri">>, Req, Opts) -> |
| 75 |
Value = case cowboy_req:path_info(Req) of |
| 76 |
[<<"origin">>] -> cowboy_req:uri(Req, #{host => undefined}); |
| 77 |
[<<"protocol-relative">>] -> cowboy_req:uri(Req, #{scheme => undefined}); |
| 78 |
[<<"no-qs">>] -> cowboy_req:uri(Req, #{qs => undefined}); |
| 79 |
[<<"no-path">>] -> cowboy_req:uri(Req, #{path => undefined, qs => undefined}); |
| 80 |
[<<"set-port">>] -> cowboy_req:uri(Req, #{port => 123}); |
| 81 |
_ -> cowboy_req:uri(Req) |
| 82 |
end, |
| 83 |
{ok, cowboy_req:reply(200, #{}, Value, Req), Opts}; |
| 84 |
echo(<<"match">>, Req, Opts) -> |
| 85 |
[Type|Fields0] = cowboy_req:path_info(Req), |
| 86 |
Fields = [binary_to_atom(F, latin1) || F <- Fields0], |
| 87 |
Value = case Type of |
| 88 |
<<"qs">> -> cowboy_req:match_qs(Fields, Req); |
| 89 |
<<"qs_with_constraints">> -> cowboy_req:match_qs([{id, integer}], Req); |
| 90 |
<<"qs_with_max_keys">> -> cowboy_req:match_qs([{a, nonempty}], Req, #{max_keys => 5}); |
| 91 |
<<"cookies">> -> cowboy_req:match_cookies(Fields, Req); |
| 92 |
<<"cookies_with_max_cookies">> -> |
| 93 |
cowboy_req:match_cookies([{a, nonempty}], Req, #{max_cookies => 5}); |
| 94 |
<<"body_qs">> -> |
| 95 |
%% Note that the Req should not be discarded but for the |
| 96 |
%% purpose of this test this has no ill impacts. |
| 97 |
{ok, Match, _} = cowboy_req:read_and_match_urlencoded_body(Fields, Req), |
| 98 |
Match |
| 99 |
end, |
| 100 |
{ok, cowboy_req:reply(200, #{}, value_to_iodata(Value), Req), Opts}; |
| 101 |
echo(<<"filter_then_parse_cookies">>, Req0, Opts) -> |
| 102 |
Req = cowboy_req:filter_cookies([cake, color], Req0), |
| 103 |
Value = cowboy_req:parse_cookies(Req), |
| 104 |
{ok, cowboy_req:reply(200, #{}, value_to_iodata(Value), Req), Opts}; |
| 105 |
echo(<<"parse_qs_with_max_keys">>, Req, Opts) -> |
| 106 |
[_|_] = cowboy_req:parse_qs(Req, #{max_keys => 5}), |
| 107 |
{ok, cowboy_req:reply(200, #{}, Req), Opts}; |
| 108 |
echo(<<"parse_cookies_with_max_cookies">>, Req, Opts) -> |
| 109 |
[_|_] = cowboy_req:parse_cookies(Req, #{max_cookies => 5}), |
| 110 |
{ok, cowboy_req:reply(200, #{}, Req), Opts}; |
| 111 |
echo(What, Req, Opts) -> |
| 112 |
Key = binary_to_atom(What, latin1), |
| 113 |
Value = case cowboy_req:path(Req) of |
| 114 |
<<"/direct/",_/bits>> -> maps:get(Key, Req); |
| 115 |
_ -> cowboy_req:Key(Req) |
| 116 |
end, |
| 117 |
{ok, cowboy_req:reply(200, #{}, value_to_iodata(Value), Req), Opts}. |
| 118 |
|
| 119 |
echo_arg(Arg0, Req, Opts) -> |
| 120 |
F = binary_to_atom(cowboy_req:binding(key, Req), latin1), |
| 121 |
Arg = case F of |
| 122 |
binding -> binary_to_atom(Arg0, latin1); |
| 123 |
_ -> Arg0 |
| 124 |
end, |
| 125 |
Value = case cowboy_req:binding(default, Req) of |
| 126 |
undefined -> cowboy_req:F(Arg, Req); |
| 127 |
Default -> cowboy_req:F(Arg, Req, Default) |
| 128 |
end, |
| 129 |
{ok, cowboy_req:reply(200, #{}, value_to_iodata(Value), Req), Opts}. |
| 130 |
|
| 131 |
read_body(Req0, Acc) -> |
| 132 |
case cowboy_req:read_body(Req0) of |
| 133 |
{ok, Data, Req} -> {ok, << Acc/binary, Data/binary >>, Req}; |
| 134 |
{more, Data, Req} -> read_body(Req, << Acc/binary, Data/binary >>) |
| 135 |
end. |
| 136 |
|
| 137 |
read_body_auto_sync(Req0, Acc) -> |
| 138 |
Opts = #{length => auto, period => infinity}, |
| 139 |
case cowboy_req:read_body(Req0, Opts) of |
| 140 |
{ok, Data, Req} -> {ok, << Acc/binary, Data/binary >>, Req}; |
| 141 |
{more, Data, Req} -> read_body_auto_sync(Req, << Acc/binary, Data/binary >>) |
| 142 |
end. |
| 143 |
|
| 144 |
read_body_auto_async(Req, Acc) -> |
| 145 |
read_body_auto_async(Req, make_ref(), Acc). |
| 146 |
|
| 147 |
read_body_auto_async(Req, ReadBodyRef, Acc) -> |
| 148 |
cowboy_req:cast({read_body, self(), ReadBodyRef, auto, infinity}, Req), |
| 149 |
receive |
| 150 |
{request_body, ReadBodyRef, nofin, Data} -> |
| 151 |
read_body_auto_async(Req, ReadBodyRef, <<Acc/binary, Data/binary>>); |
| 152 |
{request_body, ReadBodyRef, fin, _, Data} -> |
| 153 |
{ok, <<Acc/binary, Data/binary>>, Req} |
| 154 |
end. |
| 155 |
|
| 156 |
value_to_iodata(V) when is_integer(V) -> integer_to_binary(V); |
| 157 |
value_to_iodata(V) when is_atom(V) -> atom_to_binary(V, latin1); |
| 158 |
value_to_iodata(V) when is_list(V); is_tuple(V); is_map(V) -> io_lib:format("~999999p", [V]); |
| 159 |
value_to_iodata(V) -> V. |