Skip to content

Commit b6ffb44

Browse files
committed
8335135: HttpURLConnection#HttpInputStream does not throw IOException when response is truncated
Reviewed-by: dfuchs
1 parent 4ab7e98 commit b6ffb44

File tree

4 files changed

+120
-5
lines changed

4 files changed

+120
-5
lines changed

src/java.base/share/classes/sun/net/www/MeteredStream.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1994, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1994, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -54,6 +54,9 @@ private final void justRead(long n) throws IOException {
5454
assert isLockHeldByCurrentThread();
5555

5656
if (n == -1) {
57+
if (expected > count) {
58+
throw new IOException("Premature EOF");
59+
}
5760

5861
/*
5962
* don't close automatically when mark is set and is valid;

test/jdk/java/net/Authenticator/BasicTest4.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2002, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2002, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -151,7 +151,7 @@ public void run () {
151151
System.out.println ("checkfor returned " + success);
152152
readAll (s);
153153
os = s.getOutputStream();
154-
os.write (reply2.getBytes());
154+
os.write ((reply2+"HelloAgain").getBytes());
155155
s.close ();
156156

157157
if (success)
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
* Copyright (c) 2024, 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 8335135
27+
* @library /test/lib
28+
* @summary Check that reading from inputStream throws an IOException
29+
* if the fixed response stream is closed before reading all bytes.
30+
*/
31+
32+
import jdk.test.lib.net.URIBuilder;
33+
34+
import java.io.BufferedOutputStream;
35+
import java.io.BufferedReader;
36+
import java.io.IOException;
37+
import java.io.InputStream;
38+
import java.io.InputStreamReader;
39+
import java.io.PrintStream;
40+
import java.net.HttpURLConnection;
41+
import java.net.InetAddress;
42+
import java.net.InetSocketAddress;
43+
import java.net.Proxy;
44+
import java.net.ServerSocket;
45+
import java.net.Socket;
46+
import java.net.URL;
47+
48+
public class TruncatedFixedResponse implements Runnable {
49+
50+
ServerSocket ss;
51+
52+
/*
53+
* Our "http" server to return a truncated fixed response
54+
*/
55+
public void run() {
56+
try {
57+
Socket s = ss.accept();
58+
59+
BufferedReader in = new BufferedReader(
60+
new InputStreamReader(s.getInputStream()));
61+
while (true) {
62+
String req = in.readLine();
63+
if (req.isEmpty()) {
64+
break;
65+
}
66+
}
67+
PrintStream out = new PrintStream(
68+
new BufferedOutputStream(s.getOutputStream()));
69+
70+
/* send the header */
71+
out.print("HTTP/1.1 200\r\n");
72+
out.print("Content-Length: 100\r\n");
73+
out.print("Content-Type: text/html\r\n");
74+
out.print("\r\n");
75+
out.print("Some content, but too short");
76+
out.close();
77+
s.close();
78+
ss.close();
79+
} catch (Exception e) {
80+
e.printStackTrace();
81+
}
82+
}
83+
84+
TruncatedFixedResponse() throws Exception {
85+
/* start the server */
86+
ss = new ServerSocket();
87+
ss.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
88+
new Thread(this).start();
89+
90+
/* establish http connection to server */
91+
URL url = URIBuilder.newBuilder()
92+
.scheme("http")
93+
.loopback()
94+
.port(ss.getLocalPort())
95+
.path("/foo")
96+
.toURL();
97+
HttpURLConnection http = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);
98+
99+
try (InputStream in = http.getInputStream()) {
100+
while (in.read() != -1) {
101+
// discard response
102+
}
103+
throw new AssertionError("Expected IOException was not thrown");
104+
} catch (IOException ex) {
105+
System.out.println("Got expected exception: " + ex);
106+
}
107+
}
108+
109+
public static void main(String args[]) throws Exception {
110+
new TruncatedFixedResponse();
111+
}
112+
}

test/jdk/sun/net/www/http/KeepAliveStream/KeepAliveStreamCloseWithWrongContentLength.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2002, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2002, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -173,7 +173,7 @@ public static void main (String[] args) throws Exception {
173173
c=is.read();
174174
System.out.println("client reads: "+c);
175175
} catch (IOException ioe) {
176-
is.read ();
176+
System.out.println("client got expected exception: "+ioe);
177177
break;
178178
}
179179
}

0 commit comments

Comments
 (0)