stop_handler_h.erl 5.6 KB

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