|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, 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 6968351 |
| 27 | + * @summary tcp no delay not required for small payloads |
| 28 | + * @library /test/lib |
| 29 | + * @run main/othervm/timeout=5 -Dsun.net.httpserver.nodelay=false TcpNoDelayNotRequired |
| 30 | + */ |
| 31 | + |
| 32 | +import com.sun.net.httpserver.Headers; |
| 33 | +import com.sun.net.httpserver.HttpContext; |
| 34 | +import com.sun.net.httpserver.HttpExchange; |
| 35 | +import com.sun.net.httpserver.HttpHandler; |
| 36 | +import com.sun.net.httpserver.HttpServer; |
| 37 | + |
| 38 | +import com.sun.net.httpserver.HttpsConfigurator; |
| 39 | +import com.sun.net.httpserver.HttpsServer; |
| 40 | +import jdk.test.lib.net.SimpleSSLContext; |
| 41 | +import jdk.test.lib.net.URIBuilder; |
| 42 | + |
| 43 | +import javax.net.ssl.SSLContext; |
| 44 | +import java.io.IOException; |
| 45 | +import java.net.InetAddress; |
| 46 | +import java.net.InetSocketAddress; |
| 47 | +import java.net.http.HttpClient; |
| 48 | +import java.net.http.HttpRequest; |
| 49 | +import java.net.http.HttpResponse; |
| 50 | +import java.nio.charset.StandardCharsets; |
| 51 | +import java.util.concurrent.ExecutorService; |
| 52 | +import java.util.concurrent.Executors; |
| 53 | +import java.util.logging.Level; |
| 54 | +import java.util.logging.Logger; |
| 55 | +import java.util.logging.SimpleFormatter; |
| 56 | +import java.util.logging.StreamHandler; |
| 57 | + |
| 58 | +public class TcpNoDelayNotRequired { |
| 59 | + |
| 60 | + public static final Logger LOGGER = Logger.getLogger("sun.net.www.protocol.http"); |
| 61 | + |
| 62 | + public static void main (String[] args) throws Exception { |
| 63 | + |
| 64 | + java.util.logging.Handler outHandler = new StreamHandler(System.out, |
| 65 | + new SimpleFormatter()); |
| 66 | + outHandler.setLevel(Level.FINEST); |
| 67 | + LOGGER.setLevel(Level.FINEST); |
| 68 | + LOGGER.addHandler(outHandler); |
| 69 | + |
| 70 | + InetAddress loopback = InetAddress.getLoopbackAddress(); |
| 71 | + InetSocketAddress addr = new InetSocketAddress (loopback, 0); |
| 72 | + |
| 73 | + SSLContext sslContext = new SimpleSSLContext().get(); |
| 74 | + |
| 75 | + HttpServer httpServer = HttpServer.create (addr, 0); |
| 76 | + testHttpServer("http",httpServer,sslContext); |
| 77 | + |
| 78 | + HttpsServer httpsServer = HttpsServer.create (addr, 0); |
| 79 | + httpsServer.setHttpsConfigurator(new HttpsConfigurator(sslContext)); |
| 80 | + |
| 81 | + testHttpServer("https",httpsServer,sslContext); |
| 82 | + } |
| 83 | + |
| 84 | + private static void testHttpServer(String scheme,HttpServer server,SSLContext sslContext) throws Exception { |
| 85 | + HttpContext ctx = server.createContext ("/test", new Handler()); |
| 86 | + HttpContext ctx2 = server.createContext ("/chunked", new ChunkedHandler()); |
| 87 | + ExecutorService executor = Executors.newCachedThreadPool(); |
| 88 | + server.setExecutor (executor); |
| 89 | + server.start (); |
| 90 | + try { |
| 91 | + try (HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build()) { |
| 92 | + long start = System.currentTimeMillis(); |
| 93 | + for (int i = 0; i < 1000; i++) { |
| 94 | + var uri = URIBuilder.newBuilder().scheme(scheme).loopback().port(server.getAddress().getPort()).path("/test").build(); |
| 95 | + var response = client.send(HttpRequest.newBuilder(uri).build(), HttpResponse.BodyHandlers.ofString()); |
| 96 | + if (!response.body().equals("hello")) |
| 97 | + throw new IllegalStateException("incorrect body " + response.body()); |
| 98 | + } |
| 99 | + for (int i = 0; i < 1000; i++) { |
| 100 | + var uri = URIBuilder.newBuilder().scheme(scheme).loopback().port(server.getAddress().getPort()).path("/chunked").build(); |
| 101 | + var response = client.send(HttpRequest.newBuilder(uri).build(), HttpResponse.BodyHandlers.ofString()); |
| 102 | + if (!response.body().equals("hello")) |
| 103 | + throw new IllegalStateException("incorrect body " + response.body()); |
| 104 | + } |
| 105 | + long time = System.currentTimeMillis() - start; |
| 106 | + System.out.println("time " + time); |
| 107 | + } |
| 108 | + } finally { |
| 109 | + server.stop(0); |
| 110 | + } |
| 111 | + executor.shutdown(); |
| 112 | + } |
| 113 | + |
| 114 | + static class Handler implements HttpHandler { |
| 115 | + public void handle (HttpExchange t) |
| 116 | + throws IOException |
| 117 | + { |
| 118 | + Headers rmap = t.getResponseHeaders(); |
| 119 | + try (var is = t.getRequestBody()) { |
| 120 | + is.readAllBytes(); |
| 121 | + } |
| 122 | + rmap.add("content-type","text/plain"); |
| 123 | + t.sendResponseHeaders(200,5); |
| 124 | + try (var os = t.getResponseBody()) { |
| 125 | + os.write("hello".getBytes(StandardCharsets.ISO_8859_1)); |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + static class ChunkedHandler implements HttpHandler { |
| 130 | + public void handle (HttpExchange t) |
| 131 | + throws IOException |
| 132 | + { |
| 133 | + Headers rmap = t.getResponseHeaders(); |
| 134 | + try (var is = t.getRequestBody()) { |
| 135 | + is.readAllBytes(); |
| 136 | + } |
| 137 | + rmap.add("content-type","text/plain"); |
| 138 | + t.sendResponseHeaders(200,0); |
| 139 | + try (var os = t.getResponseBody()) { |
| 140 | + os.write("hello".getBytes(StandardCharsets.ISO_8859_1)); |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | +} |
0 commit comments