-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy paths3_meta_request.c
2461 lines (2035 loc) · 100 KB
/
s3_meta_request.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
#include "aws/s3/private/s3_auto_ranged_get.h"
#include "aws/s3/private/s3_auto_ranged_put.h"
#include "aws/s3/private/s3_checksums.h"
#include "aws/s3/private/s3_client_impl.h"
#include "aws/s3/private/s3_meta_request_impl.h"
#include "aws/s3/private/s3_parallel_input_stream.h"
#include "aws/s3/private/s3_request_messages.h"
#include "aws/s3/private/s3_util.h"
#include "aws/s3/s3express_credentials_provider.h"
#include <aws/auth/signable.h>
#include <aws/auth/signing.h>
#include <aws/auth/signing_config.h>
#include <aws/auth/signing_result.h>
#include <aws/common/clock.h>
#include <aws/common/encoding.h>
#include <aws/common/string.h>
#include <aws/common/system_info.h>
#include <aws/io/async_stream.h>
#include <aws/io/event_loop.h>
#include <aws/io/retry_strategy.h>
#include <aws/io/socket.h>
#include <aws/io/stream.h>
#include <inttypes.h>
static const size_t s_dynamic_body_initial_buf_size = KB_TO_BYTES(1);
static const size_t s_default_body_streaming_priority_queue_size = 16;
static const size_t s_default_event_delivery_array_size = 16;
static int s_s3_request_priority_queue_pred(const void *a, const void *b);
static void s_s3_meta_request_destroy(void *user_data);
static void s_s3_meta_request_init_signing_date_time(
struct aws_s3_meta_request *meta_request,
struct aws_date_time *date_time);
static void s_s3_meta_request_sign_request(
struct aws_s3_meta_request *meta_request,
struct aws_s3_request *request,
aws_signing_complete_fn *on_signing_complete,
void *user_data);
static void s_s3_meta_request_request_on_signed(
struct aws_signing_result *signing_result,
int error_code,
void *user_data);
static int s_s3_meta_request_incoming_body(
struct aws_http_stream *stream,
const struct aws_byte_cursor *data,
void *user_data);
static int s_s3_meta_request_incoming_headers(
struct aws_http_stream *stream,
enum aws_http_header_block header_block,
const struct aws_http_header *headers,
size_t headers_count,
void *user_data);
static int s_s3_meta_request_headers_block_done(
struct aws_http_stream *stream,
enum aws_http_header_block header_block,
void *user_data);
static void s_s3_meta_request_stream_metrics(
struct aws_http_stream *stream,
const struct aws_http_stream_metrics *metrics,
void *user_data);
static void s_s3_meta_request_stream_complete(struct aws_http_stream *stream, int error_code, void *user_data);
static void s_s3_meta_request_send_request_finish(
struct aws_s3_connection *connection,
struct aws_http_stream *stream,
int error_code);
static bool s_s3_meta_request_read_from_pending_async_writes(
struct aws_s3_meta_request *meta_request,
struct aws_byte_buf *dest);
void aws_s3_meta_request_lock_synced_data(struct aws_s3_meta_request *meta_request) {
AWS_PRECONDITION(meta_request);
aws_mutex_lock(&meta_request->synced_data.lock);
}
void aws_s3_meta_request_unlock_synced_data(struct aws_s3_meta_request *meta_request) {
AWS_PRECONDITION(meta_request);
aws_mutex_unlock(&meta_request->synced_data.lock);
}
static int s_meta_request_get_response_headers_checksum_callback(
struct aws_s3_meta_request *meta_request,
const struct aws_http_headers *headers,
int response_status,
void *user_data) {
for (int i = AWS_SCA_INIT; i <= AWS_SCA_END; i++) {
if (!aws_s3_meta_request_checksum_config_has_algorithm(meta_request, i)) {
/* If user doesn't select this algorithm, skip */
continue;
}
const struct aws_byte_cursor *algorithm_header_name = aws_get_http_header_name_from_algorithm(i);
if (aws_http_headers_has(headers, *algorithm_header_name) &&
!aws_http_headers_has(headers, g_mp_parts_count_header_name)) {
struct aws_byte_cursor header_sum;
aws_http_headers_get(headers, *algorithm_header_name, &header_sum);
size_t encoded_len = 0;
aws_base64_compute_encoded_len(aws_get_digest_size_from_algorithm(i), &encoded_len);
if (header_sum.len == encoded_len - 1) {
/* encoded_len includes the nullptr length. -1 is the expected length. */
aws_byte_buf_init_copy_from_cursor(
&meta_request->meta_request_level_response_header_checksum, meta_request->allocator, header_sum);
meta_request->meta_request_level_running_response_sum = aws_checksum_new(meta_request->allocator, i);
}
break;
}
}
if (meta_request->headers_user_callback_after_checksum) {
return meta_request->headers_user_callback_after_checksum(meta_request, headers, response_status, user_data);
} else {
return AWS_OP_SUCCESS;
}
}
/* warning this might get screwed up with retries/restarts */
static int s_meta_request_get_response_body_checksum_callback(
struct aws_s3_meta_request *meta_request,
const struct aws_byte_cursor *body,
uint64_t range_start,
void *user_data) {
if (meta_request->meta_request_level_running_response_sum) {
aws_checksum_update(meta_request->meta_request_level_running_response_sum, body);
}
if (meta_request->body_user_callback_after_checksum) {
return meta_request->body_user_callback_after_checksum(meta_request, body, range_start, user_data);
} else {
return AWS_OP_SUCCESS;
}
}
static void s_meta_request_get_response_finish_checksum_callback(
struct aws_s3_meta_request *meta_request,
const struct aws_s3_meta_request_result *meta_request_result,
void *user_data) {
struct aws_byte_buf response_body_sum;
struct aws_byte_buf encoded_response_body_sum;
AWS_ZERO_STRUCT(response_body_sum);
AWS_ZERO_STRUCT(encoded_response_body_sum);
struct aws_s3_meta_request_result *mut_meta_request_result =
(struct aws_s3_meta_request_result *)meta_request_result;
if (meta_request_result->error_code == AWS_OP_SUCCESS && meta_request->meta_request_level_running_response_sum) {
mut_meta_request_result->did_validate = true;
mut_meta_request_result->validation_algorithm =
meta_request->meta_request_level_running_response_sum->algorithm;
size_t encoded_checksum_len = 0;
/* what error should I raise for these? */
aws_base64_compute_encoded_len(
meta_request->meta_request_level_running_response_sum->digest_size, &encoded_checksum_len);
aws_byte_buf_init(&encoded_response_body_sum, meta_request->allocator, encoded_checksum_len);
aws_byte_buf_init(
&response_body_sum,
meta_request->allocator,
meta_request->meta_request_level_running_response_sum->digest_size);
aws_checksum_finalize(meta_request->meta_request_level_running_response_sum, &response_body_sum, 0);
struct aws_byte_cursor response_body_sum_cursor = aws_byte_cursor_from_buf(&response_body_sum);
aws_base64_encode(&response_body_sum_cursor, &encoded_response_body_sum);
if (!aws_byte_buf_eq(&encoded_response_body_sum, &meta_request->meta_request_level_response_header_checksum)) {
mut_meta_request_result->error_code = AWS_ERROR_S3_RESPONSE_CHECKSUM_MISMATCH;
}
}
if (meta_request->finish_user_callback_after_checksum) {
meta_request->finish_user_callback_after_checksum(meta_request, meta_request_result, user_data);
}
aws_byte_buf_clean_up(&response_body_sum);
aws_byte_buf_clean_up(&encoded_response_body_sum);
aws_checksum_destroy(meta_request->meta_request_level_running_response_sum);
aws_byte_buf_clean_up(&meta_request->meta_request_level_response_header_checksum);
}
int aws_s3_meta_request_init_base(
struct aws_allocator *allocator,
struct aws_s3_client *client,
size_t part_size,
bool should_compute_content_md5,
const struct aws_s3_meta_request_options *options,
void *impl,
struct aws_s3_meta_request_vtable *vtable,
struct aws_s3_meta_request *meta_request) {
AWS_PRECONDITION(allocator);
AWS_PRECONDITION(options);
AWS_PRECONDITION(options->message);
AWS_PRECONDITION(impl);
AWS_PRECONDITION(meta_request);
AWS_ZERO_STRUCT(*meta_request);
AWS_ASSERT(vtable->update);
AWS_ASSERT(vtable->prepare_request);
AWS_ASSERT(vtable->destroy);
AWS_ASSERT(vtable->sign_request);
AWS_ASSERT(vtable->init_signing_date_time);
AWS_ASSERT(vtable->finished_request);
AWS_ASSERT(vtable->send_request_finish);
meta_request->allocator = allocator;
meta_request->type = options->type;
/* Set up reference count. */
aws_ref_count_init(&meta_request->ref_count, meta_request, s_s3_meta_request_destroy);
aws_linked_list_init(&meta_request->synced_data.cancellable_http_streams_list);
if (part_size == SIZE_MAX) {
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
goto error;
}
if (aws_mutex_init(&meta_request->synced_data.lock)) {
AWS_LOGF_ERROR(
AWS_LS_S3_META_REQUEST, "id=%p Could not initialize mutex for meta request", (void *)meta_request);
goto error;
}
if (aws_priority_queue_init_dynamic(
&meta_request->synced_data.pending_body_streaming_requests,
meta_request->allocator,
s_default_body_streaming_priority_queue_size,
sizeof(struct aws_s3_request *),
s_s3_request_priority_queue_pred)) {
AWS_LOGF_ERROR(
AWS_LS_S3_META_REQUEST, "id=%p Could not initialize priority queue for meta request", (void *)meta_request);
/* Priority queue */
goto error;
}
aws_array_list_init_dynamic(
&meta_request->synced_data.event_delivery_array,
meta_request->allocator,
s_default_event_delivery_array_size,
sizeof(struct aws_s3_meta_request_event));
aws_array_list_init_dynamic(
&meta_request->io_threaded_data.event_delivery_array,
meta_request->allocator,
s_default_event_delivery_array_size,
sizeof(struct aws_s3_meta_request_event));
*((size_t *)&meta_request->part_size) = part_size;
*((bool *)&meta_request->should_compute_content_md5) = should_compute_content_md5;
checksum_config_init(&meta_request->checksum_config, options->checksum_config);
if (options->signing_config) {
meta_request->cached_signing_config = aws_cached_signing_config_new(client, options->signing_config);
}
/* Client is currently optional to allow spinning up a meta_request without a client in a test. */
if (client != NULL) {
meta_request->client = aws_s3_client_acquire(client);
meta_request->io_event_loop = aws_event_loop_group_get_next_loop(client->body_streaming_elg);
meta_request->synced_data.read_window_running_total = client->initial_read_window;
}
/* Keep original message around, for headers, method, and synchronous body-stream (if any) */
meta_request->initial_request_message = aws_http_message_acquire(options->message);
/* If the request's body is being passed in some other way, set that up.
* (we checked earlier that the request body is not being passed multiple ways) */
if (options->send_filepath.len > 0) {
/* Create parallel read stream from file */
meta_request->request_body_parallel_stream =
client->vtable->parallel_input_stream_new_from_file(allocator, options->send_filepath);
if (meta_request->request_body_parallel_stream == NULL) {
goto error;
}
} else if (options->send_async_stream != NULL) {
meta_request->request_body_async_stream = aws_async_input_stream_acquire(options->send_async_stream);
} else if (options->send_using_async_writes == true) {
meta_request->request_body_using_async_writes = true;
}
meta_request->synced_data.next_streaming_part = 1;
meta_request->meta_request_level_running_response_sum = NULL;
meta_request->user_data = options->user_data;
meta_request->shutdown_callback = options->shutdown_callback;
meta_request->progress_callback = options->progress_callback;
meta_request->telemetry_callback = options->telemetry_callback;
meta_request->upload_review_callback = options->upload_review_callback;
if (meta_request->checksum_config.validate_response_checksum) {
/* TODO: the validate for auto range get should happen for each response received. */
meta_request->headers_user_callback_after_checksum = options->headers_callback;
meta_request->body_user_callback_after_checksum = options->body_callback;
meta_request->finish_user_callback_after_checksum = options->finish_callback;
meta_request->headers_callback = s_meta_request_get_response_headers_checksum_callback;
meta_request->body_callback = s_meta_request_get_response_body_checksum_callback;
meta_request->finish_callback = s_meta_request_get_response_finish_checksum_callback;
} else {
meta_request->headers_callback = options->headers_callback;
meta_request->body_callback = options->body_callback;
meta_request->finish_callback = options->finish_callback;
}
/* Nothing can fail after here. Leave the impl not affected by failure of initializing base. */
meta_request->impl = impl;
meta_request->vtable = vtable;
return AWS_OP_SUCCESS;
error:
s_s3_meta_request_destroy((void *)meta_request);
return AWS_OP_ERR;
}
void aws_s3_meta_request_increment_read_window(struct aws_s3_meta_request *meta_request, uint64_t bytes) {
AWS_PRECONDITION(meta_request);
if (bytes == 0) {
return;
}
if (!meta_request->client->enable_read_backpressure) {
AWS_LOGF_DEBUG(
AWS_LS_S3_META_REQUEST,
"id=%p: Ignoring call to increment read window. This client has not enabled read backpressure.",
(void *)meta_request);
return;
}
AWS_LOGF_TRACE(AWS_LS_S3_META_REQUEST, "id=%p: Incrementing read window by %" PRIu64, (void *)meta_request, bytes);
/* BEGIN CRITICAL SECTION */
aws_s3_meta_request_lock_synced_data(meta_request);
/* Response will never approach UINT64_MAX, so do a saturating sum instead of worrying about overflow */
meta_request->synced_data.read_window_running_total =
aws_add_u64_saturating(bytes, meta_request->synced_data.read_window_running_total);
aws_s3_meta_request_unlock_synced_data(meta_request);
/* END CRITICAL SECTION */
/* Schedule the work task, to continue processing the meta-request */
aws_s3_client_schedule_process_work(meta_request->client);
}
void aws_s3_meta_request_cancel(struct aws_s3_meta_request *meta_request) {
/* BEGIN CRITICAL SECTION */
aws_s3_meta_request_lock_synced_data(meta_request);
aws_s3_meta_request_set_fail_synced(meta_request, NULL, AWS_ERROR_S3_CANCELED);
aws_s3_meta_request_cancel_cancellable_requests_synced(meta_request, AWS_ERROR_S3_CANCELED);
aws_s3_meta_request_unlock_synced_data(meta_request);
/* END CRITICAL SECTION */
/* Schedule the work task, to continue processing the meta-request */
aws_s3_client_schedule_process_work(meta_request->client);
}
int aws_s3_meta_request_pause(
struct aws_s3_meta_request *meta_request,
struct aws_s3_meta_request_resume_token **out_resume_token) {
AWS_PRECONDITION(meta_request);
AWS_PRECONDITION(meta_request->vtable);
*out_resume_token = NULL;
if (!meta_request->vtable->pause) {
return aws_raise_error(AWS_ERROR_UNSUPPORTED_OPERATION);
}
return meta_request->vtable->pause(meta_request, out_resume_token);
}
void aws_s3_meta_request_set_fail_synced(
struct aws_s3_meta_request *meta_request,
struct aws_s3_request *failed_request,
int error_code) {
AWS_PRECONDITION(meta_request);
ASSERT_SYNCED_DATA_LOCK_HELD(meta_request);
/* Protect against bugs */
if (error_code == AWS_ERROR_SUCCESS) {
AWS_ASSERT(false);
AWS_LOGF_ERROR(
AWS_LS_S3_META_REQUEST,
"id=%p Meta request failed but error code not set, AWS_ERROR_UNKNOWN will be reported",
(void *)meta_request);
error_code = AWS_ERROR_UNKNOWN;
}
if (meta_request->synced_data.finish_result_set) {
return;
}
meta_request->synced_data.finish_result_set = true;
if ((error_code == AWS_ERROR_S3_INVALID_RESPONSE_STATUS || error_code == AWS_ERROR_S3_NON_RECOVERABLE_ASYNC_ERROR ||
error_code == AWS_ERROR_S3_OBJECT_MODIFIED) &&
failed_request != NULL) {
aws_s3_meta_request_result_setup(
meta_request,
&meta_request->synced_data.finish_result,
failed_request,
failed_request->send_data.response_status,
error_code);
} else {
AWS_ASSERT(error_code != AWS_ERROR_S3_INVALID_RESPONSE_STATUS);
aws_s3_meta_request_result_setup(meta_request, &meta_request->synced_data.finish_result, NULL, 0, error_code);
}
}
void aws_s3_meta_request_set_success_synced(struct aws_s3_meta_request *meta_request, int response_status) {
AWS_PRECONDITION(meta_request);
ASSERT_SYNCED_DATA_LOCK_HELD(meta_request);
if (meta_request->synced_data.finish_result_set) {
return;
}
meta_request->synced_data.finish_result_set = true;
aws_s3_meta_request_result_setup(
meta_request, &meta_request->synced_data.finish_result, NULL, response_status, AWS_ERROR_SUCCESS);
}
bool aws_s3_meta_request_has_finish_result(struct aws_s3_meta_request *meta_request) {
AWS_PRECONDITION(meta_request);
/* BEGIN CRITICAL SECTION */
aws_s3_meta_request_lock_synced_data(meta_request);
bool is_finishing = aws_s3_meta_request_has_finish_result_synced(meta_request);
aws_s3_meta_request_unlock_synced_data(meta_request);
/* END CRITICAL SECTION */
return is_finishing;
}
bool aws_s3_meta_request_has_finish_result_synced(struct aws_s3_meta_request *meta_request) {
AWS_PRECONDITION(meta_request);
ASSERT_SYNCED_DATA_LOCK_HELD(meta_request);
if (!meta_request->synced_data.finish_result_set) {
return false;
}
return true;
}
struct aws_s3_meta_request *aws_s3_meta_request_acquire(struct aws_s3_meta_request *meta_request) {
AWS_PRECONDITION(meta_request);
aws_ref_count_acquire(&meta_request->ref_count);
return meta_request;
}
struct aws_s3_meta_request *aws_s3_meta_request_release(struct aws_s3_meta_request *meta_request) {
if (meta_request != NULL) {
aws_ref_count_release(&meta_request->ref_count);
}
return NULL;
}
static void s_s3_meta_request_destroy(void *user_data) {
struct aws_s3_meta_request *meta_request = user_data;
AWS_PRECONDITION(meta_request);
void *log_id = meta_request;
AWS_LOGF_DEBUG(AWS_LS_S3_META_REQUEST, "id=%p Cleaning up meta request", (void *)meta_request);
/* Clean up our initial http message */
meta_request->request_body_async_stream = aws_async_input_stream_release(meta_request->request_body_async_stream);
meta_request->initial_request_message = aws_http_message_release(meta_request->initial_request_message);
void *meta_request_user_data = meta_request->user_data;
aws_s3_meta_request_shutdown_fn *shutdown_callback = meta_request->shutdown_callback;
aws_cached_signing_config_destroy(meta_request->cached_signing_config);
aws_string_destroy(meta_request->s3express_session_host);
aws_mutex_clean_up(&meta_request->synced_data.lock);
/* endpoint should have already been released and set NULL by the meta request finish call.
* But call release() again, just in case we're tearing down a half-initialized meta request */
aws_s3_endpoint_release(meta_request->endpoint);
/* Client may be NULL if meta request failed mid-creation (or this some weird testing mock with no client) */
if (meta_request->client != NULL) {
aws_s3_buffer_pool_release_ticket(
meta_request->client->buffer_pool, meta_request->synced_data.async_write.buffered_data_ticket);
meta_request->client = aws_s3_client_release(meta_request->client);
}
AWS_ASSERT(aws_priority_queue_size(&meta_request->synced_data.pending_body_streaming_requests) == 0);
aws_priority_queue_clean_up(&meta_request->synced_data.pending_body_streaming_requests);
AWS_ASSERT(aws_array_list_length(&meta_request->synced_data.event_delivery_array) == 0);
aws_array_list_clean_up(&meta_request->synced_data.event_delivery_array);
AWS_ASSERT(aws_array_list_length(&meta_request->io_threaded_data.event_delivery_array) == 0);
aws_array_list_clean_up(&meta_request->io_threaded_data.event_delivery_array);
AWS_ASSERT(aws_linked_list_empty(&meta_request->synced_data.cancellable_http_streams_list));
aws_s3_meta_request_result_clean_up(meta_request, &meta_request->synced_data.finish_result);
if (meta_request->vtable != NULL) {
AWS_LOGF_TRACE(AWS_LS_S3_META_REQUEST, "id=%p Calling virtual meta request destroy function.", log_id);
meta_request->vtable->destroy(meta_request);
}
meta_request = NULL;
if (shutdown_callback != NULL) {
AWS_LOGF_TRACE(AWS_LS_S3_META_REQUEST, "id=%p Calling meta request shutdown callback.", log_id);
shutdown_callback(meta_request_user_data);
}
AWS_LOGF_TRACE(AWS_LS_S3_META_REQUEST, "id=%p Meta request clean up finished.", log_id);
}
static int s_s3_request_priority_queue_pred(const void *a, const void *b) {
const struct aws_s3_request *const *request_a = a;
AWS_PRECONDITION(request_a);
AWS_PRECONDITION(*request_a);
const struct aws_s3_request *const *request_b = b;
AWS_PRECONDITION(request_b);
AWS_PRECONDITION(*request_b);
return (*request_a)->part_number > (*request_b)->part_number;
}
bool aws_s3_meta_request_update(
struct aws_s3_meta_request *meta_request,
uint32_t flags,
struct aws_s3_request **out_request) {
AWS_PRECONDITION(meta_request);
AWS_PRECONDITION(meta_request->vtable);
AWS_PRECONDITION(meta_request->vtable->update);
return meta_request->vtable->update(meta_request, flags, out_request);
}
bool aws_s3_meta_request_is_active(struct aws_s3_meta_request *meta_request) {
AWS_PRECONDITION(meta_request);
/* BEGIN CRITICAL SECTION */
aws_s3_meta_request_lock_synced_data(meta_request);
bool active = meta_request->synced_data.state == AWS_S3_META_REQUEST_STATE_ACTIVE;
aws_s3_meta_request_unlock_synced_data(meta_request);
/* END CRITICAL SECTION */
return active;
}
bool aws_s3_meta_request_is_finished(struct aws_s3_meta_request *meta_request) {
AWS_PRECONDITION(meta_request);
/* BEGIN CRITICAL SECTION */
aws_s3_meta_request_lock_synced_data(meta_request);
bool is_finished = meta_request->synced_data.state == AWS_S3_META_REQUEST_STATE_FINISHED;
aws_s3_meta_request_unlock_synced_data(meta_request);
/* END CRITICAL SECTION */
return is_finished;
}
static void s_s3_meta_request_prepare_request_task(struct aws_task *task, void *arg, enum aws_task_status task_status);
static void s_s3_meta_request_on_request_prepared(void *user_data);
/* TODO: document how this is final step in prepare-request sequence.
* Could be invoked on any thread. */
static void s_s3_prepare_request_payload_callback_and_destroy(
struct aws_s3_prepare_request_payload *payload,
int error_code) {
AWS_PRECONDITION(payload);
AWS_PRECONDITION(payload->request);
struct aws_s3_meta_request *meta_request = payload->request->meta_request;
AWS_PRECONDITION(meta_request);
++payload->request->num_times_prepared;
if (error_code) {
AWS_LOGF_ERROR(
AWS_LS_S3_META_REQUEST,
"id=%p Could not prepare request %p due to error %d (%s).",
(void *)meta_request,
(void *)payload->request,
error_code,
aws_error_str(error_code));
/* BEGIN CRITICAL SECTION */
aws_s3_meta_request_lock_synced_data(meta_request);
aws_s3_meta_request_set_fail_synced(meta_request, payload->request, error_code);
aws_s3_meta_request_unlock_synced_data(meta_request);
/* END CRITICAL SECTION */
}
if (payload->callback != NULL) {
payload->callback(meta_request, payload->request, error_code, payload->user_data);
}
aws_future_void_release(payload->asyncstep_prepare_request);
aws_mem_release(payload->allocator, payload);
}
static void s_s3_meta_request_schedule_prepare_request_default(
struct aws_s3_meta_request *meta_request,
struct aws_s3_request *request,
aws_s3_meta_request_prepare_request_callback_fn *callback,
void *user_data);
void aws_s3_meta_request_prepare_request(
struct aws_s3_meta_request *meta_request,
struct aws_s3_request *request,
aws_s3_meta_request_prepare_request_callback_fn *callback,
void *user_data) {
AWS_PRECONDITION(meta_request);
AWS_PRECONDITION(meta_request->vtable);
if (meta_request->vtable->schedule_prepare_request) {
meta_request->vtable->schedule_prepare_request(meta_request, request, callback, user_data);
} else {
s_s3_meta_request_schedule_prepare_request_default(meta_request, request, callback, user_data);
}
}
static void s_s3_meta_request_schedule_prepare_request_default(
struct aws_s3_meta_request *meta_request,
struct aws_s3_request *request,
aws_s3_meta_request_prepare_request_callback_fn *callback,
void *user_data) {
AWS_PRECONDITION(meta_request);
AWS_PRECONDITION(request);
struct aws_s3_client *client = meta_request->client;
AWS_PRECONDITION(client);
struct aws_allocator *allocator = client->allocator;
AWS_PRECONDITION(allocator);
struct aws_s3_prepare_request_payload *payload =
aws_mem_calloc(allocator, 1, sizeof(struct aws_s3_prepare_request_payload));
payload->allocator = allocator;
payload->request = request;
payload->callback = callback;
payload->user_data = user_data;
aws_task_init(
&payload->task, s_s3_meta_request_prepare_request_task, payload, "s3_meta_request_prepare_request_task");
if (meta_request->request_body_parallel_stream) {
/* The body stream supports reading in parallel, so schedule task on any I/O thread.
* If we always used the meta-request's dedicated io_event_loop, we wouldn't get any parallelism. */
struct aws_event_loop *loop = aws_event_loop_group_get_next_loop(client->body_streaming_elg);
aws_event_loop_schedule_task_now(loop, &payload->task);
} else {
aws_event_loop_schedule_task_now(meta_request->io_event_loop, &payload->task);
}
}
static void s_s3_meta_request_prepare_request_task(struct aws_task *task, void *arg, enum aws_task_status task_status) {
(void)task;
(void)task_status;
struct aws_s3_prepare_request_payload *payload = arg;
AWS_PRECONDITION(payload);
struct aws_s3_request *request = payload->request;
AWS_PRECONDITION(request);
struct aws_s3_meta_request *meta_request = request->meta_request;
AWS_PRECONDITION(meta_request);
const struct aws_s3_meta_request_vtable *vtable = meta_request->vtable;
AWS_PRECONDITION(vtable);
/* Client owns this event loop group. A cancel should not be possible. */
AWS_ASSERT(task_status == AWS_TASK_STATUS_RUN_READY);
if (!request->always_send && aws_s3_meta_request_has_finish_result(meta_request)) {
s_s3_prepare_request_payload_callback_and_destroy(payload, AWS_ERROR_S3_CANCELED);
return;
}
/* Kick off the async vtable->prepare_request()
* Each subclass has its own implementation of this. */
payload->asyncstep_prepare_request = vtable->prepare_request(request);
aws_future_void_register_callback(
payload->asyncstep_prepare_request, s_s3_meta_request_on_request_prepared, payload);
return;
}
/* Called after vtable->prepare_request has succeeded or failed. */
static void s_s3_meta_request_on_request_prepared(void *user_data) {
struct aws_s3_prepare_request_payload *payload = user_data;
struct aws_s3_request *request = payload->request;
struct aws_s3_meta_request *meta_request = request->meta_request;
int error_code = aws_future_void_get_error(payload->asyncstep_prepare_request);
if (error_code) {
s_s3_prepare_request_payload_callback_and_destroy(payload, error_code);
return;
}
aws_s3_add_user_agent_header(meta_request->allocator, request->send_data.message);
/* Next step is to sign the newly created message (completion callback could happen on any thread) */
s_s3_meta_request_sign_request(meta_request, request, s_s3_meta_request_request_on_signed, payload);
}
static void s_s3_meta_request_init_signing_date_time(
struct aws_s3_meta_request *meta_request,
struct aws_date_time *date_time) {
AWS_PRECONDITION(meta_request);
AWS_PRECONDITION(meta_request->vtable);
AWS_PRECONDITION(meta_request->vtable->init_signing_date_time);
meta_request->vtable->init_signing_date_time(meta_request, date_time);
}
void aws_s3_meta_request_init_signing_date_time_default(
struct aws_s3_meta_request *meta_request,
struct aws_date_time *date_time) {
AWS_PRECONDITION(meta_request);
AWS_PRECONDITION(date_time);
(void)meta_request;
aws_date_time_init_now(date_time);
}
static void s_s3_meta_request_sign_request(
struct aws_s3_meta_request *meta_request,
struct aws_s3_request *request,
aws_signing_complete_fn *on_signing_complete,
void *user_data) {
AWS_PRECONDITION(meta_request);
AWS_PRECONDITION(meta_request->vtable);
AWS_PRECONDITION(meta_request->vtable->sign_request);
if (request->send_data.metrics) {
struct aws_s3_request_metrics *metric = request->send_data.metrics;
aws_high_res_clock_get_ticks((uint64_t *)&metric->time_metrics.sign_start_timestamp_ns);
}
meta_request->vtable->sign_request(meta_request, request, on_signing_complete, user_data);
}
struct aws_get_s3express_credentials_user_data {
/* Keep our own reference to allocator, because the meta request can be gone after the callback invoked. */
struct aws_allocator *allocator;
struct aws_s3_meta_request *meta_request;
struct aws_s3_request *request;
aws_signing_complete_fn *on_signing_complete;
const struct aws_credentials *original_credentials;
struct aws_signing_config_aws base_signing_config;
struct aws_credentials_properties_s3express properties;
void *user_data;
};
static void s_aws_get_s3express_credentials_user_data_destroy(struct aws_get_s3express_credentials_user_data *context) {
aws_s3_meta_request_release(context->meta_request);
aws_credentials_release(context->original_credentials);
aws_mem_release(context->allocator, context);
}
static void s_get_s3express_credentials_callback(struct aws_credentials *credentials, int error_code, void *user_data) {
struct aws_get_s3express_credentials_user_data *context = user_data;
struct aws_signing_config_aws signing_config = context->base_signing_config;
if (error_code) {
AWS_LOGF_ERROR(
AWS_LS_S3_META_REQUEST,
"id=%p: Failed to get S3 Express credentials %p. due to error code %d (%s)",
(void *)context->meta_request,
(void *)context->request,
error_code,
aws_error_str(error_code));
context->on_signing_complete(NULL, error_code, context->user_data);
goto done;
}
s_s3_meta_request_init_signing_date_time(context->meta_request, &signing_config.date);
/* Override the credentials */
signing_config.credentials = credentials;
signing_config.algorithm = AWS_SIGNING_ALGORITHM_V4_S3EXPRESS;
if (aws_sign_request_aws(
context->allocator,
context->request->send_data.signable,
(struct aws_signing_config_base *)&signing_config,
context->on_signing_complete,
context->user_data)) {
AWS_LOGF_ERROR(
AWS_LS_S3_META_REQUEST,
"id=%p: Could not sign request %p. due to error code %d (%s)",
(void *)context->meta_request,
(void *)context->request,
aws_last_error_or_unknown(),
aws_error_str(aws_last_error_or_unknown()));
context->on_signing_complete(NULL, aws_last_error_or_unknown(), context->user_data);
}
done:
s_aws_get_s3express_credentials_user_data_destroy(context);
}
static void s_get_original_credentials_callback(struct aws_credentials *credentials, int error_code, void *user_data) {
struct aws_get_s3express_credentials_user_data *context = user_data;
struct aws_s3_meta_request *meta_request = context->meta_request;
if (error_code) {
AWS_LOGF_ERROR(
AWS_LS_S3_META_REQUEST,
"id=%p: Failed to get S3 Express credentials %p. due to error code %d (%s)",
(void *)context->meta_request,
(void *)context->request,
error_code,
aws_error_str(error_code));
context->on_signing_complete(NULL, error_code, context->user_data);
s_aws_get_s3express_credentials_user_data_destroy(context);
return;
}
context->original_credentials = credentials;
aws_credentials_acquire(context->original_credentials);
/**
* Derive the credentials for S3 Express.
*/
struct aws_s3_client *client = meta_request->client;
if (aws_s3express_credentials_provider_get_credentials(
client->s3express_provider,
context->original_credentials,
&context->properties,
s_get_s3express_credentials_callback,
context)) {
AWS_LOGF_ERROR(
AWS_LS_S3_META_REQUEST,
"id=%p: Could not get S3 Express credentials %p",
(void *)meta_request,
(void *)context->request);
context->on_signing_complete(NULL, aws_last_error_or_unknown(), user_data);
s_aws_get_s3express_credentials_user_data_destroy(context);
}
}
static int s_meta_request_resolve_signing_config(
struct aws_signing_config_aws *out_signing_config,
struct aws_s3_request *request,
struct aws_s3_meta_request *meta_request) {
struct aws_s3_client *client = meta_request->client;
if (meta_request->cached_signing_config != NULL) {
*out_signing_config = meta_request->cached_signing_config->config;
if (out_signing_config->credentials == NULL && out_signing_config->credentials_provider == NULL) {
/* When no credentials available from meta request level override, we use the credentials from client */
out_signing_config->credentials = client->cached_signing_config->config.credentials;
out_signing_config->credentials_provider = client->cached_signing_config->config.credentials_provider;
}
} else if (client->cached_signing_config != NULL) {
*out_signing_config = client->cached_signing_config->config;
} else {
/* Not possible to have no cached signing config from both client and request */
AWS_FATAL_ASSERT(false);
}
/* If the checksum is configured to be added to the trailer, the payload will be aws-chunked encoded. The payload
* will need to be streaming signed/unsigned. */
if (meta_request->checksum_config.location == AWS_SCL_TRAILER &&
aws_byte_cursor_eq(&out_signing_config->signed_body_value, &g_aws_signed_body_value_unsigned_payload)) {
out_signing_config->signed_body_value = g_aws_signed_body_value_streaming_unsigned_payload_trailer;
}
/* However the initial request for a multipart upload does not have a trailing checksum and is not chunked so it
* must have an unsigned_payload signed_body value*/
if (request->part_number == 0 &&
aws_byte_cursor_eq(
&out_signing_config->signed_body_value, &g_aws_signed_body_value_streaming_unsigned_payload_trailer)) {
out_signing_config->signed_body_value = g_aws_signed_body_value_unsigned_payload;
}
return AWS_OP_SUCCESS;
}
/* Handles signing a message for the caller. */
void aws_s3_meta_request_sign_request_default(
struct aws_s3_meta_request *meta_request,
struct aws_s3_request *request,
aws_signing_complete_fn *on_signing_complete,
void *user_data) {
AWS_PRECONDITION(meta_request);
AWS_PRECONDITION(request);
AWS_PRECONDITION(on_signing_complete);
struct aws_s3_client *client = meta_request->client;
AWS_ASSERT(client);
struct aws_signing_config_aws signing_config;
if (s_meta_request_resolve_signing_config(&signing_config, request, meta_request)) {
AWS_LOGF_DEBUG(
AWS_LS_S3_META_REQUEST,
"id=%p: No signing config present. Not signing request %p.",
(void *)meta_request,
(void *)request);
on_signing_complete(NULL, AWS_ERROR_SUCCESS, user_data);
return;
}
request->send_data.signable = aws_signable_new_http_request(meta_request->allocator, request->send_data.message);
AWS_LOGF_TRACE(
AWS_LS_S3_META_REQUEST,
"id=%p Created signable %p for request %p with message %p",
(void *)meta_request,
(void *)request->send_data.signable,
(void *)request,
(void *)request->send_data.message);
if (request->send_data.signable == NULL) {
AWS_LOGF_ERROR(
AWS_LS_S3_META_REQUEST,
"id=%p: Could not allocate signable for request %p",
(void *)meta_request,
(void *)request);
on_signing_complete(NULL, aws_last_error_or_unknown(), user_data);
return;
}
if (signing_config.algorithm == AWS_SIGNING_ALGORITHM_V4_S3EXPRESS) {
/* Fetch credentials from S3 Express provider. */
struct aws_get_s3express_credentials_user_data *context =
aws_mem_calloc(meta_request->allocator, 1, sizeof(struct aws_get_s3express_credentials_user_data));
context->allocator = meta_request->allocator;
context->base_signing_config = signing_config;
context->meta_request = aws_s3_meta_request_acquire(meta_request);
context->on_signing_complete = on_signing_complete;
context->request = request;
context->user_data = user_data;
context->properties.host = aws_byte_cursor_from_string(meta_request->s3express_session_host);
context->properties.region = signing_config.region;
if (signing_config.credentials) {
context->original_credentials = signing_config.credentials;
aws_credentials_acquire(context->original_credentials);
/**
* Derive the credentials for S3 Express.
*/
if (aws_s3express_credentials_provider_get_credentials(
client->s3express_provider,
context->original_credentials,
&context->properties,
s_get_s3express_credentials_callback,
context)) {
AWS_LOGF_ERROR(
AWS_LS_S3_META_REQUEST,
"id=%p: Could not get S3 Express credentials %p",
(void *)meta_request,
(void *)request);
on_signing_complete(NULL, aws_last_error_or_unknown(), user_data);
s_aws_get_s3express_credentials_user_data_destroy(context);
return;
}
} else if (signing_config.credentials_provider) {
/* Get the credentials from provider first. */
if (aws_credentials_provider_get_credentials(
signing_config.credentials_provider, s_get_original_credentials_callback, context)) {
AWS_LOGF_ERROR(
AWS_LS_S3_META_REQUEST,
"id=%p: Could not get S3 Express credentials %p",
(void *)meta_request,
(void *)request);
on_signing_complete(NULL, aws_last_error_or_unknown(), user_data);
s_aws_get_s3express_credentials_user_data_destroy(context);
return;
}
}
} else {
/* Regular signing. */
s_s3_meta_request_init_signing_date_time(meta_request, &signing_config.date);
if (aws_sign_request_aws(
meta_request->allocator,
request->send_data.signable,
(struct aws_signing_config_base *)&signing_config,
on_signing_complete,
user_data)) {
AWS_LOGF_ERROR(
AWS_LS_S3_META_REQUEST, "id=%p: Could not sign request %p", (void *)meta_request, (void *)request);
on_signing_complete(NULL, aws_last_error_or_unknown(), user_data);
return;