File: B5045306.java

package info (click to toggle)
openjdk-21 21.0.8%2B9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 823,976 kB
  • sloc: java: 5,613,338; xml: 1,643,607; cpp: 1,296,296; ansic: 420,291; asm: 404,850; objc: 20,994; sh: 15,271; javascript: 11,245; python: 6,895; makefile: 2,362; perl: 357; awk: 351; sed: 172; jsp: 24; csh: 3
file content (231 lines) | stat: -rw-r--r-- 9,499 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
 * Copyright (c) 2005, 2024, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * @test
 * @bug 5045306 6356004 6993490 8255124
 * @summary Http keep-alive implementation is not efficient
 * @library /test/lib
 * @run main/othervm B5045306
 */

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;

import jdk.test.lib.net.URIBuilder;

/* Part 1:
 * The http client makes a connection to a URL whose content contains a lot of
 * data, more than can fit in the socket buffer. The client only reads
 * 1 byte of the data from the InputStream leaving behind more data than can
 * fit in the socket buffer. The client then makes a second call to the http
 * server. If the connection port used by the client is the same as for the
 * first call then that means that the connection is being reused.
 *
 * Part 2:
 * Test buggy webserver that sends less data than it specifies in its
 * Content-length header.
 */

public class B5045306 {
    static HttpServer server;
    static ExecutorService executor = Executors.newSingleThreadExecutor();

    public static void startHttpServer() {
        try {
            server = HttpServer.create(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 10, "/", new SimpleHttpTransactionHandler());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        server.setExecutor(executor);
        server.start();
        System.out.println("http server listens on: " + server.getAddress());
    }

    public static void stopHttpServer() {
        server.stop(1);
        executor.shutdown();
    }

    public static void clientHttpCalls() throws Exception {
        List<Throwable> uncaught = new ArrayList<>();
        Thread.setDefaultUncaughtExceptionHandler((t, ex) -> {
            uncaught.add(ex);
        });

        URL bigDataURL = URIBuilder.newBuilder()
                .scheme("http")
                .loopback()
                .port(server.getAddress().getPort())
                .path("/firstCall")
                .toURL();

        URL smallDataURL = URIBuilder.newBuilder()
                .scheme("http")
                .loopback()
                .port(server.getAddress().getPort())
                .path("/secondCall")
                .toURL();

        HttpURLConnection uc = (HttpURLConnection)bigDataURL.openConnection(Proxy.NO_PROXY);

        // Only read 1 byte of response data and close the stream
        try (InputStream is = uc.getInputStream()) {
            byte[] ba = new byte[1];
            is.read(ba);
        }

        // Allow the KeepAliveStreamCleaner thread to read the data left behind and cache the connection.
        try { Thread.sleep(2000); } catch (Exception e) {}

        uc = (HttpURLConnection)smallDataURL.openConnection(Proxy.NO_PROXY);
        uc.getResponseCode();

        if (SimpleHttpTransactionHandler.failed)
            throw new RuntimeException("Failed: Initial Keep Alive Connection is not being reused");

        // Part 2
        URL part2Url = URIBuilder.newBuilder()
                .scheme("http")
                .loopback()
                .port(server.getAddress().getPort())
                .path("/part2")
                .toURL();

        uc = (HttpURLConnection)part2Url.openConnection(Proxy.NO_PROXY);
        try (InputStream is = uc.getInputStream()) {}

        // Allow the KeepAliveStreamCleaner thread to try and read the data left behind and cache the connection.
        try { Thread.sleep(2000); } catch (Exception e) {}

        ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
        if (threadMXBean.isThreadCpuTimeSupported()) {
            long[] threads = threadMXBean.getAllThreadIds();
            ThreadInfo[] threadInfo = threadMXBean.getThreadInfo(threads);
            for (int i = 0; i < threadInfo.length; i++) {
                if (threadInfo[i].getThreadName().equals("Keep-Alive-SocketCleaner")) {
                    System.out.println("Found Keep-Alive-SocketCleaner thread");
                    long threadID = threadInfo[i].getThreadId();
                    long before = threadMXBean.getThreadCpuTime(threadID);
                    try { Thread.sleep(2000); } catch (Exception e) {}
                    long after = threadMXBean.getThreadCpuTime(threadID);

                    if (before ==-1 || after == -1)
                        break;  // thread has died, OK

                    // if Keep-Alive-SocketCleaner consumes more than 50% of cpu then we
                    // can assume a recursive loop.
                    long total = after - before;
                    if (total >= 1000000000)  // 1 second, or 1 billion nanoseconds
                        throw new RuntimeException("Failed: possible recursive loop in Keep-Alive-SocketCleaner");
                }
            }
        }
        if (!uncaught.isEmpty()) {
            throw new RuntimeException("Unhandled exception:", uncaught.get(0));
        }
    }

    static class SimpleHttpTransactionHandler implements HttpHandler {
        static volatile boolean failed = false;

        // Need to have enough data here that is too large for the socket buffer to hold.
        // Also http.KeepAlive.remainingData must be greater than this value, default is 256K.
        static final int RESPONSE_DATA_LENGTH = 128 * 1024;

        int port1;

        public void handle(HttpExchange trans) {
            try {
                String path = trans.getRequestURI().getPath();
                if (path.equals("/firstCall")) {
                    port1 = trans.getRemoteAddress().getPort();
                    System.out.println("First connection on client port = " + port1);

                    byte[] responseBody = new byte[RESPONSE_DATA_LENGTH];
                    for (int i=0; i<responseBody.length; i++)
                        responseBody[i] = 0x41;
                    trans.sendResponseHeaders(200, responseBody.length);
                    try (OutputStream os = trans.getResponseBody()) {
                        os.write(responseBody);
                    }
                } else if (path.equals("/secondCall")) {
                    int port2 = trans.getRemoteAddress().getPort();
                    System.out.println("Second connection on client port = " + port2);

                    if (port1 != port2)
                        failed = true;

                     /* Force the server to not respond for more that the timeout
                      * set by the keepalive cleaner (5000 millis). This ensures the
                      * timeout is correctly resets the default read timeout,
                      * infinity. See 6993490. */
                    System.out.println("server sleeping...");
                    try {Thread.sleep(6000); } catch (InterruptedException e) {}
                    trans.sendResponseHeaders(200, -1);
                } else if (path.equals("/part2")) {
                    System.out.println("Call to /part2");
                    byte[] responseBody = new byte[RESPONSE_DATA_LENGTH];
                    for (int i=0; i<responseBody.length; i++)
                        responseBody[i] = 0x41;
                    // override the Content-length header to be greater than the actual response body
                    trans.sendResponseHeaders(200, responseBody.length+1);
                    OutputStream os = trans.getResponseBody();
                    os.write(responseBody);
                    // now close the socket
                    // closing the stream here would throw; close the exchange instead
                    trans.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
                failed = true;
            }
        }
    }

    public static void main(String[] args) throws Exception {
        startHttpServer();
        try {
            clientHttpCalls();
        } finally {
            stopHttpServer();
        }
    }
}