From 8de84f1af04a716357dbc5473f7daf74cb31f44a Mon Sep 17 00:00:00 2001 From: CarlosEDP Date: Wed, 1 Jul 2015 14:13:43 -0300 Subject: Added support for proxy on rebar3 based on environment variables. --- src/rebar3.erl | 12 ++---------- src/rebar_utils.erl | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 55 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/rebar3.erl b/src/rebar3.erl index c501709..13f6017 100644 --- a/src/rebar3.erl +++ b/src/rebar3.erl @@ -273,13 +273,5 @@ start_and_load_apps() -> application:start(public_key), application:start(ssl), inets:start(), - inets:start(httpc, [{profile, hex}]), - http_opts(). - -http_opts() -> - Opts = [{max_sessions, 4}, - {max_keep_alive_length, 4}, - {keep_alive_timeout, 120000}, - {max_pipeline_length, 4}, - {pipeline_timeout, 60000}], - httpc:set_options(Opts, hex). + inets:start(httpc), + rebar_utils:set_httpc_options(). diff --git a/src/rebar_utils.erl b/src/rebar_utils.erl index 0cbc7c2..35964ea 100644 --- a/src/rebar_utils.erl +++ b/src/rebar_utils.erl @@ -56,7 +56,8 @@ wordsize/0, tup_umerge/2, tup_sort/1, - line_count/1]). + line_count/1, + set_httpc_options/0]). %% for internal use only -export([otp_release/0]). @@ -661,3 +662,54 @@ maybe_ends_in_comma(H) -> "," ++ Flag -> lists:reverse(Flag); _ -> false end. + +set_httpc_options() -> + %% Get http_proxy and https_proxy environment variables + case os:getenv("http_proxy") of + false -> + Http = ""; + Http -> + Http + end, + case os:getenv("https_proxy") of + false -> + Https = ""; + Https -> + Https + end, + + %% Parse the variables to extract host and port + Opts = [], + case http_uri:parse(Http) of + {ok,{_, [], Host, Port, _, []}} -> + Opts1 = Opts ++ [{proxy, {{Host, Port}, []}}]; + {error, _} -> + Opts1 = Opts + end, + + case http_uri:parse(Https) of + {ok,{_, [], Host2, Port2, _, []}} -> + Opts2 = Opts1 ++ [{https_proxy, {{Host2, Port2}, []}}]; + {error, _} -> + Opts2 = Opts1 + end, + + case Opts2 of + [] -> + Opts3 = [ + {max_sessions, 4}, + {max_keep_alive_length, 4}, + {keep_alive_timeout, 120000}, + {max_pipeline_length, 4}, + {pipeline_timeout, 60000} + ]; + _ -> + Opts3 = Opts2 ++ [ + {max_sessions, 4}, + {max_keep_alive_length, 4}, + {keep_alive_timeout, 120000}, + {max_pipeline_length, 4}, + {pipeline_timeout, 60000} + ] + end, + httpc:set_options(Opts3). -- cgit v1.1 From 60265aba3497aa97a35b706e00a2526e100733bd Mon Sep 17 00:00:00 2001 From: CarlosEDP Date: Wed, 1 Jul 2015 18:01:35 -0300 Subject: Get proxy vars from ~/.config/rebar3/rebar.config. Variable format is {http_proxy, http://host:port} or {http_proxy, http://host:port} --- src/rebar_utils.erl | 69 ++++++++++++++++------------------------------------- 1 file changed, 21 insertions(+), 48 deletions(-) (limited to 'src') diff --git a/src/rebar_utils.erl b/src/rebar_utils.erl index 35964ea..77d5e71 100644 --- a/src/rebar_utils.erl +++ b/src/rebar_utils.erl @@ -663,53 +663,26 @@ maybe_ends_in_comma(H) -> _ -> false end. +get_http_var() -> + {ok, [[Home]]} = init:get_argument(home), + ConfDir = filename:join(Home, ".config/rebar3"), + case file:consult(filename:join(ConfDir, "rebar.config")) of + {ok, Config} -> + Config; + _ -> + [] + end. + +get_http(Scheme) -> + proplists:get_value(Scheme, get_http_var(), ""). + set_httpc_options() -> - %% Get http_proxy and https_proxy environment variables - case os:getenv("http_proxy") of - false -> - Http = ""; - Http -> - Http - end, - case os:getenv("https_proxy") of - false -> - Https = ""; - Https -> - Https - end, - - %% Parse the variables to extract host and port - Opts = [], - case http_uri:parse(Http) of - {ok,{_, [], Host, Port, _, []}} -> - Opts1 = Opts ++ [{proxy, {{Host, Port}, []}}]; - {error, _} -> - Opts1 = Opts - end, + set_httpc_options(https_proxy, get_http(https_proxy)), + set_httpc_options(proxy, get_http(http_proxy)). - case http_uri:parse(Https) of - {ok,{_, [], Host2, Port2, _, []}} -> - Opts2 = Opts1 ++ [{https_proxy, {{Host2, Port2}, []}}]; - {error, _} -> - Opts2 = Opts1 - end, - - case Opts2 of - [] -> - Opts3 = [ - {max_sessions, 4}, - {max_keep_alive_length, 4}, - {keep_alive_timeout, 120000}, - {max_pipeline_length, 4}, - {pipeline_timeout, 60000} - ]; - _ -> - Opts3 = Opts2 ++ [ - {max_sessions, 4}, - {max_keep_alive_length, 4}, - {keep_alive_timeout, 120000}, - {max_pipeline_length, 4}, - {pipeline_timeout, 60000} - ] - end, - httpc:set_options(Opts3). +set_httpc_options(_, []) -> + ok; + +set_httpc_options(Scheme, Proxy) -> + {ok, {_, _, Host, Port, _, _}} = http_uri:parse(Proxy), + httpc:set_options([{Scheme, {{Host, Port}, []}}]). -- cgit v1.1 From 9a20d3b56a9a0d10d189224d785445c80fa6395f Mon Sep 17 00:00:00 2001 From: CarlosEDP Date: Thu, 2 Jul 2015 10:05:54 -0300 Subject: No need to use profile hex. Proxy settings are applied globally. --- src/rebar_pkg_resource.erl | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'src') diff --git a/src/rebar_pkg_resource.erl b/src/rebar_pkg_resource.erl index 5b37788..59ce0dc 100644 --- a/src/rebar_pkg_resource.erl +++ b/src/rebar_pkg_resource.erl @@ -95,8 +95,7 @@ make_vsn(_) -> request(Url, ETag) -> case httpc:request(get, {Url, [{"if-none-match", ETag} || ETag =/= false]}, [{relaxed, true}], - [{body_format, binary}], - hex) of + [{body_format, binary}]) of {ok, {{_Version, 200, _Reason}, Headers, Body}} -> ?DEBUG("Successfully downloaded ~s", [Url]), {"etag", ETag1} = lists:keyfind("etag", 1, Headers), -- cgit v1.1 From 46ca2bb6b0e9c99c7a5b4efb7b9efc310c0982b8 Mon Sep 17 00:00:00 2001 From: CarlosEDP Date: Thu, 2 Jul 2015 10:42:46 -0300 Subject: Fix for cases with no proxy configured. --- src/rebar_utils.erl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/rebar_utils.erl b/src/rebar_utils.erl index 77d5e71..70741e4 100644 --- a/src/rebar_utils.erl +++ b/src/rebar_utils.erl @@ -674,7 +674,7 @@ get_http_var() -> end. get_http(Scheme) -> - proplists:get_value(Scheme, get_http_var(), ""). + proplists:get_value(Scheme, get_http_var(), []). set_httpc_options() -> set_httpc_options(https_proxy, get_http(https_proxy)), -- cgit v1.1 From 38cc32c40afdf8e999d8705501acd7e13918cdc9 Mon Sep 17 00:00:00 2001 From: CarlosEDP Date: Thu, 2 Jul 2015 11:59:00 -0300 Subject: Added rebar profile to httpc initialization and calls. --- src/rebar3.erl | 3 ++- src/rebar_pkg_resource.erl | 3 ++- src/rebar_prv_update.erl | 3 ++- src/rebar_utils.erl | 2 +- 4 files changed, 7 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/rebar3.erl b/src/rebar3.erl index 13f6017..a797c46 100644 --- a/src/rebar3.erl +++ b/src/rebar3.erl @@ -273,5 +273,6 @@ start_and_load_apps() -> application:start(public_key), application:start(ssl), inets:start(), - inets:start(httpc), + inets:start(httpc, [{profile, rebar}]), rebar_utils:set_httpc_options(). + diff --git a/src/rebar_pkg_resource.erl b/src/rebar_pkg_resource.erl index 59ce0dc..450ff1e 100644 --- a/src/rebar_pkg_resource.erl +++ b/src/rebar_pkg_resource.erl @@ -95,7 +95,8 @@ make_vsn(_) -> request(Url, ETag) -> case httpc:request(get, {Url, [{"if-none-match", ETag} || ETag =/= false]}, [{relaxed, true}], - [{body_format, binary}]) of + [{body_format, binary}], + rebar) of {ok, {{_Version, 200, _Reason}, Headers, Body}} -> ?DEBUG("Successfully downloaded ~s", [Url]), {"etag", ETag1} = lists:keyfind("etag", 1, Headers), diff --git a/src/rebar_prv_update.erl b/src/rebar_prv_update.erl index 6838bab..64fe65e 100644 --- a/src/rebar_prv_update.erl +++ b/src/rebar_prv_update.erl @@ -43,7 +43,8 @@ do(State) -> Url = rebar_state:get(State, rebar_packages_cdn, "https://s3.amazonaws.com/s3.hex.pm/registry.ets.gz"), {ok, _RequestId} = httpc:request(get, {Url, []}, - [], [{stream, TmpFile}, {sync, true}]), + [], [{stream, TmpFile}, {sync, true}], + rebar), {ok, Data} = file:read_file(TmpFile), Unzipped = zlib:gunzip(Data), ok = file:write_file(HexFile, Unzipped), diff --git a/src/rebar_utils.erl b/src/rebar_utils.erl index 70741e4..97104ce 100644 --- a/src/rebar_utils.erl +++ b/src/rebar_utils.erl @@ -685,4 +685,4 @@ set_httpc_options(_, []) -> set_httpc_options(Scheme, Proxy) -> {ok, {_, _, Host, Port, _, _}} = http_uri:parse(Proxy), - httpc:set_options([{Scheme, {{Host, Port}, []}}]). + httpc:set_options([{Scheme, {{Host, Port}, []}}], rebar). -- cgit v1.1 From 4a25a4d9d5f968fe2445707e8077a6efda596192 Mon Sep 17 00:00:00 2001 From: CarlosEDP Date: Thu, 2 Jul 2015 11:59:39 -0300 Subject: Renamed functions. --- src/rebar_utils.erl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/rebar_utils.erl b/src/rebar_utils.erl index 97104ce..2bfe99e 100644 --- a/src/rebar_utils.erl +++ b/src/rebar_utils.erl @@ -677,8 +677,8 @@ get_http(Scheme) -> proplists:get_value(Scheme, get_http_var(), []). set_httpc_options() -> - set_httpc_options(https_proxy, get_http(https_proxy)), - set_httpc_options(proxy, get_http(http_proxy)). + set_httpc_options(https_proxy, get_http_vars(https_proxy)), + set_httpc_options(proxy, get_http_vars(http_proxy)). set_httpc_options(_, []) -> ok; -- cgit v1.1 From eb2d31be267ec0f85fe1bcd1f322c37149e3a9a3 Mon Sep 17 00:00:00 2001 From: CarlosEDP Date: Thu, 2 Jul 2015 12:00:13 -0300 Subject: Use rebar own method to read global config file. --- src/rebar_utils.erl | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/rebar_utils.erl b/src/rebar_utils.erl index 2bfe99e..1cd6694 100644 --- a/src/rebar_utils.erl +++ b/src/rebar_utils.erl @@ -663,18 +663,10 @@ maybe_ends_in_comma(H) -> _ -> false end. -get_http_var() -> - {ok, [[Home]]} = init:get_argument(home), - ConfDir = filename:join(Home, ".config/rebar3"), - case file:consult(filename:join(ConfDir, "rebar.config")) of - {ok, Config} -> - Config; - _ -> - [] - end. - -get_http(Scheme) -> - proplists:get_value(Scheme, get_http_var(), []). +get_http_vars(Scheme) -> + GlobalConfigFile = rebar_dir:global_config(), + Config = rebar_config:consult_file(GlobalConfigFile), + proplists:get_value(Scheme, Config, []). set_httpc_options() -> set_httpc_options(https_proxy, get_http_vars(https_proxy)), -- cgit v1.1