1 /*
2  * Copyright (c) 1998, 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 4145315
27  * @summary Test a read from nonexistant URL
28  */
29 
30 import java.net.*;
31 import java.io.*;
32 
33 public class GetContent implements Runnable {
34 
35      ServerSocket ss;
36 
run()37      public void run() {
38         try {
39             Socket s = ss.accept();
40             s.setTcpNoDelay(true);
41 
42             PrintStream out = new PrintStream(
43                                  new BufferedOutputStream(
44                                     s.getOutputStream() ));
45 
46             out.print("HTTP/1.1 404 Not Found\r\n");
47             out.print("Connection: close\r\n");
48             out.print("Content-Type: text/html; charset=iso-8859-1\r\n");
49             out.print("\r\n");
50             out.flush();
51             out.print("<HTML><BODY>Sorry, page not found</BODY></HTML>");
52             out.flush();
53 
54             // wait for client to read response - otherwise http
55             // client get error and re-establish connection
56             Thread.sleep(2000);
57 
58             s.close();
59         } catch (Exception e) {
60             e.printStackTrace();
61         } finally {
62             try { ss.close(); } catch (IOException unused) {}
63         }
64      }
65 
GetContent()66      GetContent() throws Exception {
67 
68          ss = new ServerSocket(0);
69          Thread thr = new Thread(this);
70          thr.start();
71 
72          boolean error = true;
73          try {
74              String name = "http://localhost:" + ss.getLocalPort() +
75                            "/no-such-name";
76              java.net.URL url = null;
77              url = new java.net.URL(name);
78              Object obj = url.getContent();
79              InputStream in = (InputStream) obj;
80              byte buff[] = new byte[200];
81              int len = in.read(buff);
82          } catch (IOException ex) {
83              error = false;
84          }
85 
86          if (error)
87              throw new RuntimeException("No IOException generated.");
88      }
89 
main(String args[])90      public static void main(String args[]) throws Exception {
91         new GetContent();
92      }
93 }
94