summaryrefslogtreecommitdiff
path: root/src/rebar_prv_local_upgrade.erl
diff options
context:
space:
mode:
Diffstat (limited to 'src/rebar_prv_local_upgrade.erl')
-rw-r--r--src/rebar_prv_local_upgrade.erl28
1 files changed, 27 insertions, 1 deletions
diff --git a/src/rebar_prv_local_upgrade.erl b/src/rebar_prv_local_upgrade.erl
index 3b3c9cb..1931d65 100644
--- a/src/rebar_prv_local_upgrade.erl
+++ b/src/rebar_prv_local_upgrade.erl
@@ -77,7 +77,7 @@ get_md5(Rebar3Path) ->
maybe_fetch_rebar3(Rebar3Md5) ->
TmpDir = ec_file:insecure_mkdtemp(),
TmpFile = filename:join(TmpDir, "rebar3"),
- case rebar_pkg_resource:request("https://s3.amazonaws.com/rebar3/rebar3", Rebar3Md5) of
+ case request("https://s3.amazonaws.com/rebar3/rebar3", Rebar3Md5) of
{ok, Binary, ETag} ->
file:write_file(TmpFile, Binary),
case etag(TmpFile) of
@@ -101,3 +101,29 @@ etag(Path) ->
{error, _} ->
false
end.
+
+-spec request(Url, ETag) -> Res when
+ Url :: string(),
+ ETag :: false | string(),
+ Res :: 'error' | {ok, cached} | {ok, any(), string()}.
+request(Url, ETag) ->
+ HttpOptions = [{ssl, rebar_utils:ssl_opts(Url)},
+ {relaxed, true} | rebar_utils:get_proxy_auth()],
+ case httpc:request(get, {Url, [{"if-none-match", "\"" ++ ETag ++ "\""}
+ || ETag =/= false] ++
+ [{"User-Agent", rebar_utils:user_agent()}]},
+ HttpOptions, [{body_format, binary}], rebar) of
+ {ok, {{_Version, 200, _Reason}, Headers, Body}} ->
+ ?DEBUG("Successfully downloaded ~ts", [Url]),
+ {"etag", ETag1} = lists:keyfind("etag", 1, Headers),
+ {ok, Body, rebar_string:trim(ETag1, both, [$"])};
+ {ok, {{_Version, 304, _Reason}, _Headers, _Body}} ->
+ ?DEBUG("Cached copy of ~ts still valid", [Url]),
+ {ok, cached};
+ {ok, {{_Version, Code, _Reason}, _Headers, _Body}} ->
+ ?DEBUG("Request to ~p failed: status code ~p", [Url, Code]),
+ error;
+ {error, Reason} ->
+ ?DEBUG("Request to ~p failed: ~p", [Url, Reason]),
+ error
+ end.