xref: /aosp_15_r20/external/aws-crt-java/src/main/java/software/amazon/awssdk/crt/io/Uri.java (revision 3c7ae9de214676c52d19f01067dc1a404272dc11)
1 /**
2  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  * SPDX-License-Identifier: Apache-2.0.
4  */
5 package software.amazon.awssdk.crt.io;
6 
7 import java.nio.charset.StandardCharsets;
8 import software.amazon.awssdk.crt.CRT;
9 
10 /**
11  * Wrapper around an http URI
12  */
13 public class Uri {
14     static {
CRT()15         new CRT();
16     };
17 
18     /**
19      * Returns a concatenation of an encoded base, and the URI path encoding of a
20      * string. This is the modified version of rfc3986 used by sigv4 signing.
21      *
22      * @param encoded The encoded original path.
23      * @param path    The path to be encoded and appended to the original path
24      *
25      * @return concatenation
26      */
appendEncodingUriPath(String encoded, String path)27     public static String appendEncodingUriPath(String encoded, String path) {
28         return new String(
29                 appendEncodingUriPath(encoded.getBytes(StandardCharsets.UTF_8), path.getBytes(StandardCharsets.UTF_8)),
30                 StandardCharsets.UTF_8);
31     }
32 
33     /**
34      * Returns the URI path encoding of a string. This is the modified version of
35      * rfc3986 used by sigv4 signing.
36      *
37      * @param path The path to be encoded
38      *
39      * @return encoded path
40      */
encodeUriPath(String path)41     public static String encodeUriPath(String path) {
42         return appendEncodingUriPath("", path);
43     }
44 
45     /**
46      * Returns a concatenation of an encoded base, and the URI query param encoding
47      * (passthrough alnum + '-' '_' '~' '.') of a UTF-8 string. For example, reading
48      * "a b_c" would write "a%20b_c".
49      *
50      * @param encoded The encoded original param.
51      * @param param   The param to be encoded and appended to the original param
52      *
53      * @return concatenation
54      */
appendEncodingUriParam(String encoded, String param)55     public static String appendEncodingUriParam(String encoded, String param) {
56         return new String(appendEncodingUriParam(encoded.getBytes(StandardCharsets.UTF_8),
57                 param.getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8);
58     }
59 
60     /**
61      * Returns the URI query param encoding (passthrough alnum + '-' '_' '~' '.') of
62      * a UTF-8 string. For example, reading "a b_c" would write "a%20b_c".
63      *
64      * @param param The param to be encoded and appended to the original param
65      *
66      * @return encoded param
67      */
encodeUriParam(String param)68     public static String encodeUriParam(String param) {
69         return appendEncodingUriParam("", param);
70     }
71 
72     /**
73      * Returns a concatenation of a decoded base, and the URI decoding of a UTF-8
74      * string, replacing %xx escapes by their single byte equivalent. For example,
75      * reading "a%20b_c" would write "a b_c".
76      *
77      * @param base    The decoded base URI.
78      * @param encoded The encoded URI to be decoded and appended to the base URI.
79      *
80      * @return concatenation
81      */
appendDecodingUri(String base, String encoded)82     public static String appendDecodingUri(String base, String encoded) {
83         return new String(
84                 appendDecodingUri(base.getBytes(StandardCharsets.UTF_8), encoded.getBytes(StandardCharsets.UTF_8)),
85                 StandardCharsets.UTF_8);
86     }
87 
88     /**
89      * Returns the URI decoding of a UTF-8 string, replacing %xx escapes by their
90      * single byte equivalent. For example, reading "a%20b_c" would write "a b_c".
91      *
92      * @param encoded The encoded URI to be decoded.
93      *
94      * @return decoded URI
95      */
decodeUri(String encoded)96     public static String decodeUri(String encoded) {
97         return appendDecodingUri("", encoded);
98     }
99 
appendEncodingUriPath(byte[] encoded, byte[] path)100     private static native byte[] appendEncodingUriPath(byte[] encoded, byte[] path);
101 
appendEncodingUriParam(byte[] encoded, byte[] param)102     private static native byte[] appendEncodingUriParam(byte[] encoded, byte[] param);
103 
appendDecodingUri(byte[] base, byte[] encoded)104     private static native byte[] appendDecodingUri(byte[] base, byte[] encoded);
105 }
106