-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathhttp2_stream_manager.c
More file actions
480 lines (411 loc) · 19 KB
/
http2_stream_manager.c
File metadata and controls
480 lines (411 loc) · 19 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
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
#include "crt.h"
#include "http_connection_manager.h"
#include "http_request_response.h"
#include "http_request_utils.h"
#include "java_class_ids.h"
#include <http_proxy_options.h>
#include <jni.h>
#include <string.h>
#include <aws/common/condition_variable.h>
#include <aws/common/string.h>
#include <aws/io/channel_bootstrap.h>
#include <aws/io/event_loop.h>
#include <aws/io/logging.h>
#include <aws/io/socket.h>
#include <aws/io/tls_channel_handler.h>
#include <aws/http/connection.h>
#include <aws/http/connection_manager.h>
#include <aws/http/http.h>
#include <aws/http/http2_stream_manager.h>
#include <aws/http/proxy.h>
/* 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
/*
* Stream manager binding, persists across the lifetime of the native object.
*/
struct aws_http2_stream_manager_binding {
JavaVM *jvm;
jobject java_http2_stream_manager;
struct aws_http2_stream_manager *stream_manager;
};
static void s_destroy_manager_binding(struct aws_http2_stream_manager_binding *binding, JNIEnv *env) {
if (binding == NULL) {
return;
}
if (binding->java_http2_stream_manager != NULL) {
(*env)->DeleteGlobalRef(env, binding->java_http2_stream_manager);
}
aws_mem_release(aws_jni_get_allocator(), binding);
}
static void s_on_stream_manager_shutdown_complete_callback(void *user_data) {
struct aws_http2_stream_manager_binding *binding = (struct aws_http2_stream_manager_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_LOGF_DEBUG(AWS_LS_HTTP_STREAM_MANAGER, "Java Stream Manager Shutdown Complete");
if (binding->java_http2_stream_manager != NULL) {
(*env)->CallVoidMethod(
env, binding->java_http2_stream_manager, http2_stream_manager_properties.onShutdownComplete);
/* If exception raised from Java callback, but we already closed the stream manager, just move on */
aws_jni_check_and_clear_exception(env);
}
/* We're done with this wrapper, free it. */
JavaVM *jvm = binding->jvm;
s_destroy_manager_binding(binding, env);
aws_jni_release_thread_env(jvm, &jvm_env_context);
/********** JNI ENV RELEASE **********/
}
JNIEXPORT jlong JNICALL Java_software_amazon_awssdk_crt_http_Http2StreamManager_http2StreamManagerNew(
JNIEnv *env,
jclass jni_class,
jobject stream_manager_jobject,
jlong jni_client_bootstrap,
jlong jni_socket_options,
jlong jni_tls_ctx,
jlong jni_tls_connection_options,
jlongArray java_marshalled_settings,
jbyteArray jni_endpoint,
jint jni_port,
jint jni_proxy_connection_type,
jbyteArray jni_proxy_host,
jint jni_proxy_port,
jlong jni_proxy_tls_context,
jint jni_proxy_authorization_type,
jbyteArray jni_proxy_authorization_username,
jbyteArray jni_proxy_authorization_password,
jbyteArray jni_no_proxy_hosts,
jboolean jni_manual_window_management,
jlong jni_init_window_size,
jlong jni_monitoring_throughput_threshold_in_bytes_per_second,
jint jni_monitoring_failure_interval_in_seconds,
jint jni_max_conns,
jint jni_ideal_concurrent_streams_per_connection,
jint jni_max_concurrent_streams_per_connection,
jint jni_max_concurrent_streams,
jboolean jni_prior_knowledge,
jboolean jni_close_connection_on_server_error,
jint jni_connection_ping_period_ms,
jint jni_connection_ping_timeout_ms) {
(void)jni_class;
aws_cache_jni_ids(env);
struct aws_client_bootstrap *client_bootstrap = (struct aws_client_bootstrap *)jni_client_bootstrap;
struct aws_socket_options *socket_options = (struct aws_socket_options *)jni_socket_options;
struct aws_tls_ctx *tls_ctx = (struct aws_tls_ctx *)jni_tls_ctx;
struct aws_tls_connection_options *tls_connection_options =
(struct aws_tls_connection_options *)jni_tls_connection_options;
struct aws_http2_stream_manager_binding *binding = NULL;
struct aws_allocator *allocator = aws_jni_get_allocator();
if (!client_bootstrap) {
aws_jni_throw_illegal_argument_exception(env, "ClientBootstrap can't be null");
return (jlong)NULL;
}
if (!socket_options) {
aws_jni_throw_illegal_argument_exception(env, "SocketOptions can't be null");
return (jlong)NULL;
}
const size_t marshalled_len = (*env)->GetArrayLength(env, java_marshalled_settings);
AWS_ASSERT(marshalled_len % 2 == 0);
size_t num_initial_settings = marshalled_len / 2;
struct aws_http2_setting *initial_settings =
num_initial_settings ? aws_mem_calloc(allocator, num_initial_settings, sizeof(struct aws_http2_setting)) : NULL;
jlong *marshalled_settings = (*env)->GetLongArrayElements(env, java_marshalled_settings, NULL);
for (size_t i = 0; i < num_initial_settings; i++) {
jlong id = marshalled_settings[i * 2];
initial_settings[i].id = (uint32_t)id;
jlong value = marshalled_settings[i * 2 + 1];
/* We checked the value can fit into uint32_t in Java already */
initial_settings[i].value = (uint32_t)value;
}
struct aws_byte_cursor endpoint = aws_jni_byte_cursor_from_jbyteArray_acquire(env, jni_endpoint);
if (jni_port == 0) {
aws_jni_throw_illegal_argument_exception(env, "Port must not be 0");
goto cleanup;
}
if (jni_max_conns <= 0) {
aws_jni_throw_illegal_argument_exception(env, "Max Connections must be > 0");
goto cleanup;
}
uint32_t port = (uint32_t)jni_port;
bool new_tls_conn_opts = (jni_tls_ctx != 0 && !tls_connection_options);
struct aws_tls_connection_options tls_conn_options;
AWS_ZERO_STRUCT(tls_conn_options);
if (new_tls_conn_opts) {
aws_tls_connection_options_init_from_ctx(&tls_conn_options, tls_ctx);
aws_tls_connection_options_set_server_name(&tls_conn_options, allocator, &endpoint);
tls_connection_options = &tls_conn_options;
}
binding = aws_mem_calloc(allocator, 1, sizeof(struct aws_http2_stream_manager_binding));
AWS_FATAL_ASSERT(binding);
binding->java_http2_stream_manager = (*env)->NewGlobalRef(env, stream_manager_jobject);
jint jvmresult = (*env)->GetJavaVM(env, &binding->jvm);
(void)jvmresult;
AWS_FATAL_ASSERT(jvmresult == 0);
struct aws_http2_stream_manager_options manager_options = {
.bootstrap = client_bootstrap,
.initial_settings_array = initial_settings,
.num_initial_settings = num_initial_settings,
.socket_options = socket_options,
.http2_prior_knowledge = jni_prior_knowledge,
.tls_connection_options = tls_connection_options,
.monitoring_options = NULL,
.host = endpoint,
.port = port,
.initial_window_size = (size_t)jni_init_window_size,
.shutdown_complete_callback = &s_on_stream_manager_shutdown_complete_callback,
.shutdown_complete_user_data = binding,
.enable_read_back_pressure = jni_manual_window_management,
.close_connection_on_server_error = jni_close_connection_on_server_error,
.connection_ping_period_ms = jni_connection_ping_period_ms,
.connection_ping_timeout_ms = jni_connection_ping_timeout_ms,
.ideal_concurrent_streams_per_connection = (size_t)jni_ideal_concurrent_streams_per_connection,
.max_concurrent_streams_per_connection = (size_t)jni_max_concurrent_streams_per_connection,
.max_connections = (size_t)jni_max_conns,
.max_concurrent_streams = (size_t)jni_max_concurrent_streams,
};
struct aws_http_connection_monitoring_options monitoring_options;
AWS_ZERO_STRUCT(monitoring_options);
if (jni_monitoring_throughput_threshold_in_bytes_per_second >= 0 &&
jni_monitoring_failure_interval_in_seconds >= 2) {
monitoring_options.minimum_throughput_bytes_per_second =
jni_monitoring_throughput_threshold_in_bytes_per_second;
monitoring_options.allowable_throughput_failure_interval_seconds = jni_monitoring_failure_interval_in_seconds;
manager_options.monitoring_options = &monitoring_options;
}
struct aws_http_proxy_options proxy_options;
AWS_ZERO_STRUCT(proxy_options);
struct aws_tls_connection_options proxy_tls_conn_options;
AWS_ZERO_STRUCT(proxy_tls_conn_options);
aws_http_proxy_options_jni_init(
env,
&proxy_options,
jni_proxy_connection_type,
&proxy_tls_conn_options,
jni_proxy_host,
jni_proxy_port,
jni_proxy_authorization_username,
jni_proxy_authorization_password,
jni_no_proxy_hosts,
jni_proxy_authorization_type,
(struct aws_tls_ctx *)jni_proxy_tls_context);
if (jni_proxy_host != NULL) {
manager_options.proxy_options = &proxy_options;
}
binding->stream_manager = aws_http2_stream_manager_new(allocator, &manager_options);
if (binding->stream_manager == NULL) {
aws_jni_throw_runtime_exception(env, "Failed to create stream manager: %s", aws_error_str(aws_last_error()));
}
aws_http_proxy_options_jni_clean_up(
env,
&proxy_options,
jni_proxy_host,
jni_proxy_authorization_username,
jni_proxy_authorization_password,
jni_no_proxy_hosts);
if (new_tls_conn_opts) {
aws_tls_connection_options_clean_up(&tls_conn_options);
}
cleanup:
aws_jni_byte_cursor_from_jbyteArray_release(env, jni_endpoint, endpoint);
(*env)->ReleaseLongArrayElements(env, java_marshalled_settings, (jlong *)marshalled_settings, JNI_ABORT);
if (binding->stream_manager == NULL) {
s_destroy_manager_binding(binding, env);
binding = NULL;
}
return (jlong)binding;
}
/*
* Stream manager binding, persists across the lifetime of the native object.
*/
struct aws_sm_acquire_stream_callback_data {
JavaVM *jvm;
struct http_stream_binding *stream_binding;
jobject java_async_callback;
};
static void s_cleanup_sm_acquire_stream_callback_data(
struct aws_sm_acquire_stream_callback_data *callback_data,
JNIEnv *env) {
if (callback_data->java_async_callback) {
(*env)->DeleteGlobalRef(env, callback_data->java_async_callback);
}
aws_mem_release(aws_jni_get_allocator(), callback_data);
}
static struct aws_sm_acquire_stream_callback_data *s_new_sm_acquire_stream_callback_data(
JNIEnv *env,
struct aws_allocator *allocator,
struct http_stream_binding *stream_binding,
jobject async_callback) {
struct aws_sm_acquire_stream_callback_data *callback_data =
aws_mem_calloc(allocator, 1, sizeof(struct aws_sm_acquire_stream_callback_data));
jint jvmresult = (*env)->GetJavaVM(env, &callback_data->jvm);
AWS_FATAL_ASSERT(jvmresult == 0);
callback_data->java_async_callback = async_callback ? (*env)->NewGlobalRef(env, async_callback) : NULL;
AWS_FATAL_ASSERT(callback_data->java_async_callback != NULL);
callback_data->stream_binding = stream_binding;
return callback_data;
}
static void s_on_stream_acquired(struct aws_http_stream *stream, int error_code, void *user_data) {
struct aws_sm_acquire_stream_callback_data *callback_data = user_data;
/********** JNI ENV ACQUIRE **********/
struct aws_jvm_env_context jvm_env_context = aws_jni_acquire_thread_env(callback_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;
}
if (error_code) {
AWS_ASSERT(stream == NULL);
jobject crt_exception = aws_jni_new_crt_exception_from_error_code(env, error_code);
(*env)->CallVoidMethod(
env, callback_data->java_async_callback, async_callback_properties.on_failure, crt_exception);
(*env)->DeleteLocalRef(env, crt_exception);
aws_http_stream_binding_release(env, callback_data->stream_binding);
} else {
/* Acquire for the native stream. The destroy callback for native stream will release the ref. */
aws_http_stream_binding_acquire(callback_data->stream_binding);
callback_data->stream_binding->native_stream = stream;
jobject j_http_stream =
aws_java_http_stream_from_native_new(env, callback_data->stream_binding, AWS_HTTP_VERSION_2);
if (!j_http_stream) {
jthrowable crt_exception = (*env)->ExceptionOccurred(env);
AWS_ASSERT(crt_exception);
(*env)->CallVoidMethod(
env, callback_data->java_async_callback, async_callback_properties.on_failure, crt_exception);
(*env)->DeleteLocalRef(env, crt_exception);
(*env)->ExceptionClear(env);
/* Release the refcount on binding for the java object that failed to be created. */
aws_http_stream_binding_release(env, callback_data->stream_binding);
} else {
callback_data->stream_binding->java_http_stream_base = (*env)->NewGlobalRef(env, j_http_stream);
(*env)->CallVoidMethod(
env,
callback_data->java_async_callback,
async_callback_properties.on_success_with_object,
callback_data->stream_binding->java_http_stream_base);
(*env)->DeleteLocalRef(env, j_http_stream);
}
}
AWS_FATAL_ASSERT(!aws_jni_check_and_clear_exception(env));
JavaVM *jvm = callback_data->jvm;
s_cleanup_sm_acquire_stream_callback_data(callback_data, env);
aws_jni_release_thread_env(jvm, &jvm_env_context);
/********** JNI ENV RELEASE **********/
}
JNIEXPORT void JNICALL Java_software_amazon_awssdk_crt_http_Http2StreamManager_http2StreamManagerAcquireStream(
JNIEnv *env,
jclass jni_class,
jlong jni_stream_manager,
jbyteArray marshalled_request,
jobject jni_http_request_body_stream,
jobject jni_http_response_callback_handler,
jobject java_async_callback,
jboolean jni_use_manual_data_writes) {
(void)jni_class;
aws_cache_jni_ids(env);
struct aws_http2_stream_manager_binding *sm_binding = (struct aws_http2_stream_manager_binding *)jni_stream_manager;
struct aws_http2_stream_manager *stream_manager = sm_binding->stream_manager;
if (!stream_manager) {
aws_jni_throw_illegal_argument_exception(env, "Stream Manager can't be null");
return;
}
if (!jni_http_response_callback_handler) {
aws_jni_throw_illegal_argument_exception(
env, "Http2StreamManager.acquireStream: Invalid jni_http_response_callback_handler");
return;
}
if (!java_async_callback) {
aws_jni_throw_illegal_argument_exception(env, "Http2StreamManager.acquireStream: Invalid async callback");
return;
}
struct http_stream_binding *stream_binding = aws_http_stream_binding_new(env, jni_http_response_callback_handler);
if (!stream_binding) {
/* Exception already thrown */
return;
}
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 */
aws_http_stream_binding_release(env, stream_binding);
return;
}
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,
.user_data = stream_binding,
/* aws_http2_stream_manager only forwards http2_use_manual_data_writes (not the
* unified use_manual_data_writes) through to aws_http_connection_make_request.
* Set the H2-specific flag here so manual writes actually take effect on the
* stream-manager path. Revisit when aws-c-http's http2_stream_manager.c is
* updated to forward use_manual_data_writes. */
.http2_use_manual_data_writes = jni_use_manual_data_writes,
};
struct aws_allocator *allocator = aws_jni_get_allocator();
struct aws_sm_acquire_stream_callback_data *callback_data =
s_new_sm_acquire_stream_callback_data(env, allocator, stream_binding, java_async_callback);
struct aws_http2_stream_manager_acquire_stream_options acquire_options = {
.options = &request_options,
.callback = s_on_stream_acquired,
.user_data = callback_data,
};
aws_http2_stream_manager_acquire_stream(sm_binding->stream_manager, &acquire_options);
}
JNIEXPORT void JNICALL Java_software_amazon_awssdk_crt_http_Http2StreamManager_http2StreamManagerRelease(
JNIEnv *env,
jclass jni_class,
jlong jni_stream_manager) {
(void)jni_class;
aws_cache_jni_ids(env);
struct aws_http2_stream_manager_binding *sm_binding = (struct aws_http2_stream_manager_binding *)jni_stream_manager;
struct aws_http2_stream_manager *stream_manager = sm_binding->stream_manager;
if (!stream_manager) {
aws_jni_throw_runtime_exception(env, "Stream Manager can't be null");
return;
}
AWS_LOGF_DEBUG(AWS_LS_HTTP_CONNECTION, "Releasing StreamManager: id: %p", (void *)stream_manager);
aws_http2_stream_manager_release(stream_manager);
}
JNIEXPORT jobject JNICALL Java_software_amazon_awssdk_crt_http_Http2StreamManager_http2StreamManagerFetchMetrics(
JNIEnv *env,
jclass jni_class,
jlong jni_stream_manager) {
(void)jni_class;
aws_cache_jni_ids(env);
struct aws_http2_stream_manager_binding *sm_binding = (struct aws_http2_stream_manager_binding *)jni_stream_manager;
struct aws_http2_stream_manager *stream_manager = sm_binding->stream_manager;
if (!stream_manager) {
aws_jni_throw_runtime_exception(env, "Stream Manager can't be null");
return NULL;
}
struct aws_http_manager_metrics metrics;
aws_http2_stream_manager_fetch_metrics(stream_manager, &metrics);
return (*env)->NewObject(
env,
http_manager_metrics_properties.http_manager_metrics_class,
http_manager_metrics_properties.constructor_method_id,
(jlong)metrics.available_concurrency,
(jlong)metrics.pending_concurrency_acquires,
(jlong)metrics.leased_concurrency);
}