JNIEXPORT jobject JNICALL Java_software_amazon_awssdk_crt_http_HttpClientConnection_httpClientConnectionMakeRequest()

in src/native/http_request_response.c [278:357]


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) {

    (void)jni_class;
    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.MakeRequest: Invalid aws_http_connection");
        return (jobject)NULL;
    }

    if (!jni_http_response_callback_handler) {
        aws_jni_throw_runtime_exception(
            env, "HttpClientConnection.MakeRequest: Invalid jni_http_response_callback_handler");
        return (jobject)NULL;
    }

    struct http_stream_callback_data *callback_data =
        http_stream_callback_alloc(env, jni_http_response_callback_handler);
    if (!callback_data) {
        /* Exception already thrown */
        return (jobject)NULL;
    }

    callback_data->native_request =
        aws_http_request_new_from_java_http_request(env, marshalled_request, jni_http_request_body_stream);
    if (callback_data->native_request == NULL) {
        /* Exception already thrown */
        http_stream_callback_destroy(env, callback_data);
        return (jobject)NULL;
    }

    struct aws_http_make_request_options request_options = {
        .self_size = sizeof(request_options),
        .request = callback_data->native_request,
        /* Set Callbacks */
        .on_response_headers = s_on_incoming_headers_fn,
        .on_response_header_block_done = s_on_incoming_header_block_done_fn,
        .on_response_body = s_on_incoming_body_fn,
        .on_complete = s_on_stream_complete_fn,
        .user_data = callback_data,
    };

    jobject jHttpStream = NULL;

    callback_data->native_stream = aws_http_connection_make_request(native_conn, &request_options);
    if (callback_data->native_stream) {
        AWS_LOGF_TRACE(
            AWS_LS_HTTP_CONNECTION,
            "Opened new Stream on Connection. conn: %p, stream: %p",
            (void *)native_conn,
            (void *)callback_data->native_stream);

        jHttpStream = s_java_http_stream_from_native_new(env, callback_data);
    }

    /* Check for errors that might have occurred while holding the lock. */
    if (!callback_data->native_stream) {
        /* Failed to create native aws_http_stream. Clean up callback_data. */
        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");
        http_stream_callback_destroy(env, callback_data);
        return NULL;
    } else if (!jHttpStream) {
        /* Failed to create java HttpStream, but did create native aws_http_stream.
          Close connection and mark native_stream for release.
          callback_data will clean itself up when stream completes. */
        aws_http_connection_close(native_conn);
        aws_http_stream_release(callback_data->native_stream);
        return NULL;
    }

    return jHttpStream;
}