Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/windows/native/sun/net/spi/DefaultProxySelector.c
32288 views
1
/*
2
* Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
#include <windows.h>
27
#include "jni.h"
28
#include "jni_util.h"
29
#include "jvm.h"
30
#include "jlong.h"
31
#include "sun_net_spi_DefaultProxySelector.h"
32
33
/**
34
* These functions are used by the sun.net.spi.DefaultProxySelector class
35
* to access some platform specific settings.
36
* This is the Windows code using the registry settings.
37
*/
38
39
static jclass proxy_class;
40
static jclass isaddr_class;
41
static jclass ptype_class;
42
static jmethodID isaddr_createUnresolvedID;
43
static jmethodID proxy_ctrID;
44
static jfieldID pr_no_proxyID;
45
static jfieldID ptype_httpID;
46
static jfieldID ptype_socksID;
47
48
#define CHECK_NULL(X) { if ((X) == NULL) fprintf (stderr,"JNI errror at line %d\n", __LINE__); }
49
50
51
/*
52
* Class: sun_net_spi_DefaultProxySelector
53
* Method: init
54
* Signature: ()Z
55
*/
56
JNIEXPORT jboolean JNICALL
57
Java_sun_net_spi_DefaultProxySelector_init(JNIEnv *env, jclass clazz) {
58
HKEY hKey;
59
LONG ret;
60
jclass cls;
61
62
/**
63
* Get all the method & field IDs for later use.
64
*/
65
CHECK_NULL(cls = (*env)->FindClass(env,"java/net/Proxy"));
66
proxy_class = (*env)->NewGlobalRef(env, cls);
67
CHECK_NULL(cls = (*env)->FindClass(env,"java/net/Proxy$Type"));
68
ptype_class = (*env)->NewGlobalRef(env, cls);
69
CHECK_NULL(cls = (*env)->FindClass(env, "java/net/InetSocketAddress"));
70
isaddr_class = (*env)->NewGlobalRef(env, cls);
71
proxy_ctrID = (*env)->GetMethodID(env, proxy_class, "<init>", "(Ljava/net/Proxy$Type;Ljava/net/SocketAddress;)V");
72
pr_no_proxyID = (*env)->GetStaticFieldID(env, proxy_class, "NO_PROXY", "Ljava/net/Proxy;");
73
ptype_httpID = (*env)->GetStaticFieldID(env, ptype_class, "HTTP", "Ljava/net/Proxy$Type;");
74
ptype_socksID = (*env)->GetStaticFieldID(env, ptype_class, "SOCKS", "Ljava/net/Proxy$Type;");
75
isaddr_createUnresolvedID = (*env)->GetStaticMethodID(env, isaddr_class, "createUnresolved", "(Ljava/lang/String;I)Ljava/net/InetSocketAddress;");
76
77
/**
78
* Let's see if we can find the proper Registry entry.
79
*/
80
ret = RegOpenKeyEx(HKEY_CURRENT_USER,
81
"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings",
82
0, KEY_READ, (PHKEY)&hKey);
83
if (ret == ERROR_SUCCESS) {
84
RegCloseKey(hKey);
85
/**
86
* It worked, we can probably rely on it then.
87
*/
88
return JNI_TRUE;
89
}
90
91
return JNI_FALSE;
92
}
93
94
#define MAX_STR_LEN 1024
95
96
/*
97
* Class: sun_net_spi_DefaultProxySelector
98
* Method: getSystemProxy
99
* Signature: ([Ljava/lang/String;Ljava/lang/String;)Ljava/net/Proxy;
100
*/
101
JNIEXPORT jobject JNICALL
102
Java_sun_net_spi_DefaultProxySelector_getSystemProxy(JNIEnv *env,
103
jobject this,
104
jstring proto,
105
jstring host)
106
{
107
jobject isa = NULL;
108
jobject proxy = NULL;
109
jobject type_proxy = NULL;
110
jobject no_proxy = NULL;
111
jboolean isCopy;
112
HKEY hKey;
113
LONG ret;
114
const char* cproto;
115
const char* urlhost;
116
char pproto[MAX_STR_LEN];
117
char regserver[MAX_STR_LEN];
118
char override[MAX_STR_LEN];
119
char *s, *s2;
120
char *ctx = NULL;
121
int pport = 0;
122
int defport = 0;
123
char *phost;
124
125
/**
126
* Let's open the Registry entry. We'll check a few values in it:
127
*
128
* - ProxyEnable: 0 means no proxy, 1 means use the proxy
129
* - ProxyServer: a string that can take 2 forms:
130
* "server[:port]"
131
* or
132
* "protocol1=server[:port][;protocol2=server[:port]]..."
133
* - ProxyOverride: a string containing a list of prefixes for hostnames.
134
* e.g.: hoth;localhost;<local>
135
*/
136
ret = RegOpenKeyEx(HKEY_CURRENT_USER,
137
"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings",
138
0, KEY_READ, (PHKEY)&hKey);
139
if (ret == ERROR_SUCCESS) {
140
DWORD dwLen;
141
DWORD dwProxyEnabled;
142
ULONG ulType;
143
dwLen = sizeof(dwProxyEnabled);
144
145
/**
146
* Let's see if the proxy settings are to be used.
147
*/
148
ret = RegQueryValueEx(hKey, "ProxyEnable", NULL, &ulType,
149
(LPBYTE)&dwProxyEnabled, &dwLen);
150
if ((ret == ERROR_SUCCESS) && (dwProxyEnabled > 0)) {
151
/*
152
* Yes, ProxyEnable == 1
153
*/
154
dwLen = sizeof(override);
155
override[0] = 0;
156
ret = RegQueryValueEx(hKey, "ProxyOverride", NULL, &ulType,
157
(LPBYTE)&override, &dwLen);
158
dwLen = sizeof(regserver);
159
regserver[0] = 0;
160
ret = RegQueryValueEx(hKey, "ProxyServer", NULL, &ulType,
161
(LPBYTE)&regserver, &dwLen);
162
RegCloseKey(hKey);
163
if (ret == ERROR_SUCCESS) {
164
if (strlen(override) > 0) {
165
/**
166
* we did get ProxyServer and may have an override.
167
* So let's check the override list first, by walking down the list
168
* The semicolons (;) separated entries have to be matched with the
169
* the beginning of the hostname.
170
*/
171
s = strtok_s(override, "; ", &ctx);
172
urlhost = (*env)->GetStringUTFChars(env, host, &isCopy);
173
if (urlhost == NULL) {
174
if (!(*env)->ExceptionCheck(env))
175
JNU_ThrowOutOfMemoryError(env, NULL);
176
return NULL;
177
}
178
while (s != NULL) {
179
if (strncmp(s, urlhost, strlen(s)) == 0) {
180
/**
181
* the URL host name matches with one of the prefixes,
182
* therefore we have to use a direct connection.
183
*/
184
if (isCopy == JNI_TRUE)
185
(*env)->ReleaseStringUTFChars(env, host, urlhost);
186
goto noproxy;
187
}
188
s = strtok_s(NULL, "; ", &ctx);
189
}
190
if (isCopy == JNI_TRUE)
191
(*env)->ReleaseStringUTFChars(env, host, urlhost);
192
}
193
194
cproto = (*env)->GetStringUTFChars(env, proto, &isCopy);
195
if (cproto == NULL) {
196
if (!(*env)->ExceptionCheck(env))
197
JNU_ThrowOutOfMemoryError(env, NULL);
198
return NULL;
199
}
200
201
/*
202
* Set default port value & proxy type from protocol.
203
*/
204
if ((strcmp(cproto, "http") == 0) ||
205
(strcmp(cproto, "ftp") == 0) ||
206
(strcmp(cproto, "gopher") == 0))
207
defport = 80;
208
if (strcmp(cproto, "https") == 0)
209
defport = 443;
210
if (strcmp(cproto, "socks") == 0) {
211
defport = 1080;
212
type_proxy = (*env)->GetStaticObjectField(env, ptype_class, ptype_socksID);
213
} else {
214
type_proxy = (*env)->GetStaticObjectField(env, ptype_class, ptype_httpID);
215
}
216
217
sprintf(pproto,"%s=", cproto);
218
if (isCopy == JNI_TRUE)
219
(*env)->ReleaseStringUTFChars(env, proto, cproto);
220
/**
221
* Let's check the protocol specific form first.
222
*/
223
if ((s = strstr(regserver, pproto)) != NULL) {
224
s += strlen(pproto);
225
} else {
226
/**
227
* If we couldn't find *this* protocol but the string is in the
228
* protocol specific format, then don't use proxy
229
*/
230
if (strchr(regserver, '=') != NULL)
231
goto noproxy;
232
s = regserver;
233
}
234
s2 = strchr(s, ';');
235
if (s2 != NULL)
236
*s2 = 0;
237
238
/**
239
* Is there a port specified?
240
*/
241
s2 = strchr(s, ':');
242
if (s2 != NULL) {
243
*s2 = 0;
244
s2++;
245
sscanf(s2, "%d", &pport);
246
}
247
phost = s;
248
249
if (phost != NULL) {
250
/**
251
* Let's create the appropriate Proxy object then.
252
*/
253
jstring jhost;
254
if (pport == 0)
255
pport = defport;
256
jhost = (*env)->NewStringUTF(env, phost);
257
CHECK_NULL_RETURN(jhost, NULL);
258
isa = (*env)->CallStaticObjectMethod(env, isaddr_class, isaddr_createUnresolvedID, jhost, pport);
259
CHECK_NULL_RETURN(isa, NULL);
260
proxy = (*env)->NewObject(env, proxy_class, proxy_ctrID, type_proxy, isa);
261
return proxy;
262
}
263
}
264
} else {
265
/* ProxyEnable == 0 or Query failed */
266
/* close the handle to the registry key */
267
RegCloseKey(hKey);
268
}
269
}
270
271
noproxy:
272
no_proxy = (*env)->GetStaticObjectField(env, proxy_class, pr_no_proxyID);
273
return no_proxy;
274
}
275
276