stop_handler_h.erl 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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([rate_limited/2]).
  24. -export([resource_exists/2]).
  25. -export([service_available/2]).
  26. -export([uri_too_long/2]).
  27. -export([valid_content_headers/2]).
  28. -export([valid_entity_length/2]).
  29. -export([accept/2]).
  30. -export([provide/2]).
  31. init(Req, State) ->
  32. {cowboy_rest, Req, State}.
  33. allowed_methods(Req, State) ->
  34. maybe_stop_handler(Req, State, ?FUNCTION_NAME).
  35. allow_missing_post(Req, State) ->
  36. maybe_stop_handler(Req, State, ?FUNCTION_NAME).
  37. charsets_provided(Req, State) ->
  38. maybe_stop_handler(Req, State, ?FUNCTION_NAME).
  39. content_types_accepted(Req, State) ->
  40. maybe_stop_handler(Req, State, ?FUNCTION_NAME).
  41. content_types_provided(Req, State) ->
  42. maybe_stop_handler(Req, State, ?FUNCTION_NAME).
  43. delete_completed(Req, State) ->
  44. maybe_stop_handler(Req, State, ?FUNCTION_NAME).
  45. delete_resource(Req, State) ->
  46. maybe_stop_handler(Req, State, ?FUNCTION_NAME).
  47. forbidden(Req, State) ->
  48. maybe_stop_handler(Req, State, ?FUNCTION_NAME).
  49. is_authorized(Req, State) ->
  50. maybe_stop_handler(Req, State, ?FUNCTION_NAME).
  51. is_conflict(Req, State) ->
  52. maybe_stop_handler(Req, State, ?FUNCTION_NAME).
  53. known_methods(Req, State) ->
  54. maybe_stop_handler(Req, State, ?FUNCTION_NAME).
  55. languages_provided(Req, State) ->
  56. maybe_stop_handler(Req, State, ?FUNCTION_NAME).
  57. malformed_request(Req, State) ->
  58. maybe_stop_handler(Req, State, ?FUNCTION_NAME).
  59. moved_permanently(Req, State) ->
  60. maybe_stop_handler(Req, State, ?FUNCTION_NAME).
  61. moved_temporarily(Req, State) ->
  62. maybe_stop_handler(Req, State, ?FUNCTION_NAME).
  63. multiple_choices(Req, State) ->
  64. maybe_stop_handler(Req, State, ?FUNCTION_NAME).
  65. options(Req, State) ->
  66. maybe_stop_handler(Req, State, ?FUNCTION_NAME).
  67. previously_existed(Req, State) ->
  68. maybe_stop_handler(Req, State, ?FUNCTION_NAME).
  69. rate_limited(Req, State) ->
  70. maybe_stop_handler(Req, State, ?FUNCTION_NAME).
  71. resource_exists(Req, State) ->
  72. maybe_stop_handler(Req, State, ?FUNCTION_NAME).
  73. service_available(Req, State) ->
  74. maybe_stop_handler(Req, State, ?FUNCTION_NAME).
  75. uri_too_long(Req, State) ->
  76. maybe_stop_handler(Req, State, ?FUNCTION_NAME).
  77. valid_content_headers(Req, State) ->
  78. maybe_stop_handler(Req, State, ?FUNCTION_NAME).
  79. valid_entity_length(Req, State) ->
  80. maybe_stop_handler(Req, State, ?FUNCTION_NAME).
  81. accept(Req, State) ->
  82. maybe_stop_handler(Req, State, ?FUNCTION_NAME).
  83. provide(Req, State) ->
  84. maybe_stop_handler(Req, State, ?FUNCTION_NAME).
  85. maybe_stop_handler(Req=#{qs := Qs}, State, StateName) ->
  86. case atom_to_binary(StateName, latin1) of
  87. Qs -> do_stop_handler(Req, State);
  88. _ -> do_default(Req, State, StateName)
  89. end.
  90. %% These are all the methods necessary to reach all callbacks.
  91. do_default(Req, State, allowed_methods) ->
  92. {[<<"GET">>, <<"POST">>, <<"PUT">>, <<"DELETE">>, <<"OPTIONS">>], Req, State};
  93. %% We need to accept/provide media types to reach these callbacks.
  94. do_default(Req, State, content_types_accepted) ->
  95. {[{<<"text/plain">>, accept}], Req, State};
  96. do_default(Req, State, content_types_provided) ->
  97. {[{<<"text/plain">>, provide}], Req, State};
  98. %% We need resource_exists to return false to reach these callbacks.
  99. do_default(Req=#{qs := <<"allow_missing_post">>}, State, resource_exists) ->
  100. {false, Req, State};
  101. do_default(Req=#{qs := <<"moved_permanently">>}, State, resource_exists) ->
  102. {false, Req, State};
  103. do_default(Req=#{qs := <<"moved_temporarily">>}, State, resource_exists) ->
  104. {false, Req, State};
  105. do_default(Req=#{qs := <<"previously_existed">>}, State, resource_exists) ->
  106. {false, Req, State};
  107. %% We need previously_existed to return true to reach these callbacks.
  108. do_default(Req=#{qs := <<"moved_permanently">>}, State, previously_existed) ->
  109. {true, Req, State};
  110. do_default(Req=#{qs := <<"moved_temporarily">>}, State, previously_existed) ->
  111. {true, Req, State};
  112. %% We need the DELETE to suceed to reach this callback.
  113. do_default(Req=#{qs := <<"delete_completed">>}, State, delete_resource) ->
  114. {true, Req, State};
  115. %% We should never reach these two callbacks.
  116. do_default(Req, State, accept) ->
  117. {false, Req, State};
  118. do_default(Req, State, provide) ->
  119. {<<"This is REST!">>, Req, State};
  120. %% Simulate the callback being missing in any other cases.
  121. do_default(_, _, _) ->
  122. no_call.
  123. do_stop_handler(Req0, State) ->
  124. Req = cowboy_req:reply(<<"248 REST handler stopped!">>, #{}, <<>>, Req0),
  125. {stop, Req, State}.