0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-15 04:30:25 +00:00
cowboy/examples/echo_get/src/toppage_handler.erl

30 lines
722 B
Erlang
Raw Normal View History

2012-07-21 21:46:21 +02:00
%% Feel free to use, reuse and abuse the code in this file.
%% @doc GET echo handler.
-module(toppage_handler).
-export([init/3]).
-export([handle/2]).
-export([terminate/2]).
init(_Transport, Req, []) ->
{ok, Req, undefined}.
handle(Req, State) ->
2012-08-27 13:28:57 +02:00
{Method, Req2} = cowboy_req:method(Req),
{Echo, Req3} = cowboy_req:qs_val(<<"echo">>, Req2),
2012-07-21 21:46:21 +02:00
{ok, Req4} = echo(Method, Echo, Req3),
{ok, Req4, State}.
echo(<<"GET">>, undefined, Req) ->
2012-08-27 13:28:57 +02:00
cowboy_req:reply(400, [], <<"Missing echo parameter.">>, Req);
echo(<<"GET">>, Echo, Req) ->
2012-08-27 13:28:57 +02:00
cowboy_req:reply(200,
[{<<"content-encoding">>, <<"utf-8">>}], Echo, Req);
2012-07-21 21:46:21 +02:00
echo(_, _, Req) ->
%% Method not allowed.
2012-08-27 13:28:57 +02:00
cowboy_req:reply(405, Req).
2012-07-21 21:46:21 +02:00
terminate(_Req, _State) ->
ok.