Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/http/KeepAliveStream/KeepAliveStreamClose.java
38868 views
/*1* Copyright (c) 2000, 2005, 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* @run main/othervm/timeout=30 KeepAliveStreamClose28*/2930import java.net.*;31import java.io.*;3233public class KeepAliveStreamClose {34static class XServer extends Thread {35ServerSocket srv;36Socket s;37InputStream is;38OutputStream os;3940XServer (ServerSocket s) {41srv = s;42}4344Socket getSocket () {45return (s);46}4748// simulated HTTP server response49static String response = "HTTP/1.1 200 OK\nDate: Thu, 07 Dec 2000 11:32:28 GMT\n"+50"Server: Apache/1.3.6 (Unix)\nKeep-Alive: timeout=15, max=100\nConnection: Keep-Alive\n"+51"Content-length: 255\nContent-Type: text/html\n\n";5253public void run() {54try {55s = srv.accept ();56is = s.getInputStream ();57os = s.getOutputStream ();58// read the first ten bytes59for (int i=0; i<10; i++) {60is.read();61}62os.write (response.getBytes());63for (int i=0; i<255; i++) {64os.write ("X".getBytes());65Thread.sleep (1000);66}67} catch (Exception e) {68}69}70}7172/*73* If the server closes the connection at the same time as the client74* does, then the client will hang (jdk1.3) because KeepAliveStream.skip75* is returning zero and the calling close() stays in a loop76*/7778public static void main (String[] args) {79try {80ServerSocket serversocket = new ServerSocket (0);81int port = serversocket.getLocalPort ();82XServer server = new XServer (serversocket);83server.start ();84URL url = new URL ("http://localhost:"+port);85URLConnection urlc = url.openConnection ();86InputStream is = urlc.getInputStream ();87int i=0, c;88while ((c=is.read())!= -1) {89i++;90if (i == 5) {91server.getSocket().close ();92is.close();93break;94}95}96} catch (Exception e) {97e.printStackTrace();98throw new RuntimeException ("Unexpected exception");99}100}101}102103104