test/jdk/java/net/httpclient/http2/server/Http2TestServer.java
author chegar
Wed, 20 Jun 2018 09:05:57 -0700
changeset 50681 4254bed3c09d
parent 49944 4690a2871b44
child 56795 03ece2518428
permissions -rw-r--r--
8204679: HTTP Client refresh Reviewed-by: chegar, dfuchs, michaelm Contributed-by: Chris Hegarty <chris.hegarty@oracle.com>, Daniel Fuchs <daniel.fuchs@oracle.com>, Michael McMahon <michael.x.mcmahon@oracle.com>, Pavel Rappo <pavel.rappo@oracle.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     1
/*
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
     2
 * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     4
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     7
 * published by the Free Software Foundation.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     8
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    13
 * accompanied this code).
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    14
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    18
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    21
 * questions.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    22
 */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    23
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    24
import java.io.IOException;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    25
import java.net.*;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    26
import java.util.*;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    27
import java.util.concurrent.ExecutorService;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    28
import java.util.concurrent.Executors;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    29
import java.util.concurrent.ThreadFactory;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    30
import java.util.concurrent.atomic.AtomicReference;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    31
import java.util.function.Consumer;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    32
import javax.net.ServerSocketFactory;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    33
import javax.net.ssl.SSLContext;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    34
import javax.net.ssl.SSLParameters;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    35
import javax.net.ssl.SSLServerSocket;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    36
import javax.net.ssl.SSLServerSocketFactory;
45713
ee3f2cbfe23a 8182589: TLS SNI in new Java 9 client is not available
michaelm
parents: 42460
diff changeset
    37
import javax.net.ssl.SNIServerName;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
    38
import jdk.internal.net.http.frame.ErrorFrame;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    39
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    40
/**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    41
 * Waits for incoming TCP connections from a client and establishes
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    42
 * a HTTP2 connection. Two threads are created per connection. One for reading
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    43
 * and one for writing. Incoming requests are dispatched to the supplied
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    44
 * Http2Handler on additional threads. All threads
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    45
 * obtained from the supplied ExecutorService.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    46
 */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    47
