|
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.testlibrary.SimpleSSLContext; |
|
27 import javax.net.ssl.SSLContext; |
|
28 import java.net.InetSocketAddress; |
|
29 import java.net.ProxySelector; |
|
30 import java.net.URI; |
|
31 import java.net.http.HttpClient; |
|
32 import java.net.http.HttpClient.Version; |
|
33 import java.net.http.HttpRequest; |
|
34 import java.net.http.HttpResponse; |
|
35 import java.util.Arrays; |
|
36 import java.util.List; |
|
37 import java.util.stream.Collectors; |
|
38 import java.util.stream.Stream; |
|
39 |
|
40 import static java.lang.String.format; |
|
41 import static java.lang.System.out; |
|
42 import static java.net.http.HttpResponse.BodyHandler.asLines; |
|
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 /lib/testlibrary http2/server |
|
53 * @build jdk.testlibrary.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 |
|
89 HttpsTunnelTest() { |
|
90 } |
|
91 |
|
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 |
|
100 public static void main(String[] args) throws Exception { |
|
101 InetSocketAddress sa = new InetSocketAddress(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("127.0.0.1", 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.getAddress() + "/foo/https1"); |
|
118 URI uri2 = new URI("https:/" + http2Server.getAddress() + "/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.BodyPublisher.fromString(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, asLines()); |
|
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.BodyPublisher.fromString(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, asLines()); |
|
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 } |