Apple has created an example application for this purpose called CFProxySupportTool.
CFProxySupportTool shows how to use the CFProxySupport API to determine if a network connection should go through a proxy; this is useful if you are not using Apple’s high-level network APIs (such as CFNetwork and the URL loading system), but still want to interpret the proxy settings provided by the system.
It is currently available at
https://developer.apple.com/library/mac/#samplecode/CFProxySupportTool/Introduction/Intro.html
The code is not completely accurate (more than 1000 lines) and is written in simple C. You can also see the source code of the startRequest ASIHTTPRequest function, which seems to be based on CFProxySupportTool.
Here begins:
systemProxySettings = [(NSDictionary *) CFNetworkCopySystemProxySettings() autorelease];
proxies = [(NSArray *) CFNetworkCopyProxiesForURL((CFURLRef) URL, (CFDictionaryRef) systemProxySettings) autorelease];
if (!proxies.count)
return;
firstProxySettings = [proxies objectAtIndex:0];
if (nil != (pacScriptURL = [firstProxySettings objectForKey:(NSString *)kCFProxyAutoConfigurationURLKey]))
{
CFErrorRef cfErrorRef = NULL;
NSError *nsError = nil;
NSString *script;
script = [NSString stringWithContentsOfURL:pacScriptURL
usedEncoding:NULL
error:&nsError];
if (nsError)
return;
proxies = [(NSArray *) CFNetworkCopyProxiesForAutoConfigurationScript((CFStringRef) script, (CFURLRef) URL, &cfErrorRef) autorelease];
if (cfErrorRef || !proxies.count)
return;
firstProxySettings = [proxies objectAtIndex:0];
}
source
share