diff options
author | Loïc Hoguin <[email protected]> | 2013-02-06 23:32:31 +0100 |
---|---|---|
committer | Loïc Hoguin <[email protected]> | 2013-02-06 23:32:31 +0100 |
commit | ae401f7460664256de588d9a0bfee4c3b1560738 (patch) | |
tree | 42035aa85bd9def0a7a9f95931bf4bf8e4491219 /src/cowboy_protocol.erl | |
parent | 1541e38d538fc713a71d82764830b0aee563be4b (diff) | |
download | cowboy-ae401f7460664256de588d9a0bfee4c3b1560738.tar.gz cowboy-ae401f7460664256de588d9a0bfee4c3b1560738.tar.bz2 cowboy-ae401f7460664256de588d9a0bfee4c3b1560738.zip |
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.
Diffstat (limited to 'src/cowboy_protocol.erl')
-rw-r--r-- | src/cowboy_protocol.erl | 18 |
1 files changed, 12 insertions, 6 deletions
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). |