Skip to content

Commit 7f15865

Browse files
authored
Merge pull request #163 from tazjin/fix/issue-160-tls-upgrades
Initialise TLS sockets directly if no socks5 proxy is configured
2 parents 555f707 + 9829a07 commit 7f15865

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed

src/ibrowse_http_client.erl

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ handle_call({send_req, _}, _From, #state{is_closing = true} = State) ->
174174
{reply, {error, connection_closing}, State};
175175

176176
handle_call({send_req, _}, _From, #state{proc_state = ?dead_proc_walking} = State) ->
177-
shutting_down(State),
177+
shutting_down(State),
178178
{reply, {error, connection_closing}, State};
179179

180180
handle_call({send_req, {Url, Headers, Method, Body, Options, Timeout}},
@@ -571,21 +571,28 @@ do_connect(Host, Port, Options, #state{is_ssl = true,
571571
use_proxy = false,
572572
ssl_options = SSLOptions},
573573
Timeout) ->
574-
Socks5Host = get_value(socks5_host, Options, undefined),
575-
Sock_options = get_sock_options(Host, Options, []),
576-
Conn = case Socks5Host of
577-
undefined ->
578-
gen_tcp:connect(Host, Port, Sock_options, Timeout);
579-
_ ->
580-
catch ibrowse_socks5:connect(Host, Port, Options, Sock_options, Timeout)
581-
end,
582-
case Conn of
583-
{ok, Sock} ->
584-
ssl:connect(Sock, SSLOptions, Timeout);
585-
_ ->
586-
error
574+
%% if a socks5 proxy is configured, open the socket separately
575+
%% before upgrading the socket to a TLS connection.
576+
case get_value(socks5_host, Options, undefined) of
577+
%% no socks5 proxy is configured, connect directly with TLS:
578+
undefined ->
579+
Sock_options = get_sock_options(Host, Options, SSLOptions),
580+
ssl:connect(Host, Port, Sock_options, Timeout);
581+
582+
%% proxy configuration is present: first establish a socket
583+
%% and then upgrade:
584+
_ ->
585+
Sock_options = get_sock_options(Host, Options, []),
586+
Conn = ibrowse_socks5:connect(Host, Port, Options,
587+
Sock_options, Timeout),
588+
case Conn of
589+
{ok, Sock} ->
590+
ssl:connect(Sock, SSLOptions, Timeout);
591+
_ ->
592+
error
593+
end
587594
end;
588-
595+
589596
do_connect(Host, Port, Options, _State, Timeout) ->
590597
Socks5Host = get_value(socks5_host, Options, undefined),
591598
Sock_options = get_sock_options(Host, Options, []),
@@ -1918,7 +1925,7 @@ format_response_data(Resp_format, Body) ->
19181925

19191926
%% dont message an unexisting server
19201927
%% triggered by :stop or :tcp_closed on an unactive connection
1921-
do_reply(State, undefined, undefined, _, _, _Msg) ->
1928+
do_reply(State, undefined, undefined, _, _, _Msg) ->
19221929
dec_pipeline_counter(State);
19231930

19241931
do_reply(State, From, undefined, _, Resp_format, {ok, St_code, Headers, Body}) ->

0 commit comments

Comments
 (0)