|
| 1 | +/* |
| 2 | + * Copyright (c) 2023, 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 8219083 |
| 27 | + * @summary Exceptions thrown from HttpHandler.handle should not close connection |
| 28 | + * if response is completed |
| 29 | + * @library /test/lib |
| 30 | + * @run junit ExceptionKeepAlive |
| 31 | + */ |
| 32 | + |
| 33 | +import com.sun.net.httpserver.HttpExchange; |
| 34 | +import com.sun.net.httpserver.HttpHandler; |
| 35 | +import com.sun.net.httpserver.HttpServer; |
| 36 | +import jdk.test.lib.net.URIBuilder; |
| 37 | +import org.junit.jupiter.api.Test; |
| 38 | + |
| 39 | +import java.io.IOException; |
| 40 | +import java.net.HttpURLConnection; |
| 41 | +import java.net.InetAddress; |
| 42 | +import java.net.InetSocketAddress; |
| 43 | +import java.net.Proxy; |
| 44 | +import java.net.URL; |
| 45 | +import java.util.logging.Handler; |
| 46 | +import java.util.logging.Level; |
| 47 | +import java.util.logging.Logger; |
| 48 | +import java.util.logging.SimpleFormatter; |
| 49 | +import java.util.logging.StreamHandler; |
| 50 | + |
| 51 | +import static org.junit.jupiter.api.Assertions.*; |
| 52 | + |
| 53 | +public class ExceptionKeepAlive |
| 54 | +{ |
| 55 | + |
| 56 | + public static final Logger LOGGER = Logger.getLogger("com.sun.net.httpserver"); |
| 57 | + |
| 58 | + @Test |
| 59 | + void test() throws IOException, InterruptedException { |
| 60 | + HttpServer httpServer = startHttpServer(); |
| 61 | + int port = httpServer.getAddress().getPort(); |
| 62 | + try { |
| 63 | + URL url = URIBuilder.newBuilder() |
| 64 | + .scheme("http") |
| 65 | + .loopback() |
| 66 | + .port(port) |
| 67 | + .path("/firstCall") |
| 68 | + .toURLUnchecked(); |
| 69 | + HttpURLConnection uc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY); |
| 70 | + int responseCode = uc.getResponseCode(); |
| 71 | + assertEquals(200, responseCode, "First request should succeed"); |
| 72 | + |
| 73 | + URL url2 = URIBuilder.newBuilder() |
| 74 | + .scheme("http") |
| 75 | + .loopback() |
| 76 | + .port(port) |
| 77 | + .path("/secondCall") |
| 78 | + .toURLUnchecked(); |
| 79 | + HttpURLConnection uc2 = (HttpURLConnection)url2.openConnection(Proxy.NO_PROXY); |
| 80 | + responseCode = uc2.getResponseCode(); |
| 81 | + assertEquals(200, responseCode, "Second request should reuse connection"); |
| 82 | + } finally { |
| 83 | + httpServer.stop(0); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Http Server |
| 89 | + */ |
| 90 | + HttpServer startHttpServer() throws IOException { |
| 91 | + Handler outHandler = new StreamHandler(System.out, |
| 92 | + new SimpleFormatter()); |
| 93 | + outHandler.setLevel(Level.FINEST); |
| 94 | + LOGGER.setLevel(Level.FINEST); |
| 95 | + LOGGER.addHandler(outHandler); |
| 96 | + InetAddress loopback = InetAddress.getLoopbackAddress(); |
| 97 | + HttpServer httpServer = HttpServer.create(new InetSocketAddress(loopback, 0), 0); |
| 98 | + httpServer.createContext("/", new MyHandler()); |
| 99 | + httpServer.start(); |
| 100 | + return httpServer; |
| 101 | + } |
| 102 | + |
| 103 | + class MyHandler implements HttpHandler { |
| 104 | + |
| 105 | + volatile int port1; |
| 106 | + @Override |
| 107 | + public void handle(HttpExchange t) throws IOException { |
| 108 | + String path = t.getRequestURI().getPath(); |
| 109 | + if (path.equals("/firstCall")) { |
| 110 | + port1 = t.getRemoteAddress().getPort(); |
| 111 | + System.out.println("First connection on client port = " + port1); |
| 112 | + |
| 113 | + // send response |
| 114 | + t.sendResponseHeaders(200, -1); |
| 115 | + // response is completed now; throw exception |
| 116 | + throw new NumberFormatException(); |
| 117 | + // the connection should still be reusable |
| 118 | + } else if (path.equals("/secondCall")) { |
| 119 | + int port2 = t.getRemoteAddress().getPort(); |
| 120 | + System.out.println("Second connection on client port = " + port2); |
| 121 | + |
| 122 | + if (port1 == port2) { |
| 123 | + t.sendResponseHeaders(200, -1); |
| 124 | + } else { |
| 125 | + t.sendResponseHeaders(500, -1); |
| 126 | + } |
| 127 | + } |
| 128 | + t.close(); |
| 129 | + } |
| 130 | + } |
| 131 | +} |
1 commit comments
openjdk-notifier[bot] commentedon Aug 7, 2024
Review
Issues