CurlProxySettings.cpp [plain text]
#include "config.h"
#include "CurlProxySettings.h"
#if USE(CURL)
#if PLATFORM(WIN)
#include <winsock2.h>
#endif
#include <curl/curl.h>
namespace WebCore {
static const uint16_t SocksProxyPort = 1080;
static std::optional<uint16_t> getProxyPort(const URL&);
static std::optional<String> createProxyUrl(const URL&);
CurlProxySettings::CurlProxySettings(URL&& proxyUrl, String&& ignoreHosts)
: m_mode(Mode::Custom)
, m_url(WTFMove(proxyUrl))
, m_ignoreHosts(WTFMove(ignoreHosts))
{
if (m_url.protocol().isEmpty())
m_url.setProtocol("http");
rebuildUrl();
}
void CurlProxySettings::rebuildUrl()
{
if (auto url = createProxyUrl(m_url))
m_urlSerializedWithPort = WTFMove(*url);
}
void CurlProxySettings::setUserPass(const String& user, const String& password)
{
m_url.setUser(user);
m_url.setPass(password);
rebuildUrl();
}
static long determineAuthMethod(long authMethod)
{
if (authMethod & CURLAUTH_NEGOTIATE)
return CURLAUTH_NEGOTIATE;
if (authMethod & CURLAUTH_DIGEST)
return CURLAUTH_DIGEST;
if (authMethod & CURLAUTH_NTLM)
return CURLAUTH_NTLM;
if (authMethod & CURLAUTH_BASIC)
return CURLAUTH_BASIC;
return CURLAUTH_NONE;
}
void CurlProxySettings::setAuthMethod(long authMethod)
{
m_authMethod = determineAuthMethod(authMethod);
}
bool protocolIsInSocksFamily(const URL& url)
{
return url.protocolIs("socks4") || url.protocolIs("socks4a") || url.protocolIs("socks5") || url.protocolIs("socks5h");
}
static std::optional<uint16_t> getProxyPort(const URL& url)
{
auto port = url.port();
if (port)
return port;
if (url.protocolIsInHTTPFamily())
return defaultPortForProtocol(url.protocol());
if (protocolIsInSocksFamily(url))
return SocksProxyPort;
return std::nullopt;
}
static std::optional<String> createProxyUrl(const URL &url)
{
if (url.isEmpty() || url.host().isEmpty())
return std::nullopt;
if (!url.protocolIsInHTTPFamily() && !protocolIsInSocksFamily(url))
return std::nullopt;
auto port = getProxyPort(url);
if (!port)
return std::nullopt;
auto userpass = (url.hasUsername() || url.hasPassword()) ? makeString(url.user(), ":", url.pass(), "@") : String();
return makeString(url.protocol(), "://", userpass, url.host(), ":", String::number(*port));
}
}
#endif