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