Debian Codegraph @

test/handlers/switch_handler_h.erl

raw
1 %% This module returns switch_handler based on the query string.
2
3 -module(switch_handler_h).
4
5 -export([init/2]).
6
7 -export([allowed_methods/2]).
8 -export([allow_missing_post/2]).
9 -export([charsets_provided/2]).
10 -export([content_types_accepted/2]).
11 -export([content_types_provided/2]).
12 -export([delete_completed/2]).
13 -export([delete_resource/2]).
14 -export([forbidden/2]).
15 -export([is_authorized/2]).
16 -export([is_conflict/2]).
17 -export([known_methods/2]).
18 -export([languages_provided/2]).
19 -export([malformed_request/2]).
20 -export([moved_permanently/2]).
21 -export([moved_temporarily/2]).
22 -export([multiple_choices/2]).
23 -export([options/2]).
24 -export([previously_existed/2]).
25 -export([range_satisfiable/2]).
26 -export([ranges_provided/2]).
27 -export([rate_limited/2]).
28 -export([resource_exists/2]).
29 -export([service_available/2]).
30 -export([uri_too_long/2]).
31 -export([valid_content_headers/2]).
32 -export([valid_entity_length/2]).
33
34 -export([accept/2]).
35 -export([provide/2]).
36 -export([provide_range/2]).
37
38 -export([info/3]).
39
40 init(Req, State) ->
41 {cowboy_rest, Req, State}.
42
43 allowed_methods(Req, State) ->
44 maybe_switch_handler(Req, State, ?FUNCTION_NAME).
45
46 allow_missing_post(Req, State) ->
47 maybe_switch_handler(Req, State, ?FUNCTION_NAME).
48
49 charsets_provided(Req, State) ->
50 maybe_switch_handler(Req, State, ?FUNCTION_NAME).
51
52 content_types_accepted(Req, State) ->
53 maybe_switch_handler(Req, State, ?FUNCTION_NAME).
54
55 content_types_provided(Req, State) ->
56 maybe_switch_handler(Req, State, ?FUNCTION_NAME).
57
58 delete_completed(Req, State) ->
59 maybe_switch_handler(Req, State, ?FUNCTION_NAME).
60
61 delete_resource(Req, State) ->
62 maybe_switch_handler(Req, State, ?FUNCTION_NAME).
63
64 forbidden(Req, State) ->
65 maybe_switch_handler(Req, State, ?FUNCTION_NAME).
66
67 is_authorized(Req, State) ->
68 maybe_switch_handler(Req, State, ?FUNCTION_NAME).
69
70 is_conflict(Req, State) ->
71 maybe_switch_handler(Req, State, ?FUNCTION_NAME).
72
73 known_methods(Req, State) ->
74 maybe_switch_handler(Req, State, ?FUNCTION_NAME).
75
76 languages_provided(Req, State) ->
77 maybe_switch_handler(Req, State, ?FUNCTION_NAME).
78
79 malformed_request(Req, State) ->
80 maybe_switch_handler(Req, State, ?FUNCTION_NAME).
81
82 moved_permanently(Req, State) ->
83 maybe_switch_handler(Req, State, ?FUNCTION_NAME).
84
85 moved_temporarily(Req, State) ->
86 maybe_switch_handler(Req, State, ?FUNCTION_NAME).
87
88 multiple_choices(Req, State) ->
89 maybe_switch_handler(Req, State, ?FUNCTION_NAME).
90
91 options(Req, State) ->
92 maybe_switch_handler(Req, State, ?FUNCTION_NAME).
93
94 previously_existed(Req, State) ->
95 maybe_switch_handler(Req, State, ?FUNCTION_NAME).
96
97 range_satisfiable(Req, State) ->
98 maybe_switch_handler(Req, State, ?FUNCTION_NAME).
99
100 ranges_provided(Req, State) ->
101 maybe_switch_handler(Req, State, ?FUNCTION_NAME).
102
103 rate_limited(Req, State) ->
104 maybe_switch_handler(Req, State, ?FUNCTION_NAME).
105
106 resource_exists(Req, State) ->
107 maybe_switch_handler(Req, State, ?FUNCTION_NAME).
108
109 service_available(Req, State) ->
110 maybe_switch_handler(Req, State, ?FUNCTION_NAME).
111
112 uri_too_long(Req, State) ->
113 maybe_switch_handler(Req, State, ?FUNCTION_NAME).
114
115 valid_content_headers(Req, State) ->
116 maybe_switch_handler(Req, State, ?FUNCTION_NAME).
117
118 valid_entity_length(Req, State) ->
119 maybe_switch_handler(Req, State, ?FUNCTION_NAME).
120
121 accept(Req, State) ->
122 maybe_switch_handler(Req, State, ?FUNCTION_NAME).
123
124 provide(Req, State) ->
125 maybe_switch_handler(Req, State, ?FUNCTION_NAME).
126
127 provide_range(Req, State) ->
128 maybe_switch_handler(Req, State, ?FUNCTION_NAME).
129
130 maybe_switch_handler(Req=#{qs := Qs}, State, StateName) ->
131 case atom_to_binary(StateName, latin1) of
132 Qs -> do_switch_handler(Req, State);
133 _ -> do_default(Req, State, StateName)
134 end.
135
136 %% These are all the methods necessary to reach all callbacks.
137 do_default(Req, State, allowed_methods) ->
138 {[<<"GET">>, <<"POST">>, <<"PUT">>, <<"DELETE">>, <<"OPTIONS">>], Req, State};
139 %% We need to accept/provide media types to reach these callbacks.
140 do_default(Req, State, content_types_accepted) ->
141 {[{<<"text/plain">>, accept}], Req, State};
142 do_default(Req, State, content_types_provided) ->
143 {[{<<"text/plain">>, provide}], Req, State};
144 %% We need to accept ranges to reach these callbacks.
145 do_default(Req=#{qs := <<"range_satisfiable">>}, State, ranges_provided) ->
146 {[{<<"bytes">>, provide_range}], Req, State};
147 do_default(Req=#{qs := <<"provide_range">>}, State, ranges_provided) ->
148 {[{<<"bytes">>, provide_range}], Req, State};
149 %% We need resource_exists to return false to reach these callbacks.
150 do_default(Req=#{qs := <<"allow_missing_post">>}, State, resource_exists) ->
151 {false, Req, State};
152 do_default(Req=#{qs := <<"moved_permanently">>}, State, resource_exists) ->
153 {false, Req, State};
154 do_default(Req=#{qs := <<"moved_temporarily">>}, State, resource_exists) ->
155 {false, Req, State};
156 do_default(Req=#{qs := <<"previously_existed">>}, State, resource_exists) ->
157 {false, Req, State};
158 %% We need previously_existed to return true to reach these callbacks.
159 do_default(Req=#{qs := <<"moved_permanently">>}, State, previously_existed) ->
160 {true, Req, State};
161 do_default(Req=#{qs := <<"moved_temporarily">>}, State, previously_existed) ->
162 {true, Req, State};
163 %% We need the DELETE to suceed to reach this callback.
164 do_default(Req=#{qs := <<"delete_completed">>}, State, delete_resource) ->
165 {true, Req, State};
166 %% We should never reach these callbacks.
167 do_default(Req, State, accept) ->
168 {false, Req, State};
169 do_default(Req, State, provide) ->
170 {<<"This is REST!">>, Req, State};
171 do_default(Req, State, provide_range) ->
172 {<<"This is ranged REST!">>, Req, State};
173 %% Simulate the callback being missing in any other cases.
174 do_default(_, _, _) ->
175 no_call.
176
177 do_switch_handler(Req0, run) ->
178 Req = cowboy_req:stream_reply(200, Req0),
179 send_after(0),
180 {{switch_handler, cowboy_loop}, Req, 0};
181 do_switch_handler(Req0, hibernate) ->
182 Req = cowboy_req:stream_reply(200, Req0),
183 send_after(0),
184 {{switch_handler, cowboy_loop, hibernate}, Req, 0}.
185
186 send_after(N) ->
187 erlang:send_after(100, self(), {stream, msg(N)}).
188
189 msg(0) -> <<"Hello\n">>;
190 msg(1) -> <<"streamed\n">>;
191 msg(2) -> <<"world!\n">>;
192 msg(3) -> stop.
193
194 info({stream, stop}, Req, State) ->
195 {stop, Req, State};
196 info({stream, What}, Req, State) ->
197 cowboy_req:stream_body(What, nofin, Req),
198 send_after(State + 1),
199 {ok, Req, State + 1}.