[erlang-questions] Cowboy long-lived HTTP handler

Loïc Hoguin essen@REDACTED
Sun Nov 25 13:45:13 CET 2012


HTTP is a request/response protocol, you can reply more than once to the 
same request.

You can either send nothing until you receive some message, reply and 
stop looping; OR initiate streaming in init/3 and then stream data in 
info/3.

On 11/25/2012 12:14 PM, tgrk wrote:
> Because I do not have any problems using cowboy_http_handler, I assume
> that my implementation of cowboy_loop_handler is not correct.
>
> Here is what I am trying to do in init/3 and info/3:
> ----------------------------------------------------------------------------------------
> ...
> init({_Transport, http}, Req, Opts) ->
>      io:format("handler:init ~p, ~p~n", [Req, Opts]),
> %    erlang:send_after(1000, self(), error_timeout),
>      {ok, Req3} = cowboy_req:reply(200, Req),
>      {loop, Req3, undefined, 5000, hibernate}.
>
> info(error_timeout, Req, State) ->
>      io:format("handler:info: error_timeout ~p, ~p~n", [Req, State]),
>      {ok, Req2} = cowboy_req:reply(500, Req),
>      {ok, Req2, State};
> info(Any, Req, State) ->
>      io:format("handler:info: any=~p, ~p, ~p~n", [Any, Req, State]),
>      {ok, Req2} = cowboy_req:reply(200, Req),
>      {loop, Req2, State, hibernate}.
> ...
>
> Error message with stacktrace:
> ----------------------------------------------------------------------------------------
> =ERROR REPORT==== 25-Nov-2012::12:02:07 ===
> ** Handler test_handler terminating in info/3
>     for the reason error:function_clause
> ** Handler state was undefined
> ** Request was [{socket,#Port<0.58013>},
>                  {transport,ranch_tcp},
>                  {connection,keepalive},
>                  {pid,<0.402.0>},
>                  {method,<<"POST">>},
>                  {version,{1,1}},
>                  {peer,undefined},
>                  {host,<<"127.0.0.1">>},
>                  {host_info,undefined},
>                  {port,8080},
>                  {path,<<"/rpc/checkout/">>},
>                  {path_info,undefined},
>                  {qs,<<>>},
>                  {qs_vals,undefined},
>                  {fragment,<<>>},
>                  {bindings,[]},
>                  {headers,[{<<"content-type">>,<<"application/json">>},
>                            {<<"content-length">>,<<"71">>},
>                            {<<"te">>,<<>>},
>                            {<<"host">>,<<"127.0.0.1:8080
> <http://127.0.0.1:8080>">>},
>                            {<<"connection">>,<<"keep-alive">>}]},
>                  {p_headers,[{<<"connection">>,[<<"keep-alive">>]}]},
>                  {cookies,undefined},
>                  {meta,[]},
>                  {body_state,waiting},
>                  {multipart,undefined},
>
> {buffer,<<"{\"jsonrpc\":\"2.0\",\"method\":\"foo\",\"params\":{\"param1\":\"val1\"},\"id\":0}">>},
>                  {resp_state,done},
>                  {resp_headers,[]},
>                  {resp_body,<<>>},
>                  {onresponse,undefined}]
> ** Stacktrace: [{cowboy_req,reply,
>                      [200,
>                       [{<<"Content-Type">>,<<"application/json">>}],
>                       <<"{message: \"hello world\"}">>,
>
>   {http_req,#Port<0.57251>,ranch_tcp,keepalive,<0.401.0>,
>                           <<"POST">>,
>                           {1,1},
>                           undefined,<<"127.0.0.1">>,undefined,8080,
>
>   <<"/rpc/checkout/">>,undefined,<<>>,undefined,<<>>,
>                           [],
>                           [{<<"content-type">>,<<"application/json">>},
>                            {<<"content-length">>,<<"71">>},
>                            {<<"te">>,<<>>},
>                            {<<"host">>,<<"127.0.0.1:8080
> <http://127.0.0.1:8080>">>},
>                            {<<"connection">>,<<"keep-alive">>}],
>                           [{<<"connection">>,[<<"keep-alive">>]}],
>                           undefined,[],waiting,undefined,
>
>   <<"{\"jsonrpc\":\"2.0\",\"method\":\"foo\",\"params\":{\"param1\":\"val1\"},\"id\":0}">>,
>                           done,[],<<>>,undefined}],
>                      [{file,"src/cowboy_req.erl"},{line,873}]},
>                  {test_handler,info,3,
>                      [{file,"src/test_handler.erl"},{line,49}]},
>                  {cowboy_protocol,handler_call,5,
>                      [{file,"src/cowboy_protocol.erl"},{line,569}]}]
>
> test/test_handler_test.erl:49:<0.396.0>: Resp = {ok,{{"HTTP/1.1",200,"OK"},
>              [{"connection","keep-alive"},
>               {"date","Sun, 25 Nov 2012 11:02:07 GMT"},
>               {"server","Cowboy"},
>               {"content-length","0"}],
>              []}}
>
> =ERROR REPORT==== 25-Nov-2012::12:02:07 ===
> Received unexpected tcp data on #Port<0.58012>
>     Data:       <<"HTTP/1.1 500 Internal Server Error\r\nconnection:
> close\r\nserver: Cowboy\r\ndate: Sun, 25 Nov 2012 11:02:07
> GMT\r\ncontent-length: 0\r\n\r\n">>
>     MFA:        undefined
>     Request:    undefined
>     Session:    {session,{{"127.0.0.1",8080},<0.399.0>},
>
> false,http,#Port<0.58012>,ip_comm,1,keep_alive,true}
>     Status:     keep_alive
>     StatusLine: undefined
>     Profile:    httpc_manager
>
>
> Regards,
> Martin
>
>
> On Sat, Nov 24, 2012 at 7:50 PM, Loïc Hoguin <essen@REDACTED
> <mailto:essen@REDACTED>> wrote:
>
>     Please post the crash message.
>
>
>     On 11/24/2012 05:53 PM, tgrk wrote:
>
>         Hello,
>
>              has anyone played with Cowboy loop_handler behaviour? I am
>         trying to
>         handle JSON-RPC using long-lived HTTP connection.  Problem is that I
>         unable to do any reply from init/3 without crash. There is a
>         test for
>         cowboy_loop_handler but it does only timeout check, no response.
>
>         Regards,
>         Martin
>
>
>         _________________________________________________
>         erlang-questions mailing list
>         erlang-questions@REDACTED <mailto:erlang-questions@REDACTED>
>         http://erlang.org/mailman/__listinfo/erlang-questions
>         <http://erlang.org/mailman/listinfo/erlang-questions>
>
>
>
>     --
>     Loďc Hoguin
>     Erlang Cowboy
>     Nine Nines
>     http://ninenines.eu
>
>


-- 
Loïc Hoguin
Erlang Cowboy
Nine Nines
http://ninenines.eu



More information about the erlang-questions mailing list