toppage_handler.erl 685 B

12345678910111213141516171819202122232425262728
  1. %% Feel free to use, reuse and abuse the code in this file.
  2. %% @doc Handler with basic HTTP authorization.
  3. -module(toppage_handler).
  4. -export([init/2]).
  5. -export([content_types_provided/2]).
  6. -export([is_authorized/2]).
  7. -export([to_text/2]).
  8. init(Req, Opts) ->
  9. {cowboy_rest, Req, Opts}.
  10. is_authorized(Req, State) ->
  11. case cowboy_req:parse_header(<<"authorization">>, Req) of
  12. {basic, User = <<"Alladin">>, <<"open sesame">>} ->
  13. {true, Req, User};
  14. _ ->
  15. {{false, <<"Basic realm=\"cowboy\"">>}, Req, State}
  16. end.
  17. content_types_provided(Req, State) ->
  18. {[
  19. {<<"text/plain">>, to_text}
  20. ], Req, State}.
  21. to_text(Req, User) ->
  22. {<< "Hello, ", User/binary, "!\n" >>, Req, User}.