How to Use libcurl to Check HTTP/S Proxy?

37 Views Asked by At

I need to figure out if a proxy supports HTTP/S. I'm using libcurl in C++, but I'm not sure which CURLOPT_PROXYTYPE option to use for this.

My goal is to develop a program that can promptly determine if a proxy exclusively supports HTTPS, exclusively supports HTTP, supports both, or if the proxy isn't functioning. However, I'm unsure whether to use CURLPROXY_HTTPS, CURLPROXY_HTTPS2, or both for HTTPS, and CURLPROXY_HTTP or CURLPROXY_HTTP_1_0 for HTTP.

If anyone knows which CURLOPT_PROXYTYPE option I should use with libcurl to check HTTPS proxy support, I'd really appreciate your advice.

void CheckProxyClass::CheckHTTPProxy()
{
    // check http
    curl_easy_setopt(curl, CURLOPT_PROXY, proxyAddress.c_str());
    curl_easy_setopt(curl, CURLOPT_URL, targetUrl.c_str());
    curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
    curl_easy_setopt(curl, CURLOPT_NOBODY, 1L);
    curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10L); // 10 seconds
    
    CURLcode res = curl_easy_perform(curl);
    if (res == CURLE_OK) {
        std::cout << "Proxy support HTTP" << proxyAddress << std::endl;
        return true;
    } else 
    {
        std::cerr << "Failed to check proxy " << proxyAddress << ": " << curl_easy_strerror(res) << std::endl;
        return false;
    }

    // check https
    curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTPS);
    CURLcode res = curl_easy_perform(curl);
    if (res == CURLE_OK) {
        std::cout << "Proxy support HTTPS" << proxyAddress << std::endl;
        return true;
    } else 
    {
        std::cerr << "Failed to check proxy " << proxyAddress << ": " << curl_easy_strerror(res) << std::endl;
        return false;
    }
}
0

There are 0 best solutions below