Path: blob/master/test/jdk/com/sun/net/httpserver/bugs/B6886436.java
41154 views
/*1* Copyright (c) 2009, 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 688643626* @summary HttpServer should not send a body with 204 response.27* @library /test/lib28* @run main B688643629* @run main/othervm -Djava.net.preferIPv6Addresses=true B688643630*/3132import com.sun.net.httpserver.*;3334import java.util.*;35import java.util.concurrent.*;36import java.util.logging.*;37import java.io.*;38import java.net.*;39import jdk.test.lib.net.URIBuilder;4041public class B6886436 {4243public static void main (String[] args) throws Exception {44Logger logger = Logger.getLogger ("com.sun.net.httpserver");45ConsoleHandler c = new ConsoleHandler();46c.setLevel (Level.WARNING);47logger.addHandler (c);48logger.setLevel (Level.WARNING);49Handler handler = new Handler();50InetAddress loopback = InetAddress.getLoopbackAddress();51InetSocketAddress addr = new InetSocketAddress (loopback, 0);52HttpServer server = HttpServer.create (addr, 0);53HttpContext ctx = server.createContext ("/test", handler);54ExecutorService executor = Executors.newCachedThreadPool();55server.setExecutor (executor);56server.start ();5758URL url = URIBuilder.newBuilder()59.scheme("http")60.loopback()61.port(server.getAddress().getPort())62.path("/test/foo.html")63.toURL();64HttpURLConnection urlc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);65try {66InputStream is = urlc.getInputStream();67while (is.read()!= -1) ;68is.close ();69urlc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);70urlc.setReadTimeout (3000);71is = urlc.getInputStream();72while (is.read()!= -1);73is.close ();7475} catch (IOException e) {76server.stop(2);77executor.shutdown();78throw new RuntimeException ("Test failed");79}80server.stop(2);81executor.shutdown();82System.out.println ("OK");83}8485public static boolean error = false;8687static class Handler implements HttpHandler {88int invocation = 1;89public void handle (HttpExchange t)90throws IOException91{92InputStream is = t.getRequestBody();93Headers map = t.getRequestHeaders();94Headers rmap = t.getResponseHeaders();95while (is.read () != -1) ;96is.close();97// send a 204 response with an empty chunked body98t.sendResponseHeaders (204, 0);99t.close();100}101}102}103104105