Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/protocol/https/HttpsURLConnection/RetryHttps.java
38889 views
1
/*
2
* Copyright (c) 2003, 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
/* @test
25
* @bug 4799427
26
* @summary Https can not retry request
27
* @run main/othervm RetryHttps
28
*
29
* SunJSSE does not support dynamic system properties, no way to re-use
30
* system properties in samevm/agentvm mode.
31
* @author Yingxian Wang
32
*/
33
34
import java.net.*;
35
import java.util.*;
36
import java.io.*;
37
import javax.net.ssl.*;
38
39
public class RetryHttps {
40
static Map cookies;
41
ServerSocket ss;
42
43
/*
44
* =============================================================
45
* Set the various variables needed for the tests, then
46
* specify what tests to run on each side.
47
*/
48
49
/*
50
* Should we run the client or server in a separate thread?
51
* Both sides can throw exceptions, but do you have a preference
52
* as to which side should be the main thread.
53
*/
54
static boolean separateServerThread = true;
55
56
/*
57
* Where do we find the keystores?
58
*/
59
static String pathToStores = "../../../../../../javax/net/ssl/etc";
60
static String keyStoreFile = "keystore";
61
static String trustStoreFile = "truststore";
62
static String passwd = "passphrase";
63
64
/*
65
* Is the server ready to serve?
66
*/
67
volatile static boolean serverReady = false;
68
69
/*
70
* Turn on SSL debugging?
71
*/
72
static boolean debug = false;
73
74
private SSLServerSocket sslServerSocket = null;
75
76
/*
77
* Define the server side of the test.
78
*
79
* If the server prematurely exits, serverReady will be set to true
80
* to avoid infinite hangs.
81
*/
82
void doServerSide() throws Exception {
83
SSLServerSocketFactory sslssf =
84
(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
85
sslServerSocket =
86
(SSLServerSocket) sslssf.createServerSocket(serverPort);
87
serverPort = sslServerSocket.getLocalPort();
88
89
/*
90
* Signal Client, we're ready for his connect.
91
*/
92
serverReady = true;
93
SSLSocket sslSocket = null;
94
try {
95
for (int i = 0; i < 2; i++) {
96
sslSocket = (SSLSocket) sslServerSocket.accept();
97
// read request
98
InputStream is = sslSocket.getInputStream ();
99
BufferedReader r = new BufferedReader(new InputStreamReader(is));
100
boolean flag = false;
101
String x;
102
while ((x=r.readLine()) != null) {
103
if (x.length() ==0) {
104
break;
105
}
106
}
107
108
PrintStream out = new PrintStream(
109
new BufferedOutputStream(
110
sslSocket.getOutputStream() ));
111
112
/* send the header */
113
out.print("HTTP/1.1 200 OK\r\n");
114
out.print("Content-Type: text/html; charset=iso-8859-1\r\n");
115
out.print("Content-Length: "+10+"\r\n");
116
out.print("\r\n");
117
out.print("Testing"+i+"\r\n");
118
out.flush();
119
sslSocket.close();
120
}
121
122
123
sslServerSocket.close();
124
} catch (Exception e) {
125
e.printStackTrace();
126
}
127
}
128
129
/*
130
* Define the client side of the test.
131
*
132
* If the server prematurely exits, serverReady will be set to true
133
* to avoid infinite hangs.
134
*/
135
void doClientSide() throws Exception {
136
HostnameVerifier reservedHV =
137
HttpsURLConnection.getDefaultHostnameVerifier();
138
try {
139
/*
140
* Wait for server to get started.
141
*/
142
while (!serverReady) {
143
Thread.sleep(50);
144
}
145
try {
146
HttpsURLConnection http = null;
147
/* establish http connection to server */
148
URL url = new URL("https://localhost:" + serverPort+"/file1");
149
System.out.println("url is "+url.toString());
150
HttpsURLConnection.setDefaultHostnameVerifier(
151
new NameVerifier());
152
http = (HttpsURLConnection)url.openConnection();
153
int respCode = http.getResponseCode();
154
int cl = http.getContentLength();
155
InputStream is = http.getInputStream ();
156
int count = 0;
157
while (is.read() != -1 && count++ < cl);
158
System.out.println("respCode1 = "+respCode);
159
Thread.sleep(2000);
160
url = new URL("https://localhost:" + serverPort+"/file2");
161
http = (HttpsURLConnection)url.openConnection();
162
respCode = http.getResponseCode();
163
System.out.println("respCode2 = "+respCode);
164
} catch (IOException ioex) {
165
if (sslServerSocket != null)
166
sslServerSocket.close();
167
throw ioex;
168
}
169
} finally {
170
HttpsURLConnection.setDefaultHostnameVerifier(reservedHV);
171
}
172
}
173
174
static class NameVerifier implements HostnameVerifier {
175
public boolean verify(String hostname, SSLSession session) {
176
return true;
177
}
178
}
179
180
/*
181
* =============================================================
182
* The remainder is just support stuff
183
*/
184
185
// use any free port by default
186
volatile int serverPort = 0;
187
188
volatile Exception serverException = null;
189
volatile Exception clientException = null;
190
191
public static void main(String args[]) throws Exception {
192
String keyFilename =
193
System.getProperty("test.src", "./") + "/" + pathToStores +
194
"/" + keyStoreFile;
195
String trustFilename =
196
System.getProperty("test.src", "./") + "/" + pathToStores +
197
"/" + trustStoreFile;
198
199
System.setProperty("javax.net.ssl.keyStore", keyFilename);
200
System.setProperty("javax.net.ssl.keyStorePassword", passwd);
201
System.setProperty("javax.net.ssl.trustStore", trustFilename);
202
System.setProperty("javax.net.ssl.trustStorePassword", passwd);
203
204
if (debug)
205
System.setProperty("javax.net.debug", "all");
206
207
/*
208
* Start the tests.
209
*/
210
new RetryHttps();
211
}
212
213
Thread clientThread = null;
214
Thread serverThread = null;
215
/*
216
* Primary constructor, used to drive remainder of the test.
217
*
218
* Fork off the other side, then do your work.
219
*/
220
RetryHttps() throws Exception {
221
if (separateServerThread) {
222
startServer(true);
223
startClient(false);
224
} else {
225
startClient(true);
226
startServer(false);
227
}
228
229
/*
230
* Wait for other side to close down.
231
*/
232
if (separateServerThread) {
233
serverThread.join();
234
} else {
235
clientThread.join();
236
}
237
238
/*
239
* When we get here, the test is pretty much over.
240
*
241
* If the main thread excepted, that propagates back
242
* immediately. If the other thread threw an exception, we
243
* should report back.
244
*/
245
if (serverException != null)
246
throw serverException;
247
if (clientException != null)
248
throw clientException;
249
}
250
251
void startServer(boolean newThread) throws Exception {
252
if (newThread) {
253
serverThread = new Thread() {
254
public void run() {
255
try {
256
doServerSide();
257
} catch (Exception e) {
258
/*
259
* Our server thread just died.
260
*
261
* Release the client, if not active already...
262
*/
263
System.err.println("Server died...");
264
serverReady = true;
265
serverException = e;
266
}
267
}
268
};
269
serverThread.start();
270
} else {
271
doServerSide();
272
}
273
}
274
275
void startClient(boolean newThread) throws Exception {
276
if (newThread) {
277
clientThread = new Thread() {
278
public void run() {
279
try {
280
doClientSide();
281
} catch (Exception e) {
282
/*
283
* Our client thread just died.
284
*/
285
System.err.println("Client died...");
286
clientException = e;
287
}
288
}
289
};
290
clientThread.start();
291
} else {
292
doClientSide();
293
}
294
}
295
}
296
297