jdk/test/java/net/httpclient/http2/server/Http2TestServer.java
changeset 45713 ee3f2cbfe23a
parent 42460 7133f144981a
child 46157 f3c2dcb8d8fe
equal deleted inserted replaced
45712:4f662a7e43a7 45713:ee3f2cbfe23a
    31 import javax.net.ServerSocketFactory;
    31 import javax.net.ServerSocketFactory;
    32 import javax.net.ssl.SSLContext;
    32 import javax.net.ssl.SSLContext;
    33 import javax.net.ssl.SSLParameters;
    33 import javax.net.ssl.SSLParameters;
    34 import javax.net.ssl.SSLServerSocket;
    34 import javax.net.ssl.SSLServerSocket;
    35 import javax.net.ssl.SSLServerSocketFactory;
    35 import javax.net.ssl.SSLServerSocketFactory;
       
    36 import javax.net.ssl.SNIServerName;
    36 
    37 
    37 /**
    38 /**
    38  * Waits for incoming TCP connections from a client and establishes
    39  * Waits for incoming TCP connections from a client and establishes
    39  * a HTTP2 connection. Two threads are created per connection. One for reading
    40  * a HTTP2 connection. Two threads are created per connection. One for reading
    40  * and one for writing. Incoming requests are dispatched to the supplied
    41  * and one for writing. Incoming requests are dispatched to the supplied
    46     volatile boolean secure;
    47     volatile boolean secure;
    47     final ExecutorService exec;
    48     final ExecutorService exec;
    48     volatile boolean stopping = false;
    49     volatile boolean stopping = false;
    49     final Map<String,Http2Handler> handlers;
    50     final Map<String,Http2Handler> handlers;
    50     final SSLContext sslContext;
    51     final SSLContext sslContext;
       
    52     final String serverName;
    51     final HashMap<InetSocketAddress,Http2TestServerConnection> connections;
    53     final HashMap<InetSocketAddress,Http2TestServerConnection> connections;
    52 
    54 
    53     private static ThreadFactory defaultThreadFac =
    55     private static ThreadFactory defaultThreadFac =
    54         (Runnable r) -> {
    56         (Runnable r) -> {
    55             Thread t = new Thread(r);
    57             Thread t = new Thread(r);
    60 
    62 
    61     private static ExecutorService getDefaultExecutor() {
    63     private static ExecutorService getDefaultExecutor() {
    62         return Executors.newCachedThreadPool(defaultThreadFac);
    64         return Executors.newCachedThreadPool(defaultThreadFac);
    63     }
    65     }
    64 
    66 
       
    67     public Http2TestServer(String serverName, boolean secure, int port) throws Exception {
       
    68         this(serverName, secure, port, getDefaultExecutor(), null);
       
    69     }
       
    70 
    65     public Http2TestServer(boolean secure, int port) throws Exception {
    71     public Http2TestServer(boolean secure, int port) throws Exception {
    66         this(secure, port, getDefaultExecutor(), null);
    72         this(null, secure, port, getDefaultExecutor(), null);
    67     }
    73     }
    68 
    74 
    69     public InetSocketAddress getAddress() {
    75     public InetSocketAddress getAddress() {
    70         return (InetSocketAddress)server.getLocalSocketAddress();
    76         return (InetSocketAddress)server.getLocalSocketAddress();
    71     }
    77     }
    72 
    78 
    73     public Http2TestServer(boolean secure,
    79     public Http2TestServer(boolean secure,
    74                            SSLContext context) throws Exception {
    80                            SSLContext context) throws Exception {
    75         this(secure, 0, null, context);
    81         this(null, secure, 0, null, context);
       
    82     }
       
    83 
       
    84     public Http2TestServer(String serverName, boolean secure,
       
    85                            SSLContext context) throws Exception {
       
    86         this(serverName, secure, 0, null, context);
       
    87     }
       
    88 
       
    89     public Http2TestServer(boolean secure,
       
    90                            int port,
       
    91                            ExecutorService exec,
       
    92                            SSLContext context) throws Exception {
       
    93         this(null, secure, port, exec, context);
    76     }
    94     }
    77 
    95 
    78     /**
    96     /**
    79      * Create a Http2Server listening on the given port. Currently needs
    97      * Create a Http2Server listening on the given port. Currently needs
    80      * to know in advance whether incoming connections are plain TCP "h2c"
    98      * to know in advance whether incoming connections are plain TCP "h2c"
    81      * or TLS "h2"/
    99      * or TLS "h2"/
    82      *
   100      *
       
   101      * @param serverName SNI servername
    83      * @param secure https or http
   102      * @param secure https or http
    84      * @param port listen port
   103      * @param port listen port
    85      * @param exec executor service (cached thread pool is used if null)
   104      * @param exec executor service (cached thread pool is used if null)
    86      * @param context the SSLContext used when secure is true
   105      * @param context the SSLContext used when secure is true
    87      */
   106      */
    88     public Http2TestServer(boolean secure,
   107     public Http2TestServer(String serverName,
       
   108                            boolean secure,
    89                            int port,
   109                            int port,
    90                            ExecutorService exec,
   110                            ExecutorService exec,
    91                            SSLContext context)
   111                            SSLContext context)
    92         throws Exception
   112         throws Exception
    93     {
   113     {
       
   114         this.serverName = serverName;
    94         if (secure) {
   115         if (secure) {
    95             server = initSecure(port);
   116             server = initSecure(port);
    96         } else {
   117         } else {
    97             server = initPlaintext(port);
   118             server = initPlaintext(port);
    98         }
   119         }
   161         se.setSSLParameters(sslp);
   182         se.setSSLParameters(sslp);
   162         se.setEnabledCipherSuites(se.getSupportedCipherSuites());
   183         se.setEnabledCipherSuites(se.getSupportedCipherSuites());
   163         se.setEnabledProtocols(se.getSupportedProtocols());
   184         se.setEnabledProtocols(se.getSupportedProtocols());
   164         // other initialisation here
   185         // other initialisation here
   165         return se;
   186         return se;
       
   187     }
       
   188 
       
   189     public String serverName() {
       
   190         return serverName;
   166     }
   191     }
   167 
   192 
   168     /**
   193     /**
   169      * Starts a thread which waits for incoming connections.
   194      * Starts a thread which waits for incoming connections.
   170      */
   195      */