Debian Codegraph @

crt/aws-c-common/include/aws/common/uri.h

raw symbols SCIP
1 #ifndef AWS_COMMON_URI_H
2 #define AWS_COMMON_URI_H
3 /**
4 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * SPDX-License-Identifier: Apache-2.0.
6 */
7
8 #include <aws/common/byte_buf.h>
9
10 AWS_PUSH_SANE_WARNING_LEVEL
11
12 /**
13 * Data representing a URI. uri_str is always allocated and filled in.
14 * The other portions are merely storing offsets into uri_str.
15 */
16 struct aws_uri {
17 size_t self_size;
18 struct aws_allocator *allocator;
19 struct aws_byte_buf uri_str;
20 struct aws_byte_cursor scheme;
21 struct aws_byte_cursor authority;
22 struct aws_byte_cursor userinfo;
23 struct aws_byte_cursor user;
24 struct aws_byte_cursor password;
25 struct aws_byte_cursor host_name;
26 uint32_t port;
27 struct aws_byte_cursor path;
28 struct aws_byte_cursor query_string;
29 struct aws_byte_cursor path_and_query;
30 };
31
32 /**
33 * key/value pairs for a query string. If the query fragment was not in format key=value, the fragment value
34 * will be stored in key
35 */
36 struct aws_uri_param {
37 struct aws_byte_cursor key;
38 struct aws_byte_cursor value;
39 };
40
41 /**
42 * Arguments for building a URI instance. All members must
43 * be initialized before passing them to aws_uri_init().
44 *
45 * query_string and query_params are exclusive to each other. If you set
46 * query_string, do not prepend it with '?'
47 */
48 struct aws_uri_builder_options {
49 struct aws_byte_cursor scheme;
50 struct aws_byte_cursor path;
51 struct aws_byte_cursor host_name;
52 uint32_t port;
53 struct aws_array_list *query_params;
54 struct aws_byte_cursor query_string;
55 };
56
57 AWS_EXTERN_C_BEGIN
58
59 /**
60 * Parses 'uri_str' and initializes uri. Returns AWS_OP_SUCCESS, on success, AWS_OP_ERR on failure.
61 * After calling this function, the parts can be accessed.
62 */
63 AWS_COMMON_API int aws_uri_init_parse(
64 struct aws_uri *uri,
65 struct aws_allocator *allocator,
66 const struct aws_byte_cursor *uri_str);
67
68 /**
69 * Initializes uri to values specified in options. Returns AWS_OP_SUCCESS, on success, AWS_OP_ERR on failure.
70 * After calling this function, the parts can be accessed.
71 */
72 AWS_COMMON_API int aws_uri_init_from_builder_options(
73 struct aws_uri *uri,
74 struct aws_allocator *allocator,
75 struct aws_uri_builder_options *options);
76 AWS_COMMON_API void aws_uri_clean_up(struct aws_uri *uri);
77
78 /**
79 * Returns the scheme portion of the uri (e.g. http, https, ftp, ftps, etc...). If the scheme was not present
80 * in the uri, the returned value will be empty. It is the users job to determine the appropriate defaults
81 * if this field is empty, based on protocol, port, etc...
82 */
83 AWS_COMMON_API const struct aws_byte_cursor *aws_uri_scheme(const struct aws_uri *uri);
84
85 /**
86 * Returns the authority portion of the uri (host[:port]). If it was not present, this was a request uri. In that
87 * case, the value will be empty.
88 */
89 AWS_COMMON_API const struct aws_byte_cursor *aws_uri_authority(const struct aws_uri *uri);
90
91 /**
92 * Returns the path portion of the uri, including any leading '/'. If not present, this value will be empty.
93 */
94 AWS_COMMON_API const struct aws_byte_cursor *aws_uri_path(const struct aws_uri *uri);
95
96 /**
97 * Returns the query string portion of the uri, minus the '?'. If not present, this value will be empty.
98 */
99 AWS_COMMON_API const struct aws_byte_cursor *aws_uri_query_string(const struct aws_uri *uri);
100
101 /**
102 * Returns the 'host_name' portion of the authority. If no authority was present, this value will be empty.
103 */
104 AWS_COMMON_API const struct aws_byte_cursor *aws_uri_host_name(const struct aws_uri *uri);
105
106 /**
107 * Returns the port portion of the authority if it was present, otherwise, returns 0.
108 * If this is 0, it is the users job to determine the correct port based on scheme and protocol.
109 */
110 AWS_COMMON_API uint32_t aws_uri_port(const struct aws_uri *uri);
111
112 /**
113 * Returns the path and query portion of the uri (i.e., the thing you send across the wire).
114 */
115 AWS_COMMON_API const struct aws_byte_cursor *aws_uri_path_and_query(const struct aws_uri *uri);
116
117 /**
118 * For iterating over the params in the query string.
119 * `param` is an in/out argument used to track progress, it MUST be zeroed out to start.
120 * If true is returned, `param` contains the value of the next param.
121 * If false is returned, there are no further params.
122 *
123 * Edge cases:
124 * 1) Entries without '=' sign are treated as having a key and no value.
125 * Example: First param in query string "a&b=c" has key="a" value=""
126 *
127 * 2) Blank entries are skipped.
128 * Example: The only param in query string "&&a=b" is key="a" value="b"
129 */
130 AWS_COMMON_API bool aws_query_string_next_param(struct aws_byte_cursor query_string, struct aws_uri_param *param);
131
132 /**
133 * Parses query string and stores the parameters in 'out_params'. Returns AWS_OP_SUCCESS on success and
134 * AWS_OP_ERR on failure. The user is responsible for initializing out_params with item size of struct aws_query_param.
135 * The user is also responsible for cleaning up out_params when finished.
136 */
137 AWS_COMMON_API int aws_query_string_params(struct aws_byte_cursor query_string, struct aws_array_list *out_params);
138
139 /**
140 * For iterating over the params in the uri query string.
141 * `param` is an in/out argument used to track progress, it MUST be zeroed out to start.
142 * If true is returned, `param` contains the value of the next param.
143 * If false is returned, there are no further params.
144 *
145 * Edge cases:
146 * 1) Entries without '=' sign are treated as having a key and no value.
147 * Example: First param in query string "a&b=c" has key="a" value=""
148 *
149 * 2) Blank entries are skipped.
150 * Example: The only param in query string "&&a=b" is key="a" value="b"
151 */
152 AWS_COMMON_API bool aws_uri_query_string_next_param(const struct aws_uri *uri, struct aws_uri_param *param);
153
154 /**
155 * Parses query string and stores the parameters in 'out_params'. Returns AWS_OP_SUCCESS on success and
156 * AWS_OP_ERR on failure. The user is responsible for initializing out_params with item size of struct aws_query_param.
157 * The user is also responsible for cleaning up out_params when finished.
158 */
159 AWS_COMMON_API int aws_uri_query_string_params(const struct aws_uri *uri, struct aws_array_list *out_params);
160
161 /**
162 * Writes the uri path encoding of a cursor to a buffer. This is the modified version of rfc3986 used by
163 * sigv4 signing.
164 */
165 AWS_COMMON_API int aws_byte_buf_append_encoding_uri_path(
166 struct aws_byte_buf *buffer,
167 const struct aws_byte_cursor *cursor);
168
169 /**
170 * Writes the uri query param encoding (passthrough alnum + '-' '_' '~' '.') of a UTF-8 cursor to a buffer
171 * For example, reading "a b_c" would write "a%20b_c".
172 */
173 AWS_COMMON_API int aws_byte_buf_append_encoding_uri_param(
174 struct aws_byte_buf *buffer,
175 const struct aws_byte_cursor *cursor);
176
177 /**
178 * Writes the uri decoding of a UTF-8 cursor to a buffer,
179 * replacing %xx escapes by their single byte equivalent.
180 * For example, reading "a%20b_c" would write "a b_c".
181 */
182 AWS_COMMON_API int aws_byte_buf_append_decoding_uri(struct aws_byte_buf *buffer, const struct aws_byte_cursor *cursor);
183
184 AWS_EXTERN_C_END
185 AWS_POP_SANE_WARNING_LEVEL
186
187 #endif /* AWS_COMMON_URI_H */