Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/net/www/protocol/https/HttpsURLConnection/CloseKeepAliveCached.java
41161 views
1
/*
2
* Copyright (c) 2008, 2011, 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.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
/*
25
* @test
26
* @bug 6618387
27
* @summary SSL client sessions do not close cleanly. A TCP reset occurs
28
* instead of a close_notify alert.
29
* @run main/othervm CloseKeepAliveCached
30
*
31
* SunJSSE does not support dynamic system properties, no way to re-use
32
* system properties in samevm/agentvm mode.
33
*
34
* @ignore
35
* After run the test manually, at the end of the debug output,
36
* if "MainThread, called close()" found, the test passed. Otherwise,
37
* if "Keep-Alive-Timer: called close()", the test failed.
38
*/
39
40
import java.net.*;
41
import java.util.*;
42
import java.io.*;
43
import javax.net.ssl.*;
44
45
public class CloseKeepAliveCached {
46
static Map cookies;
47
ServerSocket ss;
48
49
/*
50
* =============================================================
51
* Set the various variables needed for the tests, then
52
* specify what tests to run on each side.
53
*/
54
55
/*
56
* Should we run the client or server in a separate thread?
57
* Both sides can throw exceptions, but do you have a preference
58
* as to which side should be the main thread.
59
*/
60
static boolean separateServerThread = true;
61
62
/*
63
* Where do we find the keystores?
64
*/
65
static String pathToStores = "../../../../../../javax/net/ssl/etc";
66
static String keyStoreFile = "keystore";
67
static String trustStoreFile = "truststore";
68
static String passwd = "passphrase";
69
70
/*
71
* Is the server ready to serve?
72
*/
73
volatile static boolean serverReady = false;
74
75
/*
76
* Turn on SSL debugging?
77
*/
78
static boolean debug = false;
79
80
private SSLServerSocket sslServerSocket = null;
81
82
/*
83
* Define the server side of the test.
84
*
85
* If the server prematurely exits, serverReady will be set to true
86
* to avoid infinite hangs.
87
*/
88
void doServerSide() throws Exception {
89
SSLServerSocketFactory sslssf =
90
(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
91
sslServerSocket =
92
(SSLServerSocket) sslssf.createServerSocket(serverPort);
93
serverPort = sslServerSocket.getLocalPort();
94
95
/*
96
* Signal Client, we're ready for his connect.
97
*/
98
serverReady = true;
99
SSLSocket sslSocket = null;
100
try {
101
sslSocket = (SSLSocket) sslServerSocket.accept();
102
for (int i = 0; i < 3 && !sslSocket.isClosed(); i++) {
103
// read request
104
InputStream is = sslSocket.getInputStream ();
105
106
BufferedReader r = new BufferedReader(
107
new InputStreamReader(is));
108
String x;
109
while ((x=r.readLine()) != null) {
110
if (x.length() ==0) {
111
break;
112
}
113
}
114
115
116
PrintStream out = new PrintStream(
117
new BufferedOutputStream(
118
sslSocket.getOutputStream() ));
119
120
/* send the header */
121
out.print("HTTP/1.1 200 OK\r\n");
122
out.print("Keep-Alive: timeout=15, max=100\r\n");
123
out.print("Connection: Keep-Alive\r\n");
124
out.print("Content-Type: text/html; charset=iso-8859-1\r\n");
125
out.print("Content-Length: 9\r\n");
126
out.print("\r\n");
127
out.print("Testing\r\n");
128
out.flush();
129
130
Thread.sleep(50);
131
}
132
sslSocket.close();
133
sslServerSocket.close();
134
} catch (Exception e) {
135
e.printStackTrace();
136
}
137
}
138
139
/*
140
* Define the client side of the test.
141
*
142
* If the server prematurely exits, serverReady will be set to true
143
* to avoid infinite hangs.
144
*/
145
void doClientSide() throws Exception {
146
/*
147
* Wait for server to get started.
148
*/
149
while (!serverReady) {
150
Thread.sleep(50);
151
}
152
153
HostnameVerifier reservedHV =
154
HttpsURLConnection.getDefaultHostnameVerifier();
155
try {
156
HttpsURLConnection http = null;
157
158
/* establish http connection to server */
159
URL url = new URL("https://localhost:" + serverPort+"/");
160
HttpsURLConnection.setDefaultHostnameVerifier(new NameVerifier());
161
http = (HttpsURLConnection)url.openConnection();
162
InputStream is = http.getInputStream ();
163
while (is.read() != -1);
164
is.close();
165
166
url = new URL("https://localhost:" + serverPort+"/");
167
http = (HttpsURLConnection)url.openConnection();
168
is = http.getInputStream ();
169
while (is.read() != -1);
170
171
// if inputstream.close() called, the http.disconnect() will
172
// not able to close the cached connection. If application
173
// wanna close the keep-alive cached connection immediately
174
// with httpURLConnection.disconnect(), they should not call
175
// inputstream.close() explicitly, the
176
// httpURLConnection.disconnect() will do it internally.
177
// is.close();
178
179
// close the connection, sending close_notify to peer.
180
// otherwise, the connection will be closed by Finalizer or
181
// Keep-Alive-Timer if timeout.
182
http.disconnect();
183
// Thread.sleep(5000);
184
} catch (IOException ioex) {
185
if (sslServerSocket != null)
186
sslServerSocket.close();
187
throw ioex;
188
} finally {
189
HttpsURLConnection.setDefaultHostnameVerifier(reservedHV);
190
}
191
}
192
193
static class NameVerifier implements HostnameVerifier {
194
public boolean verify(String hostname, SSLSession session) {
195
return true;
196
}
197
}
198
199
/*
200
* =============================================================
201
* The remainder is just support stuff
202
*/
203
204
// use any free port by default
205
volatile int serverPort = 0;
206
207
volatile Exception serverException = null;
208
volatile Exception clientException = null;
209
210
public static void main(String args[]) throws Exception {
211
String keyFilename =
212
System.getProperty("test.src", "./") + "/" + pathToStores +
213
"/" + keyStoreFile;
214
String trustFilename =
215
System.getProperty("test.src", "./") + "/" + pathToStores +
216
"/" + trustStoreFile;
217
218
System.setProperty("javax.net.ssl.keyStore", keyFilename);
219
System.setProperty("javax.net.ssl.keyStorePassword", passwd);
220
System.setProperty("javax.net.ssl.trustStore", trustFilename);
221
System.setProperty("javax.net.ssl.trustStorePassword", passwd);
222
223
if (debug)
224
System.setProperty("javax.net.debug", "all");
225
226
/*
227
* Start the tests.
228
*/
229
new CloseKeepAliveCached();
230
}
231
232
Thread clientThread = null;
233
Thread serverThread = null;
234
/*
235
* Primary constructor, used to drive remainder of the test.
236
*
237
* Fork off the other side, then do your work.
238
*/
239
CloseKeepAliveCached() throws Exception {
240
if (separateServerThread) {
241
startServer(true);
242
startClient(false);
243
} else {
244
startClient(true);
245
startServer(false);
246
}
247
248
/*
249
* Wait for other side to close down.
250
*/
251
if (separateServerThread) {
252
serverThread.join();
253
} else {
254
clientThread.join();
255
}
256
257
/*
258
* When we get here, the test is pretty much over.
259
*
260
* If the main thread excepted, that propagates back
261
* immediately. If the other thread threw an exception, we
262
* should report back.
263
*/
264
if (serverException != null)
265
throw serverException;
266
if (clientException != null)
267
throw clientException;
268
}
269
270
void startServer(boolean newThread) throws Exception {
271
if (newThread) {
272
serverThread = new Thread() {
273
public void run() {
274
try {
275
doServerSide();
276
} catch (Exception e) {
277
/*
278
* Our server thread just died.
279
*
280
* Release the client, if not active already...
281
*/
282
System.err.println("Server died...");
283
serverReady = true;
284
serverException = e;
285
}
286
}
287
};
288
serverThread.start();
289
} else {
290
doServerSide();
291
}
292
}
293
294
void startClient(boolean newThread) throws Exception {
295
if (newThread) {
296
clientThread = new Thread() {
297
public void run() {
298
try {
299
doClientSide();
300
} catch (Exception e) {
301
/*
302
* Our client thread just died.
303
*/
304
System.err.println("Client died...");
305
clientException = e;
306
}
307
}
308
};
309
clientThread.start();
310
} else {
311
doClientSide();
312
}
313
}
314
}
315
316