rest_forbidden_resource.erl 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. -module(rest_forbidden_resource).
  2. -export([init/3, rest_init/2, allowed_methods/2, forbidden/2,
  3. content_types_provided/2, content_types_accepted/2,
  4. post_is_create/2, create_path/2, to_text/2, from_text/2]).
  5. init(_Transport, _Req, _Opts) ->
  6. {upgrade, protocol, cowboy_http_rest}.
  7. rest_init(Req, [Forbidden]) ->
  8. {Headers, Req2} = cowboy_http_req:headers(Req),
  9. {Method, Req3} = cowboy_http_req:method(Req2),
  10. ct:print("method ~p headers ~p", [Method, Headers]),
  11. {ok, Req3, Forbidden}.
  12. allowed_methods(Req, State) ->
  13. {['GET', 'HEAD', 'POST'], Req, State}.
  14. forbidden(Req, State=true) ->
  15. {true, Req, State};
  16. forbidden(Req, State=false) ->
  17. {false, Req, State}.
  18. content_types_provided(Req, State) ->
  19. {[{{<<"text">>, <<"plain">>, []}, to_text}], Req, State}.
  20. content_types_accepted(Req, State) ->
  21. {[{{<<"text">>, <<"plain">>, []}, from_text}], Req, State}.
  22. post_is_create(Req, State) ->
  23. {true, Req, State}.
  24. create_path(Req, State) ->
  25. {Path, Req2} = cowboy_http_req:raw_path(Req),
  26. {Path, Req2, State}.
  27. to_text(Req, State) ->
  28. {<<"This is REST!">>, Req, State}.
  29. from_text(Req, State) ->
  30. {true, Req, State}.