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