Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/jdk/sun/net/www/http/KeepAliveStream/KeepAliveStreamCloseWithWrongContentLength.java
66646 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 8263364
27
* @summary Closing a keep alive stream should not give NullPointerException and should accept a connection from a
28
* client only from this test
29
* @library /test/lib
30
* @run main/othervm/timeout=30 KeepAliveStreamCloseWithWrongContentLength
31
*/
32
33
import java.net.*;
34
import java.io.*;
35
import jdk.test.lib.net.URIBuilder;
36
import java.nio.ByteBuffer;
37
import java.nio.channels.ServerSocketChannel;
38
import java.nio.channels.SocketChannel;
39
40
public class KeepAliveStreamCloseWithWrongContentLength {
41
42
private final static String path = "/KeepAliveStreamCloseWithWrongContentLength";
43
private final static String getRequest1stLine = "GET %s".formatted(path);
44
45
static class XServer extends Thread implements AutoCloseable {
46
47
final ServerSocket serverSocket;
48
volatile Socket clientSocket;
49
50
XServer (InetAddress address) throws IOException {
51
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
52
ServerSocket serversocket = serverSocketChannel.socket();
53
serversocket.bind(new InetSocketAddress(address, 0));
54
this.serverSocket = serversocket;
55
}
56
57
public int getLocalPort() {
58
return serverSocket.getLocalPort();
59
}
60
61
public void run() {
62
63
try {
64
ByteArrayOutputStream clientBytes;
65
clientSocket = null;
66
67
// in a concurrent test environment it can happen that other rouge clients connect to this server
68
// so we need to identify and connect only to the client from this test
69
// if the rouge client sends as least bytes as there is in getRequest1stLine it will be discarded and
70
// the test should proceed otherwise it should timeout on readNBytes below
71
do {
72
if (clientSocket != null) {
73
final String client = "%s:%d".formatted(
74
clientSocket.getInetAddress().getHostAddress(),
75
clientSocket.getPort()
76
);
77
try {
78
clientSocket.close();
79
}
80
catch (IOException ioe) {
81
ioe.printStackTrace();
82
}
83
finally {
84
System.err.println("rogue client (%s) connection attempt, ignoring".formatted(client));
85
}
86
}
87
clientSocket = serverSocket.accept();
88
// read HTTP request from client
89
clientBytes = new ByteArrayOutputStream();
90
clientBytes.write(clientSocket.getInputStream().readNBytes(getRequest1stLine.getBytes().length));
91
}
92
while(!getRequest1stLine.equals(clientBytes.toString()));
93
}
94
catch (Exception e) {
95
e.printStackTrace();
96
}
97
try {
98
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(clientSocket.getOutputStream());
99
outputStreamWriter.write("HTTP/1.0 200 OK\n");
100
101
// Note: The client expects 10 bytes.
102
outputStreamWriter.write("Content-Length: 10\n");
103
outputStreamWriter.write("Content-Type: text/html\n");
104
105
// Note: If this line is missing, everything works fine.
106
outputStreamWriter.write("Connection: Keep-Alive\n");
107
outputStreamWriter.write("\n");
108
109
// Note: The (buggy) server only sends 9 bytes.
110
outputStreamWriter.write("123456789");
111
outputStreamWriter.flush();
112
clientSocket.getChannel().shutdownOutput();
113
}
114
catch (Exception e) {
115
e.printStackTrace();
116
}
117
}
118
119
@Override
120
public void close() throws Exception {
121
final var clientSocket = this.clientSocket;
122
try {
123
long drained = drain(clientSocket.getChannel());
124
System.err.printf("Server drained %d bytes from the channel%n", drained);
125
} catch (Exception x) {
126
System.err.println("Server failed to drain client socket: " + x);
127
x.printStackTrace();
128
}
129
serverSocket.close();
130
}
131
132
}
133
134
static long drain(SocketChannel channel) throws IOException {
135
if (!channel.isOpen()) return 0;
136
System.err.println("Not reading server: draining socket");
137
var blocking = channel.isBlocking();
138
if (blocking) channel.configureBlocking(false);
139
long count = 0;
140
try {
141
ByteBuffer buffer = ByteBuffer.allocateDirect(8 * 1024);
142
int read;
143
while ((read = channel.read(buffer)) > 0) {
144
count += read;
145
buffer.clear();
146
}
147
return count;
148
} finally {
149
if (blocking != channel.isBlocking()) {
150
channel.configureBlocking(blocking);
151
}
152
}
153
}
154
155
156
public static void main (String[] args) throws Exception {
157
158
final InetAddress loopback = InetAddress.getLoopbackAddress();
159
160
try (XServer server = new XServer(loopback)) {
161
server.start();
162
URL url = URIBuilder.newBuilder()
163
.scheme("http")
164
.loopback()
165
.path(path)
166
.port(server.getLocalPort())
167
.toURL();
168
HttpURLConnection urlc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);
169
InputStream is = urlc.getInputStream();
170
int c = 0;
171
while (c != -1) {
172
try {
173
c=is.read();
174
System.out.println("client reads: "+c);
175
} catch (IOException ioe) {
176
is.read ();
177
break;
178
}
179
}
180
is.close();
181
}
182
183
}
184
}
185
186