diff options
author | Nelson Vides <[email protected]> | 2021-04-20 14:58:58 +0200 |
---|---|---|
committer | Loïc Hoguin <[email protected]> | 2023-12-21 15:01:33 +0100 |
commit | 5ef64557b5b4c92224d68d1445cdb7846a76a0be (patch) | |
tree | a9bab5b54dc25b15de13708118645f69294c570c /src | |
parent | f74b69c3edd6e8c8891a3a8e18d14c8ae93bf5c4 (diff) | |
download | cowboy-5ef64557b5b4c92224d68d1445cdb7846a76a0be.tar.gz cowboy-5ef64557b5b4c92224d68d1445cdb7846a76a0be.tar.bz2 cowboy-5ef64557b5b4c92224d68d1445cdb7846a76a0be.zip |
Exit gracefully on {error,closed} when reading the PROXY header
LH: Simplified the test a little.
Diffstat (limited to 'src')
-rw-r--r-- | src/cowboy_clear.erl | 16 | ||||
-rw-r--r-- | src/cowboy_tls.erl | 16 |
2 files changed, 18 insertions, 14 deletions
diff --git a/src/cowboy_clear.erl b/src/cowboy_clear.erl index 4f3a234..e4ca9da 100644 --- a/src/cowboy_clear.erl +++ b/src/cowboy_clear.erl @@ -33,13 +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 maps:get(proxy_header, Opts, false) of - true -> - {ok, ProxyInfo0} = ranch:recv_proxy_header(Ref, 1000), - ProxyInfo0; - false -> - undefined - 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. @@ -58,3 +52,11 @@ init(Parent, Ref, Socket, Transport, ProxyInfo, Opts, Protocol) -> supervisor -> process_flag(trap_exit, true) end, Protocol:init(Parent, Ref, Socket, Transport, ProxyInfo, Opts). + +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. diff --git a/src/cowboy_tls.erl b/src/cowboy_tls.erl index c049ecb..4385cbc 100644 --- a/src/cowboy_tls.erl +++ b/src/cowboy_tls.erl @@ -33,13 +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 maps:get(proxy_header, Opts, false) of - true -> - {ok, ProxyInfo0} = ranch:recv_proxy_header(Ref, 1000), - ProxyInfo0; - false -> - undefined - end, + ProxyInfo = get_proxy_info(Ref, Opts), {ok, Socket} = ranch:handshake(Ref), case ssl:negotiated_protocol(Socket) of {ok, <<"h2">>} -> @@ -54,3 +48,11 @@ init(Parent, Ref, Socket, Transport, ProxyInfo, Opts, Protocol) -> supervisor -> process_flag(trap_exit, true) end, Protocol:init(Parent, Ref, Socket, Transport, ProxyInfo, Opts). + +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. |