public class Http2TestServer implements AutoCloseable {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    48
    final ServerSocket server;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    49
    volatile boolean secure;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    50
    final ExecutorService exec;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    51
    volatile boolean stopping = false;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    52
    final Map<String,Http2Handler> handlers;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    53
    final SSLContext sslContext;
45713
ee3f2cbfe23a 8182589: TLS SNI in new Java 9 client is not available
michaelm
parents: 42460
diff changeset
    54
    final String serverName;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    55
    final HashMap<InetSocketAddress,Http2TestServerConnection> connections;
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
    56
    final Properties properties;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    57
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    58
    private static ThreadFactory defaultThreadFac =
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    59
        (Runnable r) -> {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    60
            Thread t = new Thread(r);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    61
            t.setName("Test-server-pool");
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    62
            return t;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    63
        };
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    64
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    65
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    66
    private static ExecutorService getDefaultExecutor() {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    67
        return Executors.newCachedThreadPool(defaultThreadFac);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    68
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    69
45713
ee3f2cbfe23a 8182589: TLS SNI in new Java 9 client is not available
michaelm
parents: 42460
diff changeset
    70
    public Http2TestServer(String serverName, boolean secure, int port) throws Exception {
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
    71
        this(serverName, secure, port, getDefaultExecutor(), 50, null, null);
45713
ee3f2cbfe23a 8182589: TLS SNI in new Java 9 client is not available
michaelm
parents: 42460
diff changeset
    72
    }
ee3f2cbfe23a 8182589: TLS SNI in new Java 9 client is not available
michaelm
parents: 42460
diff changeset
    73
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    74
    public Http2TestServer(boolean secure, int port) throws Exception {
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
    75
        this(null, secure, port, getDefaultExecutor(), 50, null, null);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    76
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    77
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    78
    public InetSocketAddress getAddress() {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    79
        return (InetSocketAddress)server.getLocalSocketAddress();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    80
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    81
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
    82
    public String serverAuthority() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
    83
        return InetAddress.getLoopbackAddress().getHostName() + ":"
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
    84
                + getAddress().getPort();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
    85
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
    86
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    87
    public Http2TestServer(boolean secure,
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    88
                           SSLContext context) throws Exception {
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
    89
        this(null, secure, 0, null, 50, null, context);
45713
ee3f2cbfe23a 8182589: TLS SNI in new Java 9 client is not available
michaelm
parents: 42460
diff changeset
    90
    }
ee3f2cbfe23a 8182589: TLS SNI in new Java 9 client is not available
michaelm
parents: 42460
diff changeset
    91
ee3f2cbfe23a 8182589: TLS SNI in new Java 9 client is not available
michaelm
parents: 42460
diff changeset
    92
    public Http2TestServer(String serverName, boolean secure,
ee3f2cbfe23a 8182589: TLS SNI in new Java 9 client is not available
michaelm
parents: 42460
diff changeset
    93
                           SSLContext context) throws Exception {
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
    94
        this(serverName, secure, 0, null, 50, null, context);
45713
ee3f2cbfe23a 8182589: TLS SNI in new Java 9 client is not available
michaelm
parents: 42460
diff changeset
    95
    }
ee3f2cbfe23a 8182589: TLS SNI in new Java 9 client is not available
michaelm
parents: 42460
diff changeset
    96
ee3f2cbfe23a 8182589: TLS SNI in new Java 9 client is not available
michaelm
parents: 42460
diff changeset
    97
    public Http2TestServer(boolean secure,
ee3f2cbfe23a 8182589: TLS SNI in new Java 9 client is not available
michaelm
parents: 42460
diff changeset
    98
                           int port,
ee3f2cbfe23a 8182589: TLS SNI in new Java 9 client is not available
michaelm
parents: 42460
diff changeset
    99
                           ExecutorService exec,
ee3f2cbfe23a 8182589: TLS SNI in new Java 9 client is not available
michaelm
parents: 42460
diff changeset
   100
                           SSLContext context) throws Exception {
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   101
        this(null, secure, port, exec, 50, null, context);
49944
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   102
    }
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   103
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   104
    public Http2TestServer(String serverName,
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   105
                           boolean secure,
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   106
                           int port,
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   107
                           ExecutorService exec,
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   108
                           SSLContext context)
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   109
        throws Exception
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   110
    {
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   111
        this(serverName, secure, port, exec, 50, null, context);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   112
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   113
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   114
    /**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   115
     * Create a Http2Server listening on the given port. Currently needs
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   116
     * to know in advance whether incoming connections are plain TCP "h2c"
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   117
     * or TLS "h2"/
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   118
     *
45713
ee3f2cbfe23a 8182589: TLS SNI in new Java 9 client is not available
michaelm
parents: 42460
diff changeset
   119
     * @param serverName SNI servername
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   120
     * @param secure https or http
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   121
     * @param port listen port
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   122
     * @param exec executor service (cached thread pool is used if null)
49944
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   123
     * @param backlog the server socket backlog
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   124
     * @param properties additional configuration properties
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   125
     * @param context the SSLContext used when secure is true
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   126
     */
45713
ee3f2cbfe23a 8182589: TLS SNI in new Java 9 client is not available
michaelm
parents: 42460
diff changeset
   127
    public Http2TestServer(String serverName,
ee3f2cbfe23a 8182589: TLS SNI in new Java 9 client is not available
michaelm
parents: 42460
diff changeset
   128
                           boolean secure,
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   129
                           int port,
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   130
                           ExecutorService exec,
49944
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   131
                           int backlog,
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   132
                           Properties properties,
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   133
                           SSLContext context)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   134
        throws Exception
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   135
    {
45713
ee3f2cbfe23a 8182589: TLS SNI in new Java 9 client is not available
michaelm
parents: 42460
diff changeset
   136
        this.serverName = serverName;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   137
        if (secure) {
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   138
           if (context != null)
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   139
               this.sslContext = context;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   140
           else
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   141
               this.sslContext = SSLContext.getDefault();
49944
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   142
            server = initSecure(port, backlog);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   143
        } else {
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   144
            this.sslContext = context;
49944
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   145
            server = initPlaintext(port, backlog);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   146
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   147
        this.secure = secure;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   148
        this.exec = exec == null ? getDefaultExecutor() : exec;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   149
        this.handlers = Collections.synchronizedMap(new HashMap<>());
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   150
        this.properties = properties;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   151
        this.connections = new HashMap<>();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   152
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   153
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   154
    /**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   155
     * Adds the given handler for the given path
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   156
     */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   157
    public void addHandler(Http2Handler handler, String path) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   158
        handlers.put(path, handler);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   159
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   160
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   161
    volatile Http2TestExchangeSupplier exchangeSupplier = Http2TestExchangeSupplier.ofDefault();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   162
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   163
    /**
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   164
     * Sets an explicit exchange handler to be used for all future connections.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   165
     * Useful for testing scenarios where non-standard or specific server
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   166
     * behaviour is required, either direct control over the frames sent, "bad"
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   167
     * behaviour, or something else.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   168
     */
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   169
    public void setExchangeSupplier(Http2TestExchangeSupplier exchangeSupplier) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   170
        this.exchangeSupplier = exchangeSupplier;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   171
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   172
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   173
    Http2Handler getHandlerFor(String path) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   174
        if (path == null || path.equals(""))
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   175
            path = "/";
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   176
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   177
        final String fpath = path;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   178
        AtomicReference<String> bestMatch = new AtomicReference<>("");
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   179
        AtomicReference<Http2Handler> href = new AtomicReference<>();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   180
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   181
        handlers.forEach((key, value) -> {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   182
            if (fpath.startsWith(key) && key.length() > bestMatch.get().length()) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   183
                bestMatch.set(key);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   184
                href.set(value);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   185
            }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   186
        });
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   187
        Http2Handler handler = href.get();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   188
        if (handler == null)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   189
            throw new RuntimeException("No handler found for path " + path);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   190
        System.err.println("Using handler for: " + bestMatch.get());
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   191
        return handler;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   192
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   193
49944
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   194
    final ServerSocket initPlaintext(int port, int backlog) throws Exception {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   195
        ServerSocket ss = new ServerSocket();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   196
        ss.setReuseAddress(false);
49944
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   197
        ss.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), backlog);
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   198
        return ss;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   199
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   200
48376
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48083
diff changeset
   201
    public synchronized void stop() {
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   202
        // TODO: clean shutdown GoAway
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   203
        stopping = true;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   204
        System.err.printf("Server stopping %d connections\n", connections.size());
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   205
        for (Http2TestServerConnection connection : connections.values()) {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   206
            connection.close(ErrorFrame.NO_ERROR);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   207
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   208
        try {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   209
            server.close();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   210
        } catch (IOException e) {}
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   211
        exec.shutdownNow();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   212
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   213
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   214
49944
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   215
    final ServerSocket initSecure(int port, int backlog) throws Exception {
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   216
        ServerSocketFactory fac;
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   217
        SSLParameters sslp = null;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   218
        fac = sslContext.getServerSocketFactory();
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   219
        sslp = sslContext.getSupportedSSLParameters();
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   220
        SSLServerSocket se = (SSLServerSocket) fac.createServerSocket();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   221
        se.setReuseAddress(false);
49944
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   222
        se.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), backlog);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   223
        sslp.setApplicationProtocols(new String[]{"h2"});
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   224
        sslp.setEndpointIdentificationAlgorithm("HTTPS");
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   225
        se.setSSLParameters(sslp);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   226
        se.setEnabledCipherSuites(se.getSupportedCipherSuites());
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   227
        se.setEnabledProtocols(se.getSupportedProtocols());
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   228
        // other initialisation here
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   229
        return se;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   230
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   231
45713
ee3f2cbfe23a 8182589: TLS SNI in new Java 9 client is not available
michaelm
parents: 42460
diff changeset
   232
    public String serverName() {
ee3f2cbfe23a 8182589: TLS SNI in new Java 9 client is not available
michaelm
parents: 42460
diff changeset
   233
        return serverName;
ee3f2cbfe23a 8182589: TLS SNI in new Java 9 client is not available
michaelm
parents: 42460
diff changeset
   234
    }
ee3f2cbfe23a 8182589: TLS SNI in new Java 9 client is not available
michaelm
parents: 42460
diff changeset
   235
48376
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48083
diff changeset
   236
    private synchronized void putConnection(InetSocketAddress addr, Http2TestServerConnection c) {
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48083
diff changeset
   237
        if (!stopping)
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48083
diff changeset
   238
            connections.put(addr, c);
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48083
diff changeset
   239
    }
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48083
diff changeset
   240
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48083
diff changeset
   241
    private synchronized void removeConnection(InetSocketAddress addr, Http2TestServerConnection c) {
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48083
diff changeset
   242
        connections.remove(addr, c);
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48083
diff changeset
   243
    }
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48083
diff changeset
   244
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   245
    /**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   246
     * Starts a thread which waits for incoming connections.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   247
     */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   248
    public void start() {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   249
        exec.submit(() -> {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   250
            try {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   251
                while (!stopping) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   252
                    Socket socket = server.accept();
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   253
                    Http2TestServerConnection c = null;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   254
                    InetSocketAddress addr = null;
46157
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents: 45713
diff changeset
   255
                    try {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   256
                        addr = (InetSocketAddress) socket.getRemoteSocketAddress();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   257
                        c = createConnection(this, socket, exchangeSupplier);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   258
                        putConnection(addr, c);
46157
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents: 45713
diff changeset
   259
                        c.run();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   260
                    } catch (Throwable e) {
46157
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents: 45713
diff changeset
   261
                        // we should not reach here, but if we do
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents: 45713
diff changeset
   262
                        // the connection might not have been closed
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents: 45713
diff changeset
   263
                        // and if so then the client might wait
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents: 45713
diff changeset
   264
                        // forever.
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   265
                        if (c != null) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   266
                            removeConnection(addr, c);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   267
                            c.close(ErrorFrame.PROTOCOL_ERROR);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   268
                        } else {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   269
                            socket.close();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   270
                        }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   271
                        System.err.println("TestServer: start exception: " + e);
46157
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents: 45713
diff changeset
   272
                    }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   273
                }
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   274
            } catch (SecurityException se) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   275
                System.err.println("TestServer: terminating, caught " + se);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   276
                se.printStackTrace();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   277
                stopping = true;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   278
                try { server.close(); } catch (IOException ioe) { /* ignore */}
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   279
            } catch (Throwable e) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   280
                if (!stopping) {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   281
                    System.err.println("TestServer: terminating, caught " + e);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   282
                    e.printStackTrace();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   283
                }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   284
            }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   285
        });
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   286
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   287
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   288
    protected Http2TestServerConnection createConnection(Http2TestServer http2TestServer,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   289
                                                         Socket socket,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   290
                                                         Http2TestExchangeSupplier exchangeSupplier)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   291
            throws IOException {
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   292
        return new Http2TestServerConnection(http2TestServer, socket, exchangeSupplier, properties);
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   293
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   294
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   295
    @Override
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   296
    public void close() throws Exception {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   297
        stop();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   298
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   299
}