Skip to content

Commit f850e7b

Browse files
authored
Merge pull request #2565 from nalundgaard/3.13.x-introduce-rebar_http_adapter
Introduce hex_core HTTP adapter for rebar3 (3.13.x)
2 parents 73c3613 + 24c5980 commit f850e7b

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

src/rebar_httpc_adapter.erl

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
%% Derived from hex_core v0.5.1 for extra flexibility.
2+
3+
-module(rebar_httpc_adapter).
4+
-behaviour(r3_hex_http).
5+
-export([request/5]).
6+
7+
%%====================================================================
8+
%% API functions
9+
%%====================================================================
10+
11+
request(Method, URI, ReqHeaders, Body, AdapterConfig) ->
12+
Profile = maps:get(profile, AdapterConfig, default),
13+
Request = build_request(URI, ReqHeaders, Body),
14+
SSLOpts = [{ssl, rebar_utils:ssl_opts(URI)}],
15+
case httpc:request(Method, Request, SSLOpts, [{body_format, binary}], Profile) of
16+
{ok, {{_, StatusCode, _}, RespHeaders, RespBody}} ->
17+
RespHeaders2 = load_headers(RespHeaders),
18+
{ok, {StatusCode, RespHeaders2, RespBody}};
19+
{error, Reason} -> {error, Reason}
20+
end.
21+
22+
%%====================================================================
23+
%% Internal functions
24+
%%====================================================================
25+
26+
build_request(URI, ReqHeaders, Body) ->
27+
build_request2(binary_to_list(URI), dump_headers(ReqHeaders), Body).
28+
29+
build_request2(URI, ReqHeaders, undefined) ->
30+
{URI, ReqHeaders};
31+
build_request2(URI, ReqHeaders, {ContentType, Body}) ->
32+
{URI, ReqHeaders, ContentType, Body}.
33+
34+
dump_headers(Map) ->
35+
maps:fold(fun(K, V, Acc) ->
36+
[{binary_to_list(K), binary_to_list(V)} | Acc] end, [], Map).
37+
38+
load_headers(List) ->
39+
lists:foldl(fun({K, V}, Acc) ->
40+
maps:put(list_to_binary(K), list_to_binary(V), Acc) end, #{}, List).
41+

src/rebar_pkg_resource.erl

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
-spec init(atom(), rebar_state:t()) -> {ok, rebar_resource_v2:resource()}.
3030
init(Type, State) ->
3131
{ok, Vsn} = application:get_key(rebar, vsn),
32-
BaseConfig = #{http_adapter => r3_hex_http_httpc,
32+
BaseConfig = #{http_adapter => rebar_httpc_adapter,
3333
http_user_agent_fragment =>
3434
<<"(rebar3/", (list_to_binary(Vsn))/binary, ") (httpc)">>,
3535
http_adapter_config => #{profile => rebar}},

test/rebar_pkg_repos_SUITE.erl

+9
Original file line numberDiff line numberDiff line change
@@ -319,13 +319,15 @@ use_first_repo_match(Config) ->
319319
?assertMatch({ok,{package,{<<"B">>, {{2,0,0}, {[],[]}}, Repo2},
320320
<<"some checksum">>, false, []},
321321
#{name := Repo2,
322+
http_adapter := rebar_httpc_adapter,
322323
http_adapter_config := #{profile := rebar}}},
323324
rebar_packages:resolve_version(<<"B">>, <<"> 1.4.0">>, undefined,
324325
?PACKAGE_TABLE, State)),
325326

326327
?assertMatch({ok,{package,{<<"B">>, {{1,4,0}, {[],[]}}, Repo3},
327328
<<"some checksum">>, false, []},
328329
#{name := Repo3,
330+
http_adapter := rebar_httpc_adapter,
329331
http_adapter_config := #{profile := rebar}}},
330332
rebar_packages:resolve_version(<<"B">>, <<"~> 1.4.0">>, undefined,
331333
?PACKAGE_TABLE, State)).
@@ -337,6 +339,7 @@ use_exact_with_hash(Config) ->
337339
?assertMatch({ok,{package,{<<"C">>, {{1,3,1}, {[],[]}}, Repo2},
338340
<<"good checksum">>, false, []},
339341
#{name := Repo2,
342+
http_adapter := rebar_httpc_adapter,
340343
http_adapter_config := #{profile := rebar}}},
341344
rebar_packages:resolve_version(<<"C">>, <<"1.3.1">>, <<"good checksum">>,
342345
?PACKAGE_TABLE, State)).
@@ -347,6 +350,7 @@ fail_repo_update(Config) ->
347350
?assertMatch({ok,{package,{<<"B">>, {{1,4,0}, {[],[]}}, Repo3},
348351
<<"some checksum">>, false, []},
349352
#{name := Repo3,
353+
http_adapter := rebar_httpc_adapter,
350354
http_adapter_config := #{profile := rebar}}},
351355
rebar_packages:resolve_version(<<"B">>, <<"~> 1.4.0">>, undefined,
352356
?PACKAGE_TABLE, State)).
@@ -358,6 +362,7 @@ ignore_match_in_excluded_repo(Config) ->
358362
?assertMatch({ok,{package,{<<"B">>, {{1,4,6}, {[],[]}}, Hexpm},
359363
<<"some checksum">>, #{reason := 'RETIRED_INVALID'}, []},
360364
#{name := Hexpm,
365+
http_adapter := rebar_httpc_adapter,
361366
http_adapter_config := #{profile := rebar}}},
362367
rebar_packages:resolve_version(<<"B">>, <<"~> 1.4.0">>, undefined,
363368
?PACKAGE_TABLE, State)),
@@ -366,6 +371,7 @@ ignore_match_in_excluded_repo(Config) ->
366371
?assertMatch({ok,{package,{<<"A">>, {{0,1,1}, {[],[]}}, Repo2},
367372
<<"good checksum">>, false, []},
368373
#{name := Repo2,
374+
http_adapter := rebar_httpc_adapter,
369375
http_adapter_config := #{profile := rebar}}},
370376
rebar_packages:resolve_version(<<"A">>, <<"0.1.1">>, <<"good checksum">>,
371377
?PACKAGE_TABLE, State)).
@@ -376,13 +382,15 @@ optional_prereleases(Config) ->
376382
?assertMatch({ok,{package,{<<"B">>, {{1,5,0}, {[],[]}}, Hexpm},
377383
<<"some checksum">>, false, []},
378384
#{name := Hexpm,
385+
http_adapter := rebar_httpc_adapter,
379386
http_adapter_config := #{profile := rebar}}},
380387
rebar_packages:resolve_version(<<"B">>, <<"~> 1.5.0">>, undefined,
381388
?PACKAGE_TABLE, State)),
382389

383390
?assertMatch({ok,{package,{<<"B">>, {{1,5,6}, {[<<"rc">>,0],[]}}, Hexpm},
384391
<<"some checksum">>, true, []},
385392
#{name := Hexpm,
393+
http_adapter := rebar_httpc_adapter,
386394
http_adapter_config := #{profile := rebar}}},
387395
rebar_packages:resolve_version(<<"B">>, <<"1.5.6-rc.0">>, <<"some checksum">>,
388396
?PACKAGE_TABLE, State)),
@@ -392,6 +400,7 @@ optional_prereleases(Config) ->
392400
?assertMatch({ok,{package,{<<"B">>, {{1,5,6}, {[<<"rc">>,0],[]}}, Hexpm},
393401
<<"some checksum">>, true, []},
394402
#{name := Hexpm,
403+
http_adapter := rebar_httpc_adapter,
395404
http_adapter_config := #{profile := rebar}}},
396405
rebar_packages:resolve_version(<<"B">>, <<"~> 1.5.0">>, <<"some checksum">>,
397406
?PACKAGE_TABLE, State1)).

0 commit comments

Comments
 (0)