xref: /aosp_15_r20/external/aws-crt-java/src/native/http_proxy_options.c (revision 3c7ae9de214676c52d19f01067dc1a404272dc11)
1*3c7ae9deSAndroid Build Coastguard Worker /**
2*3c7ae9deSAndroid Build Coastguard Worker  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3*3c7ae9deSAndroid Build Coastguard Worker  * SPDX-License-Identifier: Apache-2.0.
4*3c7ae9deSAndroid Build Coastguard Worker  */
5*3c7ae9deSAndroid Build Coastguard Worker 
6*3c7ae9deSAndroid Build Coastguard Worker #include "crt.h"
7*3c7ae9deSAndroid Build Coastguard Worker #include "java_class_ids.h"
8*3c7ae9deSAndroid Build Coastguard Worker 
9*3c7ae9deSAndroid Build Coastguard Worker #include <jni.h>
10*3c7ae9deSAndroid Build Coastguard Worker #include <string.h>
11*3c7ae9deSAndroid Build Coastguard Worker 
12*3c7ae9deSAndroid Build Coastguard Worker #include <aws/http/proxy.h>
13*3c7ae9deSAndroid Build Coastguard Worker #include <aws/io/tls_channel_handler.h>
14*3c7ae9deSAndroid Build Coastguard Worker 
15*3c7ae9deSAndroid Build Coastguard Worker /* on 32-bit platforms, casting pointers to longs throws a warning we don't need */
16*3c7ae9deSAndroid Build Coastguard Worker #if UINTPTR_MAX == 0xffffffff
17*3c7ae9deSAndroid Build Coastguard Worker #    if defined(_MSC_VER)
18*3c7ae9deSAndroid Build Coastguard Worker #        pragma warning(push)
19*3c7ae9deSAndroid Build Coastguard Worker #        pragma warning(disable : 4305) /* 'type cast': truncation from 'jlong' to 'jni_tls_ctx_options *' */
20*3c7ae9deSAndroid Build Coastguard Worker #    else
21*3c7ae9deSAndroid Build Coastguard Worker #        pragma GCC diagnostic push
22*3c7ae9deSAndroid Build Coastguard Worker #        pragma GCC diagnostic ignored "-Wpointer-to-int-cast"
23*3c7ae9deSAndroid Build Coastguard Worker #        pragma GCC diagnostic ignored "-Wint-to-pointer-cast"
24*3c7ae9deSAndroid Build Coastguard Worker #    endif
25*3c7ae9deSAndroid Build Coastguard Worker #endif
26*3c7ae9deSAndroid Build Coastguard Worker 
aws_http_proxy_options_jni_init(JNIEnv * env,struct aws_http_proxy_options * options,jint proxy_connection_type,struct aws_tls_connection_options * tls_options,jbyteArray proxy_host,jint proxy_port,jbyteArray proxy_authorization_username,jbyteArray proxy_authorization_password,int proxy_authorization_type,struct aws_tls_ctx * proxy_tls_ctx)27*3c7ae9deSAndroid Build Coastguard Worker void aws_http_proxy_options_jni_init(
28*3c7ae9deSAndroid Build Coastguard Worker     JNIEnv *env,
29*3c7ae9deSAndroid Build Coastguard Worker     struct aws_http_proxy_options *options,
30*3c7ae9deSAndroid Build Coastguard Worker     jint proxy_connection_type,
31*3c7ae9deSAndroid Build Coastguard Worker     struct aws_tls_connection_options *tls_options,
32*3c7ae9deSAndroid Build Coastguard Worker     jbyteArray proxy_host,
33*3c7ae9deSAndroid Build Coastguard Worker     jint proxy_port,
34*3c7ae9deSAndroid Build Coastguard Worker     jbyteArray proxy_authorization_username,
35*3c7ae9deSAndroid Build Coastguard Worker     jbyteArray proxy_authorization_password,
36*3c7ae9deSAndroid Build Coastguard Worker     int proxy_authorization_type,
37*3c7ae9deSAndroid Build Coastguard Worker     struct aws_tls_ctx *proxy_tls_ctx) {
38*3c7ae9deSAndroid Build Coastguard Worker 
39*3c7ae9deSAndroid Build Coastguard Worker     struct aws_allocator *allocator = aws_jni_get_allocator();
40*3c7ae9deSAndroid Build Coastguard Worker 
41*3c7ae9deSAndroid Build Coastguard Worker     options->connection_type = proxy_connection_type;
42*3c7ae9deSAndroid Build Coastguard Worker     options->port = (uint32_t)proxy_port;
43*3c7ae9deSAndroid Build Coastguard Worker     options->auth_type = proxy_authorization_type;
44*3c7ae9deSAndroid Build Coastguard Worker 
45*3c7ae9deSAndroid Build Coastguard Worker     if (proxy_host != NULL) {
46*3c7ae9deSAndroid Build Coastguard Worker         options->host = aws_jni_byte_cursor_from_jbyteArray_acquire(env, proxy_host);
47*3c7ae9deSAndroid Build Coastguard Worker     }
48*3c7ae9deSAndroid Build Coastguard Worker 
49*3c7ae9deSAndroid Build Coastguard Worker     if (proxy_authorization_username != NULL) {
50*3c7ae9deSAndroid Build Coastguard Worker         options->auth_username = aws_jni_byte_cursor_from_jbyteArray_acquire(env, proxy_authorization_username);
51*3c7ae9deSAndroid Build Coastguard Worker     }
52*3c7ae9deSAndroid Build Coastguard Worker 
53*3c7ae9deSAndroid Build Coastguard Worker     if (proxy_authorization_password != NULL) {
54*3c7ae9deSAndroid Build Coastguard Worker         options->auth_password = aws_jni_byte_cursor_from_jbyteArray_acquire(env, proxy_authorization_password);
55*3c7ae9deSAndroid Build Coastguard Worker     }
56*3c7ae9deSAndroid Build Coastguard Worker 
57*3c7ae9deSAndroid Build Coastguard Worker     if (proxy_tls_ctx != NULL) {
58*3c7ae9deSAndroid Build Coastguard Worker         aws_tls_connection_options_init_from_ctx(tls_options, proxy_tls_ctx);
59*3c7ae9deSAndroid Build Coastguard Worker         aws_tls_connection_options_set_server_name(tls_options, allocator, &options->host);
60*3c7ae9deSAndroid Build Coastguard Worker         options->tls_options = tls_options;
61*3c7ae9deSAndroid Build Coastguard Worker     }
62*3c7ae9deSAndroid Build Coastguard Worker }
63*3c7ae9deSAndroid Build Coastguard Worker 
aws_http_proxy_options_jni_clean_up(JNIEnv * env,struct aws_http_proxy_options * options,jbyteArray proxy_host,jbyteArray proxy_authorization_username,jbyteArray proxy_authorization_password)64*3c7ae9deSAndroid Build Coastguard Worker void aws_http_proxy_options_jni_clean_up(
65*3c7ae9deSAndroid Build Coastguard Worker     JNIEnv *env,
66*3c7ae9deSAndroid Build Coastguard Worker     struct aws_http_proxy_options *options,
67*3c7ae9deSAndroid Build Coastguard Worker     jbyteArray proxy_host,
68*3c7ae9deSAndroid Build Coastguard Worker     jbyteArray proxy_authorization_username,
69*3c7ae9deSAndroid Build Coastguard Worker     jbyteArray proxy_authorization_password) {
70*3c7ae9deSAndroid Build Coastguard Worker 
71*3c7ae9deSAndroid Build Coastguard Worker     if (options->host.ptr != NULL) {
72*3c7ae9deSAndroid Build Coastguard Worker         aws_jni_byte_cursor_from_jbyteArray_release(env, proxy_host, options->host);
73*3c7ae9deSAndroid Build Coastguard Worker     }
74*3c7ae9deSAndroid Build Coastguard Worker 
75*3c7ae9deSAndroid Build Coastguard Worker     if (options->auth_username.ptr != NULL) {
76*3c7ae9deSAndroid Build Coastguard Worker         aws_jni_byte_cursor_from_jbyteArray_release(env, proxy_authorization_username, options->auth_username);
77*3c7ae9deSAndroid Build Coastguard Worker     }
78*3c7ae9deSAndroid Build Coastguard Worker 
79*3c7ae9deSAndroid Build Coastguard Worker     if (options->auth_password.ptr != NULL) {
80*3c7ae9deSAndroid Build Coastguard Worker         aws_jni_byte_cursor_from_jbyteArray_release(env, proxy_authorization_password, options->auth_password);
81*3c7ae9deSAndroid Build Coastguard Worker     }
82*3c7ae9deSAndroid Build Coastguard Worker 
83*3c7ae9deSAndroid Build Coastguard Worker     if (options->tls_options != NULL) {
84*3c7ae9deSAndroid Build Coastguard Worker         aws_tls_connection_options_clean_up((struct aws_tls_connection_options *)options->tls_options);
85*3c7ae9deSAndroid Build Coastguard Worker     }
86*3c7ae9deSAndroid Build Coastguard Worker }
87*3c7ae9deSAndroid Build Coastguard Worker 
88*3c7ae9deSAndroid Build Coastguard Worker #if UINTPTR_MAX == 0xffffffff
89*3c7ae9deSAndroid Build Coastguard Worker #    if defined(_MSC_VER)
90*3c7ae9deSAndroid Build Coastguard Worker #        pragma warning(pop)
91*3c7ae9deSAndroid Build Coastguard Worker #    else
92*3c7ae9deSAndroid Build Coastguard Worker #        pragma GCC diagnostic pop
93*3c7ae9deSAndroid Build Coastguard Worker #    endif
94*3c7ae9deSAndroid Build Coastguard Worker #endif
95