Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/net/httpserver/bugs/B6526913.java
38867 views
/*1* Copyright (c) 2007, 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* @run main/othervm -Dhttp.keepAlive=false B652691327* @summary HttpExchange.getResponseBody().close() throws Exception28*/2930import com.sun.net.httpserver.*;3132import java.util.*;33import java.util.concurrent.*;34import java.io.*;35import java.net.*;36import java.security.*;37import java.security.cert.*;38import javax.net.ssl.*;3940public class B6526913 {4142public static void main (String[] args) throws Exception {43Handler handler = new Handler();44InetSocketAddress addr = new InetSocketAddress (0);45HttpServer server = HttpServer.create (addr, 0);46HttpContext ctx = server.createContext ("/test", handler);4748ExecutorService executor = Executors.newCachedThreadPool();49server.setExecutor (executor);50server.start ();5152URL url = new URL ("http://localhost:"+server.getAddress().getPort()+"/test/foo.html");53HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();54try {55InputStream is = urlc.getInputStream();56int c ,count = 0;57byte [] buf = new byte [32 * 1024];58while (count < 32 * 1024) {59count += is.read (buf);60}61is.close();62} finally {63server.stop(2);64executor.shutdown();65}66if (error) {67throw new RuntimeException ("Test failed");68}69}7071public static boolean error = false;7273static class Handler implements HttpHandler {74int invocation = 1;75public void handle (HttpExchange t)76throws IOException77{78InputStream is = t.getRequestBody();79try {80while (is.read() != -1) ;81is.close();82} catch (IOException e) {83e.printStackTrace();84error = true;85}86/* send a chunked response, but wait a while before87* sending the final empty chunk88*/89t.sendResponseHeaders (200, 0);90OutputStream os = t.getResponseBody();91byte[] bb = new byte [32 * 1024];92os.write (bb);93os.flush();94try {Thread.sleep (5000); } catch (InterruptedException e){}95try {96/* empty chunk sent here */97os.close();98} catch (IOException e) {99error = true;100e.printStackTrace();101}102t.close();103}104}105}106107108