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