switch_handler_h.erl 5.3 KB

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