Skip to content

Commit

Permalink
Accept only closed as error, crash otherwise.
Browse files Browse the repository at this point in the history
  • Loading branch information
NelsonVides committed May 7, 2021
1 parent 53e8841 commit 85b05de
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 22 deletions.
19 changes: 8 additions & 11 deletions src/cowboy_clear.erl
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ start_link(Ref, Transport, Opts) ->

-spec connection_process(pid(), ranch:ref(), module(), cowboy:opts()) -> ok.
connection_process(Parent, Ref, Transport, Opts) ->
ProxyInfo = case maybe_proxy_info(Ref, Opts) of
{ok, ProxyInfo0} -> ProxyInfo0;
Error -> exit({shutdown, Error})
end,
ProxyInfo = get_proxy_info(Ref, Opts),
{ok, Socket} = ranch:handshake(Ref),
%% Use cowboy_http2 directly only when 'http' is missing.
%% Otherwise switch to cowboy_http2 from cowboy_http.
Expand All @@ -56,10 +53,10 @@ init(Parent, Ref, Socket, Transport, ProxyInfo, Opts, Protocol) ->
end,
Protocol:init(Parent, Ref, Socket, Transport, ProxyInfo, Opts).

maybe_proxy_info(Ref, Opts) ->
case maps:get(proxy_header, Opts, false) of
true ->
ranch:recv_proxy_header(Ref, 1000);
false ->
undefined
end.
get_proxy_info(Ref, #{proxy_header := true}) ->
case ranch:recv_proxy_header(Ref, 1000) of
{ok, ProxyInfo} -> ProxyInfo;
{error, closed} -> exit({shutdown, closed})
end;
get_proxy_info(_, _) ->
undefined.
19 changes: 8 additions & 11 deletions src/cowboy_tls.erl
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ start_link(Ref, Transport, Opts) ->

-spec connection_process(pid(), ranch:ref(), module(), cowboy:opts()) -> ok.
connection_process(Parent, Ref, Transport, Opts) ->
ProxyInfo = case maybe_proxy_info(Ref, Opts) of
{ok, ProxyInfo0} -> ProxyInfo0;
Error -> exit({shutdown, Error})
end,
ProxyInfo = get_proxy_info(Ref, Opts),
{ok, Socket} = ranch:handshake(Ref),
case ssl:negotiated_protocol(Socket) of
{ok, <<"h2">>} ->
Expand All @@ -52,10 +49,10 @@ init(Parent, Ref, Socket, Transport, ProxyInfo, Opts, Protocol) ->
end,
Protocol:init(Parent, Ref, Socket, Transport, ProxyInfo, Opts).

maybe_proxy_info(Ref, Opts) ->
case maps:get(proxy_header, Opts, false) of
true ->
ranch:recv_proxy_header(Ref, 1000);
false ->
undefined
end.
get_proxy_info(Ref, #{proxy_header := true}) ->
case ranch:recv_proxy_header(Ref, 1000) of
{ok, ProxyInfo} -> ProxyInfo;
{error, closed} -> exit({shutdown, closed})
end;
get_proxy_info(_, _) ->
undefined.

0 comments on commit 85b05de

Please sign in to comment.