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