|
| 1 | +/* |
| 2 | + * Copyright (c) 2018, 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 8211420 |
| 27 | + * @run main/othervm B8211420 |
| 28 | + * @summary |
| 29 | + */ |
| 30 | + |
| 31 | +import com.sun.net.httpserver.*; |
| 32 | + |
| 33 | +import java.util.*; |
| 34 | +import java.util.concurrent.*; |
| 35 | +import java.util.logging.*; |
| 36 | +import java.io.*; |
| 37 | +import java.net.*; |
| 38 | + |
| 39 | +public class B8211420 { |
| 40 | + |
| 41 | + public static void main (String[] args) throws Exception { |
| 42 | + Logger logger = Logger.getLogger ("com.sun.net.httpserver"); |
| 43 | + ConsoleHandler c = new ConsoleHandler(); |
| 44 | + c.setLevel (Level.WARNING); |
| 45 | + logger.addHandler (c); |
| 46 | + logger.setLevel (Level.WARNING); |
| 47 | + Handler handler = new Handler(); |
| 48 | + InetSocketAddress addr = new InetSocketAddress (0); |
| 49 | + HttpServer server = HttpServer.create (addr, 0); |
| 50 | + HttpContext ctx = server.createContext ("/test", handler); |
| 51 | + ExecutorService executor = Executors.newCachedThreadPool(); |
| 52 | + server.setExecutor (executor); |
| 53 | + server.start (); |
| 54 | + |
| 55 | + URL url = new URL ("http://localhost:"+server.getAddress().getPort()+"/test/foo.html"); |
| 56 | + HttpURLConnection urlc = (HttpURLConnection)url.openConnection (); |
| 57 | + try { |
| 58 | + InputStream is = urlc.getInputStream(); |
| 59 | + while (is.read()!= -1) ; |
| 60 | + is.close (); |
| 61 | + String prop = urlc.getHeaderField("Content-length"); |
| 62 | + System.out.println ("Content-length = " + prop + " should be null"); |
| 63 | + if (prop != null) |
| 64 | + throw new RuntimeException("Content-length was present"); |
| 65 | + |
| 66 | + urlc = (HttpURLConnection)url.openConnection(); |
| 67 | + is = urlc.getInputStream(); |
| 68 | + while (is.read()!= -1) ; |
| 69 | + is.close (); |
| 70 | + if (urlc.getResponseCode() != 304) // expected for 2nd test |
| 71 | + throw new RuntimeException("wrong response code"); |
| 72 | + String clen = urlc.getHeaderField("Content-length"); |
| 73 | + System.out.println ("Content-length = " + clen + " should be 99"); |
| 74 | + System.out.println ("len = " + clen.length()); |
| 75 | + if (clen == null || !clen.equals("99")) |
| 76 | + throw new RuntimeException("Content-length not present or has wrong value"); |
| 77 | + System.out.println ("OK"); |
| 78 | + } finally { |
| 79 | + server.stop(2); |
| 80 | + executor.shutdown(); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + public static boolean error = false; |
| 85 | + |
| 86 | + static class Handler implements HttpHandler { |
| 87 | + volatile int invocation = 1; |
| 88 | + public void handle (HttpExchange t) |
| 89 | + throws IOException |
| 90 | + { |
| 91 | + InputStream is = t.getRequestBody(); |
| 92 | + Headers map = t.getRequestHeaders(); |
| 93 | + Headers rmap = t.getResponseHeaders(); |
| 94 | + while (is.read () != -1) ; |
| 95 | + is.close(); |
| 96 | + if (invocation++ == 1) { |
| 97 | + // send a 204 response with no body |
| 98 | + t.sendResponseHeaders(204, -1); |
| 99 | + t.close(); |
| 100 | + } else { |
| 101 | + // send a 304 response with no body but with content - length |
| 102 | + rmap.add("Content-length", "99"); |
| 103 | + t.sendResponseHeaders(304, -1); |
| 104 | + t.close(); |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments