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