Browse Source

Make Cowboy compatible with upcoming Ranch 2.0

juhlig 6 years ago
parent
commit
5b4e78fac4
4 changed files with 32 additions and 18 deletions
  1. 7 0
      src/cowboy_clear.erl
  2. 9 9
      src/cowboy_http.erl
  3. 4 4
      src/cowboy_http2.erl
  4. 12 5
      src/cowboy_tls.erl

+ 7 - 0
src/cowboy_clear.erl

@@ -15,11 +15,18 @@
 -module(cowboy_clear).
 -module(cowboy_clear).
 -behavior(ranch_protocol).
 -behavior(ranch_protocol).
 
 
+-export([start_link/3]).
 -export([start_link/4]).
 -export([start_link/4]).
 -export([connection_process/4]).
 -export([connection_process/4]).
 
 
+%% Ranch 1.
 -spec start_link(ranch:ref(), inet:socket(), module(), cowboy:opts()) -> {ok, pid()}.
 -spec start_link(ranch:ref(), inet:socket(), module(), cowboy:opts()) -> {ok, pid()}.
 start_link(Ref, _Socket, Transport, Opts) ->
 start_link(Ref, _Socket, Transport, Opts) ->
+	start_link(Ref, Transport, Opts).
+
+%% Ranch 2.
+-spec start_link(ranch:ref(), module(), cowboy:opts()) -> {ok, pid()}.
+start_link(Ref, Transport, Opts) ->
 	Pid = proc_lib:spawn_link(?MODULE, connection_process,
 	Pid = proc_lib:spawn_link(?MODULE, connection_process,
 		[self(), Ref, Transport, Opts]),
 		[self(), Ref, Transport, Opts]),
 	{ok, Pid}.
 	{ok, Pid}.

+ 9 - 9
src/cowboy_http.erl

@@ -196,24 +196,24 @@ before_loop(State=#state{socket=Socket, transport=Transport}, Buffer) ->
 loop(State=#state{parent=Parent, socket=Socket, transport=Transport, opts=Opts,
 loop(State=#state{parent=Parent, socket=Socket, transport=Transport, opts=Opts,
 		timer=TimerRef, children=Children, in_streamid=InStreamID,
 		timer=TimerRef, children=Children, in_streamid=InStreamID,
 		last_streamid=LastStreamID, streams=Streams}, Buffer) ->
 		last_streamid=LastStreamID, streams=Streams}, Buffer) ->
-	{OK, Closed, Error} = Transport:messages(),
+	Messages = Transport:messages(),
 	InactivityTimeout = maps:get(inactivity_timeout, Opts, 300000),
 	InactivityTimeout = maps:get(inactivity_timeout, Opts, 300000),
 	receive
 	receive
 		%% Discard data coming in after the last request
 		%% Discard data coming in after the last request
 		%% we want to process was received fully.
 		%% we want to process was received fully.
-		{OK, Socket, _} when InStreamID > LastStreamID ->
+		{OK, Socket, _} when OK =:= element(1, Messages), InStreamID > LastStreamID ->
 			before_loop(State, Buffer);
 			before_loop(State, Buffer);
 		%% Socket messages.
 		%% Socket messages.
-		{OK, Socket, Data} ->
+		{OK, Socket, Data} when OK =:= element(1, Messages) ->
 			%% Only reset the timeout if it is idle_timeout (active streams).
 			%% Only reset the timeout if it is idle_timeout (active streams).
 			State1 = case Streams of
 			State1 = case Streams of
 				[] -> State;
 				[] -> State;
 				_ -> set_timeout(State)
 				_ -> set_timeout(State)
 			end,
 			end,
 			parse(<< Buffer/binary, Data/binary >>, State1);
 			parse(<< Buffer/binary, Data/binary >>, State1);
-		{Closed, Socket} ->
+		{Closed, Socket} when Closed =:= element(2, Messages) ->
 			terminate(State, {socket_error, closed, 'The socket has been closed.'});
 			terminate(State, {socket_error, closed, 'The socket has been closed.'});
-		{Error, Socket, Reason} ->
+		{Error, Socket, Reason} when Error =:= element(3, Messages) ->
 			terminate(State, {socket_error, Reason, 'An error has occurred on the socket.'});
 			terminate(State, {socket_error, Reason, 'An error has occurred on the socket.'});
 		%% Timeouts.
 		%% Timeouts.
 		{timeout, Ref, {shutdown, Pid}} ->
 		{timeout, Ref, {shutdown, Pid}} ->
