Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/aws_s3_presigned_url.erl
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ make_presigned_v4_url(Client0, Method, ExpireSeconds, Bucket, Key, Style) ->
undefined ->
Options0;
_ ->
[{session_token, aws_util:encode_uri(SecurityToken)} | Options0]
[{session_token, aws_util:encode_uri(SecurityToken, full)} | Options0]
end,
{ok, aws_signature:sign_v4_query_params(AccessKeyID, SecretAccessKey, Region, Service, Now, MethodBin, URL, Options)}.

Expand Down
28 changes: 21 additions & 7 deletions src/aws_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
add_headers/2,
encode_query/1,
encode_uri/1,
encode_uri/2,
encode_multi_segment_uri/1,
encode_xml/1,
decode_xml/1
Expand Down Expand Up @@ -51,14 +52,22 @@ encode_multi_segment_uri(Value) ->
binary_join(Encoded, <<"/">>).

%% @doc Encode URI into a percent-encoding string.
-spec encode_uri(binary()) -> binary().
encode_uri(Value) when is_list(Value) ->
encode_uri(list_to_binary(Value));
encode_uri(list_to_binary(Value), skip_slash);
encode_uri(Value) when is_binary(Value) ->
<< (uri_encode_path_byte(Byte)) || <<Byte>> <= Value >>.

-spec uri_encode_path_byte(byte()) -> binary().
uri_encode_path_byte($/) -> <<"/">>;
uri_encode_path_byte(Byte)
encode_uri(Value, skip_slash).

-spec encode_uri(binary(), skip_slash | full) -> binary().
encode_uri(Value, Type) when is_list(Value) ->
encode_uri(list_to_binary(Value), Type);
encode_uri(Value, Type) when is_binary(Value) ->
<< (uri_encode_path_byte(Byte, Type)) || <<Byte>> <= Value >>.

-spec uri_encode_path_byte(byte(), atom()) -> binary().
uri_encode_path_byte($/, skip_slash) -> <<"/">>;
uri_encode_path_byte($/, full) -> <<"%2F">>;
uri_encode_path_byte(Byte, _Type)
when $0 =< Byte, Byte =< $9;
$a =< Byte, Byte =< $z;
$A =< Byte, Byte =< $Z;
Expand All @@ -67,7 +76,7 @@ uri_encode_path_byte(Byte)
Byte =:= $-;
Byte =:= $. ->
<<Byte>>;
uri_encode_path_byte(Byte) ->
uri_encode_path_byte(Byte, _Type) ->
H = Byte band 16#F0 bsr 4,
L = Byte band 16#0F,
<<"%", (hex(H, upper)), (hex(L, upper))>>.
Expand Down Expand Up @@ -282,6 +291,11 @@ encode_uri_test() ->
?assertEqual(<<"hello%20world%21">>, encode_uri(Segment)),
?assertEqual(encode_uri(Segment), encode_uri(binary_to_list(Segment))).

encode_forward_slash_test() ->
Segment = <<"hello/world!">>,
?assertEqual(<<"hello%2Fworld%21">>, encode_uri(Segment, full)),
?assertEqual(encode_uri(Segment, full), encode_uri(binary_to_list(Segment), full)).

encode_uri_parenthesis_test() ->
Segment = <<"hello world(!)">>,
?assertEqual(<<"hello%20world%28%21%29">>, encode_uri(Segment)).
Expand Down