toppage_handler.erl 722 B

1234567891011121314151617181920212223242526272829
  1. %% Feel free to use, reuse and abuse the code in this file.
  2. %% @doc GET echo handler.
  3. -module(toppage_handler).
  4. -export([init/3]).
  5. -export([handle/2]).
  6. -export([terminate/2]).
  7. init(_Transport, Req, []) ->
  8. {ok, Req, undefined}.
  9. handle(Req, State) ->
  10. {Method, Req2} = cowboy_req:method(Req),
  11. {Echo, Req3} = cowboy_req:qs_val(<<"echo">>, Req2),
  12. {ok, Req4} = echo(Method, Echo, Req3),
  13. {ok, Req4, State}.
  14. echo(<<"GET">>, undefined, Req) ->
  15. cowboy_req:reply(400, [], <<"Missing echo parameter.">>, Req);
  16. echo(<<"GET">>, Echo, Req) ->
  17. cowboy_req:reply(200,
  18. [{<<"Content-Encoding">>, <<"utf-8">>}], Echo, Req);
  19. echo(_, _, Req) ->
  20. %% Method not allowed.
  21. cowboy_req:reply(405, Req).
  22. terminate(_Req, _State) ->
  23. ok.