jdk/src/java.base/share/native/libnet/proxy_util.c
author clanger
Thu, 02 Feb 2017 10:28:47 +0100
changeset 43501 243c346dc905
permissions -rw-r--r--
8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome Reviewed-by: chegar, simonis, clanger, stuefe, erikj Contributed-by: arno.zeller@sap.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
43501
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
     1
/*
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
     2
 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
     4
 *
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    10
 *
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    15
 * accompanied this code).
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    16
 *
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    20
 *
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    23
 * questions.
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    24
 */
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    25
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    26
#include "jni.h"
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    27
#include "jni_util.h"
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    28
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    29
#include "proxy_util.h"
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    30
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    31
jclass proxy_class;
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    32
jclass isaddr_class;
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    33
jclass ptype_class;
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    34
jmethodID isaddr_createUnresolvedID;
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    35
jmethodID proxy_ctrID;
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    36
jfieldID pr_no_proxyID;
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    37
jfieldID ptype_httpID;
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    38
jfieldID ptype_socksID;
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    39
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    40
int initJavaClass(JNIEnv *env) {
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    41
    jclass proxy_cls = NULL;
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    42
    jclass ptype_cls = NULL;
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    43
    jclass isaddr_cls = NULL;
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    44
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    45
    // Proxy initialization
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    46
    proxy_cls = (*env)->FindClass(env,"java/net/Proxy");
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    47
    CHECK_NULL_RETURN(proxy_cls, 0);
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    48
    proxy_class = (*env)->NewGlobalRef(env, proxy_cls);
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    49
    CHECK_NULL_RETURN(proxy_class, 0);
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    50
    proxy_ctrID = (*env)->GetMethodID(env, proxy_class, "<init>",
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    51
            "(Ljava/net/Proxy$Type;Ljava/net/SocketAddress;)V");
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    52
    CHECK_NULL_RETURN(proxy_ctrID, 0);
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    53
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    54
    // Proxy$Type initialization
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    55
    ptype_cls = (*env)->FindClass(env,"java/net/Proxy$Type");
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    56
    CHECK_NULL_RETURN(ptype_cls, 0);
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    57
    ptype_class = (*env)->NewGlobalRef(env, ptype_cls);
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    58
    CHECK_NULL_RETURN(ptype_class, 0);
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    59
    ptype_httpID = (*env)->GetStaticFieldID(env, ptype_class, "HTTP",
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    60
                                            "Ljava/net/Proxy$Type;");
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    61
    CHECK_NULL_RETURN(ptype_httpID, 0);
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    62
    ptype_socksID = (*env)->GetStaticFieldID(env, ptype_class, "SOCKS",
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    63
                                             "Ljava/net/Proxy$Type;");
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    64
    CHECK_NULL_RETURN(ptype_socksID, 0);
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    65
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    66
    // NO_PROXY
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    67
    pr_no_proxyID = (*env)->GetStaticFieldID(env, proxy_class, "NO_PROXY",
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    68
                                             "Ljava/net/Proxy;");
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    69
    CHECK_NULL_RETURN(pr_no_proxyID, 0);
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    70
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    71
    // InetSocketAddress initialization
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    72
    isaddr_cls = (*env)->FindClass(env, "java/net/InetSocketAddress");
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    73
    CHECK_NULL_RETURN(isaddr_cls, 0);
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    74
    isaddr_class = (*env)->NewGlobalRef(env, isaddr_cls);
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    75
    CHECK_NULL_RETURN(isaddr_class, 0);
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    76
    isaddr_createUnresolvedID = (*env)->GetStaticMethodID(env, isaddr_class,
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    77
            "createUnresolved",
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    78
            "(Ljava/lang/String;I)Ljava/net/InetSocketAddress;");
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    79
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    80
    return isaddr_createUnresolvedID != NULL ? 1 : 0;
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    81
}
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    82
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    83
jobject createProxy(JNIEnv *env, jfieldID ptype_ID, const char* phost, unsigned short pport) {
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    84
    jobject jProxy = NULL;
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    85
    jobject type_proxy = NULL;
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    86
    type_proxy = (*env)->GetStaticObjectField(env, ptype_class, ptype_ID);
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    87
    if (type_proxy) {
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    88
        jstring jhost = NULL;
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    89
        jhost = (*env)->NewStringUTF(env, phost);
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    90
        if (jhost) {
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    91
            jobject isa = NULL;
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    92
            isa = (*env)->CallStaticObjectMethod(env, isaddr_class,
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    93
                    isaddr_createUnresolvedID, jhost, pport);
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    94
            if (isa) {
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    95
                jProxy = (*env)->NewObject(env, proxy_class, proxy_ctrID,
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    96
                                          type_proxy, isa);
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    97
            }
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    98
        }
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
    99
    }
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
   100
    return jProxy;
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents:
diff changeset
   101
}