Debian Codegraph @

test/handlers/stop_handler_h.erl

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