Browse Source

check md5sum of package against that sent by s3

pull/414/head
Tristan Sloughter 10 years ago
parent
commit
b636822d73
2 changed files with 17 additions and 4 deletions
  1. +4
    -0
      src/rebar_fetch.erl
  2. +13
    -4
      src/rebar_pkg_resource.erl

+ 4
- 0
src/rebar_fetch.erl View File

@ -43,6 +43,8 @@ download_source(AppDir, Source, State) ->
verify_and_extract(File, Source, AppDir1, State) verify_and_extract(File, Source, AppDir1, State)
end end
catch catch
_:bad_etag ->
throw(?PRV_ERROR({bad_etag, Source}));
C:T -> C:T ->
?DEBUG("rebar_fetch exception ~p ~p ~p", [C, T, erlang:get_stacktrace()]), ?DEBUG("rebar_fetch exception ~p ~p ~p", [C, T, erlang:get_stacktrace()]),
throw(?PRV_ERROR({fetch_fail, Source})) throw(?PRV_ERROR({fetch_fail, Source}))
@ -59,6 +61,8 @@ needs_update(AppDir, Source, State) ->
true true
end. end.
format_error({bad_etag, Source}) ->
io_lib:format("MD5 Checksum comparison failed for: ~p", [Source]);
format_error({fetch_fail, Source}) -> format_error({fetch_fail, Source}) ->
io_lib:format("Failed to fetch and copy dep: ~p", [Source]); io_lib:format("Failed to fetch and copy dep: ~p", [Source]);
format_error({bad_checksum, File}) -> format_error({bad_checksum, File}) ->

+ 13
- 4
src/rebar_pkg_resource.erl View File

@ -37,9 +37,17 @@ download(_Dir, {pkg, Name, Vsn}, State) ->
case request(Url, etag(Path)) of case request(Url, etag(Path)) of
{ok, cached} -> {ok, cached} ->
{tarball, Path}; {tarball, Path};
{ok, Binary} ->
{ok, Binary, EtagHeader} ->
file:write_file(Path, Binary), file:write_file(Path, Binary),
{tarball, Path};
Etag = etag(Path),
case EtagHeader =:= Etag of
true ->
{tarball, Path};
false ->
?DEBUG("Bad md5sum for ~s of ~s comparing to ~s sent by server",
[Path, Etag, EtagHeader]),
throw(bad_etag)
end;
error -> error ->
case filelib:is_regular(Path) of case filelib:is_regular(Path) of
true -> true ->
@ -68,9 +76,10 @@ request(Url, ETag) ->
case httpc:request(get, {Url, [{"if-none-match", ETag} || ETag =/= false]}, case httpc:request(get, {Url, [{"if-none-match", ETag} || ETag =/= false]},
[{relaxed, true}], [{relaxed, true}],
[{body_format, binary}]) of [{body_format, binary}]) of
{ok, {{_Version, 200, _Reason}, _Headers, Body}} ->
{ok, {{_Version, 200, _Reason}, Headers, Body}} ->
{"etag", ETag1} = lists:keyfind("etag", 1, Headers),
?DEBUG("Successfully downloaded ~s", [Url]), ?DEBUG("Successfully downloaded ~s", [Url]),
{ok, Body};
{ok, Body, string:strip(ETag1, both, $")};
{ok, {{_Version, 304, _Reason}, _Headers, _Body}} -> {ok, {{_Version, 304, _Reason}, _Headers, _Body}} ->
?DEBUG("Cached copy of ~s still valid", [Url]), ?DEBUG("Cached copy of ~s still valid", [Url]),
{ok, cached}; {ok, cached};

Loading…
Cancel
Save