0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-14 20:30:23 +00:00

Accept only closed as error, crash otherwise.

This commit is contained in:
Nelson Vides 2021-05-07 18:16:17 +02:00
parent 148620ccf4
commit 286c10637a
No known key found for this signature in database
GPG key ID: D63DE880D7BA9432
2 changed files with 16 additions and 22 deletions

View file

@ -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.
@ -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.

View file

@ -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">>} ->
@ -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.