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