Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/net/www/protocol/https/ChunkedOutputStream.java
41159 views
1
/*
2
* Copyright (c) 2004, 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 5026745
27
* @modules java.base/sun.net.www
28
* @build TestHttpsServer HttpCallback
29
* @run main/othervm ChunkedOutputStream
30
* @run main/othervm -Djava.net.preferIPv6Addresses=true ChunkedOutputStream
31
*
32
* SunJSSE does not support dynamic system properties, no way to re-use
33
* system properties in samevm/agentvm mode.
34
* @summary Cannot flush output stream when writing to an HttpUrlConnection
35
*/
36
37
import java.io.*;
38
import java.net.*;
39
import javax.net.ssl.*;
40
import java.util.concurrent.atomic.AtomicInteger;
41
42
public class ChunkedOutputStream implements HttpCallback {
43
/*
44
* Where do we find the keystores for ssl?
45
*/
46
static String pathToStores = "../../../../../javax/net/ssl/etc";
47
static String keyStoreFile = "keystore";
48
static String trustStoreFile = "truststore";
49
static String passwd = "passphrase";
50
static int count = 0;
51
static final AtomicInteger rogueCount = new AtomicInteger();
52
53
static final String str1 = "Helloworld1234567890abcdefghijklmnopqrstuvwxyz"+
54
"1234567890abcdefkjsdlkjflkjsldkfjlsdkjflkj"+
55
"1434567890abcdefkjsdlkjflkjsldkfjlsdkjflkj";
56
57
static final String str2 = "Helloworld1234567890abcdefghijklmnopqrstuvwxyz"+
58
"1234567890";
59
60
public void request(HttpTransaction req) {
61
try {
62
// this is needed (count++ doesn't work), 'cause we
63
// are doing concurrent tests
64
String path = req.getRequestURI().getPath();
65
if (path.equals("/d0")) {
66
count = 0;
67
} else if (path.equals("/d01")) {
68
count = 1;
69
} else if (path.equals("/d3")) {
70
count = 2;
71
} else if (path.equals("/d4") || path.equals("/d5")) {
72
count = 3;
73
} else if (path.equals("/d6")) {
74
count = 3;
75
} else if (path.equals("/d7")) {
76
count = 4;
77
} else if (path.equals("/d8")) {
78
count = 5;
79
}
80
81
switch (count) {
82
case 0: /* test1 -- keeps conn alive */
83
case 1: /* test2 -- closes conn */
84
String reqbody = req.getRequestEntityBody();
85
if (!reqbody.equals(str1)) {
86
req.sendResponse(500, "Internal server error");
87
req.orderlyClose();
88
}
89
String chunk = req.getRequestHeader("Transfer-encoding");
90
if (!"chunked".equals(chunk)) {
91
req.sendResponse(501, "Internal server error");
92
req.orderlyClose();
93
}
94
req.setResponseEntityBody(reqbody);
95
if (count == 1) {
96
req.setResponseHeader("Connection", "close");
97
}
98
req.sendResponse(200, "OK");
99
if (count == 1) {
100
req.orderlyClose();
101
}
102
break;
103
case 2: /* test 3 */
104
reqbody = req.getRequestEntityBody();
105
if (!reqbody.equals(str2)) {
106
req.sendResponse(500, "Internal server error");
107
req.orderlyClose();
108
}
109
int clen = Integer.parseInt (
110
req.getRequestHeader("Content-length"));
111
if (clen != str2.length()) {
112
req.sendResponse(501, "Internal server error");
113
req.orderlyClose();
114
}
115
req.setResponseEntityBody (reqbody);
116
req.setResponseHeader("Connection", "close");
117
req.sendResponse(200, "OK");
118
req.orderlyClose();
119
break;
120
case 3: /* test 6 */
121
req.setResponseHeader("Location", "https://foo.bar/");
122
req.setResponseHeader("Connection", "close");
123
req.sendResponse(307, "Temporary Redirect");
124
req.orderlyClose();
125
break;
126
case 4: /* test 7 */
127
case 5: /* test 8 */
128
reqbody = req.getRequestEntityBody();
129
if (reqbody != null && !"".equals(reqbody)) {
130
req.sendResponse(501, "Internal server error");
131
req.orderlyClose();
132
}
133
req.setResponseHeader("Connection", "close");
134
req.sendResponse(200, "OK");
135
req.orderlyClose();
136
break;
137
default:
138
req.sendResponse(404, "Not Found");
139
req.orderlyClose();
140
break;
141
}
142
} catch (IOException e) {
143
e.printStackTrace();
144
}
145
}
146
147
public boolean dropPlainTextConnections() {
148
System.out.println("Unrecognized SSL message, plaintext connection?");
149
System.out.println("TestHttpsServer receveived rogue connection: ignoring it.");
150
rogueCount.incrementAndGet();
151
return true;
152
}
153
154
static void readAndCompare(InputStream is, String cmp) throws IOException {
155
int c;
156
byte buf[] = new byte[1024];
157
int off = 0;
158
int len = 1024;
159
while ((c=is.read(buf, off, len)) != -1) {
160
off += c;
161
len -= c;
162
}
163
String s1 = new String(buf, 0, off, "ISO8859_1");
164
if (!cmp.equals(s1)) {
165
throw new IOException("strings not same");
166
}
167
}
168
169
/* basic smoke test: verify that server drops plain connections */
170
static void testPlainText(String authority) throws Exception {
171
URL url = new URL("http://" + authority + "/Donauschiffsgesellschaftskapitaenskajuete");
172
System.out.println("client opening connection to: " + url);
173
HttpURLConnection urlc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);
174
int rogue = rogueCount.get();
175
try {
176
int code = urlc.getResponseCode();
177
System.out.println("Unexpected response: " + code);
178
throw new AssertionError("Unexpected response: " + code);
179
} catch (SocketException x) {
180
// we expect that the server will drop the connection and
181
// close the accepted socket, so we should get a SocketException
182
// on the client side, and confirm that this::dropPlainTextConnections
183
// has ben called.
184
if (rogueCount.get() == rogue) throw x;
185
System.out.println("Got expected exception: " + x);
186
}
187
}
188
189
/* basic chunked test (runs twice) */
190
191
static void test1(String u) throws Exception {
192
URL url = new URL(u);
193
System.out.println("client opening connection to: " + u);
194
HttpURLConnection urlc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);
195
urlc.setChunkedStreamingMode(20);
196
urlc.setDoOutput(true);
197
urlc.setRequestMethod("POST");
198
OutputStream os = urlc.getOutputStream();
199
os.write(str1.getBytes());
200
os.close();
201
InputStream is = urlc.getInputStream();
202
readAndCompare(is, str1);
203
is.close();
204
}
205
206
/* basic fixed length test */
207
208
static void test3(String u) throws Exception {
209
URL url = new URL(u);
210
System.out.println("client opening connection to: " + u);
211
HttpURLConnection urlc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);
212
urlc.setFixedLengthStreamingMode(str2.length());
213
urlc.setDoOutput(true);
214
urlc.setRequestMethod("POST");
215
OutputStream os = urlc.getOutputStream();
216
os.write (str2.getBytes());
217
os.close();
218
InputStream is = urlc.getInputStream();
219
readAndCompare(is, str2);
220
is.close();
221
}
222
223
/* write too few bytes */
224
225
static void test4(String u) throws Exception {
226
URL url = new URL(u);
227
System.out.println("client opening connection to: " + u);
228
HttpURLConnection urlc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);
229
urlc.setFixedLengthStreamingMode(str2.length()+1);
230
urlc.setDoOutput(true);
231
urlc.setRequestMethod("POST");
232
OutputStream os = urlc.getOutputStream();
233
os.write(str2.getBytes());
234
try {
235
os.close();
236
throw new Exception("should have thrown IOException");
237
} catch (IOException e) {}
238
}
239
240
/* write too many bytes */
241
242
static void test5(String u) throws Exception {
243
URL url = new URL(u);
244
System.out.println("client opening connection to: " + u);
245
HttpURLConnection urlc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);
246
urlc.setFixedLengthStreamingMode(str2.length()-1);
247
urlc.setDoOutput(true);
248
urlc.setRequestMethod("POST");
249
OutputStream os = urlc.getOutputStream();
250
try {
251
os.write(str2.getBytes());
252
throw new Exception("should have thrown IOException");
253
} catch (IOException e) {}
254
}
255
256
/* check for HttpRetryException on redirection */
257
258
static void test6(String u) throws Exception {
259
URL url = new URL(u);
260
System.out.println("client opening connection to: " + u);
261
HttpURLConnection urlc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);
262
urlc.setChunkedStreamingMode(20);
263
urlc.setDoOutput(true);
264
urlc.setRequestMethod("POST");
265
OutputStream os = urlc.getOutputStream();
266
os.write(str1.getBytes());
267
os.close();
268
try {
269
InputStream is = urlc.getInputStream();
270
throw new Exception("should have gotten HttpRetryException");
271
} catch (HttpRetryException e) {
272
if (e.responseCode() != 307) {
273
throw new Exception("Wrong response code " + e.responseCode());
274
}
275
if (!e.getLocation().equals("https://foo.bar/")) {
276
throw new Exception("Wrong location " + e.getLocation());
277
}
278
}
279
}
280
281
/* next two tests send zero length posts */
282
283
static void test7(String u) throws Exception {
284
URL url = new URL(u);
285
System.out.println("client opening connection to: " + u);
286
HttpURLConnection urlc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);
287
urlc.setChunkedStreamingMode(20);
288
urlc.setDoOutput(true);
289
urlc.setRequestMethod("POST");
290
OutputStream os = urlc.getOutputStream();
291
os.close();
292
int ret = urlc.getResponseCode();
293
if (ret != 200) {
294
throw new Exception("Expected 200: got " + ret);
295
}
296
}
297
298
static void test8(String u) throws Exception {
299
URL url = new URL(u);
300
System.out.println("client opening connection to: " + u);
301
HttpURLConnection urlc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);
302
urlc.setFixedLengthStreamingMode(0);
303
urlc.setDoOutput(true);
304
urlc.setRequestMethod("POST");
305
OutputStream os = urlc.getOutputStream();
306
os.close();
307
int ret = urlc.getResponseCode();
308
if (ret != 200) {
309
throw new Exception("Expected 200: got " + ret);
310
}
311
}
312
313
static TestHttpsServer server;
314
315
public static void main(String[] args) throws Exception {
316
// setup properties to do ssl
317
String keyFilename =
318
System.getProperty("test.src", "./") + "/" + pathToStores +
319
"/" + keyStoreFile;
320
String trustFilename =
321
System.getProperty("test.src", "./") + "/" + pathToStores +
322
"/" + trustStoreFile;
323
324
InetAddress loopback = InetAddress.getLoopbackAddress();
325
326
HostnameVerifier reservedHV =
327
HttpsURLConnection.getDefaultHostnameVerifier();
328
try {
329
System.setProperty("javax.net.ssl.keyStore", keyFilename);
330
System.setProperty("javax.net.ssl.keyStorePassword", passwd);
331
System.setProperty("javax.net.ssl.trustStore", trustFilename);
332
System.setProperty("javax.net.ssl.trustStorePassword", passwd);
333
HttpsURLConnection.setDefaultHostnameVerifier(new NameVerifier());
334
335
try {
336
server = new TestHttpsServer(
337
new ChunkedOutputStream(), 1, 10, loopback, 0);
338
System.out.println("Server started: listening on: " + server.getAuthority());
339
testPlainText(server.getAuthority());
340
// the test server doesn't support keep-alive yet
341
// test1("http://" + server.getAuthority() + "/d0");
342
test1("https://" + server.getAuthority() + "/d01");
343
test3("https://" + server.getAuthority() + "/d3");
344
test4("https://" + server.getAuthority() + "/d4");
345
test5("https://" + server.getAuthority() + "/d5");
346
test6("https://" + server.getAuthority() + "/d6");
347
test7("https://" + server.getAuthority() + "/d7");
348
test8("https://" + server.getAuthority() + "/d8");
349
} catch (Exception e) {
350
if (server != null) {
351
server.terminate();
352
}
353
throw e;
354
}
355
server.terminate();
356
} finally {
357
HttpsURLConnection.setDefaultHostnameVerifier(reservedHV);
358
}
359
}
360
361
static class NameVerifier implements HostnameVerifier {
362
public boolean verify(String hostname, SSLSession session) {
363
return true;
364
}
365
}
366
367
public static void except(String s) {
368
server.terminate();
369
throw new RuntimeException(s);
370
}
371
}
372
373