napi_value aws_napi_http_proxy_options_new()

in source/http_connection.c [35:135]


napi_value aws_napi_http_proxy_options_new(napi_env env, napi_callback_info info) {

    napi_value node_args[7];
    size_t num_args = AWS_ARRAY_SIZE(node_args);

    napi_value *arg = &node_args[0];
    if (napi_get_cb_info(env, info, &num_args, node_args, NULL, NULL)) {
        napi_throw_error(env, NULL, "Failed to retrieve callback information");
        return NULL;
    }
    if (num_args != AWS_ARRAY_SIZE(node_args)) {
        napi_throw_error(env, NULL, "http_proxy_options_new requires exactly 7 arguments");
        return NULL;
    }

    napi_value node_external = NULL; /* return value, external that wraps the aws_tls_connection_options */

    struct aws_allocator *allocator = aws_napi_get_allocator();
    struct http_proxy_options_binding *binding =
        aws_mem_calloc(allocator, 1, sizeof(struct http_proxy_options_binding));
    AWS_FATAL_ASSERT(binding && "Failed to allocate new http_proxy_options_binding");
    binding->allocator = allocator;

    napi_value node_host_name = *arg++;
    binding->host_name = aws_string_new_from_napi(env, node_host_name);
    if (!binding->host_name) {
        AWS_NAPI_ENSURE(env, napi_throw_type_error(env, NULL, "Unable to convert host_name to string"));
        goto cleanup;
    }
    binding->native.host = aws_byte_cursor_from_string(binding->host_name);

    napi_value node_port = *arg++;
    uint32_t port;
    AWS_NAPI_CALL(env, napi_get_value_uint32(env, node_port, &port), {
        napi_throw_type_error(env, NULL, "port must be a number");
        goto cleanup;
    });
    binding->native.port = (uint16_t)port;

    napi_value node_auth_method = *arg++;
    if (!aws_napi_is_null_or_undefined(env, node_auth_method)) {
        uint32_t auth_method = 0;
        AWS_NAPI_CALL(env, napi_get_value_uint32(env, node_auth_method, &auth_method), {
            napi_throw_type_error(env, NULL, "auth_method must be a number");
            goto cleanup;
        });
        binding->native.auth_type = auth_method;
    }

    napi_value node_username = *arg++;
    if (!aws_napi_is_null_or_undefined(env, node_username)) {
        binding->auth_username = aws_string_new_from_napi(env, node_username);
        if (!binding->auth_username) {
            napi_throw_type_error(env, NULL, "Unable to convert auth_username to string");
            goto cleanup;
        }
        binding->native.auth_username = aws_byte_cursor_from_string(binding->auth_username);
    }

    napi_value node_password = *arg++;
    if (!aws_napi_is_null_or_undefined(env, node_password)) {
        binding->auth_password = aws_string_new_from_napi(env, node_password);
        if (!binding->auth_password) {
            napi_throw_type_error(env, NULL, "Unable to convert auth_password to string");
            goto cleanup;
        }
        binding->native.auth_password = aws_byte_cursor_from_string(binding->auth_password);
    }

    napi_value node_tls_opts = *arg++;
    if (!aws_napi_is_null_or_undefined(env, node_tls_opts)) {
        AWS_NAPI_CALL(env, napi_get_value_external(env, node_tls_opts, (void **)&binding->native.tls_options), {
            napi_throw_error(env, NULL, "Failed to extract tls_ctx from external");
            goto cleanup;
        });
    }

    napi_value node_connection_type = *arg++;
    if (!aws_napi_is_null_or_undefined(env, node_connection_type)) {
        uint32_t connection_type = 0;
        AWS_NAPI_CALL(env, napi_get_value_uint32(env, node_connection_type, &connection_type), {
            napi_throw_type_error(env, NULL, "connection_type must be a number");
            goto cleanup;
        });
        binding->native.connection_type = connection_type;
    }

    if (binding->native.connection_type == AWS_HPCT_HTTP_FORWARD && binding->native.tls_options != NULL) {
        AWS_NAPI_ENSURE(env, napi_throw_type_error(env, NULL, "Forwarding proxy connections cannot use tls"));
        goto cleanup;
    }

    AWS_NAPI_CALL(
        env, napi_create_external(env, binding, s_proxy_options_finalize, NULL, &node_external), { goto cleanup; });

    return node_external;

cleanup:
    s_proxy_options_finalize(env, binding, NULL);
    return NULL;
}