2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 2001, Oracle and/or its affiliates. All rights reserved.
|
2
|
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 |
*
|
5506
|
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.
|
2
|
22 |
*/
|
|
23 |
|
|
24 |
/**
|
|
25 |
*
|
|
26 |
* @bug 4333920
|
|
27 |
* @bug 4394548
|
|
28 |
* @summary Check that chunked encoding response doesn't cause
|
|
29 |
* getInputStream to block until last chunk arrives.
|
|
30 |
* Also regression against NPE in ChunkedInputStream.
|
|
31 |
*/
|
|
32 |
import java.net.*;
|
|
33 |
import java.io.*;
|
|
34 |
import java.util.Random;
|
|
35 |
|
|
36 |
public class ChunkedEncoding implements Runnable {
|
|
37 |
|
|
38 |
ServerSocket ss;
|
|
39 |
|
|
40 |
/*
|
|
41 |
* Our "http" server to return a chunked response
|
|
42 |
*/
|
|
43 |
public void run() {
|
|
44 |
try {
|
|
45 |
Socket s = ss.accept();
|
|
46 |
|
|
47 |
PrintStream out = new PrintStream(
|
|
48 |
new BufferedOutputStream(
|
|
49 |
s.getOutputStream() ));
|
|
50 |
|
|
51 |
/* send the header */
|
|
52 |
out.print("HTTP/1.1 200\r\n");
|
|
53 |
out.print("Transfer-Encoding: chunked\r\n");
|
|
54 |
out.print("Content-Type: text/html\r\n");
|
|
55 |
out.print("\r\n");
|
|
56 |
out.flush();
|
|
57 |
|
|
58 |
/* delay the server before first chunk */
|
|
59 |
Thread.sleep(5000);
|
|
60 |
|
|
61 |
/*
|
|
62 |
* Our response will be of random length
|
|
63 |
* but > 32k
|
|
64 |
*/
|
|
65 |
Random rand = new Random();
|
|
66 |
|
|
67 |
int len;
|
|
68 |
do {
|
|
69 |
len = rand.nextInt(128*1024);
|
|
70 |
} while (len < 32*1024);
|
|
71 |
|
|
72 |
/*
|
|
73 |
* Our chunk size will be 2-32k
|
|
74 |
*/
|
|
75 |
int chunkSize;
|
|
76 |
do {
|
|
77 |
chunkSize = rand.nextInt(len / 3);
|
|
78 |
} while (chunkSize < 2*1024);
|
|
79 |
|
|
80 |
/*
|
|
81 |
* Generate random content and check sum it
|
|
82 |
*/
|
|
83 |
byte buf[] = new byte[len];
|
|
84 |
int cs = 0;
|
|
85 |
for (int i=0; i<len; i++) {
|
|
86 |
buf[i] = (byte)('a' + rand.nextInt(26));
|
|
87 |
cs = (cs + buf[i]) % 65536;
|
|
88 |
}
|
|
89 |
|
|
90 |
/*
|
|
91 |
* Stream the chunks to the client
|
|
92 |
*/
|
|
93 |
int remaining = len;
|
|
94 |
int pos = 0;
|
|
95 |
while (remaining > 0) {
|
|
96 |
int size = Math.min(remaining, chunkSize);
|
|
97 |
out.print( Integer.toHexString(size) );
|
|
98 |
out.print("\r\n");
|
|
99 |
out.write( buf, pos, size );
|
|
100 |
pos += size;
|
|
101 |
remaining -= size;
|
|
102 |
out.print("\r\n");
|
|
103 |
out.flush();
|
|
104 |
}
|
|
105 |
|
|
106 |
/* send EOF chunk */
|
|
107 |
out.print("0\r\n");
|
|
108 |
out.flush();
|
|
109 |
|
|
110 |
/*
|
|
111 |
* Send trailer with checksum
|
|
112 |
*/
|
|
113 |
String trailer = "Checksum:" + cs + "\r\n";
|
|
114 |
out.print(trailer);
|
|
115 |
out.print("\r\n");
|
|
116 |
out.flush();
|
|
117 |
|
|
118 |
s.close();
|
|
119 |
ss.close();
|
|
120 |
} catch (Exception e) {
|
|
121 |
e.printStackTrace();
|
|
122 |
}
|
|
123 |
}
|
|
124 |
|
|
125 |
ChunkedEncoding() throws Exception {
|
|
126 |
|
|
127 |
/* start the server */
|
|
128 |
ss = new ServerSocket(0);
|
|
129 |
(new Thread(this)).start();
|
|
130 |
|
|
131 |
/* establish http connection to server */
|
|
132 |
String uri = "http://localhost:" +
|
|
133 |
Integer.toString(ss.getLocalPort()) +
|
|
134 |
"/foo";
|
|
135 |
URL url = new URL(uri);
|
|
136 |
HttpURLConnection http = (HttpURLConnection)url.openConnection();
|
|
137 |
|
|
138 |
/*
|
|
139 |
* Server should only send headers if TE:trailers
|
|
140 |
* specified - see updated HTTP 1.1 spec.
|
|
141 |
*/
|
|
142 |
http.setRequestProperty("TE", "trailers");
|
|
143 |
|
|
144 |
/* Time how long the getInputStream takes */
|
|
145 |
long ts = System.currentTimeMillis();
|
|
146 |
InputStream in = http.getInputStream();
|
|
147 |
long te = System.currentTimeMillis();
|
|
148 |
|
|
149 |
/*
|
|
150 |
* If getInputStream takes >2 seconds it probably means
|
|
151 |
* that the implementation is waiting for the chunks to
|
|
152 |
* arrive.
|
|
153 |
*/
|
|
154 |
if ( (te-ts) > 2000) {
|
|
155 |
throw new Exception("getInputStream didn't return immediately");
|
|
156 |
}
|
|
157 |
|
|
158 |
/*
|
|
159 |
* Read the stream and checksum it as it arrives
|
|
160 |
*/
|
|
161 |
int nread;
|
|
162 |
int cs = 0;
|
|
163 |
byte b[] = new byte[1024];
|
|
164 |
do {
|
|
165 |
nread = in.read(b);
|
|
166 |
if (nread > 0) {
|
|
167 |
for (int i=0; i<nread; i++) {
|
|
168 |
cs = (cs + b[i]) % 65536;
|
|
169 |
}
|
|
170 |
}
|
|
171 |
} while (nread > 0);
|
|
172 |
|
|
173 |
/*
|
|
174 |
* Verify that the checksums match
|
|
175 |
*/
|
|
176 |
String trailer = http.getHeaderField("Checksum");
|
|
177 |
if (trailer == null) {
|
|
178 |
throw new Exception("Checksum trailer missing from response");
|
|
179 |
}
|
|
180 |
int rcvd_cs = Integer.parseInt(trailer);
|
|
181 |
if (rcvd_cs != cs) {
|
|
182 |
throw new Exception("Trailer checksum doesn't equal calculated checksum");
|
|
183 |
}
|
|
184 |
|
|
185 |
http.disconnect();
|
|
186 |
|
|
187 |
}
|
|
188 |
|
|
189 |
public static void main(String args[]) throws Exception {
|
|
190 |
new ChunkedEncoding();
|
|
191 |
}
|
|
192 |
|
|
193 |
}
|