test/jdk/java/net/httpclient/LightWeightHttpServer.java
author jboes
Fri, 08 Nov 2019 11:15:16 +0000
changeset 59029 3786a0962570
parent 52121 934969c63223
permissions -rw-r--r--
8232853: AuthenticationFilter.Cache::remove may throw ConcurrentModificationException Summary: Change implementation to use iterator instead of plain LinkedList Reviewed-by: dfuchs, vtewari
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
     1
/*
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
     2
 * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
     4
 *
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
     7
 * published by the Free Software Foundation.
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
     8
 *
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    13
 * accompanied this code).
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    14
 *
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    18
 *
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    21
 * questions.
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    22
 */
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    23
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    24
/**
52121
934969c63223 8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents: 49765
diff changeset
    25
 * library /test/lib /
934969c63223 8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents: 49765
diff changeset
    26
 * build jdk.test.lib.net.SimpleSSLContext ProxyServer
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36131
diff changeset
    27
 * compile ../../../com/sun/net/httpserver/LogFilter.java
45535
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 37720
diff changeset
    28
 * compile ../../../com/sun/net/httpserver/EchoHandler.java
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36131
diff changeset
    29
 * compile ../../../com/sun/net/httpserver/FileServerHandler.java
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    30
 */
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    31
import com.sun.net.httpserver.Headers;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    32
import com.sun.net.httpserver.HttpContext;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    33
import com.sun.net.httpserver.HttpExchange;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    34
import com.sun.net.httpserver.HttpHandler;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    35
import com.sun.net.httpserver.HttpServer;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    36
import com.sun.net.httpserver.HttpsConfigurator;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    37
import com.sun.net.httpserver.HttpsServer;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    38
import java.io.IOException;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    39
import java.io.InputStream;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    40
import java.io.OutputStream;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    41
import java.net.InetAddress;
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    42
import java.net.InetSocketAddress;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    43
import java.nio.file.Path;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    44
import java.util.HashSet;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    45
import java.util.concurrent.BrokenBarrierException;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    46
import java.util.concurrent.CyclicBarrier;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    47
import java.util.concurrent.ExecutorService;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    48
import java.util.concurrent.Executors;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    49
import java.util.logging.ConsoleHandler;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    50
import java.util.logging.Level;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    51
import java.util.logging.Logger;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    52
import javax.net.ssl.SSLContext;
52121
934969c63223 8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents: 49765
diff changeset
    53
