Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/net/www/http/ChunkedOutputStream/Test.java
41154 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 6631048
27
* @modules jdk.httpserver
28
* @library /test/lib
29
* @run main/othervm/timeout=500 Test
30
* @summary Cannot flush output stream when writing to an HttpUrlConnection
31
*/
32
33
import java.io.*;
34
import java.net.*;
35
import com.sun.net.httpserver.*;
36
37
import jdk.test.lib.net.URIBuilder;
38
39
public class Test implements HttpHandler {
40
41
static volatile int count = 0;
42
43
static final String str1 = "Helloworld1234567890abcdefghijklmnopqrstuvwxyz"+
44
"1234567890abcdefkjsdlkjflkjsldkfjlsdkjflkj"+
45
"1434567890abcdefkjsdlkjflkjsldkfjlsdkjflkj";
46
47
static final String str2 = "Helloworld1234567890abcdefghijklmnopqrstuvwxyz"+
48
"1234567890";
49
50
public void handle(HttpExchange exchange) {
51
String reqbody;
52
try {
53
switch (exchange.getRequestURI().toString()) {
54
case "/test/test1": /* test1 -- keeps conn alive */
55
case "/test/test2": /* test2 -- closes conn */
56
printRequestURI(exchange);
57
reqbody = read(exchange.getRequestBody());
58
if (!reqbody.equals(str1)) {
59
exchange.sendResponseHeaders(500, 0);
60
break;
61
}
62
63
Headers headers = exchange.getRequestHeaders();
64
String chunk = headers.getFirst("Transfer-encoding");
65
66
if (!"chunked".equals (chunk)) {
67
exchange.sendResponseHeaders(501, 0);
68
break;
69
}
70
71
exchange.sendResponseHeaders(200, reqbody.length());
72
write(exchange.getResponseBody(), reqbody);
73
74
if (count == 1) {
75
Headers resHeaders = exchange.getResponseHeaders() ;
76
resHeaders.set("Connection", "close");
77
}
78
break;
79
case "/test/test3": /* test 3 */
80
printRequestURI(exchange);
81
reqbody = read(exchange.getRequestBody());
82
83
if (!reqbody.equals(str2)) {
84
exchange.sendResponseHeaders(500, 0);
85
break;
86
}
87
headers = exchange.getRequestHeaders();
88
int clen = Integer.parseInt( headers.getFirst("Content-length"));
89
90
if (clen != str2.length()) {
91
exchange.sendResponseHeaders(501, 0);
92
break;
93
}
94
Headers resHeaders = exchange.getResponseHeaders() ;
95
resHeaders.set("Connection", "close");
96
97
exchange.sendResponseHeaders(200, reqbody.length());
98
write(exchange.getResponseBody(), reqbody);
99
break;
100
case "/test/test4": /* test 4 */
101
case "/test/test5": /* test 5 */
102
printRequestURI(exchange);
103
break;
104
case "/test/test6": /* test 6 */
105
printRequestURI(exchange);
106
resHeaders = exchange.getResponseHeaders() ;
107
resHeaders.set("Location", "http://foo.bar/");
108
resHeaders.set("Connection", "close");
109
exchange.sendResponseHeaders(307, 0);
110
break;
111
case "/test/test7": /* test 7 */
112
case "/test/test8": /* test 8 */
113
printRequestURI(exchange);
114
reqbody = read(exchange.getRequestBody());
115
if (reqbody != null && !"".equals(reqbody)) {
116
exchange.sendResponseHeaders(501, 0);
117
break;
118
}
119
resHeaders = exchange.getResponseHeaders() ;
120
resHeaders.set("Connection", "close");
121
exchange.sendResponseHeaders(200, 0);
122
break;
123
case "/test/test9": /* test 9 */
124
printRequestURI(exchange);
125
reqbody = read(exchange.getRequestBody());
126
if (!reqbody.equals(str1)) {
127
exchange.sendResponseHeaders(500, 0);
128
break;
129
}
130
131
headers = exchange.getRequestHeaders();
132
chunk = headers.getFirst("Transfer-encoding");
133
if (!"chunked".equals(chunk)) {
134
exchange.sendResponseHeaders(501, 0);
135
break;
136
}
137
138
exchange.sendResponseHeaders(200, reqbody.length());
139
write(exchange.getResponseBody(), reqbody);
140
break;
141
case "/test/test10": /* test10 */
142
printRequestURI(exchange);
143
InputStream is = exchange.getRequestBody();
144
String s = read (is, str1.length());
145
146
boolean error = false;
147
for (int i=10; i< 200 * 1024; i++) {
148
byte c = (byte)is.read();
149
150
if (c != (byte)i) {
151
error = true;
152
System.out.println ("error at position " + i);
153
}
154
}
155
if (!s.equals(str1) ) {
156
System.out.println ("received string : " + s);
157
exchange.sendResponseHeaders(500, 0);
158
} else if (error) {
159
System.out.println ("error");
160
exchange.sendResponseHeaders(500, 0);
161
} else {
162
exchange.sendResponseHeaders(200, 0);
163
}
164
break;
165
case "/test/test11": /* test11 */
166
printRequestURI(exchange);
167
is = exchange.getRequestBody();
168
s = read (is, str1.length());
169
170
error = false;
171
for (int i=10; i< 30 * 1024; i++) {
172
byte c = (byte)is.read();
173
174
if (c != (byte)i) {
175
error = true;
176
System.out.println ("error at position " + i);
177
}
178
}
179
if (!s.equals(str1) ) {
180
System.out.println ("received string : " + s);
181
exchange.sendResponseHeaders(500, 0);
182
} else if (error) {
183
System.out.println ("error");
184
exchange.sendResponseHeaders(500, 0);
185
} else {
186
exchange.sendResponseHeaders(200, 0);
187
}
188
break;
189
case "/test/test12": /* test12 */
190
printRequestURI(exchange);
191
is = exchange.getRequestBody();
192
193
error = false;
194
for (int i=10; i< 30 * 1024; i++) {
195
byte c = (byte)is.read();
196
197
if (c != (byte)i) {
198
error = true;
199
System.out.println ("error at position " + i);
200
}
201
}
202
if (error) {
203
System.out.println ("error");
204
exchange.sendResponseHeaders(500, 0);
205
} else {
206
exchange.sendResponseHeaders(200, 0);
207
}
208
break;
209
}
210
count ++;
211
exchange.close();
212
} catch (IOException e) {
213
e.printStackTrace();
214
}
215
}
216
217
static void printRequestURI(HttpExchange exchange) {
218
URI uri = exchange.getRequestURI();
219
System.out.println("HttpServer: handle " + uri);
220
}
221
222
223
static String read (InputStream is, int len) {
224
try {
225
byte[] ba = new byte [len];
226
int c;
227
int l = 0;
228
while ((c= is.read(ba, l, ba.length-l)) != -1 && l<len) {
229
l += c;
230
}
231
return new String (ba, 0, l, "ISO8859-1");
232
} catch (Exception e) {
233
e.printStackTrace();
234
}
235
return null;
236
}
237
238
static String read(InputStream is) {
239
try {
240
byte[] ba = new byte [8096];
241
int off = 0, c;
242
while ((c= is.read(ba, off, ba.length)) != -1) {
243
off += c;
244
}
245
return new String(ba, 0, off, "ISO8859-1");
246
} catch (Exception e) {
247
e.printStackTrace();
248
}
249
return null;
250
}
251
252
static void write(OutputStream os, String str) {
253
try {
254
byte[] ba = str.getBytes("ISO8859-1");
255
os.write(ba);
256
} catch (Exception e) {
257
e.printStackTrace();
258
}
259
}
260
261
static void readAndCompare(InputStream is, String cmp) throws IOException {
262
int c;
263
byte buf[] = new byte [1024];
264
int off = 0;
265
int len = 1024;
266
while ((c=is.read(buf, off, len)) != -1) {
267
off += c;
268
len -= c;
269
}
270
String s1 = new String(buf, 0, off, "ISO8859_1");
271
if (!cmp.equals(s1)) {
272
throw new IOException("strings not same");
273
}
274
}
275
276
/* basic chunked test (runs twice) */
277
278
static void test1(URL url) throws Exception {
279
System.out.println("client opening connection to: " + url);
280
HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
281
urlc.setChunkedStreamingMode (20);
282
urlc.setDoOutput(true);
283
urlc.setRequestMethod ("POST");
284
OutputStream os = urlc.getOutputStream ();
285
os.write (str1.getBytes());
286
os.close();
287
InputStream is = urlc.getInputStream();
288
readAndCompare (is, str1);
289
is.close();
290
}
291
292
/* basic fixed length test */
293
294
static void test3(URL url) throws Exception {
295
System.out.println("client opening connection to: " + url);
296
HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
297
urlc.setFixedLengthStreamingMode (str2.length());
298
urlc.setDoOutput(true);
299
urlc.setRequestMethod ("POST");
300
OutputStream os = urlc.getOutputStream ();
301
os.write (str2.getBytes());
302
os.close();
303
InputStream is = urlc.getInputStream();
304
readAndCompare (is, str2);
305
is.close();
306
}
307
308
/* write too few bytes */
309
310
static void test4(URL url) throws Exception {
311
System.out.println("client opening connection to: " + url);
312
HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
313
urlc.setFixedLengthStreamingMode (str2.length()+1);
314
urlc.setDoOutput(true);
315
urlc.setRequestMethod ("POST");
316
OutputStream os = urlc.getOutputStream ();
317
os.write (str2.getBytes());
318
try {
319
os.close();
320
throw new Exception ("should have thrown IOException");
321
} catch (IOException e) {}
322
}
323
324
/* write too many bytes */
325
326
static void test5(URL url) throws Exception {
327
System.out.println("client opening connection to: " + url);
328
HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
329
urlc.setFixedLengthStreamingMode (str2.length()-1);
330
urlc.setDoOutput(true);
331
urlc.setRequestMethod ("POST");
332
OutputStream os = urlc.getOutputStream ();
333
try {
334
os.write (str2.getBytes());
335
throw new Exception ("should have thrown IOException");
336
} catch (IOException e) {}
337
}
338
339
/* check for HttpRetryException on redirection */
340
341
static void test6(URL url) throws Exception {
342
System.out.println("client opening connection to: " + url);
343
HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
344
urlc.setChunkedStreamingMode (20);
345
urlc.setDoOutput(true);
346
urlc.setRequestMethod ("POST");
347
OutputStream os = urlc.getOutputStream ();
348
os.write (str1.getBytes());
349
os.close();
350
try {
351
InputStream is = urlc.getInputStream();
352
throw new Exception ("should have gotten HttpRetryException");
353
} catch (HttpRetryException e) {
354
if (e.responseCode() != 307) {
355
throw new Exception ("Wrong response code " + e.responseCode());
356
}
357
if (!e.getLocation().equals ("http://foo.bar/")) {
358
throw new Exception ("Wrong location " + e.getLocation());
359
}
360
}
361
}
362
363
/* next two tests send zero length posts */
364
365
static void test7(URL url) throws Exception {
366
System.out.println("client opening connection to: " + url);
367
HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
368
urlc.setChunkedStreamingMode (20);
369
urlc.setDoOutput(true);
370
urlc.setRequestMethod ("POST");
371
OutputStream os = urlc.getOutputStream ();
372
os.close();
373
int ret = urlc.getResponseCode();
374
if (ret != 200) {
375
throw new Exception ("Expected 200: got " + ret);
376
}
377
}
378
379
static void test8(URL url) throws Exception {
380
System.out.println("client opening connection to: " + url);
381
HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
382
urlc.setFixedLengthStreamingMode (0);
383
urlc.setDoOutput(true);
384
urlc.setRequestMethod ("POST");
385
OutputStream os = urlc.getOutputStream ();
386
os.close();
387
int ret = urlc.getResponseCode();
388
if (ret != 200) {
389
throw new Exception ("Expected 200: got " + ret);
390
}
391
}
392
393
/* calling setChunkedStreamingMode with -1 should entail using
394
the default chunk size */
395
static void test9(URL url) throws Exception {
396
System.out.println("client opening connection to: " + url);
397
HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
398
urlc.setChunkedStreamingMode (-1);
399
urlc.setDoOutput(true);
400
urlc.setRequestMethod ("POST");
401
OutputStream os = urlc.getOutputStream ();
402
os.write (str1.getBytes());
403
os.close();
404
InputStream is = urlc.getInputStream();
405
readAndCompare (is, str1);
406
is.close();
407
}
408
409
static void test10(URL url) throws Exception {
410
System.out.println("client opening connection to: " + url);
411
HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
412
urlc.setChunkedStreamingMode (4 * 1024);
413
urlc.setDoOutput(true);
414
urlc.setRequestMethod ("POST");
415
OutputStream os = urlc.getOutputStream ();
416
byte[] buf = new byte [200 * 1024];
417
for (int i=0; i< 200 * 1024; i++) {
418
buf[i] = (byte) i;
419
}
420
/* write a small bit first, and then the large buffer */
421
os.write (str1.getBytes());
422
os.write (buf, 10, buf.length - 10); /* skip 10 bytes to test offset */
423
os.close();
424
InputStream is = urlc.getInputStream();
425
is.close();
426
int ret = urlc.getResponseCode();
427
if (ret != 200) {
428
throw new Exception ("Expected 200: got " + ret);
429
}
430
}
431
432
static void test11(URL url) throws Exception {
433
System.out.println("client opening connection to: " + url);
434
HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
435
urlc.setChunkedStreamingMode (36 * 1024);
436
urlc.setDoOutput(true);
437
urlc.setRequestMethod ("POST");
438
OutputStream os = urlc.getOutputStream ();
439
byte[] buf = new byte [30 * 1024];
440
for (int i=0; i< 30 * 1024; i++) {
441
buf[i] = (byte) i;
442
}
443
/* write a small bit first, and then the large buffer */
444
os.write (str1.getBytes());
445
//os.write (buf, 10, buf.length - 10); /* skip 10 bytes to test offset */
446
os.write (buf, 10, (10 * 1024) - 10);
447
os.write (buf, (10 * 1024), (10 * 1024));
448
os.write (buf, (20 * 1024), (10 * 1024));
449
os.close();
450
InputStream is = urlc.getInputStream();
451
is.close();
452
int ret = urlc.getResponseCode();
453
if (ret != 200) {
454
throw new Exception ("Expected 200: got " + ret);
455
}
456
}
457
458
static void test12(URL url) throws Exception {
459
System.out.println("client opening connection to: " + url);
460
HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
461
urlc.setChunkedStreamingMode (36 * 1024);
462
urlc.setDoOutput(true);
463
urlc.setRequestMethod ("POST");
464
OutputStream os = urlc.getOutputStream ();
465
byte[] buf = new byte [30 * 1024];
466
for (int i=0; i< 30 * 1024; i++) {
467
buf[i] = (byte) i;
468
}
469
os.write (buf, 10, buf.length - 10); /* skip 10 bytes to test offset */
470
os.close();
471
InputStream is = urlc.getInputStream();
472
is.close();
473
int ret = urlc.getResponseCode();
474
if (ret != 200) {
475
throw new Exception ("Expected 200: got " + ret);
476
}
477
}
478
479
480
static HttpServer httpserver;
481
482
private static URL buildTestURL(int port, String path)
483
throws MalformedURLException, URISyntaxException {
484
return URIBuilder.newBuilder()
485
.scheme("http")
486
.loopback()
487
.port(port)
488
.path(path)
489
.toURL();
490
}
491
492
public static void main (String[] args) throws Exception {
493
try {
494
httpserver = HttpServer.create(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 0);
495
httpserver.createContext("/test/", new Test());
496
httpserver.start();
497
498
int port = httpserver.getAddress().getPort();
499
500
System.out.println ("Server started: listening on port: " + port);
501
test1(buildTestURL(port, "/test/test1"));
502
test1(buildTestURL(port, "/test/test2"));
503
test3(buildTestURL(port, "/test/test3"));
504
test4(buildTestURL(port, "/test/test4"));
505
test5(buildTestURL(port, "/test/test5"));
506
test6(buildTestURL(port, "/test/test6"));
507
test7(buildTestURL(port, "/test/test7"));
508
test8(buildTestURL(port, "/test/test8"));
509
test9(buildTestURL(port, "/test/test9"));
510
test10(buildTestURL(port, "/test/test10"));
511
test11(buildTestURL(port, "/test/test11"));
512
test12(buildTestURL(port, "/test/test12"));
513
} finally {
514
if (httpserver != null)
515
httpserver.stop(0);
516
}
517
}
518
519
}
520
521