|
| 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 | +} |
0 commit comments