This source file includes following definitions.
- GetSchemeFromPacTypeInternal
- GetSchemeFromURIInternal
- HostNoBrackets
- host_port_pair_
- host_port_pair
- FromURI
- FromURI
- ToURI
- FromPacString
- FromPacString
- ToPacString
- GetDefaultPortForScheme
- GetSchemeFromURI
- isDataReductionProxy
- isDataReductionProxyFallback
- FromSchemeHostAndPort
#include "net/proxy/proxy_server.h"
#include <algorithm>
#include "base/strings/string_util.h"
#include "net/base/net_util.h"
#include "net/http/http_util.h"
namespace net {
namespace {
ProxyServer::Scheme GetSchemeFromPacTypeInternal(
std::string::const_iterator begin,
std::string::const_iterator end) {
if (LowerCaseEqualsASCII(begin, end, "proxy"))
return ProxyServer::SCHEME_HTTP;
if (LowerCaseEqualsASCII(begin, end, "socks")) {
return ProxyServer::SCHEME_SOCKS4;
}
if (LowerCaseEqualsASCII(begin, end, "socks4"))
return ProxyServer::SCHEME_SOCKS4;
if (LowerCaseEqualsASCII(begin, end, "socks5"))
return ProxyServer::SCHEME_SOCKS5;
if (LowerCaseEqualsASCII(begin, end, "direct"))
return ProxyServer::SCHEME_DIRECT;
if (LowerCaseEqualsASCII(begin, end, "https"))
return ProxyServer::SCHEME_HTTPS;
if (LowerCaseEqualsASCII(begin, end, "quic"))
return ProxyServer::SCHEME_QUIC;
return ProxyServer::SCHEME_INVALID;
}
ProxyServer::Scheme GetSchemeFromURIInternal(std::string::const_iterator begin,
std::string::const_iterator end) {
if (LowerCaseEqualsASCII(begin, end, "http"))
return ProxyServer::SCHEME_HTTP;
if (LowerCaseEqualsASCII(begin, end, "socks4"))
return ProxyServer::SCHEME_SOCKS4;
if (LowerCaseEqualsASCII(begin, end, "socks"))
return ProxyServer::SCHEME_SOCKS5;
if (LowerCaseEqualsASCII(begin, end, "socks5"))
return ProxyServer::SCHEME_SOCKS5;
if (LowerCaseEqualsASCII(begin, end, "direct"))
return ProxyServer::SCHEME_DIRECT;
if (LowerCaseEqualsASCII(begin, end, "https"))
return ProxyServer::SCHEME_HTTPS;
if (LowerCaseEqualsASCII(begin, end, "quic"))
return ProxyServer::SCHEME_QUIC;
return ProxyServer::SCHEME_INVALID;
}
std::string HostNoBrackets(const std::string& host) {
const std::string::size_type len = host.size();
if (len >= 2 && host[0] == '[' && host[len - 1] == ']')
return host.substr(1, len - 2);
return host;
}
}
ProxyServer::ProxyServer(Scheme scheme, const HostPortPair& host_port_pair)
: scheme_(scheme), host_port_pair_(host_port_pair) {
if (scheme_ == SCHEME_DIRECT || scheme_ == SCHEME_INVALID) {
DCHECK(host_port_pair.Equals(HostPortPair()));
host_port_pair_ = HostPortPair();
}
}
const HostPortPair& ProxyServer::host_port_pair() const {
DCHECK(is_valid() && !is_direct());
return host_port_pair_;
}
ProxyServer ProxyServer::FromURI(const std::string& uri,
Scheme default_scheme) {
return FromURI(uri.begin(), uri.end(), default_scheme);
}
ProxyServer ProxyServer::FromURI(std::string::const_iterator begin,
std::string::const_iterator end,
Scheme default_scheme) {
Scheme scheme = default_scheme;
HttpUtil::TrimLWS(&begin, &end);
std::string::const_iterator colon = std::find(begin, end, ':');
if (colon != end &&
(end - colon) >= 3 &&
*(colon + 1) == '/' &&
*(colon + 2) == '/') {
scheme = GetSchemeFromURIInternal(begin, colon);
begin = colon + 3;
}
return FromSchemeHostAndPort(scheme, begin, end);
}
std::string ProxyServer::ToURI() const {
switch (scheme_) {
case SCHEME_DIRECT:
return "direct://";
case SCHEME_HTTP:
return host_port_pair().ToString();
case SCHEME_SOCKS4:
return std::string("socks4://") + host_port_pair().ToString();
case SCHEME_SOCKS5:
return std::string("socks5://") + host_port_pair().ToString();
case SCHEME_HTTPS:
return std::string("https://") + host_port_pair().ToString();
case SCHEME_QUIC:
return std::string("quic://") + host_port_pair().ToString();
default:
NOTREACHED();
return std::string();
}
}
ProxyServer ProxyServer::FromPacString(const std::string& pac_string) {
return FromPacString(pac_string.begin(), pac_string.end());
}
ProxyServer ProxyServer::FromPacString(std::string::const_iterator begin,
std::string::const_iterator end) {
HttpUtil::TrimLWS(&begin, &end);
std::string::const_iterator space;
for (space = begin; space != end; ++space) {
if (HttpUtil::IsLWS(*space)) {
break;
}
}
Scheme scheme = GetSchemeFromPacTypeInternal(begin, space);
return FromSchemeHostAndPort(scheme, space, end);
}
std::string ProxyServer::ToPacString() const {
switch (scheme_) {
case SCHEME_DIRECT:
return "DIRECT";
case SCHEME_HTTP:
return std::string("PROXY ") + host_port_pair().ToString();
case SCHEME_SOCKS4:
return std::string("SOCKS ") + host_port_pair().ToString();
case SCHEME_SOCKS5:
return std::string("SOCKS5 ") + host_port_pair().ToString();
case SCHEME_HTTPS:
return std::string("HTTPS ") + host_port_pair().ToString();
case SCHEME_QUIC:
return std::string("QUIC ") + host_port_pair().ToString();
default:
NOTREACHED();
return std::string();
}
}
int ProxyServer::GetDefaultPortForScheme(Scheme scheme) {
switch (scheme) {
case SCHEME_HTTP:
return 80;
case SCHEME_SOCKS4:
case SCHEME_SOCKS5:
return 1080;
case SCHEME_HTTPS:
case SCHEME_QUIC:
return 443;
case SCHEME_INVALID:
case SCHEME_DIRECT:
break;
}
return -1;
}
ProxyServer::Scheme ProxyServer::GetSchemeFromURI(const std::string& scheme) {
return GetSchemeFromURIInternal(scheme.begin(), scheme.end());
}
#if defined(SPDY_PROXY_AUTH_ORIGIN)
bool ProxyServer::isDataReductionProxy() const {
bool dev_host = false;
#if defined (DATA_REDUCTION_DEV_HOST)
dev_host = host_port_pair_.Equals(
HostPortPair::FromURL(GURL(DATA_REDUCTION_DEV_HOST)));
#endif
return dev_host || host_port_pair_.Equals(
HostPortPair::FromURL(GURL(SPDY_PROXY_AUTH_ORIGIN)));
}
bool ProxyServer::isDataReductionProxyFallback() const {
#if defined(DATA_REDUCTION_FALLBACK_HOST)
return host_port_pair_.Equals(
HostPortPair::FromURL(GURL(DATA_REDUCTION_FALLBACK_HOST)));
#endif
return false;
}
#endif
ProxyServer ProxyServer::FromSchemeHostAndPort(
Scheme scheme,
std::string::const_iterator begin,
std::string::const_iterator end) {
HttpUtil::TrimLWS(&begin, &end);
if (scheme == SCHEME_DIRECT && begin != end)
return ProxyServer();
HostPortPair host_port_pair;
if (scheme != SCHEME_INVALID && scheme != SCHEME_DIRECT) {
std::string host;
int port = -1;
bool ok = net::ParseHostAndPort(begin, end, &host, &port);
if (!ok)
return ProxyServer();
if (port == -1)
port = GetDefaultPortForScheme(scheme);
host_port_pair = HostPortPair(HostNoBrackets(host), port);
}
return ProxyServer(scheme, host_port_pair);
}
}