Debian Codegraph @

test/handlers/stream_handler_h.erl

raw
1 %% This module behaves differently depending on a specific header.
2
3 -module(stream_handler_h).
4 -behavior(cowboy_stream).
5
6 -export([init/3]).
7 -export([data/4]).
8 -export([info/3]).
9 -export([terminate/3]).
10 -export([early_error/5]).
11
12 %% For switch_protocol.
13 -export([takeover/7]).
14
15 -record(state, {
16 pid,
17 test
18 }).
19
20 init(StreamID, Req, Opts) ->
21 Pid = list_to_pid(binary_to_list(cowboy_req:header(<<"x-test-pid">>, Req))),
22 Test = binary_to_atom(cowboy_req:header(<<"x-test-case">>, Req), latin1),
23 State = #state{pid=Pid, test=Test},
24 Pid ! {Pid, self(), init, StreamID, Req, Opts},
25 {init_commands(StreamID, Req, State), State}.
26
27 init_commands(_, _, #state{test=crash_in_init}) ->
28 error(crash);
29 init_commands(_, _, #state{test=crash_in_data}) ->
30 [];
31 init_commands(_, _, #state{test=crash_in_info}) ->
32 [];
33 init_commands(_, _, #state{test=crash_in_terminate}) ->
34 [{response, 200, #{<<"content-length">> => <<"12">>}, <<"Hello world!">>}, stop];
35 init_commands(_, _, #state{test=crash_in_early_error}) ->
36 error(crash);
37 init_commands(_, _, #state{test=flow_after_body_fully_read}) ->
38 [];
39 init_commands(_, _, #state{test=set_options_ignore_unknown}) ->
40 [
41 {set_options, #{unknown_options => true}},
42 {response, 200, #{<<"content-length">> => <<"12">>}, <<"Hello world!">>},
43 stop
44 ];
45 init_commands(_, _, State=#state{test=shutdown_on_stream_stop}) ->
46 Spawn = init_process(false, State),
47 [{spawn, Spawn, 5000}, {headers, 200, #{}}, stop];
48 init_commands(_, _, State=#state{test=shutdown_on_socket_close}) ->
49 Spawn = init_process(false, State),
50 [{spawn, Spawn, 5000}, {headers, 200, #{}}];
51 init_commands(_, _, State=#state{test=shutdown_timeout_on_stream_stop}) ->
52 Spawn = init_process(true, State),
53 [{spawn, Spawn, 2000}, {headers, 200, #{}}, stop];
54 init_commands(_, _, State=#state{test=shutdown_timeout_on_socket_close}) ->
55 Spawn = init_process(true, State),
56 [{spawn, Spawn, 2000}, {headers, 200, #{}}];
57 init_commands(_, _, State=#state{test=switch_protocol_after_headers}) ->
58 [{headers, 200, #{}}, {switch_protocol, #{}, ?MODULE, State}];
59 init_commands(_, _, State=#state{test=switch_protocol_after_headers_data}) ->
60 [{headers, 200, #{}}, {data, fin, <<"{}">>}, {switch_protocol, #{}, ?MODULE, State}];
61 init_commands(_, _, State=#state{test=switch_protocol_after_response}) ->
62 [{response, 200, #{}, <<"{}">>}, {switch_protocol, #{}, ?MODULE, State}];
63 init_commands(_, _, State=#state{test=terminate_on_switch_protocol}) ->
64 [{switch_protocol, #{
65 <<"connection">> => <<"upgrade">>,
66 <<"upgrade">> => <<"shsp">>
67 }, ?MODULE, State}];
68 init_commands(_, _, #state{test=terminate_on_stop}) ->
69 [{response, 204, #{}, <<>>}];
70 init_commands(_, _, _) ->
71 [{headers, 200, #{}}].
72
73 init_process(TrapExit, #state{pid=Pid}) ->
74 Self = self(),
75 Spawn = spawn_link(fun() ->
76 process_flag(trap_exit, TrapExit),
77 Pid ! {Pid, Self, spawned, self()},
78 receive {Pid, ready} -> ok after 1000 -> error(timeout) end,
79 Self ! {self(), ready},
80 receive after 5000 ->
81 Pid ! {Pid, Self, still_alive, self()}
82 end
83 end),
84 receive {Spawn, ready} -> ok after 1000 -> error(timeout) end,
85 Spawn.
86
87 data(_, _, _, #state{test=crash_in_data}) ->
88 error(crash);
89 data(_, fin, <<"Hello world!">>, State=#state{test=flow_after_body_fully_read}) ->
90 {[{flow, 12}, {response, 200, #{}, <<"{}">>}], State};
91 data(StreamID, IsFin, Data, State=#state{pid=Pid}) ->
92 Pid ! {Pid, self(), data, StreamID, IsFin, Data, State},
93 {[], State}.
94
95 info(_, Resp={response, _, _, _}, State) ->
96 {[Resp], State};
97 info(_, crash, #state{test=crash_in_info}) ->
98 error(crash);
99 info(StreamID, Info, State=#state{pid=Pid}) ->
100 Pid ! {Pid, self(), info, StreamID, Info, State},
101 case Info of
102 please_stop -> {[stop], State};
103 _ -> {[Info], State}
104 end.
105
106 terminate(StreamID, Reason, State=#state{pid=Pid, test=crash_in_terminate}) ->
107 Pid ! {Pid, self(), terminate, StreamID, Reason, State},
108 error(crash);
109 terminate(StreamID, Reason, State=#state{pid=Pid}) ->
110 Pid ! {Pid, self(), terminate, StreamID, Reason, State},
111 ok.
112
113 %% This clause can only test for early errors that reached the required headers.
114 early_error(StreamID, Reason, PartialReq, Resp, Opts) ->
115 Pid = list_to_pid(binary_to_list(cowboy_req:header(<<"x-test-pid">>, PartialReq))),
116 Pid ! {Pid, self(), early_error, StreamID, Reason, PartialReq, Resp, Opts},
117 case cowboy_req:header(<<"x-test-case">>, PartialReq) of
118 <<"crash_in_early_error",_/bits>> -> error(crash);
119 _ -> Resp
120 end.
121
122 %% @todo It would be good if we could allow this function to return normally.
123 -spec takeover(_, _, _, _, _, _, _) -> no_return().
124 takeover(Parent, Ref, Socket, Transport, Opts, Buffer, State=#state{pid=Pid}) ->
125 Pid ! {Pid, self(), takeover, Parent, Ref, Socket, Transport, Opts, Buffer, State},
126 exit(normal).