57d714be72b380488b52272e88b691ee5bedd779
[WebKit-https.git] / Source / WebCore / platform / network / cf / ProxyServerCFNet.cpp
1 /*
2  * Copyright (C) 2010 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include "ProxyServer.h"
28
29 #include "KURL.h"
30 #include <wtf/RetainPtr.h>
31
32 #if PLATFORM(WIN)
33 #include <CFNetwork/CFNetwork.h>
34 #endif
35
36 namespace WebCore {
37
38 #if !defined(BUILDING_ON_TIGER) && !defined(BUILDING_ON_LEOPARD)
39 static void addProxyServersForURL(Vector<ProxyServer>& proxyServers, const KURL& url)
40 {
41     RetainPtr<CFDictionaryRef> proxySettings(AdoptCF, CFNetworkCopySystemProxySettings());
42     if (!proxySettings)
43         return;
44
45     RetainPtr<CFURLRef> cfURL(AdoptCF, url.createCFURL());
46     RetainPtr<CFArrayRef> proxiesForURL(AdoptCF, CFNetworkCopyProxiesForURL(cfURL.get(), proxySettings.get()));
47     if (!proxiesForURL)
48         return;
49
50     CFIndex numProxies = CFArrayGetCount(proxiesForURL.get());
51     for (CFIndex i = 0; i < numProxies; ++i) {
52         CFDictionaryRef proxyDictionary = static_cast<CFDictionaryRef>(CFArrayGetValueAtIndex(proxiesForURL.get(), i));
53
54         ProxyServer::Type type = ProxyServer::Direct;
55         CFStringRef typeString = static_cast<CFStringRef>(CFDictionaryGetValue(proxyDictionary, kCFProxyTypeKey));
56         if (CFEqual(typeString, kCFProxyTypeAutoConfigurationURL)) {
57             // FIXME: Handle PAC URLs.
58             continue;
59         } 
60         
61         if (CFEqual(typeString, kCFProxyTypeNone)) {
62             proxyServers.append(ProxyServer(ProxyServer::Direct, String(), -1));
63             continue;
64         }
65         
66         if (CFEqual(typeString, kCFProxyTypeHTTP))
67             type = ProxyServer::HTTP;
68         else if (CFEqual(typeString, kCFProxyTypeHTTPS))
69             type = ProxyServer::HTTPS;
70         else if (CFEqual(typeString, kCFProxyTypeSOCKS))
71             type = ProxyServer::SOCKS;
72         else {
73             // We don't know how to handle this type.
74             continue;
75         }
76
77         CFStringRef host = static_cast<CFStringRef>(CFDictionaryGetValue(proxyDictionary, kCFProxyHostNameKey));
78         CFNumberRef port = static_cast<CFNumberRef>(CFDictionaryGetValue(proxyDictionary, kCFProxyPortNumberKey));
79         SInt32 portValue;
80         CFNumberGetValue(port, kCFNumberSInt32Type, &portValue);
81
82         proxyServers.append(ProxyServer(type, host, portValue));
83     }
84 }
85
86 Vector<ProxyServer> proxyServersForURL(const KURL& url, const NetworkingContext*)
87 {
88     Vector<ProxyServer> proxyServers;
89     
90     addProxyServersForURL(proxyServers, url);
91     return proxyServers;
92     
93 }
94 #else
95 Vector<ProxyServer> proxyServersForURL(const KURL&, const NetworkingContext*)
96 {
97     // FIXME: Implement.
98     return Vector<ProxyServer>();
99 }
100 #endif
101
102 } // namespace WebCore