Debian Codegraph @

crt/aws-c-s3/include/aws/s3/private/s3_util.h

raw symbols SCIP
1 #ifndef AWS_S3_UTIL_H
2 #define AWS_S3_UTIL_H
3
4 /**
5 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
6 * SPDX-License-Identifier: Apache-2.0.
7 */
8
9 /* This file provides access to useful constants and simple utility functions. */
10
11 #include <aws/auth/signing_config.h>
12 #include <aws/common/byte_buf.h>
13 #include <aws/s3/s3_client.h>
14
15 #if ASSERT_LOCK_HELD
16 # define ASSERT_SYNCED_DATA_LOCK_HELD(object) \
17 { \
18 int cached_error = aws_last_error(); \
19 AWS_ASSERT(aws_mutex_try_lock(&(object)->synced_data.lock) == AWS_OP_ERR); \
20 aws_raise_error(cached_error); \
21 }
22 #else
23 # define ASSERT_SYNCED_DATA_LOCK_HELD(object)
24 #endif
25 #define KB_TO_BYTES(kb) ((kb) * 1024)
26 #define MB_TO_BYTES(mb) ((mb) * 1024 * 1024)
27 #define GB_TO_BYTES(gb) ((uint64_t)(gb) * 1024 * 1024 * 1024ULL)
28 #define TB_TO_BYTES(tb) ((uint64_t)(tb) * 1024 * 1024 * 1024 * 1024ULL)
29
30 #define MS_TO_NS(ms) ((uint64_t)(ms) * 1000000)
31 #define SEC_TO_NS(ms) ((uint64_t)(ms) * 1000000000)
32
33 struct aws_allocator;
34 struct aws_http_stream;
35 struct aws_http_headers;
36 struct aws_http_message;
37 struct aws_s3_client;
38 struct aws_s3_request;
39 struct aws_s3_meta_request;
40 struct aws_s3_checksum;
41
42 struct aws_cached_signing_config_aws {
43 struct aws_allocator *allocator;
44 struct aws_string *service;
45 struct aws_string *region;
46 struct aws_string *signed_body_value;
47
48 struct aws_signing_config_aws config;
49 };
50
51 AWS_EXTERN_C_BEGIN
52
53 AWS_S3_API
54 extern const struct aws_byte_cursor g_content_md5_header_name;
55
56 AWS_S3_API
57 extern const struct aws_byte_cursor g_trailer_header_name;
58
59 AWS_S3_API
60 extern const struct aws_byte_cursor g_request_validation_mode;
61
62 AWS_S3_API
63 extern const struct aws_byte_cursor g_enabled;
64
65 /**
66 * The checksum-algorithm header name used for CopyObject and CreateMultipartUpload
67 */
68 AWS_S3_API
69 extern const struct aws_byte_cursor g_checksum_algorithm_header_name;
70
71 /**
72 * The checksum-algorithm header name used for PutObject, UploadParts and PutObject*
73 */
74 AWS_S3_API
75 extern const struct aws_byte_cursor g_sdk_checksum_algorithm_header_name;
76
77 AWS_S3_API
78 extern const struct aws_byte_cursor g_s3_client_version;
79
80 AWS_S3_API
81 extern const struct aws_byte_cursor g_user_agent_header_name;
82
83 AWS_S3_API
84 extern const struct aws_byte_cursor g_user_agent_header_product_name;
85
86 AWS_S3_API
87 extern const struct aws_byte_cursor g_user_agent_header_platform;
88
89 AWS_S3_API
90 extern const struct aws_byte_cursor g_user_agent_header_unknown;
91
92 AWS_S3_API
93 extern const struct aws_byte_cursor g_acl_header_name;
94
95 AWS_S3_API
96 extern const struct aws_byte_cursor g_host_header_name;
97
98 AWS_S3_API
99 extern const struct aws_byte_cursor g_content_type_header_name;
100
101 AWS_S3_API
102 extern const struct aws_byte_cursor g_content_encoding_header_name;
103
104 AWS_S3_API
105 extern const struct aws_byte_cursor g_content_encoding_header_aws_chunked;
106
107 AWS_S3_API
108 extern const struct aws_byte_cursor g_content_length_header_name;
109
110 AWS_S3_API
111 extern const struct aws_byte_cursor g_decoded_content_length_header_name;
112
113 AWS_S3_API
114 extern const struct aws_byte_cursor g_etag_header_name;
115
116 AWS_S3_API
117 extern const size_t g_s3_min_upload_part_size;
118
119 AWS_S3_API
120 extern const struct aws_byte_cursor g_s3_service_name;
121
122 AWS_S3_API
123 extern const struct aws_byte_cursor g_s3express_service_name;
124
125 AWS_S3_API
126 extern const struct aws_byte_cursor g_range_header_name;
127
128 extern const struct aws_byte_cursor g_if_match_header_name;
129
130 extern const struct aws_byte_cursor g_request_id_header_name;
131 extern const struct aws_byte_cursor g_amz_id_2_header_name;
132
133 AWS_S3_API
134 extern const struct aws_byte_cursor g_content_range_header_name;
135
136 AWS_S3_API
137 extern const struct aws_byte_cursor g_accept_ranges_header_name;
138
139 AWS_S3_API
140 extern const struct aws_byte_cursor g_mp_parts_count_header_name;
141
142 AWS_S3_API
143 extern const struct aws_byte_cursor g_post_method;
144
145 AWS_S3_API
146 extern const struct aws_byte_cursor g_head_method;
147
148 AWS_S3_API
149 extern const struct aws_byte_cursor g_delete_method;
150
151 AWS_S3_API
152 extern const uint32_t g_s3_max_num_upload_parts;
153
154 AWS_S3_API
155 /* TODO: now this is hard-coded as 8MB, but maybe something else is better. */
156 extern const size_t g_streaming_buffer_size;
157
158 AWS_S3_API
159 extern const double g_default_throughput_target_gbps;
160
161 AWS_S3_API
162 extern const uint64_t g_streaming_object_size_threshold;
163
164 AWS_S3_API
165 extern const uint64_t g_default_part_size_fallback;
166
167 AWS_S3_API
168 extern const uint64_t g_default_max_part_size;
169
170 AWS_S3_API
171 extern const uint64_t g_s3_optimal_range_size_alignment;
172
173 AWS_S3_API
174 extern const uint32_t g_s3express_connection_limitation;
175 AWS_S3_API
176 extern const uint64_t g_s3express_connection_limitation_part_size_threshold;
177 AWS_S3_API
178 extern const uint64_t g_s3express_connection_limitation_object_size_threshold;
179
180 /**
181 * Returns AWS_S3_REQUEST_TYPE_UNKNOWN if name doesn't map to an enum value.
182 */
183 AWS_S3_API
184 enum aws_s3_request_type aws_s3_request_type_from_operation_name(struct aws_byte_cursor name);
185
186 /**
187 * Return operation name for aws_s3_request_type as static-lifetime aws_string*
188 * or NULL if the type doesn't map to an actual operation.
189 * For example:
190 * AWS_S3_REQUEST_TYPE_HEAD_OBJECT -> static aws_string "HeadObject"
191 * AWS_S3_REQUEST_TYPE_UNKNOWN -> NULL
192 * AWS_S3_REQUEST_TYPE_MAX -> NULL
193 */
194 AWS_S3_API
195 struct aws_string *aws_s3_request_type_to_operation_name_static_string(enum aws_s3_request_type type);
196
197 /**
198 * Cache and initial the signing config based on the client.
199 *
200 * @param client
201 * @param signing_config
202 * @return struct aws_cached_signing_config_aws*
203 */
204 struct aws_cached_signing_config_aws *aws_cached_signing_config_new(
205 struct aws_s3_client *client,
206 const struct aws_signing_config_aws *signing_config);
207
208 void aws_cached_signing_config_destroy(struct aws_cached_signing_config_aws *cached_signing_config);
209
210 /* Sets all headers specified for src on dest */
211 AWS_S3_API
212 void copy_http_headers(const struct aws_http_headers *src, struct aws_http_headers *dest);
213
214 /**
215 * Get content of XML element at path.
216 *
217 * path_name_array must be a C array of char*, with a NULL as its final entry.
218 *
219 * For example:
220 * Given `xml_doc`: "<Error><Code>SlowDown</Code></Error>"
221 * And `path_name_array`: {"Error", "Code", NULL}
222 * `out_body` will get set to: "SlowDown"
223 *
224 * Returns AWS_OP_SUCCESS or AWS_OP_ERR.
225 * Raises AWS_ERROR_STRING_MATCH_NOT_FOUND if path not found in XML,
226 * or AWS_ERROR_INVALID_XML if the XML can't be parsed.
227 *
228 * DO NOT make this function public without a lot of thought.
229 * The whole thing of passing a C-array of C-strings with a NULL sentinel
230 * is unconventional for this codebase.
231 */
232 AWS_S3_API
233 int aws_xml_get_body_at_path(
234 struct aws_allocator *allocator,
235 struct aws_byte_cursor xml_doc,
236 const char *path_name_array[],
237 struct aws_byte_cursor *out_body);
238
239 /* strip quotes if string is enclosed in quotes. does not remove quotes if they only appear on either side of the string
240 */
241 AWS_S3_API
242 struct aws_string *aws_strip_quotes(struct aws_allocator *allocator, struct aws_byte_cursor in_cur);
243
244 /* TODO could be moved to aws-c-common. */
245 AWS_S3_API
246 int aws_last_error_or_unknown(void);
247
248 AWS_S3_API
249 void aws_s3_add_user_agent_header(struct aws_allocator *allocator, struct aws_http_message *message);
250
251 /* Given the response headers list, finds the Content-Range header and parses the range-start, range-end and
252 * object-size. All output arguments are optional.*/
253 AWS_S3_API
254 int aws_s3_parse_content_range_response_header(
255 struct aws_http_headers *response_headers,
256 uint64_t *out_range_start,
257 uint64_t *out_range_end,
258 uint64_t *out_object_size);
259
260 /* Given a Content-Range header value as a byte cursor, parses the range-start, range-end and
261 * object-size. All output arguments are optional. */
262 AWS_S3_API
263 int aws_s3_parse_content_range_cursor(
264 struct aws_byte_cursor content_range_cursor,
265 uint64_t *out_range_start,
266 uint64_t *out_range_end,
267 uint64_t *out_object_size);
268
269 /* Given response headers, parses the content-length from a content-length response header.*/
270 AWS_S3_API
271 int aws_s3_parse_content_length_response_header(
272 struct aws_allocator *allocator,
273 struct aws_http_headers *response_headers,
274 uint64_t *out_content_length);
275
276 /*
277 * Given the request headers list, finds the Range header and parses the range-start and range-end. All arguments are
278 * required.
279 * */
280 AWS_S3_API
281 int aws_s3_parse_request_range_header(
282 struct aws_http_headers *request_headers,
283 bool *out_has_start_range,
284 bool *out_has_end_range,
285 uint64_t *out_start_range,
286 uint64_t *out_end_range);
287
288 /* Calculate the number of parts based on overall object-range and part_size. */
289 AWS_S3_API
290 uint32_t aws_s3_calculate_auto_ranged_get_num_parts(
291 size_t part_size,
292 uint64_t first_part_size,
293 uint64_t object_range_start,
294 uint64_t object_range_end);
295
296 /**
297 * Calculates the optimal part size and num parts given the 'content_length' and 'client_part_size'.
298 * This will increase the part size to stay within S3's number of parts.
299 * If the required part size exceeds the 'client_max_part_size' or
300 * if the system cannot support the required part size, it will raise an 'AWS_ERROR_INVALID_ARGUMENT' argument.
301 */
302 AWS_S3_API
303 int aws_s3_calculate_optimal_mpu_part_size_and_num_parts(
304 uint64_t content_length,
305 size_t client_part_size,
306 uint64_t client_max_part_size,
307 size_t *out_part_size,
308 uint32_t *out_num_parts);
309
310 /* Calculates the part range for a part given overall object range, size of each part, and the part's number. Note: part
311 * numbers begin at one. Intended to be used in conjunction
312 * with aws_s3_calculate_auto_ranged_get_num_parts. part_number should be less than or equal to the result of
313 * aws_s3_calculate_auto_ranged_get_num_parts. */
314 AWS_S3_API
315 void aws_s3_calculate_auto_ranged_get_part_range(
316 uint64_t object_range_start,
317 uint64_t object_range_end,
318 size_t part_size,
319 uint64_t first_part_size,
320 uint32_t part_number,
321 uint64_t *out_part_range_start,
322 uint64_t *out_part_range_end);
323
324 /* Match the retry-able S3 error code to CRT error code, return AWS_ERROR_UNKNOWN when not matched. */
325 AWS_S3_API
326 int aws_s3_crt_error_code_from_recoverable_server_error_code_string(struct aws_byte_cursor error_code_string);
327
328 AWS_S3_API
329 void aws_s3_request_finish_up_metrics_synced(struct aws_s3_request *request, struct aws_s3_meta_request *meta_request);
330
331 /* Check the response headers for checksum to verify, return a running checksum based on the algorithm found. If no
332 * checksum found from header, return null. */
333 AWS_S3_API
334 int aws_s3_check_headers_for_checksum(
335 struct aws_s3_meta_request *meta_request,
336 const struct aws_http_headers *headers,
337 struct aws_s3_checksum **out_checksum,
338 struct aws_byte_buf *out_checksum_buffer,
339 bool meta_request_level);
340
341 /**
342 * Calculate client-level optimal range size based on memory and connection constraints.
343 * This function is called during client initialization to determine the base range size
344 * using the formula: MemoryLimit / concurrency / divisor.
345 * The result is rounded up to ensure proper alignment and applies minimum size constraints.
346 *
347 * @param memory_limit_in_bytes Total memory limit available for buffering
348 * @param max_connections Maximum number of concurrent connections
349 * @param out_client_optimal_range_size Output parameter for calculated client-level optimal range size
350 * @return AWS_OP_SUCCESS on success, AWS_OP_ERR on failure (caller should fall back to default)
351 */
352 AWS_S3_API
353 int aws_s3_calculate_client_optimal_range_size(
354 uint64_t memory_limit_in_bytes,
355 uint32_t max_connections,
356 uint64_t *out_client_optimal_range_size);
357
358 /**
359 * Calculate request-level optimal range size by considering object-specific information.
360 * This function is called per request to adjust the client-level range size based on
361 * estimated object stored part size using: min(client_optimal_range_size, estimated_object_stored_part_size).
362 *
363 * @param client_optimal_range_size The client-level optimal range size from initialization
364 * @param estimated_object_stored_part_size Estimated size of object stored parts in S3
365 * @param is_express If the request is a s3express request or not.
366 * @param out_request_optimal_range_size Output parameter for calculated request-level optimal range size
367 * @return AWS_OP_SUCCESS on success, AWS_OP_ERR on failure (caller should fall back to client size)
368 */
369 AWS_S3_API
370 int aws_s3_calculate_request_optimal_range_size(
371 uint64_t client_optimal_range_size,
372 uint64_t estimated_object_stored_part_size,
373 bool is_express,
374 uint64_t *out_request_optimal_range_size);
375
376 /**
377 * Extract the number of parts from an S3 ETag header value.
378 * S3 multipart upload ETags have the format "<hash>-<number_of_parts>".
379 * Single-part uploads have ETags without dashes.
380 *
381 * @param etag_header_value The ETag header value (may include quotes)
382 * @param out_num_parts Output parameter for the number of parts (1 for single-part uploads)
383 * @return AWS_OP_SUCCESS on success, AWS_OP_ERR on failure (invalid ETag format)
384 */
385 AWS_S3_API
386 int aws_s3_extract_parts_from_etag(struct aws_byte_cursor etag_header_value, uint32_t *out_num_parts);
387
388 /**
389 * Helper to figure out if given header name is one of the checksum value headers.
390 * ex. returns true for x-amz-checksum-crc32 or x-amz-checksum-sha256, but false for x-amz-checksum-type.
391 * Note: relies on hardcoded list of non-checksum headers, which needs to be updated if s3 expands list of those.
392 */
393 AWS_S3_API
394 bool aws_s3_is_checksum_value_header_name(struct aws_byte_cursor header_name);
395
396 AWS_EXTERN_C_END
397
398 #endif /* AWS_S3_UTIL_H */