Debian Codegraph @

crt/aws-c-s3/source/s3_request_messages.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_request_messages.h"
7 #include "aws/s3/private/s3_checksum_context.h"
8 #include "aws/s3/private/s3_meta_request_impl.h"
9 #include "aws/s3/private/s3_util.h"
10 #include <aws/cal/hash.h>
11 #include <aws/common/byte_buf.h>
12 #include <aws/common/encoding.h>
13 #include <aws/common/string.h>
14 #include <aws/common/uri.h>
15 #include <aws/http/request_response.h>
16 #include <aws/io/async_stream.h>
17 #include <aws/io/stream.h>
18 #include <inttypes.h>
19
20 const struct aws_byte_cursor g_s3_create_multipart_upload_excluded_headers[] = {
21 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Content-Length"),
22 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Content-MD5"),
23 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-copy-source"),
24 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-copy-source-range"),
25 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("if-none-match"),
26 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-create-session-mode"),
27 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-sdk-checksum-algorithm"),
28 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-metadata-directive"),
29 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-tagging-directive"),
30 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-object-annotation-directive"),
31 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-copy-source-if-match"),
32 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-copy-source-if-none-match"),
33 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-copy-source-if-modified-since"),
34 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-copy-source-if-unmodified-since"),
35 };
36
37 const size_t g_s3_create_multipart_upload_excluded_headers_count =
38 AWS_ARRAY_SIZE(g_s3_create_multipart_upload_excluded_headers);
39
40 const struct aws_byte_cursor g_s3_upload_part_excluded_headers[] = {
41 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-acl"),
42 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Cache-Control"),
43 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Content-Disposition"),
44 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Content-Encoding"),
45 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Content-Language"),
46 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Content-Length"),
47 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Content-MD5"),
48 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Content-Type"),
49 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Expires"),
50 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-grant-full-control"),
51 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-grant-read"),
52 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-grant-read-acp"),
53 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-grant-write-acp"),
54 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-server-side-encryption"),
55 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-storage-class"),
56 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-website-redirect-location"),
57 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-server-side-encryption-aws-kms-key-id"),
58 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-server-side-encryption-context"),
59 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-server-side-encryption-bucket-key-enabled"),
60 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-tagging"),
61 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-object-lock-mode"),
62 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-object-lock-retain-until-date"),
63 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-object-lock-legal-hold"),
64 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("if-none-match"),
65 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-create-session-mode"),
66 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-metadata-directive"),
67 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-tagging-directive"),
68 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-object-annotation-directive"),
69 };
70
71 const size_t g_s3_upload_part_excluded_headers_count = AWS_ARRAY_SIZE(g_s3_upload_part_excluded_headers);
72
73 const struct aws_byte_cursor g_s3_complete_multipart_upload_excluded_headers[] = {
74 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-acl"),
75 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Cache-Control"),
76 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Content-Disposition"),
77 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Content-Encoding"),
78 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Content-Language"),
79 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Content-Length"),
80 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Content-MD5"),
81 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Content-Type"),
82 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Expires"),
83 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-grant-full-control"),
84 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-grant-read"),
85 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-grant-read-acp"),
86 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-grant-write-acp"),
87 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-server-side-encryption"),
88 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-storage-class"),
89 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-website-redirect-location"),
90 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-server-side-encryption-customer-algorithm"),
91 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-server-side-encryption-customer-key"),
92 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-server-side-encryption-customer-key-MD5"),
93 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-server-side-encryption-aws-kms-key-id"),
94 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-server-side-encryption-context"),
95 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-server-side-encryption-bucket-key-enabled"),
96 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-tagging"),
97 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-object-lock-mode"),
98 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-object-lock-retain-until-date"),
99 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-object-lock-legal-hold"),
100 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-copy-source"),
101 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-copy-source-range"),
102 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-mp-object-size"),
103 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-create-session-mode"),
104 };
105
106 const size_t g_s3_complete_multipart_upload_excluded_headers_count =
107 AWS_ARRAY_SIZE(g_s3_complete_multipart_upload_excluded_headers);
108
109 /* The server-side encryption (SSE) is needed only when the object was created using a checksum algorithm for complete
110 * multipart upload. */
111 const struct aws_byte_cursor g_s3_complete_multipart_upload_with_checksum_excluded_headers[] = {
112 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-acl"),
113 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Cache-Control"),
114 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Content-Disposition"),
115 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Content-Encoding"),
116 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Content-Language"),
117 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Content-Length"),
118 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Content-MD5"),
119 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Content-Type"),
120 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Expires"),
121 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-grant-full-control"),
122 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-grant-read"),
123 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-grant-read-acp"),
124 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-grant-write-acp"),
125 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-server-side-encryption"),
126 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-storage-class"),
127 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-website-redirect-location"),
128 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-server-side-encryption-aws-kms-key-id"),
129 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-server-side-encryption-context"),
130 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-server-side-encryption-bucket-key-enabled"),
131 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-tagging"),
132 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-object-lock-mode"),
133 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-object-lock-retain-until-date"),
134 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-object-lock-legal-hold"),
135 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-copy-source"),
136 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-copy-source-range"),
137 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-sdk-checksum-algorithm"),
138 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-mp-object-size"),
139 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-create-session-mode"),
140 };
141
142 const struct aws_byte_cursor g_s3_list_parts_excluded_headers[] = {
143 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-acl"),
144 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Cache-Control"),
145 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Content-Disposition"),
146 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Content-Encoding"),
147 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Content-Language"),
148 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Content-Length"),
149 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Content-MD5"),
150 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Content-Type"),
151 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Expires"),
152 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-grant-full-control"),
153 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-grant-read"),
154 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-grant-read-acp"),
155 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-grant-write-acp"),
156 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-server-side-encryption"),
157 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-storage-class"),
158 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-website-redirect-location"),
159 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-server-side-encryption-customer-algorithm"),
160 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-server-side-encryption-customer-key"),
161 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-server-side-encryption-customer-key-MD5"),
162 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-server-side-encryption-aws-kms-key-id"),
163 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-server-side-encryption-context"),
164 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-server-side-encryption-bucket-key-enabled"),
165 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-tagging"),
166 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-object-lock-mode"),
167 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-object-lock-retain-until-date"),
168 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-object-lock-legal-hold"),
169 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-copy-source"),
170 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-copy-source-range"),
171 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-create-session-mode"),
172 };
173
174 const size_t g_s3_list_parts_excluded_headers_count = AWS_ARRAY_SIZE(g_s3_list_parts_excluded_headers);
175
176 const struct aws_byte_cursor g_s3_list_parts_with_checksum_excluded_headers[] = {
177 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-acl"),
178 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Cache-Control"),
179 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Content-Disposition"),
180 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Content-Encoding"),
181 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Content-Language"),
182 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Content-Length"),
183 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Content-MD5"),
184 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Content-Type"),
185 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Expires"),
186 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-grant-full-control"),
187 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-grant-read"),
188 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-grant-read-acp"),
189 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-grant-write-acp"),
190 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-server-side-encryption"),
191 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-storage-class"),
192 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-website-redirect-location"),
193 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-server-side-encryption-aws-kms-key-id"),
194 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-server-side-encryption-context"),
195 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-server-side-encryption-bucket-key-enabled"),
196 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-tagging"),
197 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-object-lock-mode"),
198 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-object-lock-retain-until-date"),
199 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-object-lock-legal-hold"),
200 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-copy-source"),
201 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-copy-source-range"),
202 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-create-session-mode"),
203 };
204
205 const size_t g_s3_list_parts_with_checksum_excluded_headers_count =
206 AWS_ARRAY_SIZE(g_s3_list_parts_with_checksum_excluded_headers);
207
208 const struct aws_byte_cursor g_s3_abort_multipart_upload_excluded_headers[] = {
209 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-acl"),
210 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Cache-Control"),
211 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Content-Disposition"),
212 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Content-Encoding"),
213 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Content-Language"),
214 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Content-Length"),
215 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Content-MD5"),
216 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Content-Type"),
217 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Expires"),
218 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-grant-full-control"),
219 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-grant-read"),
220 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-grant-read-acp"),
221 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-grant-write-acp"),
222 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-server-side-encryption"),
223 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-storage-class"),
224 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-website-redirect-location"),
225 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-server-side-encryption-customer-algorithm"),
226 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-server-side-encryption-customer-key"),
227 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-server-side-encryption-customer-key-MD5"),
228 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-server-side-encryption-aws-kms-key-id"),
229 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-server-side-encryption-context"),
230 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-server-side-encryption-bucket-key-enabled"),
231 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-tagging"),
232 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-object-lock-mode"),
233 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-object-lock-retain-until-date"),
234 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-object-lock-legal-hold"),
235 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-copy-source"),
236 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-copy-source-range"),
237 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("if-none-match"),
238 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-create-session-mode"),
239 };
240
241 const struct aws_byte_cursor g_s3_create_session_allowed_headers[] = {
242 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-create-session-mode"),
243 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-server-side-encryption"),
244 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-server-side-encryption-aws-kms-key-id"),
245 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-server-side-encryption-context"),
246 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-server-side-encryption-bucket-key-enabled"),
247 };
248
249 const size_t g_s3_create_session_allowed_headers_count = AWS_ARRAY_SIZE(g_s3_create_session_allowed_headers);
250
251 static const struct aws_byte_cursor s_x_amz_meta_prefix = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-meta-");
252
253 static const struct aws_byte_cursor s_checksum_type_header =
254 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-checksum-type");
255 static const struct aws_byte_cursor s_checksum_type_full_object = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("full_object");
256
257 const size_t g_s3_abort_multipart_upload_excluded_headers_count =
258 AWS_ARRAY_SIZE(g_s3_abort_multipart_upload_excluded_headers);
259
260 static void s_s3_message_util_add_range_header(
261 uint64_t part_range_start,
262 uint64_t part_range_end,
263 struct aws_http_message *out_message);
264
265 /* Create a new get object request from an existing get object request. Currently just adds an optional ranged header.
266 */
267 struct aws_http_message *aws_s3_ranged_get_object_message_new(
268 struct aws_allocator *allocator,
269 struct aws_http_message *base_message,
270 uint64_t range_start,
271 uint64_t range_end) {
272 AWS_PRECONDITION(allocator);
273 AWS_PRECONDITION(base_message);
274
275 struct aws_http_message *message =
276 aws_s3_message_util_copy_http_message_no_body_all_headers(allocator, base_message);
277
278 if (message == NULL) {
279 return NULL;
280 }
281
282 s_s3_message_util_add_range_header(range_start, range_end, message);
283
284 return message;
285 }
286
287 /* Creates a create-multipart-upload request from a given put object request. */
288 struct aws_http_message *aws_s3_create_multipart_upload_message_new(
289 struct aws_allocator *allocator,
290 struct aws_http_message *base_message,
291 const struct aws_s3_meta_request_checksum_config_storage *checksum_config) {
292 AWS_PRECONDITION(allocator);
293
294 /* For multipart upload, some headers should ONLY be in the initial create-multipart request.
295 * Headers such as:
296 * - SSE related headers
297 * - user metadata (prefixed "x-amz-meta-") headers */
298 struct aws_http_message *message = aws_s3_message_util_copy_http_message_no_body_filter_headers(
299 allocator,
300 base_message,
301 g_s3_create_multipart_upload_excluded_headers,
302 AWS_ARRAY_SIZE(g_s3_create_multipart_upload_excluded_headers),
303 false /*exclude_x_amz_meta*/,
304 true /*exclude_x_checksum_meta*/);
305
306 if (message == NULL) {
307 return NULL;
308 }
309
310 if (aws_s3_message_util_set_multipart_request_path(allocator, NULL, 0, true, message)) {
311 goto error_clean_up;
312 }
313
314 struct aws_http_headers *headers = aws_http_message_get_headers(message);
315
316 if (headers == NULL) {
317 goto error_clean_up;
318 }
319
320 if (aws_http_headers_erase(headers, g_content_md5_header_name)) {
321 if (aws_last_error_or_unknown() != AWS_ERROR_HTTP_HEADER_NOT_FOUND) {
322 goto error_clean_up;
323 }
324 }
325
326 if (checksum_config && (checksum_config->location != AWS_SCL_NONE || checksum_config->has_full_object_checksum)) {
327 if (checksum_config->checksum_algorithm == AWS_SCA_UNKNOWN) {
328 struct aws_byte_cursor checksum_name = aws_byte_cursor_from_buf(&checksum_config->unknown_checksum_algo);
329 if (aws_http_headers_set(headers, g_checksum_algorithm_header_name, checksum_name)) {
330 goto error_clean_up;
331 }
332 } else if (checksum_config->checksum_algorithm) {
333 if (aws_http_headers_set(
334 headers,
335 g_checksum_algorithm_header_name,
336 aws_get_checksum_algorithm_name(checksum_config->checksum_algorithm))) {
337 goto error_clean_up;
338 }
339 }
340 if (checksum_config->has_full_object_checksum) {
341 /* Request S3 to store the full object checksum as it's set from user. */
342 if (aws_http_headers_set(headers, s_checksum_type_header, s_checksum_type_full_object)) {
343 goto error_clean_up;
344 }
345 }
346 }
347
348 struct aws_byte_cursor content_length_cursor = aws_byte_cursor_from_c_str("0");
349 if (aws_http_headers_set(headers, g_content_length_header_name, content_length_cursor)) {
350 goto error_clean_up;
351 }
352
353 aws_http_message_set_request_method(message, g_post_method);
354 aws_http_message_set_body_stream(message, NULL);
355
356 return message;
357
358 error_clean_up:
359 aws_http_message_release(message);
360 return NULL;
361 }
362
363 /**
364 * Create a new put object request from an existing put object request. Currently just optionally adds part information
365 * for a multipart upload.
366 **/
367 struct aws_http_message *aws_s3_upload_part_message_new_streaming(
368 struct aws_allocator *allocator,
369 struct aws_http_message *base_message,
370 struct aws_input_stream *input_stream,
371 uint32_t part_number,
372 const struct aws_string *upload_id,
373 bool should_compute_content_md5,
374 struct aws_s3_upload_request_checksum_context *checksum_context) {
375 AWS_PRECONDITION(allocator);
376 AWS_PRECONDITION(base_message);
377 AWS_PRECONDITION(part_number > 0);
378
379 struct aws_http_message *message = aws_s3_message_util_copy_http_message_no_body_filter_headers(
380 allocator,
381 base_message,
382 g_s3_upload_part_excluded_headers,
383 AWS_ARRAY_SIZE(g_s3_upload_part_excluded_headers),
384 true /*exclude_x_amz_meta*/,
385 true /*exclude_x_checksum_meta*/);
386
387 if (message == NULL) {
388 return NULL;
389 }
390
391 if (aws_s3_message_util_set_multipart_request_path(allocator, upload_id, part_number, false, message)) {
392 goto error_clean_up;
393 }
394 /* Acquire the extra refcount on the input stream, since the assign_body will transfer the ownership of the input
395 * stream to the message, while here we still want to the ownership of the input stream separately. */
396 aws_input_stream_acquire(input_stream);
397 if (aws_s3_message_util_assign_body(allocator, NULL /*body_buf*/, input_stream, message, checksum_context) ==
398 NULL) {
399 goto error_clean_up;
400 }
401
402 if (should_compute_content_md5) {
403 if (!checksum_context || checksum_context->location == AWS_SCL_NONE) {
404 /* MD5 is not supported for streaming. */
405 AWS_LOGF_ERROR(AWS_LS_S3_GENERAL, "Content MD5 is not supported for file I/O streaming.");
406 aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
407 goto error_clean_up;
408 }
409 }
410
411 return message;
412
413 error_clean_up:
414 aws_http_message_release(message);
415 return NULL;
416 }
417
418 /**
419 * Create a new put object request from an existing put object request. Currently just optionally adds part information
420 * for a multipart upload.
421 **/
422 struct aws_http_message *aws_s3_upload_part_message_new(
423 struct aws_allocator *allocator,
424 struct aws_http_message *base_message,
425 struct aws_byte_buf *buffer,
426 uint32_t part_number,
427 const struct aws_string *upload_id,
428 bool should_compute_content_md5,
429 struct aws_s3_upload_request_checksum_context *checksum_context) {
430 AWS_PRECONDITION(allocator);
431 AWS_PRECONDITION(base_message);
432 AWS_PRECONDITION(part_number > 0);
433 AWS_PRECONDITION(buffer);
434
435 struct aws_http_message *message = aws_s3_message_util_copy_http_message_no_body_filter_headers(
436 allocator,
437 base_message,
438 g_s3_upload_part_excluded_headers,
439 AWS_ARRAY_SIZE(g_s3_upload_part_excluded_headers),
440 true /*exclude_x_amz_meta*/,
441 true /*exclude_x_checksum_meta*/);
442
443 if (message == NULL) {
444 return NULL;
445 }
446
447 if (aws_s3_message_util_set_multipart_request_path(allocator, upload_id, part_number, false, message)) {
448 goto error_clean_up;
449 }
450
451 if (aws_s3_message_util_assign_body(allocator, buffer, NULL /*body_stream*/, message, checksum_context) == NULL) {
452 goto error_clean_up;
453 }
454
455 if (should_compute_content_md5) {
456 if (!checksum_context || checksum_context->location == AWS_SCL_NONE) {
457 /* MD5 will be skipped if flexible checksum used */
458 if (aws_s3_message_util_add_content_md5_header(allocator, buffer, message)) {
459 goto error_clean_up;
460 }
461 }
462 }
463
464 return message;
465
466 error_clean_up:
467 aws_http_message_release(message);
468 return NULL;
469 }
470
471 struct aws_http_message *aws_s3_upload_part_copy_message_new(
472 struct aws_allocator *allocator,
473 struct aws_http_message *base_message,
474 struct aws_byte_buf *buffer,
475 uint32_t part_number,
476 uint64_t range_start,
477 uint64_t range_end,
478 const struct aws_string *upload_id,
479 bool should_compute_content_md5) {
480 AWS_PRECONDITION(allocator);
481 AWS_PRECONDITION(base_message);
482 AWS_PRECONDITION(part_number > 0);
483
484 struct aws_http_message *message = aws_s3_message_util_copy_http_message_no_body_filter_headers(
485 allocator,
486 base_message,
487 g_s3_upload_part_excluded_headers,
488 AWS_ARRAY_SIZE(g_s3_upload_part_excluded_headers),
489 true /*exclude_x_amz_meta*/,
490 true /*exclude_x_checksum_meta*/);
491
492 if (message == NULL) {
493 goto error_clean_up;
494 }
495
496 if (aws_s3_message_util_set_multipart_request_path(allocator, upload_id, part_number, false, message)) {
497 goto error_clean_up;
498 }
499
500 if (buffer != NULL) {
501 /* part copy does not have a ChecksumAlgorithm member, it will use the same algorithm as the create
502 * multipart upload request specifies */
503 if (aws_s3_message_util_assign_body(
504 allocator, buffer, NULL /*body_stream*/, message, NULL /*checksum_context*/) == NULL) {
505 goto error_clean_up;
506 }
507
508 if (should_compute_content_md5) {
509 if (aws_s3_message_util_add_content_md5_header(allocator, buffer, message)) {
510 goto error_clean_up;
511 }
512 }
513 }
514
515 char source_range[1024];
516 snprintf(source_range, sizeof(source_range), "bytes=%" PRIu64 "-%" PRIu64, range_start, range_end);
517
518 struct aws_http_header source_range_header = {
519 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-copy-source-range"),
520 .value = aws_byte_cursor_from_c_str(source_range),
521 };
522
523 struct aws_http_headers *headers = aws_http_message_get_headers(message);
524 aws_http_headers_add_header(headers, &source_range_header);
525
526 return message;
527
528 error_clean_up:
529
530 if (message != NULL) {
531 aws_http_message_release(message);
532 message = NULL;
533 }
534
535 return NULL;
536 }
537
538 static const struct aws_byte_cursor s_slash_char = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("/");
539 /**
540 * For the CopyObject operation, create the initial HEAD message to retrieve the size of the copy source.
541 */
542 struct aws_http_message *aws_s3_get_source_object_size_message_new(
543 struct aws_allocator *allocator,
544 struct aws_http_message *base_message,
545 struct aws_uri *source_uri) {
546
547 struct aws_http_message *message = aws_http_message_new_request(allocator);
548 struct aws_byte_buf head_object_host_header;
549 AWS_ZERO_STRUCT(head_object_host_header);
550
551 if (message == NULL) {
552 goto error_cleanup;
553 }
554
555 if (aws_http_message_set_request_method(message, g_head_method)) {
556 goto error_cleanup;
557 }
558 if (source_uri != NULL && source_uri->self_size > 0) {
559 /* Parse source host header and path from the provided URI */
560 struct aws_byte_cursor host = *aws_uri_host_name(source_uri);
561 struct aws_byte_cursor path = *aws_uri_path(source_uri);
562 if (host.len == 0 || path.len == 0) {
563 aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
564 goto error_cleanup;
565 }
566 struct aws_http_header host_header = {
567 .name = g_host_header_name,
568 .value = host,
569 };
570 if (aws_http_message_add_header(message, host_header)) {
571 goto error_cleanup;
572 }
573
574 if (aws_http_message_set_request_path(message, path)) {
575 goto error_cleanup;
576 }
577 return message;
578 }
579
580 /* Parse the source host header and path from the x-amz-copy-source header and the destination URI */
581
582 AWS_PRECONDITION(allocator);
583
584 /* Find the x-amz-copy-source header, to extract source bucket/key information. */
585 struct aws_http_headers *headers = aws_http_message_get_headers(base_message);
586 if (!headers) {
587 AWS_LOGF_ERROR(AWS_LS_S3_GENERAL, "CopyRequest is missing headers");
588 goto error_cleanup;
589 }
590
591 struct aws_byte_cursor source_header;
592 const struct aws_byte_cursor copy_source_header = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-copy-source");
593 if (aws_http_headers_get(headers, copy_source_header, &source_header) != AWS_OP_SUCCESS) {
594 AWS_LOGF_ERROR(AWS_LS_S3_GENERAL, "CopyRequest is missing the x-amz-copy-source header");
595 goto error_cleanup;
596 }
597 struct aws_byte_cursor host;
598 if (aws_http_headers_get(headers, g_host_header_name, &host) != AWS_OP_SUCCESS) {
599 AWS_LOGF_ERROR(AWS_LS_S3_GENERAL, "CopyRequest is missing the Host header");
600 goto error_cleanup;
601 }
602
603 struct aws_byte_cursor request_path = source_header;
604
605 /* Skip optional leading slash. */
606 if (aws_byte_cursor_starts_with(&request_path, &s_slash_char)) {
607 aws_byte_cursor_advance(&request_path, 1);
608 }
609
610 /* From this point forward, the format is {bucket}/{key} - split
611 components.*/
612
613 struct aws_byte_cursor source_bucket = {0};
614
615 if (aws_byte_cursor_next_split(&request_path, '/', &source_bucket)) {
616 aws_byte_cursor_advance(&request_path, source_bucket.len);
617 }
618
619 if (source_bucket.len == 0 || request_path.len == 0) {
620 AWS_LOGF_ERROR(
621 AWS_LS_S3_GENERAL,
622 "CopyRequest x-amz-copy-source header does not follow expected bucket/key format: " PRInSTR,
623 AWS_BYTE_CURSOR_PRI(source_header));
624 goto error_cleanup;
625 }
626
627 if (aws_byte_buf_init_copy_from_cursor(&head_object_host_header, allocator, source_bucket)) {
628 goto error_cleanup;
629 }
630
631 /* Reuse the domain name from the original Host header for the HEAD request.
632 * TODO: following code works by replacing bucket name in the host with the
633 * source bucket name. this only works for virtual host endpoints and has a
634 * slew of other issues, like not supporting source in a different region.
635 * This covers common case, but we need to rethink how we can support all
636 * cases in general.
637 */
638 struct aws_byte_cursor domain_name;
639 const struct aws_byte_cursor dot = aws_byte_cursor_from_c_str(".");
640 if (aws_byte_cursor_find_exact(&host, &dot, &domain_name)) {
641 AWS_LOGF_ERROR(AWS_LS_S3_GENERAL, "CopyRequest Host header not in FQDN format");
642 goto error_cleanup;
643 }
644
645 if (aws_byte_buf_append_dynamic(&head_object_host_header, &domain_name)) {
646 goto error_cleanup;
647 }
648
649 struct aws_http_header host_header = {
650 .name = g_host_header_name,
651 .value = aws_byte_cursor_from_buf(&head_object_host_header),
652 };
653 if (aws_http_message_add_header(message, host_header)) {
654 goto error_cleanup;
655 }
656
657 if (aws_http_message_set_request_path(message, request_path)) {
658 goto error_cleanup;
659 }
660
661 aws_byte_buf_clean_up(&head_object_host_header);
662 return message;
663
664 error_cleanup:
665 aws_byte_buf_clean_up(&head_object_host_header);
666 aws_http_message_release(message);
667 return NULL;
668 }
669
670 static const struct aws_byte_cursor s_complete_payload_begin = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(
671 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
672 "<CompleteMultipartUpload xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\">\n");
673
674 static const struct aws_byte_cursor s_complete_payload_end =
675 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("</CompleteMultipartUpload>");
676
677 static const struct aws_byte_cursor s_part_section_string_0 = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(" <Part>\n"
678 " <ETag>");
679
680 static const struct aws_byte_cursor s_part_section_string_1 =
681 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("</ETag>\n"
682 " <PartNumber>");
683
684 static const struct aws_byte_cursor s_close_part_number_tag = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("</PartNumber>\n");
685 static const struct aws_byte_cursor s_close_part_tag = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(" </Part>\n");
686 static const struct aws_byte_cursor s_open_start_bracket = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(" <");
687 static const struct aws_byte_cursor s_open_end_bracket = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("</");
688 static const struct aws_byte_cursor s_close_bracket = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(">");
689 static const struct aws_byte_cursor s_close_bracket_new_line = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(">\n");
690 /* Create a complete-multipart message, which includes an XML payload of all completed parts. */
691 struct aws_http_message *aws_s3_complete_multipart_message_with_object_size_new(
692 struct aws_allocator *allocator,
693 struct aws_http_message *base_message,
694 struct aws_byte_buf *body_buffer,
695 const struct aws_string *upload_id,
696 const struct aws_array_list *parts,
697 const struct aws_s3_meta_request_checksum_config_storage *checksum_config,
698 uint64_t object_size) {
699 AWS_PRECONDITION(allocator);
700 AWS_PRECONDITION(base_message);
701 AWS_PRECONDITION(body_buffer);
702 AWS_PRECONDITION(upload_id);
703 AWS_PRECONDITION(parts);
704
705 struct aws_byte_cursor mpu_algorithm_checksum_name;
706 AWS_ZERO_STRUCT(mpu_algorithm_checksum_name);
707 struct aws_http_message *message = NULL;
708 bool set_checksums =
709 checksum_config && (checksum_config->location != AWS_SCL_NONE || checksum_config->has_full_object_checksum);
710 if (set_checksums) {
711 mpu_algorithm_checksum_name =
712 aws_get_completed_part_name_from_checksum_algorithm(checksum_config->checksum_algorithm);
713 message = aws_s3_message_util_copy_http_message_no_body_filter_headers(
714 allocator,
715 base_message,
716 g_s3_complete_multipart_upload_with_checksum_excluded_headers,
717 AWS_ARRAY_SIZE(g_s3_complete_multipart_upload_with_checksum_excluded_headers),
718 true /*exclude_x_amz_meta*/,
719 false /*exclude_x_checksum_meta*/);
720
721 } else {
722 message = aws_s3_message_util_copy_http_message_no_body_filter_headers(
723 allocator,
724 base_message,
725 g_s3_complete_multipart_upload_excluded_headers,
726 AWS_ARRAY_SIZE(g_s3_complete_multipart_upload_excluded_headers),
727 true /*exclude_x_amz_meta*/,
728 false /*exclude_x_checksum_meta*/);
729 }
730
731 struct aws_http_headers *headers = NULL;
732
733 if (message == NULL) {
734 goto error_clean_up;
735 }
736
737 if (aws_s3_message_util_set_multipart_request_path(allocator, upload_id, 0, false, message)) {
738 goto error_clean_up;
739 }
740
741 aws_http_message_set_request_method(message, g_post_method);
742
743 headers = aws_http_message_get_headers(message);
744
745 if (headers == NULL) {
746 goto error_clean_up;
747 }
748 if (set_checksums && checksum_config->has_full_object_checksum) {
749 /* Set the full object checksum header. */
750 AWS_ASSERT(checksum_config->checksum_algorithm != AWS_SCA_NONE);
751 if (aws_http_headers_set(
752 headers,
753 aws_get_http_header_name_from_checksum_algorithm(checksum_config->checksum_algorithm),
754 aws_byte_cursor_from_buf(&checksum_config->full_object_checksum))) {
755 goto error_clean_up;
756 }
757 /* Request S3 to store the full object checksum as it's set from user. */
758 if (aws_http_headers_set(headers, s_checksum_type_header, s_checksum_type_full_object)) {
759 goto error_clean_up;
760 }
761 }
762 /* Send the total object size as x-amz-mp-object-size so S3 can validate the assembled object. object_size of 0
763 * means "unknown" (e.g. a streaming upload of unknown length); in that case the header is omitted, matching the
764 * prior behavior of skipping it when the base message had no Content-Length. */
765 if (object_size > 0) {
766 char object_size_buffer[32];
767 int object_size_len = snprintf(object_size_buffer, sizeof(object_size_buffer), "%" PRIu64, object_size);
768 struct aws_byte_cursor object_size_cursor =
769 aws_byte_cursor_from_array(object_size_buffer, (size_t)object_size_len);
770 if (aws_http_headers_set(headers, aws_byte_cursor_from_c_str("x-amz-mp-object-size"), object_size_cursor)) {
771 goto error_clean_up;
772 }
773 }
774
775 /* Create XML payload with all the etags of finished parts */
776 {
777 aws_byte_buf_reset(body_buffer, false);
778
779 if (aws_byte_buf_append_dynamic(body_buffer, &s_complete_payload_begin)) {
780 goto error_clean_up;
781 }
782
783 for (size_t part_index = 0; part_index < aws_array_list_length(parts); ++part_index) {
784 struct aws_s3_mpu_part_info *part = NULL;
785
786 aws_array_list_get_at(parts, &part, part_index);
787 AWS_FATAL_ASSERT(part != NULL);
788
789 if (aws_byte_buf_append_dynamic(body_buffer, &s_part_section_string_0)) {
790 goto error_clean_up;
791 }
792
793 struct aws_byte_cursor etag_byte_cursor = aws_byte_cursor_from_string(part->etag);
794
795 if (aws_byte_buf_append_dynamic(body_buffer, &etag_byte_cursor)) {
796 goto error_clean_up;
797 }
798
799 if (aws_byte_buf_append_dynamic(body_buffer, &s_part_section_string_1)) {
800 goto error_clean_up;
801 }
802
803 char part_number_buffer[32] = "";
804 int part_number = (int)(part_index + 1);
805 int part_number_num_char = snprintf(part_number_buffer, sizeof(part_number_buffer), "%d", part_number);
806 struct aws_byte_cursor part_number_byte_cursor =
807 aws_byte_cursor_from_array(part_number_buffer, part_number_num_char);
808
809 if (aws_byte_buf_append_dynamic(body_buffer, &part_number_byte_cursor)) {
810 goto error_clean_up;
811 }
812
813 if (aws_byte_buf_append_dynamic(body_buffer, &s_close_part_number_tag)) {
814 goto error_clean_up;
815 }
816
817 if (mpu_algorithm_checksum_name.len) {
818 struct aws_byte_cursor checksum =
819 aws_s3_upload_request_checksum_context_get_checksum_cursor(part->checksum_context);
820
821 if (aws_byte_buf_append_dynamic(body_buffer, &s_open_start_bracket)) {
822 goto error_clean_up;
823 }
824 if (aws_byte_buf_append_dynamic(body_buffer, &mpu_algorithm_checksum_name)) {
825 goto error_clean_up;
826 }
827 if (aws_byte_buf_append_dynamic(body_buffer, &s_close_bracket)) {
828 goto error_clean_up;
829 }
830 if (aws_byte_buf_append_dynamic(body_buffer, &checksum)) {
831 goto error_clean_up;
832 }
833 if (aws_byte_buf_append_dynamic(body_buffer, &s_open_end_bracket)) {
834 goto error_clean_up;
835 }
836 if (aws_byte_buf_append_dynamic(body_buffer, &mpu_algorithm_checksum_name)) {
837 goto error_clean_up;
838 }
839 if (aws_byte_buf_append_dynamic(body_buffer, &s_close_bracket_new_line)) {
840 goto error_clean_up;
841 }
842 }
843 if (aws_byte_buf_append_dynamic(body_buffer, &s_close_part_tag)) {
844 goto error_clean_up;
845 }
846 }
847
848 if (aws_byte_buf_append_dynamic(body_buffer, &s_complete_payload_end)) {
849 goto error_clean_up;
850 }
851
852 AWS_LOGF_TRACE(
853 AWS_LS_S3_GENERAL, "Payload for Complete MPU is:\n" PRInSTR "\n", AWS_BYTE_BUF_PRI(*body_buffer));
854 aws_s3_message_util_assign_body(allocator, body_buffer, NULL /*body_stream*/, message, NULL);
855 }
856
857 return message;
858
859 error_clean_up:
860
861 AWS_LOGF_ERROR(AWS_LS_S3_GENERAL, "Could not create complete multipart message");
862
863 if (message != NULL) {
864 aws_http_message_release(message);
865 message = NULL;
866 }
867
868 return NULL;
869 }
870
871 struct aws_http_message *aws_s3_complete_multipart_message_new(
872 struct aws_allocator *allocator,
873 struct aws_http_message *base_message,
874 struct aws_byte_buf *body_buffer,
875 const struct aws_string *upload_id,
876 const struct aws_array_list *parts,
877 const struct aws_s3_meta_request_checksum_config_storage *checksum_config) {
878
879 /* Derive the object size from the base message's Content-Length, as the multipart upload path expects. A missing
880 * or unparseable Content-Length (e.g. a streaming upload of unknown length) yields 0, which tells the delegate to
881 * omit the x-amz-mp-object-size header. */
882 uint64_t object_size = 0;
883 struct aws_byte_cursor content_length_cursor;
884 const struct aws_http_headers *initial_message_headers = aws_http_message_get_headers(base_message);
885 if (initial_message_headers != NULL &&
886 aws_http_headers_get(initial_message_headers, g_content_length_header_name, &content_length_cursor) ==
887 AWS_OP_SUCCESS) {
888 if (aws_byte_cursor_utf8_parse_u64(content_length_cursor, &object_size)) {
889 object_size = 0;
890 }
891 }
892
893 return aws_s3_complete_multipart_message_with_object_size_new(
894 allocator, base_message, body_buffer, upload_id, parts, checksum_config, object_size);
895 }
896
897 struct aws_http_message *aws_s3_abort_multipart_upload_message_new(
898 struct aws_allocator *allocator,
899 struct aws_http_message *base_message,
900 const struct aws_string *upload_id) {
901
902 struct aws_http_message *message = aws_s3_message_util_copy_http_message_no_body_filter_headers(
903 allocator,
904 base_message,
905 g_s3_abort_multipart_upload_excluded_headers,
906 AWS_ARRAY_SIZE(g_s3_abort_multipart_upload_excluded_headers),
907 true /*exclude_x_amz_meta*/,
908 true /*exclude_x_checksum_meta*/);
909
910 if (aws_s3_message_util_set_multipart_request_path(allocator, upload_id, 0, false, message)) {
911 goto error_clean_up;
912 }
913 aws_http_message_set_request_method(message, g_delete_method);
914
915 return message;
916
917 error_clean_up:
918
919 AWS_LOGF_ERROR(AWS_LS_S3_GENERAL, "Could not create abort multipart upload message");
920
921 if (message != NULL) {
922 aws_http_message_release(message);
923 message = NULL;
924 }
925
926 return NULL;
927 }
928
929 /**
930 * Calculate the in memory checksum based on the checksum config. Initialize and set the out_checksum to the encoded
931 * checksum result
932 */
933 static int s_calculate_in_memory_checksum_helper(
934 struct aws_allocator *allocator,
935 struct aws_byte_cursor data,
936 struct aws_s3_upload_request_checksum_context *checksum_context) {
937 AWS_ASSERT(checksum_context);
938
939 int ret_code = AWS_OP_ERR;
940 /* Calculate checksum for output buffer only (no header/trailer) */
941 struct aws_byte_buf raw_checksum;
942 size_t digest_size = aws_get_digest_size_from_checksum_algorithm(checksum_context->algorithm);
943 aws_byte_buf_init(&raw_checksum, allocator, digest_size);
944
945 if (aws_checksum_compute(allocator, checksum_context->algorithm, &data, &raw_checksum)) {
946 aws_byte_buf_clean_up(&raw_checksum);
947 goto done;
948 }
949
950 struct aws_byte_cursor raw_checksum_cursor = aws_byte_cursor_from_buf(&raw_checksum);
951 if (aws_s3_upload_request_checksum_context_finalize_checksum(checksum_context, raw_checksum_cursor)) {
952 aws_byte_buf_clean_up(&raw_checksum);
953 goto done;
954 }
955 aws_byte_buf_clean_up(&raw_checksum);
956
957 ret_code = AWS_OP_SUCCESS;
958 done:
959 aws_byte_buf_clean_up(&raw_checksum);
960 return ret_code;
961 }
962
963 /**
964 * Calculate the in memory checksum based on the checksum config.
965 * If out_checksum set, initialize and set it to the encoded checksum result.
966 * Set the corresponding header in out_message to the encoded checksum result.
967 */
968 static int s_calculate_and_add_checksum_to_header_helper(
969 struct aws_allocator *allocator,
970 struct aws_byte_cursor data,
971 struct aws_http_message *out_message,
972 struct aws_s3_upload_request_checksum_context *checksum_context) {
973 AWS_ASSERT(out_message != NULL);
974 int ret_code = AWS_OP_ERR;
975 if (s_calculate_in_memory_checksum_helper(allocator, data, checksum_context)) {
976 goto done;
977 }
978
979 /* Add the encoded checksum to header. */
980 const struct aws_byte_cursor header_name =
981 aws_get_http_header_name_from_checksum_algorithm(checksum_context->algorithm);
982 struct aws_byte_cursor encoded_checksum_val =
983 aws_s3_upload_request_checksum_context_get_checksum_cursor(checksum_context);
984 struct aws_http_headers *headers = aws_http_message_get_headers(out_message);
985 if (aws_http_headers_set(headers, header_name, encoded_checksum_val)) {
986 goto done;
987 }
988
989 ret_code = AWS_OP_SUCCESS;
990 done:
991 return ret_code;
992 }
993
994 /* Assign a buffer to an HTTP message:
995 - from a buffer to create a stream and setting the content-length and checksum headers
996 - from a stream directly and setting the content-length and checksum headers
997 */
998 struct aws_input_stream *aws_s3_message_util_assign_body(
999 struct aws_allocator *allocator,
1000 struct aws_byte_buf *byte_buf,
1001 struct aws_input_stream *stream,
1002 struct aws_http_message *out_message,
1003 struct aws_s3_upload_request_checksum_context *checksum_context) {
1004 AWS_PRECONDITION(allocator);
1005 AWS_PRECONDITION(out_message);
1006 if ((byte_buf && stream) || (!byte_buf && !stream)) {
1007 /* Should pass one and only one of them. */
1008 aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
1009 return NULL;
1010 }
1011 struct aws_http_headers *headers = aws_http_message_get_headers(out_message);
1012 struct aws_byte_buf content_encoding_header_buf;
1013 AWS_ZERO_STRUCT(content_encoding_header_buf);
1014 if (headers == NULL) {
1015 return NULL;
1016 }
1017
1018 struct aws_input_stream *input_stream = stream;
1019 if (byte_buf) {
1020 AWS_ASSERT(stream == NULL);
1021 struct aws_byte_cursor buffer_byte_cursor = aws_byte_cursor_from_buf(byte_buf);
1022 input_stream = aws_input_stream_new_from_cursor(allocator, &buffer_byte_cursor);
1023 }
1024 if (input_stream == NULL) {
1025 goto error_clean_up;
1026 }
1027 int64_t original_content_length = 0;
1028 if (aws_input_stream_get_length(input_stream, &original_content_length)) {
1029 goto error_clean_up;
1030 }
1031 if (checksum_context && checksum_context->algorithm != AWS_SCA_NONE) {
1032 if (aws_s3_upload_request_checksum_context_should_add_trailer(checksum_context)) {
1033 /* aws-chunked encode the payload and add related headers */
1034
1035 /* set Content-Encoding header. If the header already exists, append the existing value to aws-chunked
1036 * We already made sure that the existing value is not 'aws_chunked' in 'aws_s3_client_make_meta_request'
1037 * function.
1038 */
1039 struct aws_byte_cursor content_encoding_header_cursor;
1040 bool has_content_encoding_header =
1041 aws_http_headers_get(headers, g_content_encoding_header_name, &content_encoding_header_cursor) ==
1042 AWS_OP_SUCCESS;
1043 size_t content_encoding_header_buf_size =
1044 has_content_encoding_header
1045 ? g_content_encoding_header_aws_chunked.len + content_encoding_header_cursor.len + 1
1046 : g_content_encoding_header_aws_chunked.len;
1047 aws_byte_buf_init(&content_encoding_header_buf, allocator, content_encoding_header_buf_size);
1048
1049 if (has_content_encoding_header) {
1050 aws_byte_buf_append_dynamic(&content_encoding_header_buf, &content_encoding_header_cursor);
1051 aws_byte_buf_append_byte_dynamic(&content_encoding_header_buf, ',');
1052 }
1053 aws_byte_buf_append_dynamic(&content_encoding_header_buf, &g_content_encoding_header_aws_chunked);
1054
1055 if (aws_http_headers_set(
1056 headers, g_content_encoding_header_name, aws_byte_cursor_from_buf(&content_encoding_header_buf))) {
1057 goto error_clean_up;
1058 }
1059
1060 /* set x-amz-trailer header */
1061 if (aws_http_headers_set(
1062 headers,
1063 g_trailer_header_name,
1064 aws_get_http_header_name_from_checksum_algorithm(checksum_context->algorithm))) {
1065 goto error_clean_up;
1066 }
1067 /* set x-amz-decoded-content-length header */
1068 char decoded_content_length_buffer[64] = "";
1069 snprintf(
1070 decoded_content_length_buffer,
1071 sizeof(decoded_content_length_buffer),
1072 "%" PRIu64,
1073 (uint64_t)original_content_length);
1074 struct aws_byte_cursor decode_content_length_cursor =
1075 aws_byte_cursor_from_array(decoded_content_length_buffer, strlen(decoded_content_length_buffer));
1076 if (aws_http_headers_set(headers, g_decoded_content_length_header_name, decode_content_length_cursor)) {
1077 goto error_clean_up;
1078 }
1079 /* set input stream to chunk stream */
1080 struct aws_input_stream *chunk_stream = aws_chunk_stream_new(allocator, input_stream, checksum_context);
1081 if (!chunk_stream) {
1082 goto error_clean_up;
1083 }
1084 aws_input_stream_release(input_stream);
1085 input_stream = chunk_stream;
1086 } else if (aws_s3_upload_request_checksum_context_should_add_header(checksum_context)) {
1087 if (byte_buf == NULL) {
1088 /* Streaming don't support HEADER checksum */
1089 AWS_LOGF_ERROR(
1090 AWS_LS_S3_CLIENT,
1091 "Streaming upload does not support checksum calculation in header, "
1092 "use trailer checksum instead.");
1093 aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
1094 goto error_clean_up;
1095 }
1096 /* Calculate the checksum directly from memory and add it to the header. */
1097 if (s_calculate_and_add_checksum_to_header_helper(
1098 allocator, aws_byte_cursor_from_buf(byte_buf), out_message, checksum_context)) {
1099 goto error_clean_up;
1100 }
1101 } else if (aws_s3_upload_request_checksum_context_should_calculate(checksum_context)) {
1102 /* The checksum won't be uploaded, but we still need it for the upload review callback */
1103 struct aws_input_stream *checksum_stream =
1104 aws_checksum_stream_new_with_context(allocator, input_stream, checksum_context);
1105 if (!checksum_stream) {
1106 goto error_clean_up;
1107 }
1108 aws_input_stream_release(input_stream);
1109 input_stream = checksum_stream;
1110 }
1111 }
1112
1113 /* Set content-length header */
1114 int64_t stream_length = 0;
1115 if (aws_input_stream_get_length(input_stream, &stream_length)) {
1116 goto error_clean_up;
1117 }
1118 char content_length_buffer[64] = "";
1119 snprintf(content_length_buffer, sizeof(content_length_buffer), "%" PRIu64, (uint64_t)stream_length);
1120 struct aws_byte_cursor content_length_cursor =
1121 aws_byte_cursor_from_array(content_length_buffer, strlen(content_length_buffer));
1122 if (aws_http_headers_set(headers, g_content_length_header_name, content_length_cursor)) {
1123 goto error_clean_up;
1124 }
1125
1126 aws_http_message_set_body_stream(out_message, input_stream);
1127 /* Let the message take the full ownership */
1128 aws_input_stream_release(input_stream);
1129 aws_byte_buf_clean_up(&content_encoding_header_buf);
1130 return input_stream;
1131
1132 error_clean_up:
1133 AWS_LOGF_ERROR(AWS_LS_S3_CLIENT, "Failed to assign body for s3 request http message, from body buffer.");
1134 aws_input_stream_release(input_stream);
1135 aws_byte_buf_clean_up(&content_encoding_header_buf);
1136 return NULL;
1137 }
1138
1139 /* Add a content-md5 header. */
1140 int aws_s3_message_util_add_content_md5_header(
1141 struct aws_allocator *allocator,
1142 struct aws_byte_buf *input_buf,
1143 struct aws_http_message *out_message) {
1144
1145 AWS_PRECONDITION(out_message);
1146
1147 /* Compute MD5 */
1148 struct aws_byte_cursor md5_input = aws_byte_cursor_from_buf(input_buf);
1149 uint8_t md5_output[AWS_MD5_LEN];
1150 struct aws_byte_buf md5_output_buf = aws_byte_buf_from_empty_array(md5_output, sizeof(md5_output));
1151 if (aws_md5_compute(allocator, &md5_input, &md5_output_buf, 0)) {
1152 return AWS_OP_ERR;
1153 }
1154
1155 /* Compute Base64 encoding of MD5 */
1156 struct aws_byte_cursor base64_input = aws_byte_cursor_from_buf(&md5_output_buf);
1157 size_t base64_output_size = 0;
1158 if (aws_base64_compute_encoded_len(md5_output_buf.len, &base64_output_size)) {
1159 return AWS_OP_ERR;
1160 }
1161 struct aws_byte_buf base64_output_buf;
1162 aws_byte_buf_init(&base64_output_buf, allocator, base64_output_size);
1163 if (aws_base64_encode(&base64_input, &base64_output_buf)) {
1164 goto error_clean_up;
1165 }
1166
1167 struct aws_http_headers *headers = aws_http_message_get_headers(out_message);
1168 if (aws_http_headers_set(headers, g_content_md5_header_name, aws_byte_cursor_from_buf(&base64_output_buf))) {
1169 goto error_clean_up;
1170 }
1171
1172 aws_byte_buf_clean_up(&base64_output_buf);
1173 return AWS_OP_SUCCESS;
1174
1175 error_clean_up:
1176
1177 aws_byte_buf_clean_up(&base64_output_buf);
1178 return AWS_OP_ERR;
1179 }
1180
1181 /* Copy an existing HTTP message's headers, method, and path. */
1182 struct aws_http_message *aws_s3_message_util_copy_http_message_no_body_all_headers(
1183 struct aws_allocator *allocator,
1184 struct aws_http_message *base_message) {
1185
1186 return aws_s3_message_util_copy_http_message_no_body_filter_headers(allocator, base_message, NULL, 0, false, false);
1187 }
1188
1189 struct aws_http_message *aws_s3_message_util_copy_http_message_no_body_filter_headers(
1190 struct aws_allocator *allocator,
1191 struct aws_http_message *base_message,
1192 const struct aws_byte_cursor *excluded_header_array,
1193 size_t excluded_header_array_size,
1194 bool exclude_x_amz_meta,
1195 bool exclude_x_amz_checksum) {
1196
1197 AWS_PRECONDITION(allocator);
1198 AWS_PRECONDITION(base_message);
1199
1200 struct aws_http_message *message = aws_http_message_new_request(allocator);
1201 AWS_ASSERT(message);
1202
1203 struct aws_byte_cursor request_method;
1204 if (aws_http_message_get_request_method(base_message, &request_method)) {
1205 AWS_LOGF_ERROR(AWS_LS_S3_CLIENT, "Failed to get request method.");
1206 goto error_clean_up;
1207 }
1208
1209 if (aws_http_message_set_request_method(message, request_method)) {
1210 goto error_clean_up;
1211 }
1212
1213 struct aws_byte_cursor request_path;
1214 if (aws_http_message_get_request_path(base_message, &request_path)) {
1215 AWS_LOGF_ERROR(AWS_LS_S3_CLIENT, "Failed to get request path.");
1216 goto error_clean_up;
1217 }
1218
1219 if (aws_http_message_set_request_path(message, request_path)) {
1220 goto error_clean_up;
1221 }
1222
1223 aws_s3_message_util_copy_headers(
1224 base_message,
1225 message,
1226 excluded_header_array,
1227 excluded_header_array_size,
1228 exclude_x_amz_meta,
1229 exclude_x_amz_checksum);
1230
1231 return message;
1232
1233 error_clean_up:
1234 aws_http_message_release(message);
1235 return NULL;
1236 }
1237
1238 void aws_s3_message_util_copy_headers(
1239 struct aws_http_message *source_message,
1240 struct aws_http_message *dest_message,
1241 const struct aws_byte_cursor *excluded_header_array,
1242 size_t excluded_header_array_size,
1243 bool exclude_x_amz_meta,
1244 bool exclude_x_amz_checksum) {
1245
1246 size_t num_headers = aws_http_message_get_header_count(source_message);
1247
1248 for (size_t header_index = 0; header_index < num_headers; ++header_index) {
1249 struct aws_http_header header;
1250
1251 int error = aws_http_message_get_header(source_message, &header, header_index);
1252
1253 if (excluded_header_array && excluded_header_array_size > 0) {
1254 bool exclude_header = false;
1255
1256 for (size_t exclude_index = 0; exclude_index < excluded_header_array_size; ++exclude_index) {
1257 if (aws_byte_cursor_eq_ignore_case(&header.name, &excluded_header_array[exclude_index])) {
1258 exclude_header = true;
1259 break;
1260 }
1261 }
1262
1263 if (exclude_header) {
1264 continue;
1265 }
1266 }
1267
1268 if (exclude_x_amz_meta) {
1269 if (aws_byte_cursor_starts_with_ignore_case(&header.name, &s_x_amz_meta_prefix)) {
1270 continue;
1271 }
1272 }
1273
1274 if (exclude_x_amz_checksum) {
1275 if (aws_s3_is_checksum_value_header_name(header.name)) {
1276 continue;
1277 }
1278 }
1279
1280 error |= aws_http_message_add_header(dest_message, header);
1281 (void)error;
1282 AWS_ASSERT(!error);
1283 }
1284 }
1285
1286 /* Add a range header.*/
1287 static void s_s3_message_util_add_range_header(
1288 uint64_t part_range_start,
1289 uint64_t part_range_end,
1290 struct aws_http_message *out_message) {
1291 AWS_PRECONDITION(out_message);
1292
1293 /* ((2^64)-1 = 20 characters; 2*20 + length-of("bytes=-") < 128) */
1294 char range_value_buffer[128] = "";
1295 snprintf(
1296 range_value_buffer, sizeof(range_value_buffer), "bytes=%" PRIu64 "-%" PRIu64, part_range_start, part_range_end);
1297
1298 struct aws_http_header range_header;
1299 AWS_ZERO_STRUCT(range_header);
1300 range_header.name = g_range_header_name;
1301 range_header.value = aws_byte_cursor_from_c_str(range_value_buffer);
1302
1303 struct aws_http_headers *headers = aws_http_message_get_headers(out_message);
1304 AWS_ASSERT(headers != NULL);
1305
1306 int erase_result = aws_http_headers_erase(headers, range_header.name);
1307 AWS_ASSERT(erase_result == AWS_OP_SUCCESS || aws_last_error() == AWS_ERROR_HTTP_HEADER_NOT_FOUND);
1308
1309 /* Only failed when the header has invalid name, which is impossible here. */
1310 erase_result = aws_http_message_add_header(out_message, range_header);
1311 AWS_ASSERT(erase_result == AWS_OP_SUCCESS);
1312 (void)erase_result;
1313 }
1314
1315 /* Handle setting up the multipart request path for a message. */
1316 int aws_s3_message_util_set_multipart_request_path(
1317 struct aws_allocator *allocator,
1318 const struct aws_string *upload_id,
1319 uint32_t part_number,
1320 bool append_uploads_suffix,
1321 struct aws_http_message *message) {
1322
1323 const struct aws_byte_cursor question_mark = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("?");
1324 const struct aws_byte_cursor ampersand = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("&");
1325
1326 const struct aws_byte_cursor uploads_suffix = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("uploads");
1327 const struct aws_byte_cursor part_number_arg = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("partNumber=");
1328 const struct aws_byte_cursor upload_id_arg = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("uploadId=");
1329
1330 struct aws_byte_buf request_path_buf;
1331 struct aws_byte_cursor request_path;
1332
1333 if (aws_http_message_get_request_path(message, &request_path)) {
1334 return AWS_OP_ERR;
1335 }
1336
1337 aws_byte_buf_init(&request_path_buf, allocator, request_path.len);
1338
1339 if (aws_byte_buf_append_dynamic(&request_path_buf, &request_path)) {
1340 goto error_clean_up;
1341 }
1342
1343 bool has_existing_query_parameters = false;
1344
1345 for (size_t i = 0; i < request_path.len; ++i) {
1346 if (request_path.ptr[i] == '?') {
1347 has_existing_query_parameters = true;
1348 break;
1349 }
1350 }
1351
1352 if (part_number > 0) {
1353 if (aws_byte_buf_append_dynamic(
1354 &request_path_buf, has_existing_query_parameters ? &ampersand : &question_mark)) {
1355 goto error_clean_up;
1356 }
1357
1358 if (aws_byte_buf_append_dynamic(&request_path_buf, &part_number_arg)) {
1359 goto error_clean_up;
1360 }
1361
1362 char part_number_buffer[32] = "";
1363 snprintf(part_number_buffer, sizeof(part_number_buffer), "%d", part_number);
1364 struct aws_byte_cursor part_number_cursor =
1365 aws_byte_cursor_from_array(part_number_buffer, strlen(part_number_buffer));
1366
1367 if (aws_byte_buf_append_dynamic(&request_path_buf, &part_number_cursor)) {
1368 goto error_clean_up;
1369 }
1370
1371 has_existing_query_parameters = true;
1372 }
1373
1374 if (upload_id != NULL) {
1375
1376 struct aws_byte_cursor upload_id_cursor = aws_byte_cursor_from_string(upload_id);
1377
1378 if (aws_byte_buf_append_dynamic(
1379 &request_path_buf, has_existing_query_parameters ? &ampersand : &question_mark)) {
1380 goto error_clean_up;
1381 }
1382
1383 if (aws_byte_buf_append_dynamic(&request_path_buf, &upload_id_arg)) {
1384 goto error_clean_up;
1385 }
1386
1387 if (aws_byte_buf_append_dynamic(&request_path_buf, &upload_id_cursor)) {
1388 goto error_clean_up;
1389 }
1390
1391 has_existing_query_parameters = true;
1392 }
1393
1394 if (append_uploads_suffix) {
1395 if (aws_byte_buf_append_dynamic(
1396 &request_path_buf, has_existing_query_parameters ? &ampersand : &question_mark)) {
1397 goto error_clean_up;
1398 }
1399
1400 if (aws_byte_buf_append_dynamic(&request_path_buf, &uploads_suffix)) {
1401 goto error_clean_up;
1402 }
1403
1404 has_existing_query_parameters = true;
1405 }
1406
1407 struct aws_byte_cursor new_request_path = aws_byte_cursor_from_buf(&request_path_buf);
1408
1409 if (aws_http_message_set_request_path(message, new_request_path)) {
1410 goto error_clean_up;
1411 }
1412
1413 aws_byte_buf_clean_up(&request_path_buf);
1414 return AWS_OP_SUCCESS;
1415
1416 error_clean_up:
1417
1418 aws_byte_buf_clean_up(&request_path_buf);
1419
1420 return AWS_OP_ERR;
1421 }