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