Skip to content

Commit

Permalink
Merge pull request #163 from tazjin/fix/issue-160-tls-upgrades
Browse files Browse the repository at this point in the history
Initialise TLS sockets directly if no socks5 proxy is configured
  • Loading branch information
cmullaparthi authored Jul 13, 2018
2 parents 555f707 + 9829a07 commit 7f15865
Showing 1 changed file with 23 additions and 16 deletions.
39 changes: 23 additions & 16 deletions src/ibrowse_http_client.erl
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ handle_call({send_req, _}, _From, #state{is_closing = true} = State) ->
{reply, {error, connection_closing}, State};

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

handle_call({send_req, {Url, Headers, Method, Body, Options, Timeout}},
Expand Down Expand Up @@ -571,21 +571,28 @@ do_connect(Host, Port, Options, #state{is_ssl = true,
use_proxy = false,
ssl_options = SSLOptions},
Timeout) ->
Socks5Host = get_value(socks5_host, Options, undefined),
Sock_options = get_sock_options(Host, Options, []),
Conn = case Socks5Host of
undefined ->
gen_tcp:connect(Host, Port, Sock_options, Timeout);
_ ->
catch ibrowse_socks5:connect(Host, Port, Options, Sock_options, Timeout)
end,
case Conn of
{ok, Sock} ->
ssl:connect(Sock, SSLOptions, Timeout);
_ ->
error
%% if a socks5 proxy is configured, open the socket separately
%% before upgrading the socket to a TLS connection.
case get_value(socks5_host, Options, undefined) of
%% no socks5 proxy is configured, connect directly with TLS:
undefined ->
Sock_options = get_sock_options(Host, Options, SSLOptions),
ssl:connect(Host, Port, Sock_options, Timeout);

%% proxy configuration is present: first establish a socket
%% and then upgrade:
_ ->
Sock_options = get_sock_options(Host, Options, []),
Conn = ibrowse_socks5:connect(Host, Port, Options,
Sock_options, Timeout),
case Conn of
{ok, Sock} ->
ssl:connect(Sock, SSLOptions, Timeout);
_ ->
error
end
end;

do_connect(Host, Port, Options, _State, Timeout) ->
Socks5Host = get_value(socks5_host, Options, undefined),
Sock_options = get_sock_options(Host, Options, []),
Expand Down Expand Up @@ -1918,7 +1925,7 @@ format_response_data(Resp_format, Body) ->

%% dont message an unexisting server
%% triggered by :stop or :tcp_closed on an unactive connection
do_reply(State, undefined, undefined, _, _, _Msg) ->
do_reply(State, undefined, undefined, _, _, _Msg) ->
dec_pipeline_counter(State);

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

0 comments on commit 7f15865

Please sign in to comment.