From 5af76eb141f51e3e8299801109f94f8be15e5c1b Mon Sep 17 00:00:00 2001 From: Piotr Klibert Date: Sat, 17 Mar 2018 18:10:55 +0100 Subject: [PATCH 1/5] Fix accessing HTTPS sites with an IP address --- src/hackney_ssl.erl | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/hackney_ssl.erl b/src/hackney_ssl.erl index 660db0b0..3a6673f9 100644 --- a/src/hackney_ssl.erl +++ b/src/hackney_ssl.erl @@ -49,6 +49,13 @@ connect(Host, Port, Opts) -> connect(Host, Port, Opts, Timeout) when is_list(Host), is_integer(Port), (Timeout =:= infinity orelse is_integer(Timeout)) -> + Host1 = try + Parts = string:split(Host, ".", all), + 4 = length(Parts), + list_to_tuple(lists:map(fun list_to_integer/1, Parts)) + catch + _:_ -> Host + end, BaseOpts = [binary, {active, false}, {packet, raw}, {secure_renegotiate, true}, {reuse_sessions, true}, @@ -58,7 +65,7 @@ connect(Host, Port, Opts, Timeout) when is_list(Host), is_integer(Port), Opts1 = hackney_util:merge_opts(BaseOpts, Opts), %% connect - ssl:connect(Host, Port, Opts1, Timeout). + ssl:connect(Host1, Port, Opts1, Timeout). ciphers() -> From a4985247c10909993f0a3d1fa356602894afe4ba Mon Sep 17 00:00:00 2001 From: Piotr Klibert Date: Tue, 3 Apr 2018 21:10:02 +0200 Subject: [PATCH 2/5] Move the code to its own function, use inet:parse_address, add docstring --- src/hackney_ssl.erl | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/hackney_ssl.erl b/src/hackney_ssl.erl index 3a6673f9..9cc5d2ac 100644 --- a/src/hackney_ssl.erl +++ b/src/hackney_ssl.erl @@ -44,18 +44,23 @@ %% @doc Atoms used to identify messages in {active, once | true} mode. messages(_) -> {ssl, ssl_closed, ssl_error}. +%% @doc The ssl:connect/4 (and related) doesn't work with textual representation +%% of IP addresses. It accepts either a string with a DNS-resolvable name or a +%% tuple with a tag and parts of the IP as numbers. This function returns a tuple +%% like that, or the original string if it's impossible to parse as an IP. +parse_address(Host) when is_list(Host) -> + case inet:parse_address() of + {ok, Address} -> Address; + {error, _} -> Host + end. + + connect(Host, Port, Opts) -> connect(Host, Port, Opts, infinity). connect(Host, Port, Opts, Timeout) when is_list(Host), is_integer(Port), (Timeout =:= infinity orelse is_integer(Timeout)) -> - Host1 = try - Parts = string:split(Host, ".", all), - 4 = length(Parts), - list_to_tuple(lists:map(fun list_to_integer/1, Parts)) - catch - _:_ -> Host - end, + BaseOpts = [binary, {active, false}, {packet, raw}, {secure_renegotiate, true}, {reuse_sessions, true}, @@ -63,7 +68,7 @@ connect(Host, Port, Opts, Timeout) when is_list(Host), is_integer(Port), {versions,['tlsv1.2', 'tlsv1.1', tlsv1, sslv3]}, {ciphers, ciphers()}], Opts1 = hackney_util:merge_opts(BaseOpts, Opts), - + Host1 = parse_address(Host), %% connect ssl:connect(Host1, Port, Opts1, Timeout). From 10f6ba38777654e107cb2ea2e88e99381261b399 Mon Sep 17 00:00:00 2001 From: Piotr Klibert Date: Tue, 3 Apr 2018 21:19:03 +0200 Subject: [PATCH 3/5] Without a tag, actually (fixed docstring) --- src/hackney_ssl.erl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/hackney_ssl.erl b/src/hackney_ssl.erl index 9cc5d2ac..4df5b773 100644 --- a/src/hackney_ssl.erl +++ b/src/hackney_ssl.erl @@ -46,8 +46,8 @@ messages(_) -> {ssl, ssl_closed, ssl_error}. %% @doc The ssl:connect/4 (and related) doesn't work with textual representation %% of IP addresses. It accepts either a string with a DNS-resolvable name or a -%% tuple with a tag and parts of the IP as numbers. This function returns a tuple -%% like that, or the original string if it's impossible to parse as an IP. +%% tuple and parts of the IP as numbers. This function returns a tuple like +%% that, or the original string if it's impossible to parse as an IP. parse_address(Host) when is_list(Host) -> case inet:parse_address() of {ok, Address} -> Address; From 869cbfb845f7ad8edd14ee6eed48173148f381c1 Mon Sep 17 00:00:00 2001 From: Piotr Klibert Date: Tue, 3 Apr 2018 21:44:07 +0200 Subject: [PATCH 4/5] Fix wording in a comment --- src/hackney_ssl.erl | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/hackney_ssl.erl b/src/hackney_ssl.erl index 4df5b773..8bcbf901 100644 --- a/src/hackney_ssl.erl +++ b/src/hackney_ssl.erl @@ -46,8 +46,9 @@ messages(_) -> {ssl, ssl_closed, ssl_error}. %% @doc The ssl:connect/4 (and related) doesn't work with textual representation %% of IP addresses. It accepts either a string with a DNS-resolvable name or a -%% tuple and parts of the IP as numbers. This function returns a tuple like -%% that, or the original string if it's impossible to parse as an IP. +%% tuple with parts of an IP as numbers. This function attempts to parse given +%% string and either returns such tuple, or the string if it's impossible to +%% parse. parse_address(Host) when is_list(Host) -> case inet:parse_address() of {ok, Address} -> Address; From 88c4bc15972324d66fc74d3c39c09b8587bd44f2 Mon Sep 17 00:00:00 2001 From: Piotr Klibert Date: Tue, 3 Apr 2018 22:00:35 +0200 Subject: [PATCH 5/5] Right, I'm blind --- src/hackney_ssl.erl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hackney_ssl.erl b/src/hackney_ssl.erl index 8bcbf901..2aed8e5c 100644 --- a/src/hackney_ssl.erl +++ b/src/hackney_ssl.erl @@ -50,7 +50,7 @@ messages(_) -> {ssl, ssl_closed, ssl_error}. %% string and either returns such tuple, or the string if it's impossible to %% parse. parse_address(Host) when is_list(Host) -> - case inet:parse_address() of + case inet:parse_address(Host) of {ok, Address} -> Address; {error, _} -> Host end.