Skip to content

Commit 13dd2f4

Browse files
authored
Merge pull request #494 from piotrklibert/master
Fix accessing HTTPS sites with an IPv4 address
2 parents 8319244 + 88c4bc1 commit 13dd2f4

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

src/hackney_ssl.erl

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,21 +44,34 @@
4444
%% @doc Atoms used to identify messages in {active, once | true} mode.
4545
messages(_) -> {ssl, ssl_closed, ssl_error}.
4646

47+
%% @doc The ssl:connect/4 (and related) doesn't work with textual representation
48+
%% of IP addresses. It accepts either a string with a DNS-resolvable name or a
49+
%% tuple with parts of an IP as numbers. This function attempts to parse given
50+
%% string and either returns such tuple, or the string if it's impossible to
51+
%% parse.
52+
parse_address(Host) when is_list(Host) ->
53+
case inet:parse_address(Host) of
54+
{ok, Address} -> Address;
55+
{error, _} -> Host
56+
end.
57+
58+
4759
connect(Host, Port, Opts) ->
4860
connect(Host, Port, Opts, infinity).
4961

5062
connect(Host, Port, Opts, Timeout) when is_list(Host), is_integer(Port),
5163
(Timeout =:= infinity orelse is_integer(Timeout)) ->
64+
5265
BaseOpts = [binary, {active, false}, {packet, raw},
5366
{secure_renegotiate, true},
5467
{reuse_sessions, true},
5568
{honor_cipher_order, true},
5669
{versions,['tlsv1.2', 'tlsv1.1', tlsv1, sslv3]},
5770
{ciphers, ciphers()}],
5871
Opts1 = hackney_util:merge_opts(BaseOpts, Opts),
59-
72+
Host1 = parse_address(Host),
6073
%% connect
61-
ssl:connect(Host, Port, Opts1, Timeout).
74+
ssl:connect(Host1, Port, Opts1, Timeout).
6275

6376

6477
ciphers() ->

0 commit comments

Comments
 (0)