From ae401f7460664256de588d9a0bfee4c3b1560738 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Wed, 6 Feb 2013 23:32:31 +0100 Subject: Fix crashes when creating the request We now obtain the peer address before creating the Req object. If an error occurs, then something went wrong, we close the connection nicely directly. --- src/cowboy_protocol.erl | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) (limited to 'src/cowboy_protocol.erl') diff --git a/src/cowboy_protocol.erl b/src/cowboy_protocol.erl index b479fa9..caf0806 100644 --- a/src/cowboy_protocol.erl +++ b/src/cowboy_protocol.erl @@ -463,10 +463,16 @@ request(Buffer, State=#state{socket=Socket, transport=Transport, req_keepalive=ReqKeepalive, max_keepalive=MaxKeepalive, compress=Compress, onresponse=OnResponse}, Method, Path, Query, Fragment, Version, Headers, Host, Port) -> - Req = cowboy_req:new(Socket, Transport, Method, Path, Query, Fragment, - Version, Headers, Host, Port, Buffer, ReqKeepalive < MaxKeepalive, - Compress, OnResponse), - onrequest(Req, State). + case Transport:peername(Socket) of + {ok, Peer} -> + Req = cowboy_req:new(Socket, Transport, Peer, Method, Path, + Query, Fragment, Version, Headers, Host, Port, Buffer, + ReqKeepalive < MaxKeepalive, Compress, OnResponse), + onrequest(Req, State); + {error, _} -> + %% Couldn't read the peer address; connection is gone. + terminate(State) + end. %% Call the global onrequest callback. The callback can send a reply, %% in which case we consider the request handled and move on to the next @@ -562,8 +568,8 @@ error_terminate(Code, State=#state{socket=Socket, transport=Transport, {cowboy_req, resp_sent} -> ok after 0 -> _ = cowboy_req:reply(Code, cowboy_req:new(Socket, Transport, - <<"GET">>, <<>>, <<>>, <<>>, {1, 1}, [], <<>>, undefined, - <<>>, false, Compress, OnResponse)), + undefined, <<"GET">>, <<>>, <<>>, <<>>, {1, 1}, [], <<>>, + undefined, <<>>, false, Compress, OnResponse)), ok end, terminate(State). -- cgit v1.2.3