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/KeepAliveStream/KeepAliveStreamCloseWithWrongContentLength.java
38868 views
1
/*
2
* Copyright (c) 2002, 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 4533243
27
* @summary Closing a keep alive stream gives NullPointerException
28
* @run main/othervm/timeout=30 KeepAliveStreamCloseWithWrongContentLength
29
*/
30
31
import java.net.*;
32
import java.io.*;
33
34
public class KeepAliveStreamCloseWithWrongContentLength {
35
36
static class XServer extends Thread {
37
ServerSocket srv;
38
Socket s;
39
InputStream is;
40
OutputStream os;
41
42
XServer (ServerSocket s) {
43
srv = s;
44
}
45
46
public void run() {
47
try {
48
s = srv.accept ();
49
// read HTTP request from client
50
InputStream is = s.getInputStream();
51
// read the first ten bytes
52
for (int i=0; i<10; i++) {
53
is.read();
54
}
55
OutputStreamWriter ow =
56
new OutputStreamWriter((os = s.getOutputStream()));
57
ow.write("HTTP/1.0 200 OK\n");
58
59
// Note: The client expects 10 bytes.
60
ow.write("Content-Length: 10\n");
61
ow.write("Content-Type: text/html\n");
62
63
// Note: If this line is missing, everything works fine.
64
ow.write("Connection: Keep-Alive\n");
65
ow.write("\n");
66
67
// Note: The (buggy) server only sends 9 bytes.
68
ow.write("123456789");
69
ow.flush();
70
} catch (Exception e) {
71
} finally {
72
try {if (os != null) { os.close(); }} catch (IOException e) {}
73
}
74
}
75
}
76
77
public static void main (String[] args) throws Exception {
78
ServerSocket serversocket = new ServerSocket (0);
79
try {
80
int port = serversocket.getLocalPort ();
81
XServer server = new XServer (serversocket);
82
server.start ();
83
URL url = new URL ("http://localhost:"+port);
84
HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
85
InputStream is = urlc.getInputStream ();
86
int c = 0;
87
while (c != -1) {
88
try {
89
c=is.read();
90
} catch (IOException ioe) {
91
is.read ();
92
break;
93
}
94
}
95
is.close();
96
} catch (IOException e) {
97
return;
98
} catch (NullPointerException e) {
99
throw new RuntimeException (e);
100
} finally {
101
if (serversocket != null) serversocket.close();
102
}
103
}
104
}
105
106