Path: blob/master/test/jdk/sun/net/www/http/KeepAliveStream/KeepAliveStreamClose.java
41155 views
/*1* Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 439219526* @summary Infinite loop in sun.net.www.http.KeepAliveStream [due to skip()]27* @library /test/lib28* @run main/othervm/timeout=30 KeepAliveStreamClose29*/3031import java.net.*;32import java.io.*;33import jdk.test.lib.net.URIBuilder;3435public class KeepAliveStreamClose {36static class XServer extends Thread {37ServerSocket srv;38Socket s;39InputStream is;40OutputStream os;4142XServer (ServerSocket s) {43srv = s;44}4546Socket getSocket () {47return (s);48}4950// simulated HTTP server response51static String response = "HTTP/1.1 200 OK\nDate: Thu, 07 Dec 2000 11:32:28 GMT\n"+52"Server: Apache/1.3.6 (Unix)\nKeep-Alive: timeout=15, max=100\nConnection: Keep-Alive\n"+53"Content-length: 255\nContent-Type: text/html\n\n";5455public void run() {56try {57s = srv.accept ();58is = s.getInputStream ();59os = s.getOutputStream ();60// read the first ten bytes61for (int i=0; i<10; i++) {62is.read();63}64os.write (response.getBytes());65for (int i=0; i<255; i++) {66os.write ("X".getBytes());67Thread.sleep (1000);68}69} catch (Exception e) {70}71}72}7374/*75* If the server closes the connection at the same time as the client76* does, then the client will hang (jdk1.3) because KeepAliveStream.skip77* is returning zero and the calling close() stays in a loop78*/7980public static void main (String[] args) {81try {82InetAddress loopback = InetAddress.getLoopbackAddress();83ServerSocket serversocket = new ServerSocket (0, 50, loopback);84int port = serversocket.getLocalPort ();85XServer server = new XServer (serversocket);86server.start ();87URL url = URIBuilder.newBuilder()88.scheme("http")89.loopback()90.port(port)91.toURL();92URLConnection urlc = url.openConnection ();93InputStream is = urlc.getInputStream ();94int i=0, c;95while ((c=is.read())!= -1) {96i++;97if (i == 5) {98server.getSocket().close ();99is.close();100break;101}102}103} catch (Exception e) {104e.printStackTrace();105throw new RuntimeException ("Unexpected exception");106}107}108}109110111