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/HttpsPost.java
38889 views
1
/*
2
* Copyright (c) 2001, 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 4423074
27
* @summary Need to rebase all the duplicated classes from Merlin.
28
* This test will check out http POST
29
* @run main/othervm HttpsPost
30
*
31
* SunJSSE does not support dynamic system properties, no way to re-use
32
* system properties in samevm/agentvm mode.
33
*/
34
35
import java.io.*;
36
import java.net.*;
37
import javax.net.ssl.*;
38
39
public class HttpsPost {
40
41
/*
42
* =============================================================
43
* Set the various variables needed for the tests, then
44
* specify what tests to run on each side.
45
*/
46
47
/*
48
* Should we run the client or server in a separate thread?
49
* Both sides can throw exceptions, but do you have a preference
50
* as to which side should be the main thread.
51
*/
52
static boolean separateServerThread = true;
53
54
/*
55
* Where do we find the keystores?
56
*/
57
static String pathToStores = "../../../../../../javax/net/ssl/etc";
58
static String keyStoreFile = "keystore";
59
static String trustStoreFile = "truststore";
60
static String passwd = "passphrase";
61
62
/*
63
* Is the server ready to serve?
64
*/
65
volatile static boolean serverReady = false;
66
67
/*
68
* Is the connection ready to close?
69
*/
70
volatile static boolean closeReady = false;
71
72
/*
73
* Turn on SSL debugging?
74
*/
75
static boolean debug = false;
76
77
/*
78
* Message posted
79
*/
80
static String postMsg = "Testing HTTP post on a https server";
81
82
/*
83
* If the client or server is doing some kind of object creation
84
* that the other side depends on, and that thread prematurely
85
* exits, you may experience a hang. The test harness will
86
* terminate all hung threads after its timeout has expired,
87
* currently 3 minutes by default, but you might try to be
88
* smart about it....
89
*/
90
91
/*
92
* Define the server side of the test.
93
*
94
* If the server prematurely exits, serverReady will be set to true
95
* to avoid infinite hangs.
96
*/
97
void doServerSide() throws Exception {
98
SSLServerSocketFactory sslssf =
99
(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
100
SSLServerSocket sslServerSocket =
101
(SSLServerSocket) sslssf.createServerSocket(serverPort);
102
serverPort = sslServerSocket.getLocalPort();
103
104
/*
105
* Signal Client, we're ready for his connect.
106
*/
107
serverReady = true;
108
109
SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();
110
try {
111
InputStream sslIS = sslSocket.getInputStream();
112
OutputStream sslOS = sslSocket.getOutputStream();
113
BufferedReader br =
114
new BufferedReader(new InputStreamReader(sslIS));
115
PrintStream ps = new PrintStream(sslOS);
116
117
// process HTTP POST request from client
118
System.out.println("status line: "+br.readLine());
119
String msg = null;
120
while ((msg = br.readLine()) != null && msg.length() > 0);
121
122
msg = br.readLine();
123
if (msg.equals(postMsg)) {
124
ps.println("HTTP/1.1 200 OK\n\n");
125
} else {
126
ps.println("HTTP/1.1 500 Not OK\n\n");
127
}
128
ps.flush();
129
130
// close the socket
131
while (!closeReady) {
132
Thread.sleep(50);
133
}
134
} finally {
135
sslSocket.close();
136
sslServerSocket.close();
137
}
138
}
139
140
/*
141
* Define the client side of the test.
142
*
143
* If the server prematurely exits, serverReady will be set to true
144
* to avoid infinite hangs.
145
*/
146
void doClientSide() throws Exception {
147
HostnameVerifier reservedHV =
148
HttpsURLConnection.getDefaultHostnameVerifier();
149
try {
150
/*
151
* Wait for server to get started.
152
*/
153
while (!serverReady) {
154
Thread.sleep(50);
155
}
156
157
// Send HTTP POST request to server
158
URL url = new URL("https://localhost:"+serverPort);
159
160
HttpsURLConnection.setDefaultHostnameVerifier(new NameVerifier());
161
HttpsURLConnection http = (HttpsURLConnection)url.openConnection();
162
http.setDoOutput(true);
163
164
http.setRequestMethod("POST");
165
PrintStream ps = new PrintStream(http.getOutputStream());
166
try {
167
ps.println(postMsg);
168
ps.flush();
169
if (http.getResponseCode() != 200) {
170
throw new RuntimeException("test Failed");
171
}
172
} finally {
173
ps.close();
174
http.disconnect();
175
closeReady = true;
176
}
177
} finally {
178
HttpsURLConnection.setDefaultHostnameVerifier(reservedHV);
179
}
180
}
181
182
static class NameVerifier implements HostnameVerifier {
183
public boolean verify(String hostname, SSLSession session) {
184
return true;
185
}
186
}
187
188
/*
189
* =============================================================
190
* The remainder is just support stuff
191
*/
192
193
// use any free port by default
194
volatile int serverPort = 0;
195
196
volatile Exception serverException = null;
197
volatile Exception clientException = null;
198
199
public static void main(String[] args) throws Exception {
200
String keyFilename =
201
System.getProperty("test.src", "./") + "/" + pathToStores +
202
"/" + keyStoreFile;
203
String trustFilename =
204
System.getProperty("test.src", "./") + "/" + pathToStores +
205
"/" + trustStoreFile;
206
207
System.setProperty("javax.net.ssl.keyStore", keyFilename);
208
System.setProperty("javax.net.ssl.keyStorePassword", passwd);
209
System.setProperty("javax.net.ssl.trustStore", trustFilename);
210
System.setProperty("javax.net.ssl.trustStorePassword", passwd);
211
212
if (debug)
213
System.setProperty("javax.net.debug", "all");
214
215
/*
216
* Start the tests.
217
*/
218
new HttpsPost();
219
}
220
221
Thread clientThread = null;
222
Thread serverThread = null;
223
224
/*
225
* Primary constructor, used to drive remainder of the test.
226
*
227
* Fork off the other side, then do your work.
228
*/
229
HttpsPost() throws Exception {
230
if (separateServerThread) {
231
startServer(true);
232
startClient(false);
233
} else {
234
startClient(true);
235
startServer(false);
236
}
237
238
/*
239
* Wait for other side to close down.
240
*/
241
if (separateServerThread) {
242
serverThread.join();
243
} else {
244
clientThread.join();
245
}
246
247
/*
248
* When we get here, the test is pretty much over.
249
*
250
* If the main thread excepted, that propagates back
251
* immediately. If the other thread threw an exception, we
252
* should report back.
253
*/
254
if (serverException != null)
255
throw serverException;
256
if (clientException != null)
257
throw clientException;
258
}
259
260
void startServer(boolean newThread) throws Exception {
261
if (newThread) {
262
serverThread = new Thread() {
263
public void run() {
264
try {
265
doServerSide();
266
} catch (Exception e) {
267
/*
268
* Our server thread just died.
269
*
270
* Release the client, if not active already...
271
*/
272
System.err.println("Server died...");
273
serverReady = true;
274
serverException = e;
275
}
276
}
277
};
278
serverThread.start();
279
} else {
280
doServerSide();
281
}
282
}
283
284
void startClient(boolean newThread) throws Exception {
285
if (newThread) {
286
clientThread = new Thread() {
287
public void run() {
288
try {
289
doClientSide();
290
} catch (Exception e) {
291
/*
292
* Our client thread just died.
293
*/
294
System.err.println("Client died...");
295
clientException = e;
296
}
297
}
298
};
299
clientThread.start();
300
} else {
301
doClientSide();
302
}
303
}
304
}
305
306