1 /* 2 * Copyright (c) 2007, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /** 25 * @test 26 * @bug 6526913 27 * @run main/othervm -Dhttp.keepAlive=false B6526913 28 * @summary HttpExchange.getResponseBody().close() throws Exception 29 */ 30 31 import com.sun.net.httpserver.*; 32 33 import java.util.*; 34 import java.util.concurrent.*; 35 import java.io.*; 36 import java.net.*; 37 import java.security.*; 38 import java.security.cert.*; 39 import javax.net.ssl.*; 40 41 public class B6526913 { 42 main(String[] args)43 public static void main (String[] args) throws Exception { 44 Handler handler = new Handler(); 45 InetSocketAddress addr = new InetSocketAddress (0); 46 HttpServer server = HttpServer.create (addr, 0); 47 HttpContext ctx = server.createContext ("/test", handler); 48 49 ExecutorService executor = Executors.newCachedThreadPool(); 50 server.setExecutor (executor); 51 server.start (); 52 53 URL url = new URL ("http://localhost:"+server.getAddress().getPort()+"/test/foo.html"); 54 HttpURLConnection urlc = (HttpURLConnection)url.openConnection (); 55 try { 56 InputStream is = urlc.getInputStream(); 57 int c ,count = 0; 58 byte [] buf = new byte [32 * 1024]; 59 while (count < 32 * 1024) { 60 count += is.read (buf); 61 } 62 is.close(); 63 } finally { 64 server.stop(2); 65 executor.shutdown(); 66 } 67 if (error) { 68 throw new RuntimeException ("Test failed"); 69 } 70 } 71 72 public static boolean error = false; 73 74 static class Handler implements HttpHandler { 75 int invocation = 1; handle(HttpExchange t)76 public void handle (HttpExchange t) 77 throws IOException 78 { 79 InputStream is = t.getRequestBody(); 80 try { 81 while (is.read() != -1) ; 82 is.close(); 83 } catch (IOException e) { 84 e.printStackTrace(); 85 error = true; 86 } 87 /* send a chunked response, but wait a while before 88 * sending the final empty chunk 89 */ 90 t.sendResponseHeaders (200, 0); 91 OutputStream os = t.getResponseBody(); 92 byte[] bb = new byte [32 * 1024]; 93 os.write (bb); 94 os.flush(); 95 try {Thread.sleep (5000); } catch (InterruptedException e){} 96 try { 97 /* empty chunk sent here */ 98 os.close(); 99 } catch (IOException e) { 100 error = true; 101 e.printStackTrace(); 102 } 103 t.close(); 104 } 105 } 106 } 107