Path: blob/master/test/jdk/com/sun/net/httpserver/bugs/B6421581.java
41154 views
/*1* Copyright (c) 2006, 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 642158126* @summary NPE while closing HttpExchange.getResonseBody()27*/2829import com.sun.net.httpserver.*;30import java.net.*;31import java.io.*;32import java.util.concurrent.*;3334public class B6421581 {3536static boolean error = false;37static int iter = 0;3839public static void main(String[] args) throws Exception {40once();41}4243public static void once() throws Exception {44InetSocketAddress inetAddress = new InetSocketAddress(45"localhost", 0);46HttpServer server = HttpServer.create(inetAddress, 5);47int port = server.getAddress().getPort();48ExecutorService e = (Executors.newFixedThreadPool(5));49server.setExecutor(e);50HttpContext context = server.createContext("/hello");51server.start();52context.setHandler(new HttpHandler() {53public void handle(HttpExchange msg) {54iter ++;55System.out.println("Got request");56switch (iter) {57case 1:58/* close output stream without opening inpustream */59/* chunked encoding */60try {61msg.sendResponseHeaders(200, 0);62OutputStream out = msg.getResponseBody();63out.write("hello".getBytes());64out.close();65} catch(Exception e) {66error = true;67} finally {68msg.close();69}70break;71case 2:72/* close output stream without opening inpustream */73/* fixed encoding */74try {75msg.sendResponseHeaders(200, 5);76OutputStream out = msg.getResponseBody();77out.write("hello".getBytes());78out.close();79} catch(Exception e) {80error = true;81} finally {82msg.close();83}84break;85case 3:86/* close exchange without opening any stream */87try {88msg.sendResponseHeaders(200, -1);89msg.close();90} catch(Exception e) {91error = true;92}93break;94}95}96});9798URL url = new URL("http://localhost:"+port+"/hello/url.text");99doURL(url);100doURL(url);101doURL(url);102e.shutdown();103e.awaitTermination(4, TimeUnit.SECONDS);104server.stop(0);105if (error) {106throw new RuntimeException ("test failed");107}108}109110static void doURL (URL url) throws Exception {111InputStream is = url.openStream();112while (is.read() != -1) ;113is.close();114}115}116117118