Debian Codegraph @

crt/aws-c-s3/source/s3.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5
6 #include <aws/s3/private/s3_platform_info.h>
7 #include <aws/s3/private/s3_util.h>
8 #include <aws/s3/s3.h>
9 #include <aws/s3/s3_client.h>
10
11 #include <aws/auth/auth.h>
12 #include <aws/checksums/checksums.h>
13 #include <aws/common/error.h>
14 #include <aws/common/hash_table.h>
15 #include <aws/http/http.h>
16
17 #define AWS_DEFINE_ERROR_INFO_S3(CODE, STR) AWS_DEFINE_ERROR_INFO(CODE, STR, "aws-c-s3")
18
19 /* clang-format off */
20 static struct aws_error_info s_errors[] = {
21 AWS_DEFINE_ERROR_INFO_S3(AWS_ERROR_S3_MISSING_CONTENT_RANGE_HEADER, "Response missing required Content-Range header."),
22 AWS_DEFINE_ERROR_INFO_S3(AWS_ERROR_S3_INVALID_CONTENT_RANGE_HEADER, "Response contains invalid Content-Range header."),
23 AWS_DEFINE_ERROR_INFO_S3(AWS_ERROR_S3_MISSING_CONTENT_LENGTH_HEADER, "Response missing required Content-Length header."),
24 AWS_DEFINE_ERROR_INFO_S3(AWS_ERROR_S3_INVALID_CONTENT_LENGTH_HEADER, "Response contains invalid Content-Length header."),
25 AWS_DEFINE_ERROR_INFO_S3(AWS_ERROR_S3_MISSING_ETAG, "Response missing required ETag header."),
26 AWS_DEFINE_ERROR_INFO_S3(AWS_ERROR_S3_INTERNAL_ERROR, "Response code indicates internal server error"),
27 AWS_DEFINE_ERROR_INFO_S3(AWS_ERROR_S3_SLOW_DOWN, "Response code indicates throttling"),
28 AWS_DEFINE_ERROR_INFO_S3(AWS_ERROR_S3_INVALID_RESPONSE_STATUS, "Invalid response status from request"),
29 AWS_DEFINE_ERROR_INFO_S3(AWS_ERROR_S3_MISSING_UPLOAD_ID, "Upload Id not found in create-multipart-upload response"),
30 AWS_DEFINE_ERROR_INFO_S3(AWS_ERROR_S3_PROXY_PARSE_FAILED, "Could not parse proxy URI"),
31 AWS_DEFINE_ERROR_INFO_S3(AWS_ERROR_S3_UNSUPPORTED_PROXY_SCHEME, "Given Proxy URI has an unsupported scheme"),
32 AWS_DEFINE_ERROR_INFO_S3(AWS_ERROR_S3_CANCELED, "Request successfully cancelled"),
33 AWS_DEFINE_ERROR_INFO_S3(AWS_ERROR_S3_INVALID_RANGE_HEADER, "Range header has invalid syntax"),
34 AWS_DEFINE_ERROR_INFO_S3(AWS_ERROR_S3_MULTIRANGE_HEADER_UNSUPPORTED, "Range header specifies multiple ranges which is unsupported"),
35 AWS_DEFINE_ERROR_INFO_S3(AWS_ERROR_S3_RESPONSE_CHECKSUM_MISMATCH, "response checksum header does not match calculated checksum"),
36 AWS_DEFINE_ERROR_INFO_S3(AWS_ERROR_S3_CHECKSUM_CALCULATION_FAILED, "failed to calculate a checksum for the provided stream"),
37 AWS_DEFINE_ERROR_INFO_S3(AWS_ERROR_S3_PAUSED, "Request successfully paused"),
38 AWS_DEFINE_ERROR_INFO_S3(AWS_ERROR_S3_LIST_PARTS_PARSE_FAILED, "Failed to parse response from ListParts"),
39 AWS_DEFINE_ERROR_INFO_S3(AWS_ERROR_S3_RESUMED_PART_CHECKSUM_MISMATCH, "Checksum does not match previously uploaded part"),
40 AWS_DEFINE_ERROR_INFO_S3(AWS_ERROR_S3_RESUME_FAILED, "Resuming request failed"),
41 AWS_DEFINE_ERROR_INFO_S3(AWS_ERROR_S3_OBJECT_MODIFIED, "The object modifed during download."),
42 AWS_DEFINE_ERROR_INFO_S3(AWS_ERROR_S3_NON_RECOVERABLE_ASYNC_ERROR, "Async error received from S3 and not recoverable from retry."),
43 AWS_DEFINE_ERROR_INFO_S3(AWS_ERROR_S3_METRIC_DATA_NOT_AVAILABLE, "The metric data is not available, the requests ends before the metric happens."),
44 AWS_DEFINE_ERROR_INFO_S3(AWS_ERROR_S3_INCORRECT_CONTENT_LENGTH, "Request body length must match Content-Length header."),
45 AWS_DEFINE_ERROR_INFO_S3(AWS_ERROR_S3_REQUEST_TIME_TOO_SKEWED, "RequestTimeTooSkewed error received from S3."),
46 AWS_DEFINE_ERROR_INFO_S3(AWS_ERROR_S3_FILE_MODIFIED, "The file was modified during upload."),
47 AWS_DEFINE_ERROR_INFO_S3(AWS_ERROR_S3_EXCEEDS_MEMORY_LIMIT, "Request was not created due to used memory exceeding memory limit."),
48 AWS_DEFINE_ERROR_INFO_S3(AWS_ERROR_S3_INVALID_MEMORY_LIMIT_CONFIG, "Specified memory configuration is invalid for the system. "
49 "Memory limit should be at least 1GiB. Part size and max part size should be smaller than memory limit."),
50 AWS_DEFINE_ERROR_INFO_S3(AWS_ERROR_S3EXPRESS_CREATE_SESSION_FAILED, "CreateSession call failed when signing with S3 Express."),
51 AWS_DEFINE_ERROR_INFO_S3(AWS_ERROR_S3_INTERNAL_BUFFER_SIZE_MISMATCH_RETRYING_WITH_RANGE, "buffer size mismatch, content length exceeds reserved buffer size, possibly due to wrong object_size_hint. Retrying with Range instead of partNumber."),
52 AWS_DEFINE_ERROR_INFO_S3(AWS_ERROR_S3_REQUEST_HAS_COMPLETED, "Request has already completed, action cannot be performed."),
53 AWS_DEFINE_ERROR_INFO_S3(AWS_ERROR_S3_RECV_FILE_ALREADY_EXISTS, "File already exists, cannot create as new."),
54 AWS_DEFINE_ERROR_INFO_S3(AWS_ERROR_S3_RECV_FILE_NOT_FOUND, "The receive file doesn't exist, cannot create as configuration required."),
55 AWS_DEFINE_ERROR_INFO_S3(AWS_ERROR_S3_REQUEST_TIMEOUT, "RequestTimeout error received from S3."),
56 AWS_DEFINE_ERROR_INFO_S3(AWS_ERROR_S3_BUFFER_ALLOCATION_FAILED, "Could not reserve memory for object."),
57 };
58 /* clang-format on */
59
60 static struct aws_error_info_list s_error_list = {
61 .error_list = s_errors,
62 .count = AWS_ARRAY_SIZE(s_errors),
63 };
64
65 static struct aws_log_subject_info s_s3_log_subject_infos[] = {
66 DEFINE_LOG_SUBJECT_INFO(AWS_LS_S3_GENERAL, "S3General", "Subject for aws-c-s3 logging that defies categorization."),
67 DEFINE_LOG_SUBJECT_INFO(AWS_LS_S3_CLIENT, "S3Client", "Subject for aws-c-s3 logging from an aws_s3_client."),
68 DEFINE_LOG_SUBJECT_INFO(
69 AWS_LS_S3_CLIENT_STATS,
70 "S3ClientStats",
71 "Subject for aws-c-s3 logging for stats tracked by an aws_s3_client."),
72 DEFINE_LOG_SUBJECT_INFO(AWS_LS_S3_REQUEST, "S3Request", "Subject for aws-c-s3 logging from an aws_s3_request."),
73 DEFINE_LOG_SUBJECT_INFO(
74 AWS_LS_S3_META_REQUEST,
75 "S3MetaRequest",
76 "Subject for aws-c-s3 logging from an aws_s3_meta_request."),
77 DEFINE_LOG_SUBJECT_INFO(AWS_LS_S3_ENDPOINT, "S3Endpoint", "Subject for aws-c-s3 logging from an aws_s3_endpoint."),
78 };
79
80 static struct aws_log_subject_info_list s_s3_log_subject_list = {
81 .subject_list = s_s3_log_subject_infos,
82 .count = AWS_ARRAY_SIZE(s_s3_log_subject_infos),
83 };
84
85 struct aws_s3_request_type_info {
86 enum aws_s3_request_type type;
87 struct aws_string *name_string;
88 struct aws_byte_cursor name_cursor;
89 };
90
91 static struct aws_s3_request_type_info s_s3_request_type_info_array[AWS_S3_REQUEST_TYPE_MAX];
92
93 /* Hash-table for case-insensitive lookup, from operation-name -> request-type.
94 * key is operation-name (stored as `struct aws_byte_cursor*`, pointing into array above).
95 * value is request-type (stored as `enum aws_s3_request_type *`, pointing into array above). */
96 static struct aws_hash_table s_s3_operation_name_to_request_type_table;
97
98 static void s_s3_request_type_register(enum aws_s3_request_type type, const struct aws_string *name) {
99
100 AWS_PRECONDITION(type >= 0 && type < AWS_ARRAY_SIZE(s_s3_request_type_info_array));
101 AWS_PRECONDITION(AWS_IS_ZEROED(s_s3_request_type_info_array[type]));
102
103 struct aws_s3_request_type_info *info = &s_s3_request_type_info_array[type];
104 info->type = type;
105 info->name_string = (struct aws_string *)name;
106 info->name_cursor = aws_byte_cursor_from_string(name);
107
108 int err = aws_hash_table_put(&s_s3_operation_name_to_request_type_table, &info->name_cursor, &info->type, NULL);
109 AWS_FATAL_ASSERT(!err);
110 }
111
112 AWS_STATIC_STRING_FROM_LITERAL(s_HeadObject_str, "HeadObject");
113 AWS_STATIC_STRING_FROM_LITERAL(s_GetObject_str, "GetObject");
114 AWS_STATIC_STRING_FROM_LITERAL(s_ListParts_str, "ListParts");
115 AWS_STATIC_STRING_FROM_LITERAL(s_CreateMultipartUpload_str, "CreateMultipartUpload");
116 AWS_STATIC_STRING_FROM_LITERAL(s_UploadPart_str, "UploadPart");
117 AWS_STATIC_STRING_FROM_LITERAL(s_AbortMultipartUpload_str, "AbortMultipartUpload");
118 AWS_STATIC_STRING_FROM_LITERAL(s_CompleteMultipartUpload_str, "CompleteMultipartUpload");
119 AWS_STATIC_STRING_FROM_LITERAL(s_UploadPartCopy_str, "UploadPartCopy");
120 AWS_STATIC_STRING_FROM_LITERAL(s_CopyObject_str, "CopyObject");
121 AWS_STATIC_STRING_FROM_LITERAL(s_PutObject_str, "PutObject");
122 AWS_STATIC_STRING_FROM_LITERAL(s_CreateSession_str, "CreateSession");
123
124 static void s_s3_request_type_info_init(struct aws_allocator *allocator) {
125 int err = aws_hash_table_init(
126 &s_s3_operation_name_to_request_type_table,
127 allocator,
128 AWS_ARRAY_SIZE(s_s3_request_type_info_array) /*initial_size*/,
129 aws_hash_byte_cursor_ptr_ignore_case,
130 (aws_hash_callback_eq_fn *)aws_byte_cursor_eq_ignore_case,
131 NULL /*destroy_key*/,
132 NULL /*destroy_value*/);
133 AWS_FATAL_ASSERT(!err);
134
135 s_s3_request_type_register(AWS_S3_REQUEST_TYPE_HEAD_OBJECT, s_HeadObject_str);
136 s_s3_request_type_register(AWS_S3_REQUEST_TYPE_GET_OBJECT, s_GetObject_str);
137 s_s3_request_type_register(AWS_S3_REQUEST_TYPE_LIST_PARTS, s_ListParts_str);
138 s_s3_request_type_register(AWS_S3_REQUEST_TYPE_CREATE_MULTIPART_UPLOAD, s_CreateMultipartUpload_str);
139 s_s3_request_type_register(AWS_S3_REQUEST_TYPE_UPLOAD_PART, s_UploadPart_str);
140 s_s3_request_type_register(AWS_S3_REQUEST_TYPE_ABORT_MULTIPART_UPLOAD, s_AbortMultipartUpload_str);
141 s_s3_request_type_register(AWS_S3_REQUEST_TYPE_COMPLETE_MULTIPART_UPLOAD, s_CompleteMultipartUpload_str);
142 s_s3_request_type_register(AWS_S3_REQUEST_TYPE_UPLOAD_PART_COPY, s_UploadPartCopy_str);
143 s_s3_request_type_register(AWS_S3_REQUEST_TYPE_COPY_OBJECT, s_CopyObject_str);
144 s_s3_request_type_register(AWS_S3_REQUEST_TYPE_PUT_OBJECT, s_PutObject_str);
145 s_s3_request_type_register(AWS_S3_REQUEST_TYPE_CREATE_SESSION, s_CreateSession_str);
146 }
147
148 static void s_s3_request_type_info_clean_up(void) {
149 aws_hash_table_clean_up(&s_s3_operation_name_to_request_type_table);
150 AWS_ZERO_ARRAY(s_s3_request_type_info_array);
151 }
152
153 struct aws_string *aws_s3_request_type_to_operation_name_static_string(enum aws_s3_request_type type) {
154
155 if (type >= 0 && type < AWS_ARRAY_SIZE(s_s3_request_type_info_array)) {
156 struct aws_s3_request_type_info *info = &s_s3_request_type_info_array[type];
157 return info->name_string;
158 }
159
160 return NULL;
161 }
162
163 const char *aws_s3_request_type_operation_name(enum aws_s3_request_type type) {
164 struct aws_string *name_string = aws_s3_request_type_to_operation_name_static_string(type);
165 if (name_string != NULL) {
166 return aws_string_c_str(name_string);
167 }
168
169 return "";
170 }
171
172 enum aws_s3_request_type aws_s3_request_type_from_operation_name(struct aws_byte_cursor name) {
173 struct aws_hash_element *elem = NULL;
174 aws_hash_table_find(&s_s3_operation_name_to_request_type_table, &name, &elem);
175 if (elem != NULL) {
176 enum aws_s3_request_type *type = elem->value;
177 return *type;
178 }
179 return AWS_S3_REQUEST_TYPE_UNKNOWN;
180 }
181
182 static bool s_library_initialized = false;
183 static struct aws_allocator *s_library_allocator = NULL;
184 static struct aws_s3_platform_info_loader *s_loader;
185
186 void aws_s3_library_init(struct aws_allocator *allocator) {
187 if (s_library_initialized) {
188 return;
189 }
190
191 if (allocator) {
192 s_library_allocator = allocator;
193 } else {
194 s_library_allocator = aws_default_allocator();
195 }
196
197 aws_auth_library_init(s_library_allocator);
198 aws_http_library_init(s_library_allocator);
199 aws_checksums_library_init(s_library_allocator);
200
201 aws_register_error_info(&s_error_list);
202 aws_register_log_subject_info_list(&s_s3_log_subject_list);
203 s_loader = aws_s3_platform_info_loader_new(allocator);
204 AWS_FATAL_ASSERT(s_loader);
205 s_s3_request_type_info_init(allocator);
206
207 s_library_initialized = true;
208 }
209
210 const struct aws_s3_platform_info *aws_s3_get_current_platform_info(void) {
211 return aws_s3_get_platform_info_for_current_environment(s_loader);
212 }
213
214 struct aws_byte_cursor aws_s3_get_current_platform_ec2_intance_type(bool cached_only) {
215 return aws_s3_get_ec2_instance_type(s_loader, cached_only);
216 }
217
218 struct aws_array_list aws_s3_get_platforms_with_recommended_config(void) {
219 return aws_s3_get_recommended_platforms(s_loader);
220 }
221
222 void aws_s3_library_clean_up(void) {
223 if (!s_library_initialized) {
224 return;
225 }
226
227 s_library_initialized = false;
228 aws_thread_join_all_managed();
229
230 s_s3_request_type_info_clean_up();
231 s_loader = aws_s3_platform_info_loader_release(s_loader);
232 aws_unregister_log_subject_info_list(&s_s3_log_subject_list);
233 aws_unregister_error_info(&s_error_list);
234 aws_http_library_clean_up();
235 aws_auth_library_clean_up();
236 aws_checksums_library_clean_up();
237 s_library_allocator = NULL;
238 }