J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1998-2001 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any 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 | |
| 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.currentThread().sleep(2000); |
| 57 | |
| 58 | s.close(); |
| 59 | } catch (Exception e) { |
| 60 | e.printStackTrace(); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | GetContent() throws Exception { |
| 65 | |
| 66 | ss = new ServerSocket(0); |
| 67 | Thread thr = new Thread(this); |
| 68 | thr.start(); |
| 69 | |
| 70 | boolean error = true; |
| 71 | try { |
| 72 | String name = "http://localhost:" + ss.getLocalPort() + |
| 73 | "/no-such-name"; |
| 74 | java.net.URL url = null; |
| 75 | url = new java.net.URL(name); |
| 76 | Object obj = url.getContent(); |
| 77 | InputStream in = (InputStream) obj; |
| 78 | byte buff[] = new byte[200]; |
| 79 | int len = in.read(buff); |
| 80 | } catch (IOException ex) { |
| 81 | error = false; |
| 82 | } |
| 83 | |
| 84 | ss.close(); |
| 85 | |
| 86 | if (error) |
| 87 | throw new RuntimeException("No IOException generated."); |
| 88 | } |
| 89 | |
| 90 | public static void main(String args[]) throws Exception { |
| 91 | new GetContent(); |
| 92 | } |
| 93 | } |