J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2000-2005 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 4392195 |
| 27 | * @summary Infinite loop in sun.net.www.http.KeepAliveStream [due to skip()] |
| 28 | * @run main/othervm/timeout=30 KeepAliveStreamClose |
| 29 | */ |
| 30 | |
| 31 | import java.net.*; |
| 32 | import java.io.*; |
| 33 | |
| 34 | public class KeepAliveStreamClose { |
| 35 | static class XServer extends Thread { |
| 36 | ServerSocket srv; |
| 37 | Socket s; |
| 38 | InputStream is; |
| 39 | OutputStream os; |
| 40 | |
| 41 | XServer (ServerSocket s) { |
| 42 | srv = s; |
| 43 | } |
| 44 | |
| 45 | Socket getSocket () { |
| 46 | return (s); |
| 47 | } |
| 48 | |
| 49 | // simulated HTTP server response |
| 50 | static String response = "HTTP/1.1 200 OK\nDate: Thu, 07 Dec 2000 11:32:28 GMT\n"+ |
| 51 | "Server: Apache/1.3.6 (Unix)\nKeep-Alive: timeout=15, max=100\nConnection: Keep-Alive\n"+ |
| 52 | "Content-length: 255\nContent-Type: text/html\n\n"; |
| 53 | |
| 54 | public void run() { |
| 55 | try { |
| 56 | s = srv.accept (); |
| 57 | is = s.getInputStream (); |
| 58 | os = s.getOutputStream (); |
| 59 | // read the first ten bytes |
| 60 | for (int i=0; i<10; i++) { |
| 61 | is.read(); |
| 62 | } |
| 63 | os.write (response.getBytes()); |
| 64 | for (int i=0; i<255; i++) { |
| 65 | os.write ("X".getBytes()); |
| 66 | Thread.sleep (1000); |
| 67 | } |
| 68 | } catch (Exception e) { |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | /* |
| 74 | * If the server closes the connection at the same time as the client |
| 75 | * does, then the client will hang (jdk1.3) because KeepAliveStream.skip |
| 76 | * is returning zero and the calling close() stays in a loop |
| 77 | */ |
| 78 | |
| 79 | public static void main (String[] args) { |
| 80 | try { |
| 81 | ServerSocket serversocket = new ServerSocket (0); |
| 82 | int port = serversocket.getLocalPort (); |
| 83 | XServer server = new XServer (serversocket); |
| 84 | server.start (); |
| 85 | URL url = new URL ("http://localhost:"+port); |
| 86 | URLConnection urlc = url.openConnection (); |
| 87 | InputStream is = urlc.getInputStream (); |
| 88 | int i=0, c; |
| 89 | while ((c=is.read())!= -1) { |
| 90 | i++; |
| 91 | if (i == 5) { |
| 92 | server.getSocket().close (); |
| 93 | is.close(); |
| 94 | break; |
| 95 | } |
| 96 | } |
| 97 | } catch (Exception e) { |
| 98 | e.printStackTrace(); |
| 99 | throw new RuntimeException ("Unexpected exception"); |
| 100 | } |
| 101 | } |
| 102 | } |