Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/URLConnection/ZeroContentLength.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 4507412
27
* @bug 4506998
28
* @summary Check that a 304 "Not-Modified" response from a server
29
* doesn't cause http client to close a keep-alive
30
* connection.
31
* Check that a content-length of 0 results in an
32
* empty input stream.
33
* @library /test/lib
34
* @run main ZeroContentLength
35
* @run main/othervm -Djava.net.preferIPv6Addresses=true ZeroContentLength
36
*/
37
38
import java.net.*;
39
import java.io.*;
40
import jdk.test.lib.net.URIBuilder;
41
42
public class ZeroContentLength {
43
44
/*
45
* Is debugging enabled - start with -d to enable.
46
*/
47
static boolean debug = false;
48
49
static void debug(String msg) {
50
if (debug)
51
System.out.println(msg);
52
}
53
54
/*
55
* The response string and content-length that
56
* the server should return;
57
*/
58
static String response;
59
static int contentLength;
60
61
static synchronized void setResponse(String rsp, int cl) {
62
response = rsp;
63
contentLength = cl;
64
}
65
66
static synchronized String getResponse() {
67
return response;
68
}
69
70
static synchronized int getContentLength() {
71
return contentLength;
72
}
73
74
/*
75
* Worker thread to service single connection - can service
76
* multiple http requests on same connection.
77
*/
78
class Worker extends Thread {
79
Socket s;
80
int id;
81
82
Worker(Socket s, int id) {
83
this.s = s;
84
this.id = id;
85
}
86
87
final int CR = '\r';
88
final int LF = '\n';
89
90
public void run() {
91
try {
92
93
s.setSoTimeout(2000);
94
int max = 20; // there should only be 20 connections
95
InputStream in = new BufferedInputStream(s.getInputStream());
96
97
for (;;) {
98
// read entire request from client, until CR LF CR LF
99
int c, total=0;
100
101
try {
102
while ((c = in.read()) > 0) {
103
total++;
104
if (c == CR) {
105
if ((c = in.read()) > 0) {
106
total++;
107
if (c == LF) {
108
if ((c = in.read()) > 0) {
109
total++;
110
if (c == CR) {
111
if ((c = in.read()) > 0) {
112
total++;
113
if (c == LF) {
114
break;
115
}
116
}
117
}
118
}
119
}
120
}
121
}
122
123
}
124
} catch (SocketTimeoutException e) {}
125
126
debug("worker " + id +
127
": Read request from client " +
128
"(" + total + " bytes).");
129
130
if (total == 0) {
131
debug("worker: " + id + ": Shutdown");
132
return;
133
}
134
135
// response to client
136
PrintStream out = new PrintStream(
137
new BufferedOutputStream(
138
s.getOutputStream() ));
139
140
out.print("HTTP/1.1 " + getResponse() + "\r\n");
141
int clen = getContentLength();
142
if (clen >= 0) {
143
out.print("Content-Length: " + clen +
144
"\r\n");
145
}
146
out.print("\r\n");
147
for (int i=0; i<clen; i++) {
148
out.write( (byte)'.' );
149
}
150
out.flush();
151
152
debug("worked " + id +
153
": Sent response to client, length: " + clen);
154
155
if (--max == 0) {
156
s.close();
157
return;
158
}
159
}
160
161
} catch (Exception e) {
162
e.printStackTrace();
163
} finally {
164
try {
165
s.close();
166
} catch (Exception e) { }
167
}
168
}
169
}
170
171
/*
172
* Server thread to accept connection and create worker threads
173
* to service each connection.
174
*/
175
class Server extends Thread {
176
ServerSocket ss;
177
int connectionCount;
178
boolean shutdown = false;
179
180
Server(ServerSocket ss) {
181
this.ss = ss;
182
}
183
184
public synchronized int connectionCount() {
185
return connectionCount;
186
}
187
188
public synchronized void shutdown() {
189
shutdown = true;
190
}
191
192
public void run() {
193
try {
194
ss.setSoTimeout(2000);
195
196
for (;;) {
197
Socket s;
198
try {
199
debug("server: Waiting for connections");
200
s = ss.accept();
201
} catch (SocketTimeoutException te) {
202
synchronized (this) {
203
if (shutdown) {
204
debug("server: Shuting down.");
205
return;
206
}
207
}
208
continue;
209
}
210
211
int id;
212
synchronized (this) {
213
id = connectionCount++;
214
}
215
216
Worker w = new Worker(s, id);
217
w.start();
218
debug("server: Started worker " + id);
219
}
220
221
} catch (Exception e) {
222
e.printStackTrace();
223
} finally {
224
try {
225
ss.close();
226
} catch (Exception e) { }
227
}
228
}
229
}
230
231
/*
232
* Make a single http request and return the content length
233
* received. Also do sanity check to ensure that the
234
* content-length header matches the total received on
235
* the input stream.
236
*/
237
int doRequest(String uri) throws Exception {
238
URL url = new URL(uri);
239
HttpURLConnection http = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);
240
241
int cl = http.getContentLength();
242
243
InputStream in = http.getInputStream();
244
byte b[] = new byte[100];
245
int total = 0;
246
int n;
247
do {
248
n = in.read(b);
249
if (n > 0) total += n;
250
} while (n > 0);
251
in.close();
252
253
if (cl >= 0 && total != cl) {
254
System.err.println("content-length header indicated: " + cl);
255
System.err.println("Actual received: " + total);
256
throw new Exception("Content-length didn't match actual received");
257
}
258
259
return total;
260
}
261
262
263
/*
264
* Send http requests to "server" and check that they all
265
* use the same network connection and that the content
266
* length corresponds to the content length expected.
267
* stream.
268
*/
269
ZeroContentLength() throws Exception {
270
271
/* start the server */
272
ServerSocket ss = new ServerSocket();
273
ss.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
274
Server svr = new Server(ss);
275
svr.start();
276
277
String uri = URIBuilder.newBuilder()
278
.scheme("http")
279
.host(ss.getInetAddress())
280
.port(ss.getLocalPort())
281
.path("/foo.html")
282
.build().toString();
283
284
int expectedTotal = 0;
285
int actualTotal = 0;
286
287
System.out.println("**********************************");
288
System.out.println("200 OK, content-length:1024 ...");
289
setResponse("200 OK", 1024);
290
for (int i=0; i<5; i++) {
291
actualTotal += doRequest(uri);
292
expectedTotal += 1024;
293
}
294
295
System.out.println("**********************************");
296
System.out.println("200 OK, content-length:0 ...");
297
setResponse("200 OK", 0);
298
for (int i=0; i<5; i++) {
299
actualTotal += doRequest(uri);
300
}
301
302
System.out.println("**********************************");
303
System.out.println("304 Not-Modified, (no content-length) ...");
304
setResponse("304 Not-Modifed", -1);
305
for (int i=0; i<5; i++) {
306
actualTotal += doRequest(uri);
307
}
308
309
System.out.println("**********************************");
310
System.out.println("204 No-Content, (no content-length) ...");
311
setResponse("204 No-Content", -1);
312
for (int i=0; i<5; i++) {
313
actualTotal += doRequest(uri);
314
}
315
316
// shutdown server - we're done.
317
svr.shutdown();
318
319
System.out.println("**********************************");
320
321
if (actualTotal == expectedTotal) {
322
System.out.println("Passed: Actual total equal to expected total");
323
} else {
324
throw new Exception("Actual total != Expected total!!!");
325
}
326
327
int cnt = svr.connectionCount();
328
if (cnt == 1) {
329
System.out.println("Passed: Only 1 connection established");
330
} else {
331
throw new Exception("Test failed: Number of connections " +
332
"established: " + cnt + " - see log for details.");
333
}
334
}
335
336
public static void main(String args[]) throws Exception {
337
338
if (args.length > 0 && args[0].equals("-d")) {
339
debug = true;
340
}
341
342
new ZeroContentLength();
343
}
344
345
}
346
347