HTTPAPI_RESULT HTTPAPI_CloneOption()

in adapters/httpapi_curl.c [1149:1323]


HTTPAPI_RESULT HTTPAPI_CloneOption(const char* optionName, const void* value, const void** savedValue)
{
    HTTPAPI_RESULT result;
    if (
        (optionName == NULL) ||
        (value == NULL) ||
        (savedValue == NULL)
        )
    {
        result = HTTPAPI_INVALID_ARG;
        LogError("invalid argument(NULL) passed to HTTPAPI_CloneOption");
    }
    else
    {
        if (strcmp(OPTION_HTTP_TIMEOUT, optionName) == 0)
        {
            /*by convention value is pointing to an unsigned int */
            unsigned int* temp = malloc(sizeof(unsigned int)); /*shall be freed by HTTPAPIEX*/
            if (temp == NULL)
            {
                result = HTTPAPI_ERROR;
                LogError("malloc failed (result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result));
            }
            else
            {
                *temp = *(const unsigned int*)value;
                *savedValue = temp;
                result = HTTPAPI_OK;
            }
        }
        else if (strcmp(SU_OPTION_X509_CERT, optionName) == 0 || strcmp(OPTION_X509_ECC_CERT, optionName) == 0)
        {
            /*this is getting the x509 certificate. In this case, value is a pointer to a const char* that contains the certificate as a null terminated string*/
            if (mallocAndStrcpy_s((char**)savedValue, value) != 0)
            {
                LogError("unable to clone the x509 certificate content");
                result = HTTPAPI_ERROR;
            }
            else
            {
                /*return OK when the certificate has been cloned successfully*/
                result = HTTPAPI_OK;
            }
        }
#ifdef USE_OPENSSL
        else if (strcmp(OPTION_OPENSSL_PRIVATE_KEY_TYPE, optionName) == 0)
        {
            const OPTION_OPENSSL_KEY_TYPE type = *(const OPTION_OPENSSL_KEY_TYPE*)value;
            if (type == KEY_TYPE_DEFAULT || type == KEY_TYPE_ENGINE)
            {
                OPTION_OPENSSL_KEY_TYPE* temp = malloc(sizeof(OPTION_OPENSSL_KEY_TYPE));
                if (temp == NULL)
                {
                    LogError("unable to clone x509PrivatekeyType");
                    result = HTTPAPI_ERROR;
                }
                else
                {
                    *temp = type;
                    *savedValue = temp;
                    result = HTTPAPI_OK;
                }
            }
            else
            {
                LogError("Unknown x509PrivatekeyType: %i", type);
                result = HTTPAPI_ERROR;
            }
        }
        else if (strcmp(OPTION_OPENSSL_ENGINE, optionName) == 0)
        {
            /*this is getting the engine. In this case, value is a pointer to a const char* that contains the engine as a null terminated string*/
            if (mallocAndStrcpy_s((char**)savedValue, value) != 0)
            {
                LogError("unable to clone %s", optionName);
                result = HTTPAPI_ERROR;
            }
            else
            {
                /*return OK when the engine has been cloned successfully*/
                result = HTTPAPI_OK;
            }
        }
#endif
        else if (strcmp(SU_OPTION_X509_PRIVATE_KEY, optionName) == 0 || strcmp(OPTION_X509_ECC_KEY, optionName) == 0)
        {
            /*this is getting the x509 private key. In this case, value is a pointer to a const char* that contains the private key as a null terminated string*/
            if (mallocAndStrcpy_s((char**)savedValue, value) != 0)
            {
                LogError("unable to clone the x509 private key content");
                result = HTTPAPI_ERROR;
            }
            else
            {
                /*return OK when the private key has been cloned successfully*/
                result = HTTPAPI_OK;
            }
        }
        else if (strcmp("TrustedCerts", optionName) == 0)
        {
            if (mallocAndStrcpy_s((char**)savedValue, value) != 0)
            {
                LogError("unable to clone TrustedCerts");
                result = HTTPAPI_ERROR;
            }
            else
            {
                /*return OK when the certificates have been cloned successfully*/
                result = HTTPAPI_OK;
            }
        }
        else if (strcmp(OPTION_HTTP_PROXY, optionName) == 0)
        {
            HTTP_PROXY_OPTIONS* proxy_data = (HTTP_PROXY_OPTIONS*)value;

            HTTP_PROXY_OPTIONS* new_proxy_info = malloc(sizeof(HTTP_PROXY_OPTIONS));
            if (new_proxy_info == NULL)
            {
                LogError("unable to allocate proxy option information");
                result = HTTPAPI_ERROR;
            }
            else
            {
                new_proxy_info->host_address = proxy_data->host_address;
                new_proxy_info->port = proxy_data->port;
                new_proxy_info->password = proxy_data->password;
                new_proxy_info->username = proxy_data->username;
                *savedValue = new_proxy_info;
                result = HTTPAPI_OK;
            }
        }
        /*all "long" options are cloned in the same way*/
        else if (
            (strcmp(OPTION_CURL_LOW_SPEED_LIMIT, optionName) == 0) ||
            (strcmp(OPTION_CURL_LOW_SPEED_TIME, optionName) == 0) ||
            (strcmp(OPTION_CURL_FRESH_CONNECT, optionName) == 0) ||
            (strcmp(OPTION_CURL_FORBID_REUSE, optionName) == 0) ||
            (strcmp(OPTION_CURL_VERBOSE, optionName) == 0)
            )
        {
            /*by convention value is pointing to an long */
            long* temp = malloc(sizeof(long)); /*shall be freed by HTTPAPIEX*/
            if (temp == NULL)
            {
                result = HTTPAPI_ERROR;
                LogError("malloc failed (result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result));
            }
            else
            {
                *temp = *(const long*)value;
                *savedValue = temp;
                result = HTTPAPI_OK;
            }
        }
        else if (strcmp(OPTION_CURL_INTERFACE, optionName) == 0)
        {
            if (mallocAndStrcpy_s((char**)savedValue, value) != 0)
            {
                LogError("unable to clone the interface name");
                result = HTTPAPI_ERROR;
            }
            else
            {
                /*return OK when the interface name has been cloned successfully*/
                result = HTTPAPI_OK;
            }
        }
        else
        {
            result = HTTPAPI_INVALID_ARG;
            LogError("unknown option %s", optionName);
        }
    }
    return result;
}