summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTristan Sloughter <tristan.sloughter@gmail.com>2015-07-05 08:54:43 -0500
committerTristan Sloughter <tristan.sloughter@gmail.com>2015-07-05 08:54:43 -0500
commit632f2dd304cc9dbd280711424985684fa8ada365 (patch)
treeff21bbf3296bb3b20254d557f660205a868a4c38
parent6fdbea7094c7582635b3c809989497ec9dfaf137 (diff)
parent9e4bf8a3bad3f46f8094339b38ac880ec1b977d2 (diff)
Merge pull request #579 from carlosedp/add-proxy
Add proxy support to bootstrap and rebar3. Enhancement #561.
-rw-r--r--THANKS1
-rwxr-xr-xbootstrap29
-rw-r--r--src/rebar3.erl13
-rw-r--r--src/rebar_pkg_resource.erl2
-rw-r--r--src/rebar_prv_update.erl3
-rw-r--r--src/rebar_utils.erl19
-rw-r--r--test/rebar_deps_SUITE.erl68
7 files changed, 120 insertions, 15 deletions
diff --git a/THANKS b/THANKS
index a6562ed..d813970 100644
--- a/THANKS
+++ b/THANKS
@@ -134,3 +134,4 @@ Martin Karlsson
Pierre Fenoll
David Kubecka
Stefan Grundmann
+Carlos Eduardo de Paula
diff --git a/bootstrap b/bootstrap
index 41d9725..71c44da 100755
--- a/bootstrap
+++ b/bootstrap
@@ -9,6 +9,8 @@ main(_Args) ->
application:start(public_key),
application:start(ssl),
inets:start(),
+ inets:start(httpc, [{profile, rebar}]),
+ set_httpc_options(),
%% Fetch and build deps required to build rebar3
BaseDeps = [{providers, []}
@@ -84,13 +86,38 @@ extract(Binary) ->
request(Url) ->
case httpc:request(get, {Url, []},
[{relaxed, true}],
- [{body_format, binary}]) of
+ [{body_format, binary}],
+ rebar) of
{ok, {{_Version, 200, _Reason}, _Headers, Body}} ->
{ok, Body};
Error ->
Error
end.
+get_rebar_config() ->
+ {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_vars(Scheme) ->
+ proplists:get_value(Scheme, get_rebar_config(), []).
+
+set_httpc_options() ->
+ set_httpc_options(https_proxy, get_http_vars(https_proxy)),
+ set_httpc_options(proxy, get_http_vars(http_proxy)).
+
+set_httpc_options(_, []) ->
+ ok;
+
+set_httpc_options(Scheme, Proxy) ->
+ {ok, {_, _, Host, Port, _, _}} = http_uri:parse(Proxy),
+ httpc:set_options([{Scheme, {{Host, Port}, []}}], rebar).
+
compile(App, FirstFiles) ->
Dir = filename:join(filename:absname("_build/default/lib/"), App),
filelib:ensure_dir(filename:join([Dir, "ebin", "dummy.beam"])),
diff --git a/src/rebar3.erl b/src/rebar3.erl
index c501709..a797c46 100644
--- a/src/rebar3.erl
+++ b/src/rebar3.erl
@@ -273,13 +273,6 @@ 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, [{profile, rebar}]),
+ rebar_utils:set_httpc_options().
+
diff --git a/src/rebar_pkg_resource.erl b/src/rebar_pkg_resource.erl
index 5b37788..450ff1e 100644
--- a/src/rebar_pkg_resource.erl
+++ b/src/rebar_pkg_resource.erl
@@ -96,7 +96,7 @@ request(Url, ETag) ->
case httpc:request(get, {Url, [{"if-none-match", ETag} || ETag =/= false]},
[{relaxed, true}],
[{body_format, binary}],
- hex) of
+ 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 0cbc7c2..1cd6694 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,19 @@ maybe_ends_in_comma(H) ->
"," ++ Flag -> lists:reverse(Flag);
_ -> false
end.
+
+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)),
+ set_httpc_options(proxy, get_http_vars(http_proxy)).
+
+set_httpc_options(_, []) ->
+ ok;
+
+set_httpc_options(Scheme, Proxy) ->
+ {ok, {_, _, Host, Port, _, _}} = http_uri:parse(Proxy),
+ httpc:set_options([{Scheme, {{Host, Port}, []}}], rebar).
diff --git a/test/rebar_deps_SUITE.erl b/test/rebar_deps_SUITE.erl
index afd487e..73c4980 100644
--- a/test/rebar_deps_SUITE.erl
+++ b/test/rebar_deps_SUITE.erl
@@ -3,7 +3,7 @@
-include_lib("common_test/include/ct.hrl").
-include_lib("eunit/include/eunit.hrl").
-all() -> [sub_app_deps, newly_added_dep, {group, git}, {group, pkg}].
+all() -> [sub_app_deps, newly_added_dep, http_proxy_settings, https_proxy_settings, {group, git}, {group, pkg}].
groups() ->
[{all, [], [flat, pick_highest_left, pick_highest_right,
@@ -33,6 +33,47 @@ init_per_testcase(newly_added_dep, Config) ->
rebar_test_utils:init_rebar_state(Config);
init_per_testcase(sub_app_deps, Config) ->
rebar_test_utils:init_rebar_state(Config);
+init_per_testcase(http_proxy_settings, Config) ->
+ %% Create private rebar.config
+ Priv = ?config(priv_dir, Config),
+ GlobalDir = filename:join(Priv, "global"),
+ GlobalConfigDir = filename:join([GlobalDir, ".config", "rebar3"]),
+ GlobalConfig = filename:join([GlobalDir, ".config", "rebar3", "rebar.config"]),
+
+ meck:new(rebar_dir, [passthrough]),
+ meck:expect(rebar_dir, global_config, fun() -> GlobalConfig end),
+ meck:expect(rebar_dir, global_cache_dir, fun(_) -> GlobalDir end),
+
+ %% Insert proxy variables into config
+ rebar_test_utils:create_config(GlobalConfigDir,
+ [{http_proxy, "http://localhost:1234"}
+ ]),
+ rebar_test_utils:init_rebar_state(Config);
+init_per_testcase(https_proxy_settings, Config) ->
+ SupportsHttpsProxy = case erlang:system_info(otp_release) of
+ "R16"++_ -> true;
+ "R"++_ -> false;
+ _ -> true % 17 and up don't have a "R" in the version
+ end,
+ if not SupportsHttpsProxy ->
+ {skip, https_proxy_unsupported_before_R16};
+ SupportsHttpsProxy ->
+ %% Create private rebar.config
+ Priv = ?config(priv_dir, Config),
+ GlobalDir = filename:join(Priv, "global"),
+ GlobalConfigDir = filename:join([GlobalDir, ".config", "rebar3"]),
+ GlobalConfig = filename:join([GlobalDir, ".config", "rebar3", "rebar.config"]),
+
+ meck:new(rebar_dir, [passthrough]),
+ meck:expect(rebar_dir, global_config, fun() -> GlobalConfig end),
+ meck:expect(rebar_dir, global_cache_dir, fun(_) -> GlobalDir end),
+
+ %% Insert proxy variables into config
+ rebar_test_utils:create_config(GlobalConfigDir,
+ [{https_proxy, "http://localhost:1234"}
+ ]),
+ rebar_test_utils:init_rebar_state(Config)
+ end;
init_per_testcase(Case, Config) ->
{Deps, Warnings, Expect} = deps(Case),
Expected = case Expect of
@@ -45,6 +86,12 @@ init_per_testcase(Case, Config) ->
{warnings, Warnings}
| setup_project(Case, Config, rebar_test_utils:expand_deps(DepsType, Deps))].
+end_per_testcase(https_proxy_settings, Config) ->
+ meck:unload(rebar_dir),
+ Config;
+end_per_testcase(http_proxy_settings, Config) ->
+ meck:unload(rebar_dir),
+ Config;
end_per_testcase(_, Config) ->
meck:unload(),
Config.
@@ -223,6 +270,25 @@ newly_added_dep(Config) ->
{ok, [{app, Name}, {dep, "a"}, {dep, "b", "1.0.0"}, {dep, "c", "1.0.0"}]}).
+http_proxy_settings(_Config) ->
+ %% Load config
+ rebar_utils:set_httpc_options(),
+ rebar3:init_config(),
+
+ %% Assert variable is right
+ ?assertEqual({ok,{{"localhost", 1234}, []}},
+ httpc:get_option(proxy, rebar)).
+
+https_proxy_settings(_Config) ->
+ %% Load config
+ rebar_utils:set_httpc_options(),
+ rebar3:init_config(),
+
+ %% Assert variable is right
+ ?assertEqual({ok,{{"localhost", 1234}, []}},
+ httpc:get_option(https_proxy, rebar)).
+
+
run(Config) ->
{ok, RebarConfig} = file:consult(?config(rebarconfig, Config)),
rebar_test_utils:run_and_check(