Path: blob/master/test/jdk/com/sun/net/httpserver/bugs/B6526913.java
41154 views
/*1* Copyright (c) 2007, 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 652691326* @library /test/lib27* @run main/othervm -Dhttp.keepAlive=false B652691328* @run main/othervm -Djava.net.preferIPv6Addresses=true29* -Dhttp.keepAlive=false B652691330* @summary HttpExchange.getResponseBody().close() throws Exception31*/3233import com.sun.net.httpserver.*;3435import java.util.*;36import java.util.concurrent.*;37import java.io.*;38import java.net.*;39import java.security.*;40import java.security.cert.*;41import javax.net.ssl.*;42import jdk.test.lib.net.URIBuilder;4344public class B6526913 {4546public static void main (String[] args) throws Exception {47Handler handler = new Handler();48InetAddress loopback = InetAddress.getLoopbackAddress();49InetSocketAddress addr = new InetSocketAddress (loopback, 0);50HttpServer server = HttpServer.create (addr, 0);51HttpContext ctx = server.createContext ("/test", handler);5253ExecutorService executor = Executors.newCachedThreadPool();54server.setExecutor (executor);55server.start ();5657URL url = URIBuilder.newBuilder()58.scheme("http")59.loopback()60.port(server.getAddress().getPort())61.path("/test/foo.html")62.toURL();63HttpURLConnection urlc = (HttpURLConnection)url.openConnection (Proxy.NO_PROXY);64try {65InputStream is = urlc.getInputStream();66int c ,count = 0;67byte [] buf = new byte [32 * 1024];68while (count < 32 * 1024) {69count += is.read (buf);70}71is.close();72} finally {73server.stop(2);74executor.shutdown();75}76if (error) {77throw new RuntimeException ("Test failed");78}79}8081public static boolean error = false;8283static class Handler implements HttpHandler {84int invocation = 1;85public void handle (HttpExchange t)86throws IOException87{88InputStream is = t.getRequestBody();89try {90while (is.read() != -1) ;91is.close();92} catch (IOException e) {93e.printStackTrace();94error = true;95}96/* send a chunked response, but wait a while before97* sending the final empty chunk98*/99t.sendResponseHeaders (200, 0);100OutputStream os = t.getResponseBody();101byte[] bb = new byte [32 * 1024];102os.write (bb);103os.flush();104try {Thread.sleep (5000); } catch (InterruptedException e){}105try {106/* empty chunk sent here */107os.close();108} catch (IOException e) {109error = true;110e.printStackTrace();111}112t.close();113}114}115}116117118