2016-04-30 00:30:31 +01:00
|
|
|
/*
|
2020-04-03 07:27:53 +01:00
|
|
|
* Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved.
|
2016-04-30 00:30:31 +01:00
|
|
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
|
|
*
|
|
|
|
* This code is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License version 2 only, as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* version 2 for more details (a copy is included in the LICENSE file that
|
|
|
|
* accompanied this code).
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License version
|
|
|
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
|
|
|
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
|
|
* or visit www.oracle.com if you need additional information or have any
|
|
|
|
* questions.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.net.*;
|
2016-12-09 11:35:02 +00:00
|
|
|
import java.util.*;
|
2016-04-30 00:30:31 +01:00
|
|
|
import java.util.concurrent.ExecutorService;
|
|
|
|
import java.util.concurrent.Executors;
|
|
|
|
import java.util.concurrent.ThreadFactory;
|
2016-12-09 11:35:02 +00:00
|
|
|
import java.util.concurrent.atomic.AtomicReference;
|
2017-12-06 11:11:59 -08:00
|
|
|
import java.util.function.Consumer;
|
2016-04-30 00:30:31 +01:00
|
|
|
import javax.net.ServerSocketFactory;
|
|
|
|
import javax.net.ssl.SSLContext;
|
|
|
|
import javax.net.ssl.SSLParameters;
|
|
|
|
import javax.net.ssl.SSLServerSocket;
|
|
|
|
import javax.net.ssl.SSLServerSocketFactory;
|
2017-06-29 11:10:30 +01:00
|
|
|
import javax.net.ssl.SNIServerName;
|
2018-04-17 08:54:17 -07:00
|
|
|
import jdk.internal.net.http.frame.ErrorFrame;
|
2016-04-30 00:30:31 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Waits for incoming TCP connections from a client and establishes
|
|
|
|
* a HTTP2 connection. Two threads are created per connection. One for reading
|
|
|
|
* and one for writing. Incoming requests are dispatched to the supplied
|
|
|
|
* Http2Handler on additional threads. All threads
|
|
|
|
* obtained from the supplied ExecutorService.
|
|
|
|
*/
|
2016-05-18 16:39:08 +01:00
|
|
|
public class Http2TestServer implements AutoCloseable {
|
2016-04-30 00:30:31 +01:00
|
|
|
final ServerSocket server;
|
2020-04-03 07:27:53 +01:00
|
|
|
final boolean supportsHTTP11;
|
2016-12-09 11:35:02 +00:00
|
|
|
volatile boolean secure;
|
2016-04-30 00:30:31 +01:00
|
|
|
final ExecutorService exec;
|
|
|
|
volatile boolean stopping = false;
|
2016-12-09 11:35:02 +00:00
|
|
|
final Map<String,Http2Handler> handlers;
|
2016-04-30 00:30:31 +01:00
|
|
|
final SSLContext sslContext;
|
2017-06-29 11:10:30 +01:00
|
|
|
final String serverName;
|
2016-04-30 00:30:31 +01:00
|
|
|
final HashMap<InetSocketAddress,Http2TestServerConnection> connections;
|
2018-06-20 09:05:57 -07:00
|
|
|
final Properties properties;
|
2016-04-30 00:30:31 +01:00
|
|
|
|
|
|
|
private static ThreadFactory defaultThreadFac =
|
|
|
|
(Runnable r) -> {
|
|
|
|
Thread t = new Thread(r);
|
|
|
|
t.setName("Test-server-pool");
|
|
|
|
return t;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
private static ExecutorService getDefaultExecutor() {
|
|
|
|
return Executors.newCachedThreadPool(defaultThreadFac);
|
|
|
|
}
|
|
|
|
|
2017-06-29 11:10:30 +01:00
|
|
|
public Http2TestServer(String serverName, boolean secure, int port) throws Exception {
|
2018-06-20 09:05:57 -07:00
|
|
|
this(serverName, secure, port, getDefaultExecutor(), 50, null, null);
|
2017-06-29 11:10:30 +01:00
|
|
|
}
|
|
|
|
|
2016-12-09 11:35:02 +00:00
|
|
|
public Http2TestServer(boolean secure, int port) throws Exception {
|
2018-06-20 09:05:57 -07:00
|
|
|
this(null, secure, port, getDefaultExecutor(), 50, null, null);
|
2016-04-30 00:30:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public InetSocketAddress getAddress() {
|
|
|
|
return (InetSocketAddress)server.getLocalSocketAddress();
|
|
|
|
}
|
|
|
|
|
2018-04-17 08:54:17 -07:00
|
|
|
public String serverAuthority() {
|
|
|
|
return InetAddress.getLoopbackAddress().getHostName() + ":"
|
|
|
|
+ getAddress().getPort();
|
|
|
|
}
|
|
|
|
|
2016-12-09 11:35:02 +00:00
|
|
|
public Http2TestServer(boolean secure,
|
|
|
|
SSLContext context) throws Exception {
|
2018-06-20 09:05:57 -07:00
|
|
|
this(null, secure, 0, null, 50, null, context);
|
2017-06-29 11:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public Http2TestServer(String serverName, boolean secure,
|
|
|
|
SSLContext context) throws Exception {
|
2018-06-20 09:05:57 -07:00
|
|
|
this(serverName, secure, 0, null, 50, null, context);
|
2017-06-29 11:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public Http2TestServer(boolean secure,
|
|
|
|
int port,
|
|
|
|
ExecutorService exec,
|
|
|
|
SSLContext context) throws Exception {
|
2018-06-20 09:05:57 -07:00
|
|
|
this(null, secure, port, exec, 50, null, context);
|
2018-05-02 02:36:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public Http2TestServer(String serverName,
|
|
|
|
boolean secure,
|
|
|
|
int port,
|
|
|
|
ExecutorService exec,
|
|
|
|
SSLContext context)
|
|
|
|
throws Exception
|
|
|
|
{
|
2018-06-20 09:05:57 -07:00
|
|
|
this(serverName, secure, port, exec, 50, null, context);
|
2016-12-09 11:35:02 +00:00
|
|
|
}
|
|
|
|
|
2020-04-03 07:27:53 +01:00
|
|
|
public Http2TestServer(String serverName,
|
|
|
|
boolean secure,
|
|
|
|
int port,
|
|
|
|
ExecutorService exec,
|
|
|
|
int backlog,
|
|
|
|
Properties properties,
|
|
|
|
SSLContext context)
|
|
|
|
throws Exception
|
|
|
|
{
|
|
|
|
this(serverName, secure, port, exec, backlog, properties, context, false);
|
|
|
|
}
|
|
|
|
|
2021-12-09 17:38:49 +00:00
|
|
|
public Http2TestServer(String serverName,
|
|
|
|
boolean secure,
|
|
|
|
int port,
|
|
|
|
ExecutorService exec,
|
|
|
|
int backlog,
|
|
|
|
Properties properties,
|
|
|
|
SSLContext context,
|
|
|
|
boolean supportsHTTP11)
|
|
|
|
throws Exception
|
|
|
|
{
|
|
|
|
this(InetAddress.getLoopbackAddress(), serverName, secure, port, exec,
|
|
|
|
backlog, properties, context, supportsHTTP11);
|
|
|
|
}
|
|
|
|
|
2016-04-30 00:30:31 +01:00
|
|
|
/**
|
|
|
|
* Create a Http2Server listening on the given port. Currently needs
|
|
|
|
* to know in advance whether incoming connections are plain TCP "h2c"
|
2020-04-03 07:27:53 +01:00
|
|
|
* or TLS "h2".
|
|
|
|
*
|
|
|
|
* The HTTP/1.1 support, when supportsHTTP11 is true, is currently limited
|
|
|
|
* to a canned 0-length response that contains the following headers:
|
|
|
|
* "X-Magic", "HTTP/1.1 request received by HTTP/2 server",
|
|
|
|
* "X-Received-Body", <the request body>);
|
2016-04-30 00:30:31 +01:00
|
|
|
*
|
2021-12-09 17:38:49 +00:00
|
|
|
* @param localAddr local address to bind to
|
2017-06-29 11:10:30 +01:00
|
|
|
* @param serverName SNI servername
|
2016-04-30 00:30:31 +01:00
|
|
|
* @param secure https or http
|
|
|
|
* @param port listen port
|
|
|
|
* @param exec executor service (cached thread pool is used if null)
|
2018-05-02 02:36:17 -07:00
|
|
|
* @param backlog the server socket backlog
|
2018-06-20 09:05:57 -07:00
|
|
|
* @param properties additional configuration properties
|
2016-04-30 00:30:31 +01:00
|
|
|
* @param context the SSLContext used when secure is true
|
2020-04-03 07:27:53 +01:00
|
|
|
* @param supportsHTTP11 if true, the server may issue an HTTP/1.1 response
|
|
|
|
* to either 1) a non-Upgrade HTTP/1.1 request, or 2) a secure
|
|
|
|
* connection without the h2 ALPN. Otherwise, false to operate in
|
|
|
|
* HTTP/2 mode exclusively.
|
2016-04-30 00:30:31 +01:00
|
|
|
*/
|
2021-12-09 17:38:49 +00:00
|
|
|
public Http2TestServer(InetAddress localAddr,
|
|
|
|
String serverName,
|
2017-06-29 11:10:30 +01:00
|
|
|
boolean secure,
|
2016-12-09 11:35:02 +00:00
|
|
|
int port,
|
|
|
|
ExecutorService exec,
|
2018-05-02 02:36:17 -07:00
|
|
|
int backlog,
|
2018-06-20 09:05:57 -07:00
|
|
|
Properties properties,
|
2020-04-03 07:27:53 +01:00
|
|
|
SSLContext context,
|
|
|
|
boolean supportsHTTP11)
|
2016-12-09 11:35:02 +00:00
|
|
|
throws Exception
|
|
|
|
{
|
2017-06-29 11:10:30 +01:00
|
|
|
this.serverName = serverName;
|
2020-04-03 07:27:53 +01:00
|
|
|
this.supportsHTTP11 = supportsHTTP11;
|
2016-04-30 00:30:31 +01:00
|
|
|
if (secure) {
|
2018-06-20 09:05:57 -07:00
|
|
|
if (context != null)
|
|
|
|
this.sslContext = context;
|
|
|
|
else
|
|
|
|
this.sslContext = SSLContext.getDefault();
|
2021-12-09 17:38:49 +00:00
|
|
|
server = initSecure(localAddr, port, backlog);
|
2016-04-30 00:30:31 +01:00
|
|
|
} else {
|
2018-06-20 09:05:57 -07:00
|
|
|
this.sslContext = context;
|
2018-05-02 02:36:17 -07:00
|
|
|
server = initPlaintext(port, backlog);
|
2016-04-30 00:30:31 +01:00
|
|
|
}
|
|
|
|
this.secure = secure;
|
|
|
|
this.exec = exec == null ? getDefaultExecutor() : exec;
|
2016-12-09 11:35:02 +00:00
|
|
|
this.handlers = Collections.synchronizedMap(new HashMap<>());
|
2018-06-20 09:05:57 -07:00
|
|
|
this.properties = properties;
|
2016-04-30 00:30:31 +01:00
|
|
|
this.connections = new HashMap<>();
|
|
|
|
}
|
|
|
|
|
2016-12-09 11:35:02 +00:00
|
|
|
/**
|
|
|
|
* Adds the given handler for the given path
|
|
|
|
*/
|
|
|
|
public void addHandler(Http2Handler handler, String path) {
|
|
|
|
handlers.put(path, handler);
|
|
|
|
}
|
|
|
|
|
2017-12-06 11:11:59 -08:00
|
|
|
volatile Http2TestExchangeSupplier exchangeSupplier = Http2TestExchangeSupplier.ofDefault();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets an explicit exchange handler to be used for all future connections.
|
|
|
|
* Useful for testing scenarios where non-standard or specific server
|
|
|
|
* behaviour is required, either direct control over the frames sent, "bad"
|
|
|
|
* behaviour, or something else.
|
|
|
|
*/
|
|
|
|
public void setExchangeSupplier(Http2TestExchangeSupplier exchangeSupplier) {
|
|
|
|
this.exchangeSupplier = exchangeSupplier;
|
|
|
|
}
|
|
|
|
|
2016-12-09 11:35:02 +00:00
|
|
|
Http2Handler getHandlerFor(String path) {
|
|
|
|
if (path == null || path.equals(""))
|
|
|
|
path = "/";
|
|
|
|
|
|
|
|
final String fpath = path;
|
|
|
|
AtomicReference<String> bestMatch = new AtomicReference<>("");
|
|
|
|
AtomicReference<Http2Handler> href = new AtomicReference<>();
|
|
|
|
|
|
|
|
handlers.forEach((key, value) -> {
|
|
|
|
if (fpath.startsWith(key) && key.length() > bestMatch.get().length()) {
|
|
|
|
bestMatch.set(key);
|
|
|
|
href.set(value);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
Http2Handler handler = href.get();
|
|
|
|
if (handler == null)
|
|
|
|
throw new RuntimeException("No handler found for path " + path);
|
|
|
|
System.err.println("Using handler for: " + bestMatch.get());
|
|
|
|
return handler;
|
|
|
|
}
|
|
|
|
|
2018-05-02 02:36:17 -07:00
|
|
|
final ServerSocket initPlaintext(int port, int backlog) throws Exception {
|
2018-04-17 08:54:17 -07:00
|
|
|
ServerSocket ss = new ServerSocket();
|
|
|
|
ss.setReuseAddress(false);
|
2018-05-02 02:36:17 -07:00
|
|
|
ss.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), backlog);
|
2018-04-17 08:54:17 -07:00
|
|
|
return ss;
|
2016-04-30 00:30:31 +01:00
|
|
|
}
|
|
|
|
|
2017-12-19 15:48:49 +00:00
|
|
|
public synchronized void stop() {
|
2016-04-30 00:30:31 +01:00
|
|
|
// TODO: clean shutdown GoAway
|
|
|
|
stopping = true;
|
2017-12-06 11:11:59 -08:00
|
|
|
System.err.printf("Server stopping %d connections\n", connections.size());
|
2016-04-30 00:30:31 +01:00
|
|
|
for (Http2TestServerConnection connection : connections.values()) {
|
2017-12-06 11:11:59 -08:00
|
|
|
connection.close(ErrorFrame.NO_ERROR);
|
2016-04-30 00:30:31 +01:00
|
|
|
}
|
|
|
|
try {
|
|
|
|
server.close();
|
|
|
|
} catch (IOException e) {}
|
|
|
|
exec.shutdownNow();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-12-09 17:38:49 +00:00
|
|
|
final ServerSocket initSecure(InetAddress localAddr, int port, int backlog) throws Exception {
|
2016-04-30 00:30:31 +01:00
|
|
|
ServerSocketFactory fac;
|
2018-06-20 09:05:57 -07:00
|
|
|
SSLParameters sslp = null;
|
|
|
|
fac = sslContext.getServerSocketFactory();
|
|
|
|
sslp = sslContext.getSupportedSSLParameters();
|
2018-04-17 08:54:17 -07:00
|
|
|
SSLServerSocket se = (SSLServerSocket) fac.createServerSocket();
|
|
|
|
se.setReuseAddress(false);
|
2021-12-09 17:38:49 +00:00
|
|
|
se.bind(new InetSocketAddress(localAddr, 0), backlog);
|
2020-04-03 07:27:53 +01:00
|
|
|
if (supportsHTTP11) {
|
|
|
|
sslp.setApplicationProtocols(new String[]{"h2", "http/1.1"});
|
|
|
|
} else {
|
|
|
|
sslp.setApplicationProtocols(new String[]{"h2"});
|
|
|
|
}
|
2018-04-17 08:54:17 -07:00
|
|
|
sslp.setEndpointIdentificationAlgorithm("HTTPS");
|
2016-04-30 00:30:31 +01:00
|
|
|
se.setSSLParameters(sslp);
|
|
|
|
se.setEnabledCipherSuites(se.getSupportedCipherSuites());
|
|
|
|
se.setEnabledProtocols(se.getSupportedProtocols());
|
|
|
|
// other initialisation here
|
|
|
|
return se;
|
|
|
|
}
|
|
|
|
|
2017-06-29 11:10:30 +01:00
|
|
|
public String serverName() {
|
|
|
|
return serverName;
|
|
|
|
}
|
|
|
|
|
2017-12-19 15:48:49 +00:00
|
|
|
private synchronized void putConnection(InetSocketAddress addr, Http2TestServerConnection c) {
|
|
|
|
if (!stopping)
|
|
|
|
connections.put(addr, c);
|
|
|
|
}
|
|
|
|
|
|
|
|
private synchronized void removeConnection(InetSocketAddress addr, Http2TestServerConnection c) {
|
|
|
|
connections.remove(addr, c);
|
|
|
|
}
|
|
|
|
|
2016-04-30 00:30:31 +01:00
|
|
|
/**
|
2016-12-09 11:35:02 +00:00
|
|
|
* Starts a thread which waits for incoming connections.
|
2016-04-30 00:30:31 +01:00
|
|
|
*/
|
|
|
|
public void start() {
|
|
|
|
exec.submit(() -> {
|
|
|
|
try {
|
|
|
|
while (!stopping) {
|
|
|
|
Socket socket = server.accept();
|
2018-04-17 08:54:17 -07:00
|
|
|
Http2TestServerConnection c = null;
|
|
|
|
InetSocketAddress addr = null;
|
2017-08-16 10:55:05 +01:00
|
|
|
try {
|
2018-04-17 08:54:17 -07:00
|
|
|
addr = (InetSocketAddress) socket.getRemoteSocketAddress();
|
|
|
|
c = createConnection(this, socket, exchangeSupplier);
|
|
|
|
putConnection(addr, c);
|
2017-08-16 10:55:05 +01:00
|
|
|
c.run();
|
2017-12-06 11:11:59 -08:00
|
|
|
} catch (Throwable e) {
|
2017-08-16 10:55:05 +01:00
|
|
|
// we should not reach here, but if we do
|
|
|
|
// the connection might not have been closed
|
|
|
|
// and if so then the client might wait
|
|
|
|
// forever.
|
2018-04-17 08:54:17 -07:00
|
|
|
if (c != null) {
|
|
|
|
removeConnection(addr, c);
|
|
|
|
c.close(ErrorFrame.PROTOCOL_ERROR);
|
|
|
|
} else {
|
|
|
|
socket.close();
|
|
|
|
}
|
2017-12-06 11:11:59 -08:00
|
|
|
System.err.println("TestServer: start exception: " + e);
|
2017-08-16 10:55:05 +01:00
|
|
|
}
|
2016-04-30 00:30:31 +01:00
|
|
|
}
|
2018-04-17 08:54:17 -07:00
|
|
|
} catch (SecurityException se) {
|
|
|
|
System.err.println("TestServer: terminating, caught " + se);
|
|
|
|
se.printStackTrace();
|
|
|
|
stopping = true;
|
|
|
|
try { server.close(); } catch (IOException ioe) { /* ignore */}
|
2016-04-30 00:30:31 +01:00
|
|
|
} catch (Throwable e) {
|
|
|
|
if (!stopping) {
|
2017-12-06 11:11:59 -08:00
|
|
|
System.err.println("TestServer: terminating, caught " + e);
|
2016-04-30 00:30:31 +01:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-04-17 08:54:17 -07:00
|
|
|
protected Http2TestServerConnection createConnection(Http2TestServer http2TestServer,
|
|
|
|
Socket socket,
|
|
|
|
Http2TestExchangeSupplier exchangeSupplier)
|
|
|
|
throws IOException {
|
2018-06-20 09:05:57 -07:00
|
|
|
return new Http2TestServerConnection(http2TestServer, socket, exchangeSupplier, properties);
|
2018-04-17 08:54:17 -07:00
|
|
|
}
|
|
|
|
|
2016-05-18 16:39:08 +01:00
|
|
|
@Override
|
|
|
|
public void close() throws Exception {
|
|
|
|
stop();
|
|
|
|
}
|
2016-04-30 00:30:31 +01:00
|
|
|
}
|