static int ConfigureProxySettings()

in CodeSnippets/Networking/Proxy/SetProxySettings/set_proxy_settings.c [33:89]


static int ConfigureProxySettings(void)
{
    int result = -1;

    // By default, proxy configuration option Networking_ProxyOptions_Enabled is set and the proxy
    // type is Networking_ProxyType_HTTP.
    Networking_ProxyConfig *proxyConfig = Networking_Proxy_Create();
    if (proxyConfig == NULL) {
        Log_Debug("ERROR: Networking_Proxy_Create(): %d (%s)\n", errno, strerror(errno));
        goto cleanup;
    }

    // Set the proxy address and port.
    if (Networking_Proxy_SetProxyAddress(proxyConfig, proxyAddress, proxyPort) == -1) {
        Log_Debug("ERROR: Networking_Proxy_SetProxyAddress(): %d (%s)\n", errno, strerror(errno));
        goto cleanup;
    }

    // If both username and password are set, use basic authentication. Otherwise use anonymous
    // authentication.
    if ((proxyUsername != NULL) && (proxyPassword != NULL)) {
        if (Networking_Proxy_SetBasicAuthentication(proxyConfig, proxyUsername, proxyPassword) ==
            -1) {
            Log_Debug("ERROR: Networking_Proxy_SetBasicAuthentication(): %d (%s)\n", errno,
                      strerror(errno));
            goto cleanup;
        }
    } else {
        if (Networking_Proxy_SetAnonymousAuthentication(proxyConfig) == -1) {
            Log_Debug("ERROR: Networking_Proxy_SetAnonymousAuthentication(): %d (%s)\n", errno,
                      strerror(errno));
            goto cleanup;
        }
    }

    // Set addresses for which proxy should not be used if "noProxyAddresses" is modified.
    if (noProxyAddresses != NULL) {
        if (Networking_Proxy_SetProxyNoProxyAddresses(proxyConfig, noProxyAddresses) == -1) {
            Log_Debug("ERROR: Networking_Proxy_SetProxyNoProxyAddresses(): %d (%s)\n", errno,
                      strerror(errno));
            goto cleanup;
        }
    }

    // Apply the proxy configuration.
    if (Networking_Proxy_Apply(proxyConfig) == -1) {
        Log_Debug("ERROR: Networking_Proxy_Apply(): %d (%s)\n", errno, strerror(errno));
        goto cleanup;
    }
    result = 0;

cleanup:
    if (proxyConfig) {
        Networking_Proxy_Destroy(proxyConfig);
    }
    return result;
}