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 "URL.h" 30#include "Logging.h" 31#include <wtf/RetainPtr.h> 32#include <wtf/text/CString.h> 33 34#if PLATFORM(IOS) || PLATFORM(WIN) 35#include <CFNetwork/CFNetwork.h> 36#endif 37 38namespace WebCore { 39 40static void processProxyServers(Vector<ProxyServer>& proxyServers, CFArrayRef proxies, CFURLRef url); 41 42static void proxyAutoConfigurationResultCallback(void *context, CFArrayRef proxies, CFErrorRef error) 43{ 44 // We only expect a single result callback per invocation. Stop our runloop to unblock our caller. 45 CFRunLoopStop(CFRunLoopGetCurrent()); 46 47 Vector<ProxyServer>* proxyServers = (Vector<ProxyServer>*)context; 48 if (!proxies) { 49 ASSERT(error); 50 RetainPtr<CFStringRef> errorDescriptionCF = adoptCF(CFErrorCopyDescription(error)); 51 String errorDescription(errorDescriptionCF.get()); 52 LOG(Network, "Failed to process proxy auto-configuration file with error: %s", errorDescription.utf8().data()); 53 return; 54 } 55 56 processProxyServers(*proxyServers, proxies, 0); 57} 58 59static void processProxyServers(Vector<ProxyServer>& proxyServers, CFArrayRef proxies, CFURLRef url) 60{ 61 CFIndex numProxies = CFArrayGetCount(proxies); 62 for (CFIndex i = 0; i < numProxies; ++i) { 63 CFDictionaryRef proxyDictionary = static_cast<CFDictionaryRef>(CFArrayGetValueAtIndex(proxies, i)); 64 65 ProxyServer::Type type = ProxyServer::Direct; 66 CFStringRef typeString = static_cast<CFStringRef>(CFDictionaryGetValue(proxyDictionary, kCFProxyTypeKey)); 67 68 if (!url) { 69 // If we have no URL then we're processing an auto-configuration response. 70 // It isn't sensible to receive another auto-configured proxy in such a response. 71 ASSERT(!CFEqual(typeString, kCFProxyTypeAutoConfigurationURL)); 72 } 73 74 if (CFEqual(typeString, kCFProxyTypeAutoConfigurationURL)) { 75 if (!url) 76 continue; 77 78 // FIXME: We should restructure to allow this to happen asynchronously. 79 CFURLRef scriptURL = static_cast<CFURLRef>(CFDictionaryGetValue(proxyDictionary, kCFProxyAutoConfigurationURLKey)); 80 if (!scriptURL || CFGetTypeID(scriptURL) != CFURLGetTypeID()) 81 continue; 82 83 CFStreamClientContext context = { 0, (void*)&proxyServers, 0, 0, 0 }; 84 RetainPtr<CFRunLoopSourceRef> runLoopSource = adoptCF(CFNetworkExecuteProxyAutoConfigurationURL(scriptURL, url, proxyAutoConfigurationResultCallback, &context)); 85 86 CFStringRef privateRunLoopMode = CFSTR("com.apple.WebKit.ProxyAutoConfiguration"); 87 CFTimeInterval timeout = 5; 88 CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource.get(), privateRunLoopMode); 89 CFRunLoopRunInMode(privateRunLoopMode, timeout, 0); 90 CFRunLoopRemoveSource(CFRunLoopGetCurrent(), runLoopSource.get(), privateRunLoopMode); 91 CFRunLoopSourceInvalidate(runLoopSource.get()); 92 93 // The proxyAutoConfigurationResultCallback has added any relevant ProxyServers to proxyServers. 94 continue; 95 } 96 97 if (CFEqual(typeString, kCFProxyTypeNone)) { 98 proxyServers.append(ProxyServer(ProxyServer::Direct, String(), -1)); 99 continue; 100 } 101 102 if (CFEqual(typeString, kCFProxyTypeHTTP)) 103 type = ProxyServer::HTTP; 104 else if (CFEqual(typeString, kCFProxyTypeHTTPS)) 105 type = ProxyServer::HTTPS; 106 else if (CFEqual(typeString, kCFProxyTypeSOCKS)) 107 type = ProxyServer::SOCKS; 108 else { 109 // We don't know how to handle this type. 110 continue; 111 } 112 113 CFStringRef host = static_cast<CFStringRef>(CFDictionaryGetValue(proxyDictionary, kCFProxyHostNameKey)); 114 CFNumberRef port = static_cast<CFNumberRef>(CFDictionaryGetValue(proxyDictionary, kCFProxyPortNumberKey)); 115 SInt32 portValue; 116 CFNumberGetValue(port, kCFNumberSInt32Type, &portValue); 117 118 proxyServers.append(ProxyServer(type, host, portValue)); 119 } 120} 121 122static void addProxyServersForURL(Vector<ProxyServer>& proxyServers, const URL& url) 123{ 124 RetainPtr<CFDictionaryRef> proxySettings = adoptCF(CFNetworkCopySystemProxySettings()); 125 if (!proxySettings) 126 return; 127 128 RetainPtr<CFURLRef> cfURL = url.createCFURL(); 129 RetainPtr<CFArrayRef> proxiesForURL = adoptCF(CFNetworkCopyProxiesForURL(cfURL.get(), proxySettings.get())); 130 if (!proxiesForURL) 131 return; 132 133 processProxyServers(proxyServers, proxiesForURL.get(), cfURL.get()); 134} 135 136Vector<ProxyServer> proxyServersForURL(const URL& url, const NetworkingContext*) 137{ 138 Vector<ProxyServer> proxyServers; 139 140 addProxyServersForURL(proxyServers, url); 141 return proxyServers; 142 143} 144 145} // namespace WebCore 146