--- old/test/jdk/java/net/httpclient/http2/server/Http2TestServerConnection.java 2020-04-02 07:04:58.000000000 +0100 +++ new/test/jdk/java/net/httpclient/http2/server/Http2TestServerConnection.java 2020-04-02 07:04:58.000000000 +0100 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -47,6 +47,7 @@ import jdk.internal.net.http.hpack.Encoder; import sun.net.www.http.ChunkedInputStream; import sun.net.www.http.HttpClient; +import static java.nio.charset.StandardCharsets.UTF_8; import static jdk.internal.net.http.frame.SettingsFrame.HEADER_TABLE_SIZE; /** @@ -123,10 +124,15 @@ Properties properties) throws IOException { + System.err.println("TestServer: New connection from " + socket); + if (socket instanceof SSLSocket) { - handshake(server.serverName(), (SSLSocket)socket); + SSLSocket sslSocket = (SSLSocket)socket; + handshake(server.serverName(), sslSocket); + if (!server.supportsHTTP11 && !"h2".equals(sslSocket.getApplicationProtocol())) { + throw new IOException("Unexpected ALPN: [" + sslSocket.getApplicationProtocol() + "]"); + } } - System.err.println("TestServer: New connection from " + socket); this.server = server; this.exchangeSupplier = exchangeSupplier; this.streams = Collections.synchronizedMap(new HashMap<>()); @@ -248,7 +254,7 @@ private static void handshake(String name, SSLSocket sock) throws IOException { if (name == null) { - // no name set. No need to check + sock.getSession(); // blocks until handshake done return; } else if (name.equals("localhost")) { name = "localhost"; @@ -304,8 +310,7 @@ } } - Http1InitialRequest doUpgrade() throws IOException { - Http1InitialRequest upgrade = readHttp1Request(); + Http1InitialRequest doUpgrade(Http1InitialRequest upgrade) throws IOException { String h2c = getHeader(upgrade.headers, "Upgrade"); if (h2c == null || !h2c.equals("h2c")) { System.err.println("Server:HEADERS: " + upgrade); @@ -351,19 +356,57 @@ return clientSettings.getParameter(SettingsFrame.MAX_FRAME_SIZE); } + private void standardHTTP11Response(Http1InitialRequest request) + throws IOException + { + String upgradeHeader = getHeader(request.headers, "Upgrade"); + if (upgradeHeader != null) { + throw new IOException("Unexpected Upgrade header:" + upgradeHeader); + } + + sendHttp1Response(200, "OK", + "Connection", "close", + "X-Magic", "HTTP/1.1 request received by HTTP/2 server", + "X-Received-Body", new String(request.body, UTF_8)); + + } + void run() throws Exception { Http1InitialRequest upgrade = null; if (!secure) { - upgrade = doUpgrade(); - } else { - readPreface(); - sendSettingsFrame(true); - clientSettings = (SettingsFrame) readFrame(); - if (clientSettings.getFlag(SettingsFrame.ACK)) { - // we received the ack to our frame first + Http1InitialRequest request = readHttp1Request(); + String h2c = getHeader(request.headers, "Upgrade"); + if (h2c == null || !h2c.equals("h2c")) { + if (server.supportsHTTP11) { + standardHTTP11Response(request); + socket.close(); + return; + } else { + System.err.println("Server:HEADERS: " + upgrade); + throw new IOException("Bad upgrade 1 " + h2c); + } + } + upgrade = doUpgrade(request); + } else { // secure + SSLSocket sslSocket = (SSLSocket)socket; + if (sslSocket.getApplicationProtocol().equals("h2")) { + readPreface(); + sendSettingsFrame(true); clientSettings = (SettingsFrame) readFrame(); + if (clientSettings.getFlag(SettingsFrame.ACK)) { + // we received the ack to our frame first + clientSettings = (SettingsFrame) readFrame(); + } + nextstream = 1; + } else if (sslSocket.getApplicationProtocol().equals("http/1.1") || + sslSocket.getApplicationProtocol().equals("")) { + standardHTTP11Response(readHttp1Request()); + socket.shutdownOutput(); + socket.close(); + return; + } else { + throw new IOException("Unexpected ALPN:" + sslSocket.getApplicationProtocol()); } - nextstream = 1; } // Uncomment if needed, but very noisy