import jdk.test.lib.net.SimpleSSLContext;
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    54
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    55
public class LightWeightHttpServer {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    56
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    57
    static SSLContext ctx;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    58
    static HttpServer httpServer;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    59
    static HttpsServer httpsServer;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    60
    static ExecutorService executor;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    61
    static int port;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    62
    static int httpsport;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    63
    static String httproot;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    64
    static String httpsroot;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    65
    static ProxyServer proxy;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    66
    static int proxyPort;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    67
    static RedirectErrorHandler redirectErrorHandler, redirectErrorHandlerSecure;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    68
    static RedirectHandler redirectHandler, redirectHandlerSecure;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    69
    static DelayHandler delayHandler;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    70
    static final String midSizedFilename = "/files/notsobigfile.txt";
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    71
    static final String smallFilename = "/files/smallfile.txt";
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    72
    static Path midSizedFile;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    73
    static Path smallFile;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    74
    static String fileroot;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    75
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    76
    public static void initServer() throws IOException {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    77
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    78
        Logger logger = Logger.getLogger("com.sun.net.httpserver");
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    79
        ConsoleHandler ch = new ConsoleHandler();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    80
        logger.setLevel(Level.ALL);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    81
        ch.setLevel(Level.ALL);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    82
        logger.addHandler(ch);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    83
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    84
        String root = System.getProperty("test.src", ".") + "/docs";
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    85
        InetSocketAddress addr = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    86
        httpServer = HttpServer.create(addr, 0);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    87
        if (httpServer instanceof HttpsServer) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    88
            throw new RuntimeException("should not be httpsserver");
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    89
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    90
        httpsServer = HttpsServer.create(addr, 0);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    91
        HttpHandler h = new FileServerHandler(root);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    92
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    93
        HttpContext c1 = httpServer.createContext("/files", h);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    94
        HttpContext c2 = httpsServer.createContext("/files", h);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    95
        HttpContext c3 = httpServer.createContext("/echo", new EchoHandler());
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    96
        redirectHandler = new RedirectHandler("/redirect");
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    97
        redirectHandlerSecure = new RedirectHandler("/redirect");
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    98
        HttpContext c4 = httpServer.createContext("/redirect", redirectHandler);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    99
        HttpContext c41 = httpsServer.createContext("/redirect", redirectHandlerSecure);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   100
        HttpContext c5 = httpsServer.createContext("/echo", new EchoHandler());
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   101
        HttpContext c6 = httpServer.createContext("/keepalive", new KeepAliveHandler());
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   102
        redirectErrorHandler = new RedirectErrorHandler("/redirecterror");
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   103
        redirectErrorHandlerSecure = new RedirectErrorHandler("/redirecterror");
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   104
        HttpContext c7 = httpServer.createContext("/redirecterror", redirectErrorHandler);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   105
        HttpContext c71 = httpsServer.createContext("/redirecterror", redirectErrorHandlerSecure);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   106
        delayHandler = new DelayHandler();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   107
        HttpContext c8 = httpServer.createContext("/delay", delayHandler);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   108
        HttpContext c81 = httpsServer.createContext("/delay", delayHandler);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   109
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   110
        executor = Executors.newCachedThreadPool();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   111
        httpServer.setExecutor(executor);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   112
        httpsServer.setExecutor(executor);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   113
        ctx = new SimpleSSLContext().get();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   114
        httpsServer.setHttpsConfigurator(new HttpsConfigurator(ctx));
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   115
        httpServer.start();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   116
        httpsServer.start();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   117
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   118
        port = httpServer.getAddress().getPort();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   119
        System.out.println("HTTP server port = " + port);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   120
        httpsport = httpsServer.getAddress().getPort();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   121
        System.out.println("HTTPS server port = " + httpsport);
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   122
        httproot = "http://localhost:" + port + "/";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   123
        httpsroot = "https://localhost:" + httpsport + "/";
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   124
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   125
        proxy = new ProxyServer(0, false);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   126
        proxyPort = proxy.getPort();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   127
        System.out.println("Proxy port = " + proxyPort);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   128
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   129
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   130
    public static void stop() throws IOException {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   131
        if (httpServer != null) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   132
            httpServer.stop(0);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   133
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   134
        if (httpsServer != null) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   135
            httpsServer.stop(0);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   136
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   137
        if (proxy != null) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   138
            proxy.close();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   139
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   140
        if (executor != null) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   141
            executor.shutdownNow();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   142
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   143
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   144
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   145
    static class RedirectErrorHandler implements HttpHandler {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   146
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   147
        String root;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   148
        volatile int count = 1;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   149
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   150
        RedirectErrorHandler(String root) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   151
            this.root = root;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   152
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   153
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   154
        synchronized int count() {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   155
            return count;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   156
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   157
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   158
        synchronized void increment() {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   159
            count++;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   160
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   161
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   162
        @Override
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   163
        public synchronized void handle(HttpExchange t)
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   164
                throws IOException {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   165
            byte[] buf = new byte[2048];
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   166
            try (InputStream is = t.getRequestBody()) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   167
                while (is.read(buf) != -1) ;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   168
            }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   169
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   170
            Headers map = t.getResponseHeaders();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   171
            String redirect = root + "/foo/" + Integer.toString(count);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   172
            increment();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   173
            map.add("Location", redirect);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   174
            t.sendResponseHeaders(301, -1);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   175
            t.close();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   176
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   177
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   178
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   179
    static class RedirectHandler implements HttpHandler {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   180
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   181
        String root;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   182
        volatile int count = 0;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   183
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   184
        RedirectHandler(String root) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   185
            this.root = root;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   186
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   187
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   188
        @Override
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   189
        public synchronized void handle(HttpExchange t)
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   190
                throws IOException {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   191
            byte[] buf = new byte[2048];
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   192
            try (InputStream is = t.getRequestBody()) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   193
                while (is.read(buf) != -1) ;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   194
            }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   195
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   196
            Headers map = t.getResponseHeaders();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   197
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   198
            if (count++ < 1) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   199
                map.add("Location", root + "/foo/" + count);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   200
            } else {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   201
                map.add("Location", SmokeTest.midSizedFilename);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   202
            }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   203
            t.sendResponseHeaders(301, -1);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   204
            t.close();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   205
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   206
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   207
        int count() {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   208
            return count;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   209
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   210
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   211
        void reset() {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   212
            count = 0;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   213
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   214
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   215
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   216
    static class KeepAliveHandler implements HttpHandler {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   217
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   218
        volatile int counter = 0;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   219
        HashSet<Integer> portSet = new HashSet<>();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   220
        volatile int[] ports = new int[4];
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   221
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   222
        void sleep(int n) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   223
            try {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   224
                Thread.sleep(n);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   225
            } catch (InterruptedException e) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   226
            }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   227
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   228
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   229
        @Override
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   230
        public synchronized void handle(HttpExchange t)
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   231
                throws IOException {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   232
            int remotePort = t.getRemoteAddress().getPort();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   233
            String result = "OK";
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   234
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   235
            int n = counter++;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   236
            /// First test
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   237
            if (n < 4) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   238
                ports[n] = remotePort;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   239
            }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   240
            if (n == 3) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   241
                // check all values in ports[] are the same
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   242
                if (ports[0] != ports[1] || ports[2] != ports[3]
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   243
                        || ports[0] != ports[2]) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   244
                    result = "Error " + Integer.toString(n);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   245
                    System.out.println(result);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   246
                }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   247
            }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   248
            // Second test
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   249
            if (n >= 4 && n < 8) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   250
                // delay to ensure ports are different
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   251
                sleep(500);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   252
                ports[n - 4] = remotePort;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   253
            }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   254
            if (n == 7) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   255
                // should be all different
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   256
                if (ports[0] == ports[1] || ports[2] == ports[3]
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   257
                        || ports[0] == ports[2]) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   258
                    result = "Error " + Integer.toString(n);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   259
                    System.out.println(result);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   260
                    System.out.printf("Ports: %d, %d, %d, %d\n",
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   261
                                      ports[0], ports[1], ports[2], ports[3]);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   262
                }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   263
                // setup for third test
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   264
                for (int i = 0; i < 4; i++) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   265
                    portSet.add(ports[i]);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   266
                }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   267
            }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   268
            // Third test
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   269
            if (n > 7) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   270
                // just check that port is one of the ones in portSet
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   271
                if (!portSet.contains(remotePort)) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   272
                    System.out.println("UNEXPECTED REMOTE PORT " + remotePort);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   273
                    result = "Error " + Integer.toString(n);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   274
                    System.out.println(result);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   275
                }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   276
            }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   277
            byte[] buf = new byte[2048];
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   278
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   279
            try (InputStream is = t.getRequestBody()) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   280
                while (is.read(buf) != -1) ;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   281
            }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   282
            t.sendResponseHeaders(200, result.length());
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   283
            OutputStream o = t.getResponseBody();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   284
            o.write(result.getBytes("US-ASCII"));
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   285
            t.close();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   286
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   287
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   288
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   289
    static class DelayHandler implements HttpHandler {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   290
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   291
        CyclicBarrier bar1 = new CyclicBarrier(2);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   292
        CyclicBarrier bar2 = new CyclicBarrier(2);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   293
        CyclicBarrier bar3 = new CyclicBarrier(2);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   294
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   295
        CyclicBarrier barrier1() {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   296
            return bar1;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   297
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   298
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   299
        CyclicBarrier barrier2() {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   300
            return bar2;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   301
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   302
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   303
        @Override
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   304
        public synchronized void handle(HttpExchange he) throws IOException {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   305
            try(InputStream is = he.getRequestBody()) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   306
                is.readAllBytes();
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   307
                bar1.await();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   308
                bar2.await();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   309
            } catch (InterruptedException | BrokenBarrierException e) {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   310
                throw new IOException(e);
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   311
            }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   312
            he.sendResponseHeaders(200, -1); // will probably fail
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   313
            he.close();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   314
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   315
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   316
}