#include "net/proxy_resolution/proxy_chain_util_apple.h"
#include <CFNetwork/CFProxySupport.h>
#include <CoreFoundation/CoreFoundation.h>
#include <string>
#include "base/apple/foundation_util.h"
#include "base/apple/scoped_cftyperef.h"
#include "base/logging.h"
#include "base/strings/sys_string_conversions.h"
#include "net/base/host_port_pair.h"
#include "net/base/proxy_chain.h"
#include "net/base/proxy_server.h"
namespace net {
namespace {
ProxyServer::Scheme GetProxyServerScheme(CFStringRef proxy_type) {
if (CFEqual(proxy_type, kCFProxyTypeHTTP)) {
return ProxyServer::SCHEME_HTTP;
}
if (CFEqual(proxy_type, kCFProxyTypeHTTPS)) {
return ProxyServer::SCHEME_HTTP;
}
if (CFEqual(proxy_type, kCFProxyTypeSOCKS)) {
return ProxyServer::SCHEME_SOCKS5;
}
return ProxyServer::SCHEME_INVALID;
}
}
ProxyChain ProxyDictionaryToProxyChain(CFStringRef proxy_type,
CFDictionaryRef dict,
CFStringRef host_key,
CFStringRef port_key) {
ProxyServer::Scheme scheme = GetProxyServerScheme(proxy_type);
if (CFEqual(proxy_type, kCFProxyTypeNone)) {
return ProxyChain::Direct();
}
if (scheme == ProxyServer::SCHEME_INVALID) {
return ProxyChain(scheme, HostPortPair());
}
CFStringRef host_ref =
base::apple::GetValueFromDictionary<CFStringRef>(dict, host_key);
if (!host_ref) {
LOG(WARNING) << "Could not find expected key "
<< base::SysCFStringRefToUTF8(host_key)
<< " in the proxy dictionary";
return ProxyChain();
}
std::string host = base::SysCFStringRefToUTF8(host_ref);
CFNumberRef port_ref =
base::apple::GetValueFromDictionary<CFNumberRef>(dict, port_key);
int port;
if (port_ref) {
CFNumberGetValue(port_ref, kCFNumberIntType, &port);
} else {
port = ProxyServer::GetDefaultPortForScheme(scheme);
}
return ProxyChain::FromSchemeHostAndPort(scheme, host, port);
}
}