Path: blob/master/test/jdk/com/sun/net/httpserver/bugs/B8211420.java
41154 views
/*1* Copyright (c) 2018, 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 821142026* @library /test/lib27* @run main/othervm B821142028* @run main/othervm -Djava.net.preferIPv6Addresses=true B821142029* @summary30*/3132import com.sun.net.httpserver.*;3334import java.util.*;35import java.util.concurrent.*;36import java.util.logging.*;37import java.io.*;38import java.net.*;3940import jdk.test.lib.net.URIBuilder;4142public class B8211420 {4344public static void main(String[] args) throws Exception {45Logger logger = Logger.getLogger("com.sun.net.httpserver");46ConsoleHandler c = new ConsoleHandler();47c.setLevel(Level.WARNING);48logger.addHandler(c);49logger.setLevel(Level.WARNING);50Handler handler = new Handler();51InetAddress loopback = InetAddress.getLoopbackAddress();52InetSocketAddress addr = new InetSocketAddress(loopback, 0);53HttpServer server = HttpServer.create(addr, 0);54HttpContext ctx = server.createContext("/test", handler);55ExecutorService executor = Executors.newCachedThreadPool();56server.setExecutor(executor);57server.start();5859URL url = URIBuilder.newBuilder()60.scheme("http")61.host(server.getAddress().getAddress())62.port(server.getAddress().getPort())63.path("/test/foo.html")64.toURL();65HttpURLConnection urlc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);66try {67InputStream is = urlc.getInputStream();68while (is.read()!= -1) ;69is.close ();70String prop = urlc.getHeaderField("Content-length");71System.out.println("Content-length = " + prop + " should be null");72if (prop != null)73throw new RuntimeException("Content-length was present");7475urlc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);76is = urlc.getInputStream();77while (is.read()!= -1) ;78is.close();79if (urlc.getResponseCode() != 304) // expected for 2nd test80throw new RuntimeException("wrong response code");81String clen = urlc.getHeaderField("Content-length");82System.out.println("Content-length = " + clen + " should be 99");83System.out.println("len = " + clen.length());84if (clen == null || !clen.equals("99"))85throw new RuntimeException("Content-length not present or has wrong value");86System.out.println("OK");87} finally {88server.stop(2);89executor.shutdown();90}91}9293public static boolean error = false;9495static class Handler implements HttpHandler {96volatile int invocation = 1;97public void handle (HttpExchange t)98throws IOException99{100InputStream is = t.getRequestBody();101Headers map = t.getRequestHeaders();102Headers rmap = t.getResponseHeaders();103while (is.read() != -1) ;104is.close();105if (invocation++ == 1) {106// send a 204 response with no body107t.sendResponseHeaders(204, -1);108t.close();109} else {110// send a 304 response with no body but with content - length111rmap.add("Content-length", "99");112t.sendResponseHeaders(304, -1);113t.close();114}115}116}117}118119120