Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/sun/net/NetworkClient.java
41152 views
1
/*
2
* Copyright (c) 1994, 2021, 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
package sun.net;
26
27
import java.io.*;
28
import java.net.Socket;
29
import java.net.InetAddress;
30
import java.net.InetSocketAddress;
31
import java.net.UnknownHostException;
32
import java.net.Proxy;
33
import java.util.Arrays;
34
import java.security.AccessController;
35
import java.security.PrivilegedAction;
36
37
/**
38
* This is the base class for network clients.
39
*
40
* @author Jonathan Payne
41
*/
42
@SuppressWarnings("removal")
43
public class NetworkClient {
44
/* Default value of read timeout, if not specified (infinity) */
45
public static final int DEFAULT_READ_TIMEOUT = -1;
46
47
/* Default value of connect timeout, if not specified (infinity) */
48
public static final int DEFAULT_CONNECT_TIMEOUT = -1;
49
50
protected Proxy proxy = Proxy.NO_PROXY;
51
/** Socket for communicating with server. */
52
protected Socket serverSocket = null;
53
54
/** Stream for printing to the server. */
55
public PrintStream serverOutput;
56
57
/** Buffered stream for reading replies from server. */
58
public InputStream serverInput;
59
60
protected static int defaultSoTimeout;
61
protected static int defaultConnectTimeout;
62
63
protected int readTimeout = DEFAULT_READ_TIMEOUT;
64
protected int connectTimeout = DEFAULT_CONNECT_TIMEOUT;
65
/* Name of encoding to use for output */
66
protected static String encoding;
67
68
static {
69
final int vals[] = {0, 0};
70
final String encs[] = { null };
71
72
AccessController.doPrivileged(
73
new PrivilegedAction<>() {
74
public Void run() {
75
vals[0] = Integer.getInteger("sun.net.client.defaultReadTimeout", 0).intValue();
76
vals[1] = Integer.getInteger("sun.net.client.defaultConnectTimeout", 0).intValue();
77
encs[0] = System.getProperty("file.encoding", "ISO8859_1");
78
return null;
79
}
80
});
81
if (vals[0] != 0) {
82
defaultSoTimeout = vals[0];
83
}
84
if (vals[1] != 0) {
85
defaultConnectTimeout = vals[1];
86
}
87
88
encoding = encs[0];
89
try {
90
if (!isASCIISuperset (encoding)) {
91
encoding = "ISO8859_1";
92
}
93
} catch (Exception e) {
94
encoding = "ISO8859_1";
95
}
96
}
97
98
99
/**
100
* Test the named character encoding to verify that it converts ASCII
101
* characters correctly. We have to use an ASCII based encoding, or else
102
* the NetworkClients will not work correctly in EBCDIC based systems.
103
* However, we cannot just use ASCII or ISO8859_1 universally, because in
104
* Asian locales, non-ASCII characters may be embedded in otherwise
105
* ASCII based protocols (e.g. HTTP). The specifications (RFC2616, 2398)
106
* are a little ambiguous in this matter. For instance, RFC2398 [part 2.1]
107
* says that the HTTP request URI should be escaped using a defined
108
* mechanism, but there is no way to specify in the escaped string what
109
* the original character set is. It is not correct to assume that
110
* UTF-8 is always used (as in URLs in HTML 4.0). For this reason,
111
* until the specifications are updated to deal with this issue more
112
* comprehensively, and more importantly, HTTP servers are known to
113
* support these mechanisms, we will maintain the current behavior
114
* where it is possible to send non-ASCII characters in their original
115
* unescaped form.
116
*/
117
private static boolean isASCIISuperset (String encoding) throws Exception {
118
String chkS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"+
119
"abcdefghijklmnopqrstuvwxyz-_.!~*'();/?:@&=+$,";
120
121
// Expected byte sequence for string above
122
byte[] chkB = { 48,49,50,51,52,53,54,55,56,57,65,66,67,68,69,70,71,72,
123
73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,97,98,99,
124
100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,
125
115,116,117,118,119,120,121,122,45,95,46,33,126,42,39,40,41,59,
126
47,63,58,64,38,61,43,36,44};
127
128
byte[] b = chkS.getBytes (encoding);
129
return Arrays.equals (b, chkB);
130
}
131
132
/** Open a connection to the server. */
133
public void openServer(String server, int port)
134
throws IOException, UnknownHostException {
135
if (serverSocket != null)
136
closeServer();
137
serverSocket = doConnect (server, port);
138
try {
139
serverOutput = new PrintStream(new BufferedOutputStream(
140
serverSocket.getOutputStream()),
141
true, encoding);
142
} catch (UnsupportedEncodingException e) {
143
throw new InternalError(encoding +"encoding not found", e);
144
}
145
serverInput = new BufferedInputStream(serverSocket.getInputStream());
146
}
147
148
/**
149
* Return a socket connected to the server, with any
150
* appropriate options pre-established
151
*/
152
protected Socket doConnect (String server, int port)
153
throws IOException, UnknownHostException {
154
Socket s;
155
if (proxy != null) {
156
if (proxy.type() == Proxy.Type.SOCKS) {
157
s = AccessController.doPrivileged(
158
new PrivilegedAction<>() {
159
public Socket run() {
160
return new Socket(proxy);
161
}});
162
} else if (proxy.type() == Proxy.Type.DIRECT) {
163
s = createSocket();
164
} else {
165
// Still connecting through a proxy
166
// server & port will be the proxy address and port
167
s = new Socket(Proxy.NO_PROXY);
168
}
169
} else {
170
s = createSocket();
171
}
172
173
// Instance specific timeouts do have priority, that means
174
// connectTimeout & readTimeout (-1 means not set)
175
// Then global default timeouts
176
// Then no timeout.
177
if (connectTimeout >= 0) {
178
s.connect(new InetSocketAddress(server, port), connectTimeout);
179
} else {
180
if (defaultConnectTimeout > 0) {
181
s.connect(new InetSocketAddress(server, port), defaultConnectTimeout);
182
} else {
183
s.connect(new InetSocketAddress(server, port));
184
}
185
}
186
if (readTimeout >= 0)
187
s.setSoTimeout(readTimeout);
188
else if (defaultSoTimeout > 0) {
189
s.setSoTimeout(defaultSoTimeout);
190
}
191
return s;
192
}
193
194
/**
195
* The following method, createSocket, is provided to allow the
196
* https client to override it so that it may use its socket factory
197
* to create the socket.
198
*/
199
protected Socket createSocket() throws IOException {
200
return new java.net.Socket(Proxy.NO_PROXY); // direct connection
201
}
202
203
protected InetAddress getLocalAddress() throws IOException {
204
if (serverSocket == null)
205
throw new IOException("not connected");
206
return AccessController.doPrivileged(
207
new PrivilegedAction<>() {
208
public InetAddress run() {
209
return serverSocket.getLocalAddress();
210
211
}
212
});
213
}
214
215
/** Close an open connection to the server. */
216
public void closeServer() throws IOException {
217
if (! serverIsOpen()) {
218
return;
219
}
220
serverSocket.close();
221
serverSocket = null;
222
serverInput = null;
223
serverOutput = null;
224
}
225
226
/** Return server connection status */
227
public boolean serverIsOpen() {
228
return serverSocket != null;
229
}
230
231
/** Create connection with host <i>host</i> on port <i>port</i> */
232
public NetworkClient(String host, int port) throws IOException {
233
openServer(host, port);
234
}
235
236
public NetworkClient() {}
237
238
public void setConnectTimeout(int timeout) {
239
connectTimeout = timeout;
240
}
241
242
public int getConnectTimeout() {
243
return connectTimeout;
244
}
245
246
/**
247
* Sets the read timeout.
248
*
249
* Note: Public URLConnection (and protocol specific implementations)
250
* protect against negative timeout values being set. This implementation,
251
* and protocol specific implementations, use -1 to represent the default
252
* read timeout.
253
*
254
* This method may be invoked with the default timeout value when the
255
* protocol handler is trying to reset the timeout after doing a
256
* potentially blocking internal operation, e.g. cleaning up unread
257
* response data, buffering error stream response data, etc
258
*/
259
public void setReadTimeout(int timeout) {
260
if (timeout == DEFAULT_READ_TIMEOUT)
261
timeout = defaultSoTimeout;
262
263
if (serverSocket != null && timeout >= 0) {
264
try {
265
serverSocket.setSoTimeout(timeout);
266
} catch(IOException e) {
267
// We tried...
268
}
269
}
270
readTimeout = timeout;
271
}
272
273
public int getReadTimeout() {
274
return readTimeout;
275
}
276
}
277
278