< prev index next >

src/java.base/macosx/native/libnet/DefaultProxySelector.c

Print this page




  44  */
  45 
  46 #define kResolveProxyRunLoopMode CFSTR("sun.net.spi.DefaultProxySelector")
  47 
  48 #define BUFFER_SIZE 1024
  49 
  50 /* Callback for CFNetworkExecuteProxyAutoConfigurationURL. */
  51 static void proxyUrlCallback(void * client, CFArrayRef proxies, CFErrorRef error) {
  52     /* client is a pointer to a CFTypeRef and holds either proxies or an error. */
  53     CFTypeRef* resultPtr = (CFTypeRef *)client;
  54 
  55     if (error != NULL) {
  56         *resultPtr = CFRetain(error);
  57     } else {
  58         *resultPtr = CFRetain(proxies);
  59     }
  60     CFRunLoopStop(CFRunLoopGetCurrent());
  61 }
  62 
  63 /*
  64  * This will resolve all PAC URLs in the given array and return an array containing no more
  65  * PAC URLs.


  66  */
  67 static CFArrayRef createExpandedProxiesArray(CFArrayRef proxies, CFURLRef url) {
  68 
  69     CFIndex count;
  70     CFIndex index;
  71     CFMutableArrayRef expandedProxiesArray;
  72 
  73     expandedProxiesArray = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);
  74     assert(expandedProxiesArray != NULL);

  75 
  76     /* Iterate over the array of proxies */
  77     count = CFArrayGetCount(proxies);
  78     for (index = 0; index < count ; index++) {
  79         CFDictionaryRef currentProxy;
  80         CFStringRef     proxyType;
  81 
  82         currentProxy = (CFDictionaryRef) CFArrayGetValueAtIndex(proxies, index);
  83         assert(currentProxy != NULL);



  84         proxyType = (CFStringRef) CFDictionaryGetValue(currentProxy, kCFProxyTypeKey);
  85         assert(proxyType != NULL);



  86 
  87         if (!CFEqual(proxyType, kCFProxyTypeAutoConfigurationURL)) {
  88             /* This is a non PAC entry - just copy it to the new array. */
  89             CFArrayAppendValue(expandedProxiesArray, currentProxy);
  90         } else {
  91             /* This is a PAC URL and we have to resolve it. */
  92             CFRunLoopSourceRef      runLoop;
  93             CFURLRef                scriptURL;
  94             CFTypeRef               result = NULL;
  95             CFStreamClientContext   context = { 0, &result, NULL, NULL, NULL };
  96 
  97             scriptURL = CFDictionaryGetValue(currentProxy, kCFProxyAutoConfigurationURLKey);
  98 
  99             runLoop = CFNetworkExecuteProxyAutoConfigurationURL(scriptURL, url, proxyUrlCallback,
 100                                                                 &context);
 101             if (runLoop != NULL) {
 102                 /*
 103                  * Despite the fact that CFNetworkExecuteProxyAutoConfigurationURL has
 104                  * neither a "Create" nor a "Copy" in the name, we are required to
 105                  * release the return CFRunLoopSourceRef <rdar://problem/5533931>.
 106                  */
 107                 CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoop, kResolveProxyRunLoopMode);
 108                 CFRunLoopRunInMode(kResolveProxyRunLoopMode, 1.0e10, false);
 109                 CFRunLoopRemoveSource(CFRunLoopGetCurrent(), runLoop, kResolveProxyRunLoopMode);
 110 
 111                 /*
 112                  * Once the runloop returns, we should have either an error result or a
 113                  *  proxies array result. Do the appropriate thing with that result.
 114                  */
 115                 if (result != NULL) {
 116                     if (CFGetTypeID(result) == CFArrayGetTypeID()) {
 117                         /*
 118                          * Append the new array from the PAC list - it does
 119                          * contain no more PAC URIs.
 120                          */
 121                         CFArrayAppendArray(expandedProxiesArray, result,
 122                                            CFRangeMake(0, CFArrayGetCount(result)));
 123                     }
 124                     CFRelease(result);
 125                 }
 126                 CFRelease(runLoop);
 127             }
 128         }
 129     }
 130     return expandedProxiesArray;
 131 }
 132 
 133 
 134 /*
 135  * Class:     sun_net_spi_DefaultProxySelector
 136  * Method:    init
 137  * Signature: ()Z
 138  */
 139 JNIEXPORT jboolean JNICALL


 188                 memcpy(uri + protoLen, "://", 3);
 189                 memcpy(uri + protoLen + 3, chost, hostLen + 1);
 190 
 191                 urlRef = CFURLCreateWithBytes(NULL, (const UInt8 *) uri, strlen(uri),
 192                                               kCFStringEncodingUTF8, NULL);
 193                 free(uri);
 194             }
 195             (*env)->ReleaseStringUTFChars(env, host, chost);
 196         }
 197         (*env)->ReleaseStringUTFChars(env, proto, cproto);
 198     }
 199     if (urlRef != NULL) {
 200         CFArrayRef urlProxyArrayRef = CFNetworkCopyProxiesForURL(urlRef, proxyDicRef);
 201         if (urlProxyArrayRef != NULL) {
 202             CFIndex count;
 203             CFIndex index;
 204 
 205             CFArrayRef expandedProxyArray = createExpandedProxiesArray(urlProxyArrayRef, urlRef);
 206             CFRelease(urlProxyArrayRef);
 207 






 208             count = CFArrayGetCount(expandedProxyArray);
 209 
 210             proxyArray = (*env)->NewObjectArray(env, count, proxy_class, NULL);
 211             if (proxyArray != NULL || (*env)->ExceptionCheck(env)) {
 212                 /* Iterate over the expanded array of proxies */
 213                 for (index = 0; index < count ; index++) {
 214                     CFDictionaryRef currentProxy;
 215                     CFStringRef proxyType;
 216                     jobject proxy = NULL;
 217 
 218                     currentProxy = (CFDictionaryRef) CFArrayGetValueAtIndex(expandedProxyArray,
 219                                                                             index);
 220                     proxyType = (CFStringRef) CFDictionaryGetValue(currentProxy, kCFProxyTypeKey);
 221                     if (CFEqual(proxyType, kCFProxyTypeNone)) {
 222                         /*
 223                          * This entry states we should use no proxy - therefor we just create a
 224                          * NO_PROXY object.
 225                          */
 226                         proxy = (*env)->GetStaticObjectField(env, proxy_class, pr_no_proxyID);
 227                     } else {
 228                         /*
 229                          * Create a proxy object for this entry. We have to differentiate between
 230                          * socks and http type.
 231                          */
 232                         jfieldID typeID = ptype_httpID;
 233                         if (CFEqual(proxyType, kCFProxyTypeSOCKS)) {
 234                             typeID = ptype_socksID;
 235                         }
 236                         CFNumberRef portNumberRef = (CFNumberRef)CFDictionaryGetValue(currentProxy,
 237                                                     (const void*)kCFProxyPortNumberKey);
 238                         if (portNumberRef  != NULL) {
 239                             int port = 0;
 240                             if (CFNumberGetValue(portNumberRef, kCFNumberSInt32Type, &port)) {
 241                                 CFStringRef hostNameRef = (CFStringRef)CFDictionaryGetValue(
 242                                               currentProxy, (const void*)kCFProxyHostNameKey);
 243                                 if (hostNameRef != NULL) {
 244                                     char hostNameBuffer[BUFFER_SIZE];
 245                                     if (CFStringGetCString(hostNameRef, hostNameBuffer,
 246                                                            BUFFER_SIZE, kCFStringEncodingUTF8)) {
 247                                         proxy = createProxy(env, typeID, &hostNameBuffer[0], port);
 248                                     }
 249                                 }
 250                             }




  44  */
  45 
  46 #define kResolveProxyRunLoopMode CFSTR("sun.net.spi.DefaultProxySelector")
  47 
  48 #define BUFFER_SIZE 1024
  49 
  50 /* Callback for CFNetworkExecuteProxyAutoConfigurationURL. */
  51 static void proxyUrlCallback(void * client, CFArrayRef proxies, CFErrorRef error) {
  52     /* client is a pointer to a CFTypeRef and holds either proxies or an error. */
  53     CFTypeRef* resultPtr = (CFTypeRef *)client;
  54 
  55     if (error != NULL) {
  56         *resultPtr = CFRetain(error);
  57     } else {
  58         *resultPtr = CFRetain(proxies);
  59     }
  60     CFRunLoopStop(CFRunLoopGetCurrent());
  61 }
  62 
  63 /*
  64  * Returns a new array of proxies containing all the given non-PAC proxies as
  65  * well as the results of executing all the given PAC-based proxies, for the
  66  * specified URL. 'proxies' is a list that may contain both PAC and non-PAC
  67  * proxies.
  68  */
  69 static CFArrayRef createExpandedProxiesArray(CFArrayRef proxies, CFURLRef url) {
  70 
  71     CFIndex count;
  72     CFIndex index;
  73     CFMutableArrayRef expandedProxiesArray;
  74 
  75     expandedProxiesArray = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);
  76     if (expandedProxiesArray == NULL)
  77         return NULL;
  78 
  79     /* Iterate over the array of proxies */
  80     count = CFArrayGetCount(proxies);
  81     for (index = 0; index < count ; index++) {
  82         CFDictionaryRef currentProxy;
  83         CFStringRef     proxyType;
  84 
  85         currentProxy = (CFDictionaryRef) CFArrayGetValueAtIndex(proxies, index);
  86         if(currentProxy == NULL) {
  87             CFRelease(expandedProxiesArray);
  88             return NULL;
  89         }
  90         proxyType = (CFStringRef) CFDictionaryGetValue(currentProxy, kCFProxyTypeKey);
  91         if (proxyType == NULL) {
  92             CFRelease(expandedProxiesArray);
  93             return NULL;
  94         }
  95 
  96         if (!CFEqual(proxyType, kCFProxyTypeAutoConfigurationURL)) {
  97             /* Non-PAC entry, just copy it to the new array */
  98             CFArrayAppendValue(expandedProxiesArray, currentProxy);
  99         } else {
 100             /* PAC-based URL, execute its script append its results */
 101             CFRunLoopSourceRef      runLoop;
 102             CFURLRef                scriptURL;
 103             CFTypeRef               result = NULL;
 104             CFStreamClientContext   context = { 0, &result, NULL, NULL, NULL };
 105 
 106             scriptURL = CFDictionaryGetValue(currentProxy, kCFProxyAutoConfigurationURLKey);
 107 
 108             runLoop = CFNetworkExecuteProxyAutoConfigurationURL(scriptURL, url, proxyUrlCallback,
 109                                                                 &context);
 110             if (runLoop != NULL) {
 111                 /*
 112                  * Despite the fact that CFNetworkExecuteProxyAutoConfigurationURL has
 113                  * neither a "Create" nor a "Copy" in the name, we are required to
 114                  * release the return CFRunLoopSourceRef <rdar://problem/5533931>.
 115                  */
 116                 CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoop, kResolveProxyRunLoopMode);
 117                 CFRunLoopRunInMode(kResolveProxyRunLoopMode, 1.0e10, false);
 118                 CFRunLoopRemoveSource(CFRunLoopGetCurrent(), runLoop, kResolveProxyRunLoopMode);
 119 
 120                 /*
 121                  * Once the runloop returns, there will be either an error result or
 122                  * a proxies array result. Do the appropriate thing with that result.
 123                  */
 124                 if (result != NULL) {
 125                     if (CFGetTypeID(result) == CFArrayGetTypeID()) {
 126                         /*
 127                          * Append the new array from the PAC list - it contains
 128                          * only non-PAC entries.
 129                          */
 130                         CFArrayAppendArray(expandedProxiesArray, result,
 131                                            CFRangeMake(0, CFArrayGetCount(result)));
 132                     }
 133                     CFRelease(result);
 134                 }
 135                 CFRelease(runLoop);
 136             }
 137         }
 138     }
 139     return expandedProxiesArray;
 140 }
 141 
 142 
 143 /*
 144  * Class:     sun_net_spi_DefaultProxySelector
 145  * Method:    init
 146  * Signature: ()Z
 147  */
 148 JNIEXPORT jboolean JNICALL


 197                 memcpy(uri + protoLen, "://", 3);
 198                 memcpy(uri + protoLen + 3, chost, hostLen + 1);
 199 
 200                 urlRef = CFURLCreateWithBytes(NULL, (const UInt8 *) uri, strlen(uri),
 201                                               kCFStringEncodingUTF8, NULL);
 202                 free(uri);
 203             }
 204             (*env)->ReleaseStringUTFChars(env, host, chost);
 205         }
 206         (*env)->ReleaseStringUTFChars(env, proto, cproto);
 207     }
 208     if (urlRef != NULL) {
 209         CFArrayRef urlProxyArrayRef = CFNetworkCopyProxiesForURL(urlRef, proxyDicRef);
 210         if (urlProxyArrayRef != NULL) {
 211             CFIndex count;
 212             CFIndex index;
 213 
 214             CFArrayRef expandedProxyArray = createExpandedProxiesArray(urlProxyArrayRef, urlRef);
 215             CFRelease(urlProxyArrayRef);
 216 
 217             if (expandedProxyArray == NULL) {
 218                 CFRelease(urlRef);
 219                 CFRelease(proxyDicRef);
 220                 return NULL;
 221             }
 222 
 223             count = CFArrayGetCount(expandedProxyArray);
 224 
 225             proxyArray = (*env)->NewObjectArray(env, count, proxy_class, NULL);
 226             if (proxyArray != NULL || (*env)->ExceptionCheck(env)) {
 227                 /* Iterate over the expanded array of proxies */
 228                 for (index = 0; index < count ; index++) {
 229                     CFDictionaryRef currentProxy;
 230                     CFStringRef proxyType;
 231                     jobject proxy = NULL;
 232 
 233                     currentProxy = (CFDictionaryRef) CFArrayGetValueAtIndex(expandedProxyArray,
 234                                                                             index);
 235                     proxyType = (CFStringRef) CFDictionaryGetValue(currentProxy, kCFProxyTypeKey);
 236                     if (CFEqual(proxyType, kCFProxyTypeNone)) {
 237                         /* This entry states no proxy, therefore just add a NO_PROXY object. */



 238                         proxy = (*env)->GetStaticObjectField(env, proxy_class, pr_no_proxyID);
 239                     } else {
 240                         /*
 241                          * Create a proxy object for this entry.
 242                          * Differentiate between SOCKS and HTTP type.
 243                          */
 244                         jfieldID typeID = ptype_httpID;
 245                         if (CFEqual(proxyType, kCFProxyTypeSOCKS)) {
 246                             typeID = ptype_socksID;
 247                         }
 248                         CFNumberRef portNumberRef = (CFNumberRef)CFDictionaryGetValue(currentProxy,
 249                                                     (const void*)kCFProxyPortNumberKey);
 250                         if (portNumberRef  != NULL) {
 251                             int port = 0;
 252                             if (CFNumberGetValue(portNumberRef, kCFNumberSInt32Type, &port)) {
 253                                 CFStringRef hostNameRef = (CFStringRef)CFDictionaryGetValue(
 254                                               currentProxy, (const void*)kCFProxyHostNameKey);
 255                                 if (hostNameRef != NULL) {
 256                                     char hostNameBuffer[BUFFER_SIZE];
 257                                     if (CFStringGetCString(hostNameRef, hostNameBuffer,
 258                                                            BUFFER_SIZE, kCFStringEncodingUTF8)) {
 259                                         proxy = createProxy(env, typeID, &hostNameBuffer[0], port);
 260                                     }
 261                                 }
 262                             }


< prev index next >