1 // Copyright 2011 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "net/base/proxy_string_util.h"
6
7 #include <CoreFoundation/CoreFoundation.h>
8 #include <string>
9
10 #include "base/logging.h"
11 #include "base/mac/foundation_util.h"
12 #include "base/strings/sys_string_conversions.h"
13 #include "net/base/host_port_pair.h"
14 #include "net/base/proxy_server.h"
15
16 namespace net {
17
ProxyDictionaryToProxyServer(ProxyServer::Scheme scheme,CFDictionaryRef dict,CFStringRef host_key,CFStringRef port_key)18 ProxyServer ProxyDictionaryToProxyServer(ProxyServer::Scheme scheme,
19 CFDictionaryRef dict,
20 CFStringRef host_key,
21 CFStringRef port_key) {
22 if (scheme == ProxyServer::SCHEME_INVALID ||
23 scheme == ProxyServer::SCHEME_DIRECT) {
24 // No hostname port to extract; we are done.
25 return ProxyServer(scheme, HostPortPair());
26 }
27
28 CFStringRef host_ref =
29 base::mac::GetValueFromDictionary<CFStringRef>(dict, host_key);
30 if (!host_ref) {
31 LOG(WARNING) << "Could not find expected key "
32 << base::SysCFStringRefToUTF8(host_key)
33 << " in the proxy dictionary";
34 return ProxyServer(); // Invalid.
35 }
36 std::string host = base::SysCFStringRefToUTF8(host_ref);
37
38 CFNumberRef port_ref =
39 base::mac::GetValueFromDictionary<CFNumberRef>(dict, port_key);
40 int port;
41 if (port_ref) {
42 CFNumberGetValue(port_ref, kCFNumberIntType, &port);
43 } else {
44 port = ProxyServer::GetDefaultPortForScheme(scheme);
45 }
46
47 return ProxyServer(scheme, HostPortPair(host, port));
48 }
49
50 } // namespace net
51