@@ -1406,17 +1406,17 @@ terminate_linger(State=#state{socket=Socket, transport=Transport, opts=Opts}) ->
 	end.
 	end.
 
 
 terminate_linger_loop(State=#state{socket=Socket, transport=Transport}, TimerRef) ->
 terminate_linger_loop(State=#state{socket=Socket, transport=Transport}, TimerRef) ->
-	{OK, Closed, Error} = Transport:messages(),
+	Messages = Transport:messages(),
 	%% We may already have a message in the mailbox when we do this
 	%% We may already have a message in the mailbox when we do this
 	%% but it's OK because we are shutting down anyway.
 	%% but it's OK because we are shutting down anyway.
 	case Transport:setopts(Socket, [{active, once}]) of
 	case Transport:setopts(Socket, [{active, once}]) of
 		ok ->
 		ok ->
 			receive
 			receive
-				{OK, Socket, _} ->
+				{OK, Socket, _} when OK =:= element(1, Messages) ->
 					terminate_linger_loop(State, TimerRef);
 					terminate_linger_loop(State, TimerRef);
-				{Closed, Socket} ->
+				{Closed, Socket} when Closed =:= element(2, Messages) ->
 					ok;
 					ok;
-				{Error, Socket, _} ->
+				{Error, Socket, _} when Error =:= element(3, Messages) ->
 					ok;
 					ok;
 				{timeout, TimerRef, linger_timeout} ->
 				{timeout, TimerRef, linger_timeout} ->
 					ok;
 					ok;

+ 4 - 4
src/cowboy_http2.erl

@@ -169,15 +169,15 @@ loop(State=#state{parent=Parent, socket=Socket, transport=Transport,
 		opts=Opts, timer=TimerRef, children=Children}, Buffer) ->
 		opts=Opts, timer=TimerRef, children=Children}, Buffer) ->
 	%% @todo This should only be called when data was read.
 	%% @todo This should only be called when data was read.
 	Transport:setopts(Socket, [{active, once}]),
 	Transport:setopts(Socket, [{active, once}]),
-	{OK, Closed, Error} = Transport:messages(),
+	Messages = Transport:messages(),
 	InactivityTimeout = maps:get(inactivity_timeout, Opts, 300000),
 	InactivityTimeout = maps:get(inactivity_timeout, Opts, 300000),
 	receive
 	receive
 		%% Socket messages.
 		%% Socket messages.
-		{OK, Socket, Data} ->
+		{OK, Socket, Data} when OK =:= element(1, Messages) ->
 			parse(set_timeout(State), << Buffer/binary, Data/binary >>);
 			parse(set_timeout(State), << Buffer/binary, Data/binary >>);
-		{Closed, Socket} ->
+		{Closed, Socket} when Closed =:= element(2, Messages) ->
 			terminate(State, {socket_error, closed, 'The socket has been closed.'});
 			terminate(State, {socket_error, closed, 'The socket has been closed.'});
-		{Error, Socket, Reason} ->
+		{Error, Socket, Reason} when Error =:= element(3, Messages) ->
 			terminate(State, {socket_error, Reason, 'An error has occurred on the socket.'});
 			terminate(State, {socket_error, Reason, 'An error has occurred on the socket.'});
 		%% System messages.
 		%% System messages.
 		{'EXIT', Parent, Reason} ->
 		{'EXIT', Parent, Reason} ->

+ 12 - 5
src/cowboy_tls.erl

@@ -15,17 +15,24 @@
 -module(cowboy_tls).
 -module(cowboy_tls).
 -behavior(ranch_protocol).
 -behavior(ranch_protocol).
 
 
+-export([start_link/3]).
 -export([start_link/4]).
 -export([start_link/4]).
--export([connection_process/5]).
+-export([connection_process/4]).
 
 
+%% Ranch 1.
 -spec start_link(ranch:ref(), ssl:sslsocket(), module(), cowboy:opts()) -> {ok, pid()}.
 -spec start_link(ranch:ref(), ssl:sslsocket(), module(), cowboy:opts()) -> {ok, pid()}.
-start_link(Ref, Socket, Transport, Opts) ->
+start_link(Ref, _Socket, Transport, Opts) ->
+	start_link(Ref, Transport, Opts).
+
+%% Ranch 2.
+-spec start_link(ranch:ref(), module(), cowboy:opts()) -> {ok, pid()}.
+start_link(Ref, Transport, Opts) ->
 	Pid = proc_lib:spawn_link(?MODULE, connection_process,
 	Pid = proc_lib:spawn_link(?MODULE, connection_process,
-		[self(), Ref, Socket, Transport, Opts]),
+		[self(), Ref, Transport, Opts]),
 	{ok, Pid}.
 	{ok, Pid}.
 
 
--spec connection_process(pid(), ranch:ref(), ssl:sslsocket(), module(), cowboy:opts()) -> ok.
-connection_process(Parent, Ref, Socket, Transport, Opts) ->
+-spec connection_process(pid(), ranch:ref(), module(), cowboy:opts()) -> ok.
+connection_process(Parent, Ref, Transport, Opts) ->
 	ProxyInfo = case maps:get(proxy_header, Opts, false) of
 	ProxyInfo = case maps:get(proxy_header, Opts, false) of
 		true ->
 		true ->
 			{ok, ProxyInfo0} = ranch:recv_proxy_header(Ref, 1000),
 			{ok, ProxyInfo0} = ranch:recv_proxy_header(Ref, 1000),