ADC Home > Reference Library > Technical Q&As > Internet & Web > Web Services >

Accessing HTTPS Proxy Settings


Q: In Mac OS X 10.2, the Network preferences panel has new fields to set the Secure Web Proxy (HTTPS). There does not seem to be any way to get at these settings via the Internet Config API. How do I access these settings?

A: At this point there is no way to get information about the HTTPS proxy via the Internet Config API. Instead, you will need to read this information from System Configuration framework.

Note:
In many respects the Internet Config API on Mac OS X is a compatibility API. As such, when new features are added to the underlying API (in this case, System Configuration framework), they don't necessarily make it into a compatibility API (Internet Config). This is acceptable because no existing Internet Config-based application expects to use those features.

You can get this information directly from System Configuration framework. In fact, there's a nice high-level API to do this, namely SCDynamicStoreCopyProxies. To use this API in your program, add "SystemConfiguration.framework" to your project and include the <SystemConfiguration/SystemConfiguration.h> header. You can then call SCDynamicStoreCopyProxies as shown in Listing 1.

Listing 1. Using SCDynamicStoreCopyProxies

Boolean GetHTTPSProxySetting(char *host, size_t hostSize, UInt16 *port)
    // Returns the current HTTPS proxy settings as a C string 
    // (in the buffer specified by host and hostSize) and 
    // a port number.
{
    Boolean             result;
    CFDictionaryRef     proxyDict;
    CFNumberRef         enableNum;
    int                 enable;
    CFStringRef         hostStr;
    CFNumberRef         portNum;
    int                 portInt;

    assert(host != NULL);
    assert(port != NULL);
    
    // Get the dictionary.
    
    proxyDict = SCDynamicStoreCopyProxies(NULL);
    result = (proxyDict != NULL);

    // Get the enable flag.  This isn't a CFBoolean, but a CFNumber.
    
    if (result) {
        enableNum = (CFNumberRef) CFDictionaryGetValue(proxyDict,
                kSCPropNetProxiesHTTPSEnable);

        result = (enableNum != NULL)
                && (CFGetTypeID(enableNum) == CFNumberGetTypeID());
    }
    if (result) {
        result = CFNumberGetValue(enableNum, kCFNumberIntType,
                    &enable) && (enable != 0);
    }
    
    // Get the proxy host.  DNS names must be in ASCII.  If you 
    // put a non-ASCII character  in the "Secure Web Proxy"
    // field in the Network preferences panel, the CFStringGetCString
    // function will fail and this function will return false.
    
    if (result) {
        hostStr = (CFStringRef) CFDictionaryGetValue(proxyDict,
                    kSCPropNetProxiesHTTPSProxy);

        result = (hostStr != NULL)
            && (CFGetTypeID(hostStr) == CFStringGetTypeID());
    }
    if (result) {
        result = CFStringGetCString(hostStr, host,
            (CFIndex) hostSize, kCFStringEncodingASCII);
    }
    
    // Get the proxy port.
    
    if (result) {
        portNum = (CFNumberRef) CFDictionaryGetValue(proxyDict,
                kSCPropNetProxiesHTTPSPort);

        result = (portNum != NULL)
            && (CFGetTypeID(portNum) == CFNumberGetTypeID());
    }
    if (result) {
        result = CFNumberGetValue(portNum, kCFNumberIntType, &portInt);
    }
    if (result) {
        *port = (UInt16) portInt;
    }

    // Clean up.
    
    if (proxyDict != NULL) {
        CFRelease(proxyDict);
    }
    if ( ! result ) {
        *host = 0;
        *port = 0;
    }
    return result;
}

Listing 1 only shows how to get the HTTPS proxy setting. You can get the other proxies settings by examining different keys in the proxyDict dictionary. For more information about these settings, see the comments in the <SystemConfiguration/SCSchemaDefinitions.h> header file.


[Feb 06 2003]


Did this document help you?
Yes: Tell us what works for you.
It’s good, but: Report typos, inaccuracies, and so forth.
It wasn’t helpful: Tell us what would have helped.