-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathhttp_request_response.c
More file actions
1213 lines (1001 loc) · 43.5 KB
/
http_request_response.c
File metadata and controls
1213 lines (1001 loc) · 43.5 KB
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 <jni.h>
#include "crt.h"
#include "http_connection_manager.h"
#include "http_request_response.h"
#include "http_request_utils.h"
#include "java_class_ids.h"
#include <aws/common/atomics.h>
#include <aws/common/mutex.h>
#include <aws/http/connection.h>
#include <aws/http/http.h>
#include <aws/http/request_response.h>
#include <aws/io/logging.h>
#include <aws/io/stream.h>
#if _MSC_VER
# pragma warning(disable : 4204) /* non-constant aggregate initializer */
#endif
/* on 32-bit platforms, casting pointers to longs throws a warning we don't need */
#if UINTPTR_MAX == 0xffffffff
# if defined(_MSC_VER)
# pragma warning(push)
# pragma warning(disable : 4305) /* 'type cast': truncation from 'jlong' to 'jni_tls_ctx_options *' */
# else
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wpointer-to-int-cast"
# pragma GCC diagnostic ignored "-Wint-to-pointer-cast"
# endif
#endif
jobject aws_java_http_stream_from_native_new(JNIEnv *env, void *opaque, int version) {
jlong jni_native_ptr = (jlong)opaque;
AWS_ASSERT(jni_native_ptr);
jobject stream = NULL;
switch (version) {
case AWS_HTTP_VERSION_2:
stream = (*env)->NewObject(
env, http2_stream_properties.stream_class, http2_stream_properties.constructor, jni_native_ptr);
break;
case AWS_HTTP_VERSION_1_0:
case AWS_HTTP_VERSION_1_1:
stream = (*env)->NewObject(
env, http_stream_properties.stream_class, http_stream_properties.constructor, jni_native_ptr);
break;
default:
aws_jni_throw_runtime_exception(env, "Unsupported HTTP protocol.");
aws_raise_error(AWS_ERROR_UNIMPLEMENTED);
}
return stream;
}
void aws_java_http_stream_from_native_delete(JNIEnv *env, jobject jHttpStream) {
/* Delete our reference to the HttpStream Object from the JVM. */
(*env)->DeleteGlobalRef(env, jHttpStream);
}
/*******************************************************************************
* http_stream_binding - Jni native represent of the Java HTTP stream object
******************************************************************************/
static void s_http_stream_binding_destroy(JNIEnv *env, struct http_stream_binding *binding) {
if (binding->java_http_stream_base) {
aws_java_http_stream_from_native_delete(env, binding->java_http_stream_base);
}
if (binding->java_http_response_stream_handler != NULL) {
(*env)->DeleteGlobalRef(env, binding->java_http_response_stream_handler);
}
if (binding->native_request) {
aws_http_message_release(binding->native_request);
}
aws_byte_buf_clean_up(&binding->headers_buf);
aws_mem_release(aws_jni_get_allocator(), binding);
}
void *aws_http_stream_binding_acquire(struct http_stream_binding *binding) {
if (binding == NULL) {
return NULL;
}
aws_atomic_fetch_add(&binding->ref, 1);
return binding;
}
void *aws_http_stream_binding_release(JNIEnv *env, struct http_stream_binding *binding) {
if (binding == NULL) {
return NULL;
}
size_t pre_ref = aws_atomic_fetch_sub(&binding->ref, 1);
AWS_ASSERT(pre_ref > 0 && "stream binding refcount has gone negative");
if (pre_ref == 1) {
s_http_stream_binding_destroy(env, binding);
}
return NULL;
}
// If error occurs, A Java exception is thrown and NULL is returned.
struct http_stream_binding *aws_http_stream_binding_new(JNIEnv *env, jobject java_callback_handler) {
struct aws_allocator *allocator = aws_jni_get_allocator();
struct http_stream_binding *binding = aws_mem_calloc(allocator, 1, sizeof(struct http_stream_binding));
AWS_FATAL_ASSERT(binding);
// GetJavaVM() reference doesn't need a NewGlobalRef() call since it's global by default
jint jvmresult = (*env)->GetJavaVM(env, &binding->jvm);
(void)jvmresult;
AWS_FATAL_ASSERT(jvmresult == 0);
binding->java_http_response_stream_handler = (*env)->NewGlobalRef(env, java_callback_handler);
AWS_FATAL_ASSERT(binding->java_http_response_stream_handler);
AWS_FATAL_ASSERT(!aws_byte_buf_init(&binding->headers_buf, allocator, 1024));
aws_atomic_init_int(&binding->ref, 1);
return binding;
}
int aws_java_http_stream_on_incoming_headers_fn(
struct aws_http_stream *stream,
enum aws_http_header_block block_type,
const struct aws_http_header *header_array,
size_t num_headers,
void *user_data) {
(void)block_type;
struct http_stream_binding *binding = (struct http_stream_binding *)user_data;
int resp_status = -1;
int err_code = aws_http_stream_get_incoming_response_status(stream, &resp_status);
if (err_code != AWS_OP_SUCCESS) {
AWS_LOGF_ERROR(AWS_LS_HTTP_STREAM, "id=%p: Invalid Incoming Response Status", (void *)stream);
return AWS_OP_ERR;
}
binding->response_status = resp_status;
if (aws_marshal_http_headers_array_to_dynamic_buffer(&binding->headers_buf, header_array, num_headers)) {
AWS_LOGF_ERROR(
AWS_LS_HTTP_STREAM, "id=%p: Failed to allocate buffer space for incoming headers", (void *)stream);
return AWS_OP_ERR;
}
return AWS_OP_SUCCESS;
}
int aws_java_http_stream_on_incoming_header_block_done_fn(
struct aws_http_stream *stream,
enum aws_http_header_block block_type,
void *user_data) {
(void)stream;
struct http_stream_binding *binding = (struct http_stream_binding *)user_data;
/********** JNI ENV ACQUIRE **********/
struct aws_jvm_env_context jvm_env_context = aws_jni_acquire_thread_env(binding->jvm);
JNIEnv *env = jvm_env_context.env;
if (env == NULL) {
/* If we can't get an environment, then the JVM is probably shutting down. Don't crash. */
return AWS_OP_ERR;
}
int result = AWS_OP_ERR;
jint jni_block_type = block_type;
jobject jni_headers_buf =
aws_jni_direct_byte_buffer_from_raw_ptr(env, binding->headers_buf.buffer, binding->headers_buf.len);
(*env)->CallVoidMethod(
env,
binding->java_http_response_stream_handler,
http_stream_response_handler_properties.onResponseHeaders,
binding->java_http_stream_base,
(jint)binding->response_status,
(jint)block_type,
jni_headers_buf);
if (aws_jni_check_and_clear_exception(env)) {
(*env)->DeleteLocalRef(env, jni_headers_buf);
aws_raise_error(AWS_ERROR_HTTP_CALLBACK_FAILURE);
goto done;
}
/* instead of cleaning it up here, reset it in case another block is encountered */
aws_byte_buf_reset(&binding->headers_buf, false);
(*env)->DeleteLocalRef(env, jni_headers_buf);
(*env)->CallVoidMethod(
env,
binding->java_http_response_stream_handler,
http_stream_response_handler_properties.onResponseHeadersDone,
binding->java_http_stream_base,
jni_block_type);
if (aws_jni_check_and_clear_exception(env)) {
aws_raise_error(AWS_ERROR_HTTP_CALLBACK_FAILURE);
goto done;
}
result = AWS_OP_SUCCESS;
done:
aws_jni_release_thread_env(binding->jvm, &jvm_env_context);
/********** JNI ENV RELEASE **********/
return result;
}
int aws_java_http_stream_on_incoming_body_fn(
struct aws_http_stream *stream,
const struct aws_byte_cursor *data,
void *user_data) {
struct http_stream_binding *binding = (struct http_stream_binding *)user_data;
size_t total_window_increment = 0;
/********** JNI ENV ACQUIRE **********/
struct aws_jvm_env_context jvm_env_context = aws_jni_acquire_thread_env(binding->jvm);
JNIEnv *env = jvm_env_context.env;
if (env == NULL) {
/* If we can't get an environment, then the JVM is probably shutting down. Don't crash. */
return AWS_OP_ERR;
}
int result = AWS_OP_ERR;
jobject jni_payload = aws_jni_direct_byte_buffer_from_raw_ptr(env, data->ptr, data->len);
jint window_increment = (*env)->CallIntMethod(
env,
binding->java_http_response_stream_handler,
http_stream_response_handler_properties.onResponseBody,
binding->java_http_stream_base,
jni_payload);
(*env)->DeleteLocalRef(env, jni_payload);
if (aws_jni_check_and_clear_exception(env)) {
AWS_LOGF_ERROR(AWS_LS_HTTP_STREAM, "id=%p: Received Exception from onResponseBody", (void *)stream);
aws_raise_error(AWS_ERROR_HTTP_CALLBACK_FAILURE);
goto done;
}
if (window_increment < 0) {
AWS_LOGF_ERROR(AWS_LS_HTTP_STREAM, "id=%p: Window Increment from onResponseBody < 0", (void *)stream);
aws_raise_error(AWS_ERROR_HTTP_CALLBACK_FAILURE);
goto done;
}
total_window_increment += window_increment;
if (total_window_increment > 0) {
aws_http_stream_update_window(stream, total_window_increment);
}
result = AWS_OP_SUCCESS;
done:
aws_jni_release_thread_env(binding->jvm, &jvm_env_context);
/********** JNI ENV RELEASE **********/
return result;
}
void aws_java_http_stream_on_stream_complete_fn(struct aws_http_stream *stream, int error_code, void *user_data) {
struct http_stream_binding *binding = (struct http_stream_binding *)user_data;
/********** JNI ENV ACQUIRE **********/
struct aws_jvm_env_context jvm_env_context = aws_jni_acquire_thread_env(binding->jvm);
JNIEnv *env = jvm_env_context.env;
if (env == NULL) {
/* If we can't get an environment, then the JVM is probably shutting down. Don't crash. */
return;
}
/* Don't invoke Java callbacks if Java HttpStream failed to completely setup */
jint jErrorCode = error_code;
(*env)->CallVoidMethod(
env,
binding->java_http_response_stream_handler,
http_stream_response_handler_properties.onResponseComplete,
binding->java_http_stream_base,
jErrorCode);
if (aws_jni_check_and_clear_exception(env)) {
/* Close the Connection if the Java Callback throws an Exception */
aws_http_connection_close(aws_http_stream_get_connection(stream));
}
aws_jni_release_thread_env(binding->jvm, &jvm_env_context);
/********** JNI ENV RELEASE **********/
}
void aws_java_http_stream_on_stream_destroy_fn(void *user_data) {
struct http_stream_binding *binding = (struct http_stream_binding *)user_data;
/********** JNI ENV ACQUIRE **********/
struct aws_jvm_env_context jvm_env_context = aws_jni_acquire_thread_env(binding->jvm);
JNIEnv *env = jvm_env_context.env;
if (env == NULL) {
/* If we can't get an environment, then the JVM is probably shutting down. Don't crash. */
return;
}
/* Native stream destroyed, release the binding. */
JavaVM *jvm = binding->jvm;
aws_http_stream_binding_release(env, binding);
aws_jni_release_thread_env(jvm, &jvm_env_context);
/********** JNI ENV RELEASE **********/
}
void aws_java_http_stream_on_stream_metrics_fn(
struct aws_http_stream *stream,
const struct aws_http_stream_metrics *metrics,
void *user_data) {
struct http_stream_binding *binding = (struct http_stream_binding *)user_data;
/********** JNI ENV ACQUIRE **********/
struct aws_jvm_env_context jvm_env_context = aws_jni_acquire_thread_env(binding->jvm);
JNIEnv *env = jvm_env_context.env;
if (env == NULL) {
/* If we can't get an environment, then the JVM is probably shutting down. Don't crash. */
return;
}
/* Convert metrics to Java HttpStreamMetrics obj */
jobject jni_metrics = (*env)->NewObject(
env,
http_stream_metrics_properties.http_stream_metrics_class,
http_stream_metrics_properties.constructor_id,
(jlong)metrics->send_start_timestamp_ns,
(jlong)metrics->send_end_timestamp_ns,
(jlong)metrics->sending_duration_ns,
(jlong)metrics->receive_start_timestamp_ns,
(jlong)metrics->receive_end_timestamp_ns,
(jlong)metrics->receiving_duration_ns,
/* Stream IDs are 31-bit unsigned integers, which fits into Java's regular (signed) 32-bit int */
(jint)metrics->stream_id);
(*env)->CallVoidMethod(
env,
binding->java_http_response_stream_handler,
http_stream_response_handler_properties.onMetrics,
binding->java_http_stream_base,
jni_metrics);
/* Delete local reference to metrics object */
(*env)->DeleteLocalRef(env, jni_metrics);
if (aws_jni_check_and_clear_exception(env)) {
/* Close the Connection if the Java Callback throws an Exception */
aws_http_connection_close(aws_http_stream_get_connection(stream));
AWS_LOGF_ERROR(AWS_LS_HTTP_STREAM, "id=%p: Received Exception from onMetrics", (void *)stream);
aws_raise_error(AWS_ERROR_HTTP_CALLBACK_FAILURE);
}
aws_jni_release_thread_env(binding->jvm, &jvm_env_context);
/********** JNI ENV RELEASE **********/
}
jobjectArray aws_java_http_headers_from_native(JNIEnv *env, struct aws_http_headers *headers) {
(void)headers;
jobjectArray ret;
const size_t header_count = aws_http_headers_count(headers);
ret = (jobjectArray)(*env)->NewObjectArray(
env, (jsize)header_count, http_header_properties.http_header_class, (void *)NULL);
for (size_t index = 0; index < header_count; index += 1) {
struct aws_http_header header;
aws_http_headers_get_index(headers, index, &header);
jbyteArray header_name = aws_jni_byte_array_from_cursor(env, &header.name);
jbyteArray header_value = aws_jni_byte_array_from_cursor(env, &header.value);
jobject java_http_header = (*env)->NewObject(
env,
http_header_properties.http_header_class,
http_header_properties.constructor_method_id,
header_name,
header_value);
(*env)->SetObjectArrayElement(env, ret, (jsize)index, java_http_header);
}
return (ret);
}
static jobject s_make_request_general(
JNIEnv *env,
jlong jni_connection,
jbyteArray marshalled_request,
jobject jni_http_request_body_stream,
jobject jni_http_response_callback_handler,
enum aws_http_version version,
bool use_manual_data_writes) {
struct aws_http_connection_binding *connection_binding = (struct aws_http_connection_binding *)jni_connection;
struct aws_http_connection *native_conn = connection_binding->connection;
if (!native_conn) {
aws_jni_throw_null_pointer_exception(env, "HttpClientConnection.MakeRequest: Invalid aws_http_connection");
return (jobject)NULL;
}
if (!jni_http_response_callback_handler) {
aws_jni_throw_illegal_argument_exception(
env, "HttpClientConnection.MakeRequest: Invalid jni_http_response_callback_handler");
return (jobject)NULL;
}
/* initial refcount created for the Java object */
struct http_stream_binding *stream_binding = aws_http_stream_binding_new(env, jni_http_response_callback_handler);
if (!stream_binding) {
/* Exception already thrown */
return (jobject)NULL;
}
stream_binding->native_request =
aws_http_request_new_from_java_http_request(env, marshalled_request, jni_http_request_body_stream);
if (stream_binding->native_request == NULL) {
/* Exception already thrown */
goto error;
}
struct aws_http_make_request_options request_options = {
.self_size = sizeof(request_options),
.request = stream_binding->native_request,
/* Set Callbacks */
.on_response_headers = aws_java_http_stream_on_incoming_headers_fn,
.on_response_header_block_done = aws_java_http_stream_on_incoming_header_block_done_fn,
.on_response_body = aws_java_http_stream_on_incoming_body_fn,
.on_complete = aws_java_http_stream_on_stream_complete_fn,
.on_destroy = aws_java_http_stream_on_stream_destroy_fn,
.on_metrics = aws_java_http_stream_on_stream_metrics_fn,
.user_data = stream_binding,
.use_manual_data_writes = use_manual_data_writes,
};
stream_binding->native_stream = aws_http_connection_make_request(native_conn, &request_options);
if (stream_binding->native_stream == NULL) {
AWS_LOGF_ERROR(AWS_LS_HTTP_CONNECTION, "Stream Request Failed. conn: %p", (void *)native_conn);
aws_jni_throw_runtime_exception(env, "HttpClientConnection.MakeRequest: Unable to Execute Request");
goto error;
}
/* Stream created successfully, acquire on binding for the native stream lifetime. */
aws_http_stream_binding_acquire(stream_binding);
jobject jHttpStreamBase = aws_java_http_stream_from_native_new(env, stream_binding, version);
if (jHttpStreamBase == NULL) {
goto error;
}
AWS_LOGF_TRACE(
AWS_LS_HTTP_CONNECTION,
"Opened new Stream on Connection. conn: %p, stream: %p",
(void *)native_conn,
(void *)stream_binding->native_stream);
return jHttpStreamBase;
error:
aws_http_stream_release(stream_binding->native_stream);
aws_http_stream_binding_release(env, stream_binding);
return NULL;
}
JNIEXPORT jobject JNICALL Java_software_amazon_awssdk_crt_http_HttpClientConnection_httpClientConnectionMakeRequest(
JNIEnv *env,
jclass jni_class,
jlong jni_connection,
jbyteArray marshalled_request,
jobject jni_http_request_body_stream,
jobject jni_http_response_callback_handler,
jboolean jni_use_manual_data_writes) {
(void)jni_class;
aws_cache_jni_ids(env);
return s_make_request_general(
env,
jni_connection,
marshalled_request,
jni_http_request_body_stream,
jni_http_response_callback_handler,
AWS_HTTP_VERSION_1_1,
jni_use_manual_data_writes);
}
JNIEXPORT jobject JNICALL Java_software_amazon_awssdk_crt_http_Http2ClientConnection_http2ClientConnectionMakeRequest(
JNIEnv *env,
jclass jni_class,
jlong jni_connection,
jbyteArray marshalled_request,
jobject jni_http_request_body_stream,
jobject jni_http_response_callback_handler,
jboolean jni_use_manual_data_writes) {
(void)jni_class;
aws_cache_jni_ids(env);
return s_make_request_general(
env,
jni_connection,
marshalled_request,
jni_http_request_body_stream,
jni_http_response_callback_handler,
AWS_HTTP_VERSION_2,
jni_use_manual_data_writes);
}
struct http_stream_chunked_callback_data {
struct http_stream_binding *stream_cb_data;
struct aws_byte_buf chunk_data;
struct aws_input_stream *chunk_stream;
jobject completion_callback;
};
static void s_cleanup_chunked_callback_data(
JNIEnv *env,
struct http_stream_chunked_callback_data *chunked_callback_data) {
aws_input_stream_destroy(chunked_callback_data->chunk_stream);
aws_byte_buf_clean_up(&chunked_callback_data->chunk_data);
(*env)->DeleteGlobalRef(env, chunked_callback_data->completion_callback);
aws_mem_release(aws_jni_get_allocator(), chunked_callback_data);
}
static void s_write_chunk_complete(struct aws_http_stream *stream, int error_code, void *user_data) {
(void)stream;
struct http_stream_chunked_callback_data *chunked_callback_data = user_data;
/********** JNI ENV ACQUIRE **********/
struct aws_jvm_env_context jvm_env_context = aws_jni_acquire_thread_env(chunked_callback_data->stream_cb_data->jvm);
JNIEnv *env = jvm_env_context.env;
if (env == NULL) {
/* If we can't get an environment, then the JVM is probably shutting down. Don't crash. */
return;
}
(*env)->CallVoidMethod(
env,
chunked_callback_data->completion_callback,
http_stream_write_chunk_completion_properties.callback,
error_code);
aws_jni_check_and_clear_exception(env);
JavaVM *jvm = chunked_callback_data->stream_cb_data->jvm;
s_cleanup_chunked_callback_data(env, chunked_callback_data);
aws_jni_release_thread_env(jvm, &jvm_env_context);
/********** JNI ENV RELEASE **********/
}
JNIEXPORT jint JNICALL Java_software_amazon_awssdk_crt_http_HttpStream_httpStreamWriteChunk(
JNIEnv *env,
jclass jni_class,
jlong jni_cb_data,
jbyteArray chunk_data,
jboolean is_final_chunk,
jobject completion_callback) {
(void)jni_class;
aws_cache_jni_ids(env);
struct http_stream_binding *cb_data = (struct http_stream_binding *)jni_cb_data;
struct aws_http_stream *stream = cb_data->native_stream;
struct http_stream_chunked_callback_data *chunked_callback_data =
aws_mem_calloc(aws_jni_get_allocator(), 1, sizeof(struct http_stream_chunked_callback_data));
chunked_callback_data->stream_cb_data = cb_data;
chunked_callback_data->completion_callback = (*env)->NewGlobalRef(env, completion_callback);
struct aws_byte_cursor chunk_cur = aws_jni_byte_cursor_from_jbyteArray_acquire(env, chunk_data);
aws_byte_buf_init_copy_from_cursor(&chunked_callback_data->chunk_data, aws_jni_get_allocator(), chunk_cur);
aws_jni_byte_cursor_from_jbyteArray_release(env, chunk_data, chunk_cur);
struct aws_http1_chunk_options chunk_options = {
.chunk_data_size = chunked_callback_data->chunk_data.len,
.user_data = chunked_callback_data,
.on_complete = s_write_chunk_complete,
};
chunk_cur = aws_byte_cursor_from_buf(&chunked_callback_data->chunk_data);
chunked_callback_data->chunk_stream = aws_input_stream_new_from_cursor(aws_jni_get_allocator(), &chunk_cur);
chunk_options.chunk_data = chunked_callback_data->chunk_stream;
if (aws_http1_stream_write_chunk(stream, &chunk_options)) {
s_cleanup_chunked_callback_data(env, chunked_callback_data);
return AWS_OP_ERR;
}
if (is_final_chunk) {
struct aws_http1_chunk_options final_chunk_options = {
.chunk_data_size = 0,
};
if (aws_http1_stream_write_chunk(stream, &final_chunk_options)) {
return AWS_OP_ERR;
}
}
return AWS_OP_SUCCESS;
}
struct http_stream_write_data_callback_data {
struct http_stream_binding *stream_cb_data;
/*
* Global ref to the Java byte[] passed in by the caller, and the aws_byte_cursor acquired
* from it via aws_jni_byte_cursor_from_jbyteArray_acquire. Held on callback_data so the
* JNI-side bytes stay valid for the full async write, and released from
* s_cleanup_write_data_callback_data once the write completes (or fails synchronously).
* Avoids copying the payload into a native aws_byte_buf before sending.
*/
jbyteArray data_array;
struct aws_byte_cursor data_cur;
struct aws_input_stream *data_stream;
jobject completion_callback;
};
static void s_cleanup_write_data_callback_data(
JNIEnv *env,
struct http_stream_write_data_callback_data *callback_data) {
if (callback_data->data_stream) {
aws_input_stream_destroy(callback_data->data_stream);
}
if (callback_data->data_array) {
/* Release the JNI pin/copy (needs the jbyteArray + original cursor), then drop our global ref. */
aws_jni_byte_cursor_from_jbyteArray_release(env, callback_data->data_array, callback_data->data_cur);
(*env)->DeleteGlobalRef(env, callback_data->data_array);
}
(*env)->DeleteGlobalRef(env, callback_data->completion_callback);
aws_mem_release(aws_jni_get_allocator(), callback_data);
}
static void s_write_data_complete(struct aws_http_stream *stream, int error_code, void *user_data) {
(void)stream;
struct http_stream_write_data_callback_data *callback_data = user_data;
/********** JNI ENV ACQUIRE **********/
struct aws_jvm_env_context jvm_env_context = aws_jni_acquire_thread_env(callback_data->stream_cb_data->jvm);
JNIEnv *env = jvm_env_context.env;
if (env == NULL) {
return;
}
(*env)->CallVoidMethod(
env, callback_data->completion_callback, http_stream_write_data_completion_properties.callback, error_code);
aws_jni_check_and_clear_exception(env);
JavaVM *jvm = callback_data->stream_cb_data->jvm;
s_cleanup_write_data_callback_data(env, callback_data);
aws_jni_release_thread_env(jvm, &jvm_env_context);
/********** JNI ENV RELEASE **********/
}
JNIEXPORT jint JNICALL Java_software_amazon_awssdk_crt_http_HttpStreamBase_httpStreamBaseWriteData(
JNIEnv *env,
jclass jni_class,
jlong jni_cb_data,
jbyteArray data,
jboolean end_stream,
jobject completion_callback) {
(void)jni_class;
aws_cache_jni_ids(env);
struct http_stream_binding *cb_data = (struct http_stream_binding *)jni_cb_data;
struct aws_http_stream *stream = cb_data->native_stream;
struct http_stream_write_data_callback_data *callback_data =
aws_mem_calloc(aws_jni_get_allocator(), 1, sizeof(struct http_stream_write_data_callback_data));
callback_data->stream_cb_data = cb_data;
callback_data->completion_callback = (*env)->NewGlobalRef(env, completion_callback);
struct aws_http_stream_write_data_options options = {
.data = NULL,
.end_stream = end_stream,
.on_complete = s_write_data_complete,
.user_data = callback_data,
};
if (data != NULL) {
/*
* Promote to a global ref so it survives beyond this JNI frame — the async write
* completion fires on the event-loop thread after this function returns.
* Acquire the cursor once, store both on callback_data, and let
* s_cleanup_write_data_callback_data release them when the write finishes.
* aws_input_stream_new_from_cursor copies the {ptr,len} cursor struct into its own
* state, but does NOT copy the underlying bytes, so this path avoids the payload copy.
*/
callback_data->data_array = (*env)->NewGlobalRef(env, data);
callback_data->data_cur = aws_jni_byte_cursor_from_jbyteArray_acquire(env, data);
callback_data->data_stream =
aws_input_stream_new_from_cursor(aws_jni_get_allocator(), &callback_data->data_cur);
options.data = callback_data->data_stream;
}
if (aws_http_stream_write_data(stream, &options)) {
s_cleanup_write_data_callback_data(env, callback_data);
return AWS_OP_ERR;
}
return AWS_OP_SUCCESS;
}
JNIEXPORT void JNICALL Java_software_amazon_awssdk_crt_http_HttpStreamBase_httpStreamBaseActivate(
JNIEnv *env,
jclass jni_class,
jlong jni_stream_binding,
jobject j_http_stream_base) {
(void)jni_class;
aws_cache_jni_ids(env);
struct http_stream_binding *binding = (struct http_stream_binding *)jni_stream_binding;
struct aws_http_stream *stream = binding->native_stream;
if (stream == NULL) {
aws_jni_throw_runtime_exception(env, "HttpStream is null.");
return;
}
AWS_LOGF_TRACE(AWS_LS_HTTP_STREAM, "Activating Stream. stream: %p", (void *)stream);
/* global ref this because now the callbacks will be firing, and they will release their reference when the
* stream callback sequence completes. */
binding->java_http_stream_base = (*env)->NewGlobalRef(env, j_http_stream_base);
if (aws_http_stream_activate(stream)) {
(*env)->DeleteGlobalRef(env, binding->java_http_stream_base);
aws_jni_throw_runtime_exception(
env, "HttpStream activate failed with error %s\n", aws_error_str(aws_last_error()));
}
}
JNIEXPORT void JNICALL Java_software_amazon_awssdk_crt_http_HttpStreamBase_httpStreamBaseRelease(
JNIEnv *env,
jclass jni_class,
jlong jni_binding) {
(void)jni_class;
aws_cache_jni_ids(env);
struct http_stream_binding *binding = (struct http_stream_binding *)jni_binding;
struct aws_http_stream *stream = binding->native_stream;
if (stream == NULL) {
aws_jni_throw_runtime_exception(env, "HttpStream is null.");
return;
}
AWS_LOGF_TRACE(AWS_LS_HTTP_STREAM, "Releasing Stream. stream: %p", (void *)stream);
aws_http_stream_release(stream);
aws_http_stream_binding_release(env, binding);
}
JNIEXPORT jint JNICALL Java_software_amazon_awssdk_crt_http_HttpStreamBase_httpStreamBaseGetResponseStatusCode(
JNIEnv *env,
jclass jni_class,
jlong jni_binding) {
(void)jni_class;
aws_cache_jni_ids(env);
struct http_stream_binding *binding = (struct http_stream_binding *)jni_binding;
struct aws_http_stream *stream = binding->native_stream;
if (stream == NULL) {
aws_jni_throw_runtime_exception(env, "HttpStream is null.");
return -1;
}
int status = -1;
int err_code = aws_http_stream_get_incoming_response_status(stream, &status);
if (err_code != AWS_OP_SUCCESS) {
aws_jni_throw_runtime_exception(env, "Error Getting Response Status Code from HttpStream.");
return -1;
}
return (jint)status;
}
JNIEXPORT void JNICALL Java_software_amazon_awssdk_crt_http_HttpStreamBase_httpStreamBaseIncrementWindow(
JNIEnv *env,
jclass jni_class,
jlong jni_binding,
jint window_update) {
(void)jni_class;
aws_cache_jni_ids(env);
struct http_stream_binding *binding = (struct http_stream_binding *)jni_binding;
struct aws_http_stream *stream = binding->native_stream;
if (stream == NULL) {
aws_jni_throw_runtime_exception(env, "HttpStream is null.");
return;
}
if (window_update < 0) {
aws_jni_throw_runtime_exception(env, "Window Update is < 0");
return;
}
AWS_LOGF_TRACE(
AWS_LS_HTTP_STREAM, "Updating Stream Window. stream: %p, update: %d", (void *)stream, (int)window_update);
aws_http_stream_update_window(stream, window_update);
}
JNIEXPORT void JNICALL Java_software_amazon_awssdk_crt_http_HttpStreamBase_httpStreamBaseCancelDefaultError(
JNIEnv *env,
jclass jni_class,
jlong jni_binding) {
(void)jni_class;
aws_cache_jni_ids(env);
struct http_stream_binding *binding = (struct http_stream_binding *)jni_binding;
struct aws_http_stream *stream = binding->native_stream;
if (stream == NULL) {
aws_jni_throw_runtime_exception(env, "HttpStream is null.");
return;
}
AWS_LOGF_TRACE(AWS_LS_HTTP_STREAM, "Cancelling Stream with default error. stream: %p", (void *)stream);
aws_http_stream_cancel_default_error(stream);
}
JNIEXPORT void JNICALL Java_software_amazon_awssdk_crt_http_Http2Stream_http2StreamResetStream(
JNIEnv *env,
jclass jni_class,
jlong jni_cb_data,
jint error_code) {
(void)jni_class;
aws_cache_jni_ids(env);
struct http_stream_binding *binding = (struct http_stream_binding *)jni_cb_data;
struct aws_http_stream *stream = binding->native_stream;
if (stream == NULL) {
aws_jni_throw_null_pointer_exception(env, "Http2Stream is null.");
return;
}
AWS_LOGF_TRACE(AWS_LS_HTTP_STREAM, "Resetting Stream. stream: %p", (void *)stream);
if (aws_http2_stream_reset(stream, error_code)) {
aws_jni_throw_runtime_exception(
env, "reset stream failed with error %d(%s).", aws_last_error(), aws_error_debug_str(aws_last_error()));
return;
}
}
JNIEXPORT void JNICALL Java_software_amazon_awssdk_crt_http_HttpClientConnection_httpClientConnectionShutdown(
JNIEnv *env,
jclass jni_class,
jlong jni_connection) {
(void)jni_class;
aws_cache_jni_ids(env);
struct aws_http_connection_binding *connection_binding = (struct aws_http_connection_binding *)jni_connection;
struct aws_http_connection *native_conn = connection_binding->connection;
if (!native_conn) {
aws_jni_throw_runtime_exception(env, "HttpClientConnection.Shutdown: Invalid aws_http_connection");
return;
}
aws_http_connection_close(native_conn);
}
JNIEXPORT jboolean JNICALL Java_software_amazon_awssdk_crt_http_HttpClientConnection_httpClientConnectionIsOpen(
JNIEnv *env,
jclass jni_class,
jlong jni_connection) {
(void)jni_class;
aws_cache_jni_ids(env);
struct aws_http_connection_binding *connection_binding = (struct aws_http_connection_binding *)jni_connection;
struct aws_http_connection *native_conn = connection_binding->connection;
if (!native_conn) {
aws_jni_throw_runtime_exception(env, "HttpClientConnection.isOpen: Invalid aws_http_connection");
return false;
}
return aws_http_connection_is_open(native_conn);
}
JNIEXPORT jshort JNICALL Java_software_amazon_awssdk_crt_http_HttpClientConnection_httpClientConnectionGetVersion(
JNIEnv *env,
jclass jni_class,
jlong jni_connection) {
(void)jni_class;
aws_cache_jni_ids(env);
struct aws_http_connection_binding *connection_binding = (struct aws_http_connection_binding *)jni_connection;
struct aws_http_connection *native_conn = connection_binding->connection;
if (!native_conn) {
aws_jni_throw_runtime_exception(env, "HttpClientConnection.getVersion: Invalid aws_http_connection");
return 0;
}
return (jshort)aws_http_connection_get_version(native_conn);
}
JNIEXPORT jboolean JNICALL Java_software_amazon_awssdk_crt_http_HttpClientConnection_isErrorRetryable(
JNIEnv *env,
jclass jni_class,
jint error_code) {
(void)jni_class;
(void)env;
aws_cache_jni_ids(env);
switch (error_code) {
case AWS_ERROR_HTTP_HEADER_NOT_FOUND:
case AWS_ERROR_HTTP_INVALID_HEADER_FIELD:
case AWS_ERROR_HTTP_INVALID_HEADER_NAME:
case AWS_ERROR_HTTP_INVALID_HEADER_VALUE:
case AWS_ERROR_HTTP_INVALID_METHOD:
case AWS_ERROR_HTTP_INVALID_PATH:
case AWS_ERROR_HTTP_INVALID_STATUS_CODE:
case AWS_ERROR_HTTP_MISSING_BODY_STREAM:
case AWS_ERROR_HTTP_INVALID_BODY_STREAM:
case AWS_ERROR_HTTP_OUTGOING_STREAM_LENGTH_INCORRECT:
case AWS_ERROR_HTTP_CALLBACK_FAILURE:
case AWS_ERROR_HTTP_STREAM_MANAGER_SHUTTING_DOWN:
case AWS_HTTP2_ERR_CANCEL:
return false;
default:
return true;
}
}
struct aws_http2_callback_data {
JavaVM *jvm;
jobject async_callback;
};
static void s_cleanup_http2_callback_data(struct aws_http2_callback_data *callback_data, JNIEnv *env) {
if (callback_data == NULL || env == NULL) {
return;
}
if (callback_data->async_callback) {
(*env)->DeleteGlobalRef(env, callback_data->async_callback);
}
aws_mem_release(aws_jni_get_allocator(), callback_data);
}
static struct aws_http2_callback_data *s_new_http2_callback_data(
JNIEnv *env,
struct aws_allocator *allocator,
jobject async_callback) {
struct aws_http2_callback_data *callback_data =
aws_mem_calloc(allocator, 1, sizeof(struct aws_http2_callback_data));
jint jvmresult = (*env)->GetJavaVM(env, &callback_data->jvm);
AWS_FATAL_ASSERT(jvmresult == 0);
callback_data->async_callback = async_callback ? (*env)->NewGlobalRef(env, async_callback) : NULL;
AWS_FATAL_ASSERT(callback_data->async_callback != NULL);
return callback_data;
}
static void s_on_settings_completed(struct aws_http_connection *http2_connection, int error_code, void *user_data) {
(void)http2_connection;
struct aws_http2_callback_data *callback_data = user_data;
/********** JNI ENV ACQUIRE **********/
JavaVM *jvm = callback_data->jvm;
struct aws_jvm_env_context jvm_env_context = aws_jni_acquire_thread_env(jvm);
JNIEnv *env = jvm_env_context.env;
if (env == NULL) {
/* If we can't get an environment, then the JVM is probably shutting down. Don't crash. */
return;
}
if (error_code) {
jobject crt_exception = aws_jni_new_crt_exception_from_error_code(env, error_code);
(*env)->CallVoidMethod(env, callback_data->async_callback, async_callback_properties.on_failure, crt_exception);
(*env)->DeleteLocalRef(env, crt_exception);
} else {
(*env)->CallVoidMethod(env, callback_data->async_callback, async_callback_properties.on_success);
}
AWS_FATAL_ASSERT(!aws_jni_check_and_clear_exception(env));