1 /* 2 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 import com.sun.net.httpserver.HttpsConfigurator; 25 import com.sun.net.httpserver.HttpsServer; 26 import jdk.test.lib.net.SimpleSSLContext; 27 import javax.net.ssl.SSLContext; 28 import java.net.InetAddress; 29 import java.net.InetSocketAddress; 30 import java.net.ProxySelector; 31 import java.net.URI; 32 import java.net.http.HttpClient; 33 import java.net.http.HttpClient.Version; 34 import java.net.http.HttpRequest; 35 import java.net.http.HttpResponse; 36 import java.net.http.HttpResponse.BodyHandlers; 37 import java.util.Arrays; 38 import java.util.List; 39 import java.util.stream.Collectors; 40 import java.util.stream.Stream; 41 import static java.lang.String.format; 42 import static java.lang.System.out; 43 44 /** 45 * @test 46 * @summary This test verifies that if an h2 connection going through a 47 * proxy P is downgraded to HTTP/1.1, then a new h2 request 48 * going to a different host through the same proxy will not 49 * be preemptively downgraded. That, is the stack should attempt 50 * a new h2 connection to the new host. 51 * @bug 8196967 52 * @library /test/lib http2/server 53 * @build jdk.test.lib.net.SimpleSSLContext HttpServerAdapters DigestEchoServer HttpsTunnelTest 54 * @modules java.net.http/jdk.internal.net.http.common 55 * java.net.http/jdk.internal.net.http.frame 56 * java.net.http/jdk.internal.net.http.hpack 57 * java.logging 58 * java.base/sun.net.www.http 59 * java.base/sun.net.www 60 * java.base/sun.net 61 * @run main/othervm -Djdk.internal.httpclient.debug=true HttpsTunnelTest 62 */ 63 64 public class HttpsTunnelTest implements HttpServerAdapters { 65 66 static final String data[] = { 67 "Lorem ipsum", 68 "dolor sit amet", 69 "consectetur adipiscing elit, sed do eiusmod tempor", 70 "quis nostrud exercitation ullamco", 71 "laboris nisi", 72 "ut", 73 "aliquip ex ea commodo consequat." + 74 "Duis aute irure dolor in reprehenderit in voluptate velit esse" + 75 "cillum dolore eu fugiat nulla pariatur.", 76 "Excepteur sint occaecat cupidatat non proident." 77 }; 78 79 static final SSLContext context; 80 static { 81 try { 82 context = new SimpleSSLContext().get(); 83 SSLContext.setDefault(context); 84 } catch (Exception x) { 85 throw new ExceptionInInitializerError(x); 86 } 87 } 88 HttpsTunnelTest()89 HttpsTunnelTest() { 90 } 91 newHttpClient(ProxySelector ps)92 public HttpClient newHttpClient(ProxySelector ps) { 93 HttpClient.Builder builder = HttpClient 94 .newBuilder() 95 .sslContext(context) 96 .proxy(ps); 97 return builder.build(); 98 } 99 main(String[] args)100 public static void main(String[] args) throws Exception { 101 InetSocketAddress sa = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0); 102 HttpsServer server1 = HttpsServer.create(sa, 0); 103 server1.setHttpsConfigurator(new HttpsConfigurator(context)); 104 HttpTestServer http1Server = 105 HttpTestServer.of(server1); 106 http1Server.addHandler(new HttpTestEchoHandler(), "/"); 107 http1Server.start(); 108 HttpTestServer http2Server = HttpTestServer.of( 109 new Http2TestServer("localhost", true, 0)); 110 http2Server.addHandler(new HttpTestEchoHandler(), "/"); 111 http2Server.start(); 112 113 DigestEchoServer.TunnelingProxy proxy = DigestEchoServer.createHttpsProxyTunnel( 114 DigestEchoServer.HttpAuthSchemeType.NONE); 115 116 try { 117 URI uri1 = new URI("https://" + http1Server.serverAuthority() + "/foo/https1"); 118 URI uri2 = new URI("https://" + http2Server.serverAuthority() + "/foo/https2"); 119 ProxySelector ps = ProxySelector.of(proxy.getProxyAddress()); 120 //HttpClient.Builder.NO_PROXY; 121 HttpsTunnelTest test = new HttpsTunnelTest(); 122 HttpClient client = test.newHttpClient(ps); 123 out.println("Proxy is: " + ps.select(uri2)); 124 125 List<String> lines = List.of(Arrays.copyOfRange(data, 0, data.length)); 126 assert lines.size() == data.length; 127 String body = lines.stream().collect(Collectors.joining("\r\n")); 128 HttpRequest.BodyPublisher reqBody = HttpRequest.BodyPublishers.ofString(body); 129 HttpRequest req1 = HttpRequest 130 .newBuilder(uri1) 131 .version(Version.HTTP_2) 132 .POST(reqBody) 133 .build(); 134 out.println("\nPosting to HTTP/1.1 server at: " + req1); 135 HttpResponse<Stream<String>> response = client.send(req1, BodyHandlers.ofLines()); 136 out.println("Checking response..."); 137 if (response.statusCode() != 200) { 138 throw new RuntimeException("Unexpected status code: " + response); 139 } 140 if (response.version() != Version.HTTP_1_1) { 141 throw new RuntimeException("Unexpected protocol version: " 142 + response.version()); 143 } 144 List<String> respLines = response.body().collect(Collectors.toList()); 145 if (!lines.equals(respLines)) { 146 throw new RuntimeException("Unexpected response 1: " + respLines); 147 } 148 HttpRequest.BodyPublisher reqBody2 = HttpRequest.BodyPublishers.ofString(body); 149 HttpRequest req2 = HttpRequest 150 .newBuilder(uri2) 151 .version(Version.HTTP_2) 152 .POST(reqBody2) 153 .build(); 154 out.println("\nPosting to HTTP/2 server at: " + req2); 155 response = client.send(req2, BodyHandlers.ofLines()); 156 out.println("Checking response..."); 157 if (response.statusCode() != 200) { 158 throw new RuntimeException("Unexpected status code: " + response); 159 } 160 if (response.version() != Version.HTTP_2) { 161 throw new RuntimeException("Unexpected protocol version: " 162 + response.version()); 163 } 164 respLines = response.body().collect(Collectors.toList()); 165 if (!lines.equals(respLines)) { 166 throw new RuntimeException("Unexpected response 2: " + respLines); 167 } 168 } catch(Throwable t) { 169 out.println("Unexpected exception: exiting: " + t); 170 t.printStackTrace(); 171 throw t; 172 } finally { 173 proxy.stop(); 174 http1Server.stop(); 175 http2Server.stop(); 176 } 177 } 178 179 } 180