Skip to content

Commit f2c0c82

Browse files
committed
Merge branch 'su_aws/replace_httpc_with_gun_fmt' of github.com:rabbitmq/rabbitmq-server into su_aws/replace_httpc_with_gun_fmt
2 parents 8497f11 + 5172700 commit f2c0c82

File tree

3 files changed

+34
-33
lines changed

3 files changed

+34
-33
lines changed

deps/rabbitmq_aws/include/rabbitmq_aws.hrl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@
113113
-type value() :: string().
114114
-type header() :: {Field :: field(), Value :: value()}.
115115
-type headers() :: [header()].
116-
-type body() :: string() | binary().
116+
-type body() :: iodata().
117117

118118
-type ssl_options() :: [ssl:tls_client_option()].
119119

deps/rabbitmq_aws/src/rabbitmq_aws.erl

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,7 @@
3737
-include("rabbitmq_aws.hrl").
3838
-include_lib("kernel/include/logger.hrl").
3939

40-
%% Types for new concurrent API
4140
-type connection_handle() :: {gun:conn_ref(), string()}.
42-
4341
%%====================================================================
4442
%% ETS-based state management
4543
%%====================================================================
@@ -258,8 +256,9 @@ direct_request({GunPid, Service}, Method, Path, Body, Headers, Options) ->
258256
{ok, AccessKey, SecretKey, SecurityToken, Region} ->
259257
Host = endpoint_host(Region, Service),
260258
URI = create_uri(Host, Path),
259+
BodyHash = proplists:get_value(payload_hash, Options),
261260
SignedHeaders = sign_headers(
262-
AccessKey, SecretKey, SecurityToken, Region, Service, Method, URI, Headers, Body
261+
AccessKey, SecretKey, SecurityToken, Region, Service, Method, URI, Headers, Body, BodyHash
263262
),
264263
direct_gun_request(GunPid, Method, Path, SignedHeaders, Body, Options);
265264
{error, Reason} ->
@@ -275,20 +274,24 @@ direct_request({GunPid, Service}, Method, Path, Body, Headers, Options) ->
275274
Method :: method(),
276275
URI :: string(),
277276
Headers :: headers(),
278-
Body :: body()
277+
Body :: body(),
278+
BodyHash :: iodata()
279279
) -> headers().
280-
sign_headers(AccessKey, SecretKey, SecurityToken, Region, Service, Method, URI, Headers, Body) ->
281-
rabbitmq_aws_sign:headers(#request{
282-
access_key = AccessKey,
283-
secret_access_key = SecretKey,
284-
security_token = SecurityToken,
285-
region = Region,
286-
service = Service,
287-
method = Method,
288-
uri = URI,
289-
headers = Headers,
290-
body = Body
291-
}).
280+
sign_headers(AccessKey, SecretKey, SecurityToken, Region, Service, Method, URI, Headers, Body, BodyHash) ->
281+
rabbitmq_aws_sign:headers(
282+
#request{
283+
access_key = AccessKey,
284+
secret_access_key = SecretKey,
285+
security_token = SecurityToken,
286+
region = Region,
287+
service = Service,
288+
method = Method,
289+
uri = URI,
290+
headers = Headers,
291+
body = Body
292+
},
293+
BodyHash
294+
).
292295

293296
-spec request(
294297
Service :: string(),
@@ -673,8 +676,9 @@ status_text(416) -> "Range Not Satisfiable";
673676
status_text(500) -> "Internal Server Error";
674677
status_text(Code) -> integer_to_list(Code).
675678

679+
676680
-spec direct_gun_request(
677-
GunPid :: gun:conn_ref(),
681+
GunPid :: pid(),
678682
Method :: method(),
679683
Path :: path(),
680684
Headers :: headers(),

deps/rabbitmq_aws/src/rabbitmq_aws_sign.erl

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
-module(rabbitmq_aws_sign).
99

1010
%% API
11-
-export([headers/1, request_hash/5]).
11+
-export([headers/1, headers/2, request_hash/5]).
1212

1313
%% Export all for unit tests
1414
-ifdef(TEST).
@@ -24,18 +24,15 @@
2424
%% @doc Create the signed request headers
2525
%% end
2626
headers(Request) ->
27+
headers(Request, undefined).
28+
29+
headers(Request, undefined) ->
30+
headers(Request, sha256(Request#request.body));
31+
headers(Request, PayloadHash) ->
2732
RequestTimestamp = local_time(),
28-
PayloadHash = sha256(Request#request.body),
2933
URI = rabbitmq_aws_urilib:parse(Request#request.uri),
3034
{_, Host, _} = URI#uri.authority,
31-
32-
BodyLength =
33-
case Request#request.body of
34-
Body when is_binary(Body) ->
35-
size(Body);
36-
Body when is_list(Body) ->
37-
length(Body)
38-
end,
35+
BodyLength = iolist_size(Request#request.body),
3936

4037
Headers = append_headers(
4138
RequestTimestamp,
@@ -50,7 +47,7 @@ headers(Request) ->
5047
URI#uri.path,
5148
URI#uri.query,
5249
Headers,
53-
Request#request.body
50+
PayloadHash
5451
),
5552
AuthValue = authorization(
5653
Request#request.access_key,
@@ -211,11 +208,11 @@ query_string(QueryArgs) -> rabbitmq_aws_urilib:build_query_string(lists:keysort(
211208
Path :: path(),
212209
QArgs :: query_args(),
213210
Headers :: headers(),
214-
Payload :: string()
211+
PayloadHash :: string()
215212
) -> string().
216213
%% @doc Create the request hash value
217214
%% @end
218-
request_hash(Method, Path, QArgs, Headers, Payload) ->
215+
request_hash(Method, Path, QArgs, Headers, PayloadHash) ->
219216
RawPath =
220217
case string:slice(Path, 0, 1) of
221218
"/" -> Path;
@@ -229,7 +226,7 @@ request_hash(Method, Path, QArgs, Headers, Payload) ->
229226
query_string(QArgs),
230227
canonical_headers(Headers),
231228
signed_headers(Headers),
232-
sha256(Payload)
229+
PayloadHash
233230
],
234231
"\n"
235232
),
@@ -245,7 +242,7 @@ request_hash(Method, Path, QArgs, Headers, Payload) ->
245242
scope(AMZDate, Region, Service) ->
246243
string:join([AMZDate, Region, Service, "aws4_request"], "/").
247244

248-
-spec sha256(Value :: string()) -> string().
245+
-spec sha256(Value :: iodata()) -> string().
249246
%% @doc Return the SHA-256 hash for the specified value.
250247
%% @end
251248
sha256(Value) ->

0 commit comments

Comments
 (0)