1 /*
  2  * Copyright (c) 2019, 2023, 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 import com.sun.net.httpserver.HttpServer;
 24 import com.sun.net.httpserver.HttpsConfigurator;
 25 import com.sun.net.httpserver.HttpsServer;
 26 import jdk.test.lib.net.SimpleSSLContext;
 27 
 28 import javax.net.ssl.SSLContext;
 29 import java.io.IOException;
 30 import java.io.InputStream;
 31 import java.io.OutputStream;
 32 import java.net.InetAddress;
 33 import java.net.InetSocketAddress;
 34 import java.net.Proxy;
 35 import java.net.ProxySelector;
 36 import java.net.SocketAddress;
 37 import java.net.URI;
 38 import java.net.http.HttpClient;
 39 import java.net.http.HttpRequest;
 40 import java.net.http.HttpResponse;
 41 import java.nio.charset.StandardCharsets;
 42 import java.time.Duration;
 43 import java.util.List;
 44 import java.util.Set;
 45 import java.util.concurrent.CompletableFuture;
 46 import java.util.concurrent.CopyOnWriteArrayList;
 47 import java.util.concurrent.CopyOnWriteArraySet;
 48 import java.util.concurrent.ExecutorService;
 49 import java.util.concurrent.LinkedBlockingQueue;
 50 import java.util.concurrent.ThreadPoolExecutor;
 51 import java.util.concurrent.TimeUnit;
 52 import java.util.concurrent.atomic.AtomicLong;
 53 import jdk.httpclient.test.lib.common.HttpServerAdapters;
 54 import jdk.httpclient.test.lib.http2.Http2TestServer;
 55 import static java.net.http.HttpClient.Version.HTTP_1_1;
 56 import static java.net.http.HttpClient.Version.HTTP_2;
 57 
 58 /**
 59  * @test
 60  * @bug 8231449
 61  * @summary This test verifies that the HttpClient works correctly when the server
 62  *          sends large amount of data. Note that this test will pass even without
 63  *          the fix for JDK-8231449, which is unfortunate.
 64  * @library /test/lib /test/jdk/java/net/httpclient/lib
 65  * @build jdk.httpclient.test.lib.common.HttpServerAdapters
 66  *        jdk.test.lib.net.SimpleSSLContext DigestEchoServer
 67  * @run main/othervm -Dtest.requiresHost=true
 68  *                   -Djdk.httpclient.HttpClient.log=headers
 69  *                   -Djdk.internal.httpclient.debug=true
 70  *                   LargeResponseTest
 71  *
 72  */
 73 public class LargeResponseTest implements HttpServerAdapters {
 74     static final byte[] DATA;
 75     static {
 76         DATA = new byte[64 * 1024];
 77         int len = 'z' - 'a';
 78         for (int i=0; i < DATA.length; i++) {
 79             DATA[i] = (byte) ('a' + (i % len));
 80         }
 81     }
 82 
 83     static final SSLContext context;
 84     static {
 85         try {
 86             context = new SimpleSSLContext().get();
 87             SSLContext.setDefault(context);
 88         } catch (Exception x) {
 89             throw new ExceptionInInitializerError(x);
 90         }
 91     }
 92 
 93     final AtomicLong requestCounter = new AtomicLong();
 94     final AtomicLong responseCounter = new AtomicLong();
 95     HttpTestServer http1Server;
 96     HttpTestServer http2Server;
 97     HttpTestServer https1Server;
 98     HttpTestServer https2Server;
 99     DigestEchoServer.TunnelingProxy proxy;
100 
101     URI http1URI;
102     URI https1URI;
103     URI http2URI;
104     URI https2URI;
105     InetSocketAddress proxyAddress;
106     ProxySelector proxySelector;
107     HttpClient client;
108     List<CompletableFuture<?>>  futures = new CopyOnWriteArrayList<>();
109     Set<URI> pending = new CopyOnWriteArraySet<>();
110 
111     final ExecutorService executor = new ThreadPoolExecutor(12, 60, 10,
112             TimeUnit.SECONDS, new LinkedBlockingQueue<>());
113     final ExecutorService clientexec = new ThreadPoolExecutor(6, 12, 1,
114             TimeUnit.SECONDS, new LinkedBlockingQueue<>());
115 
116     public HttpClient newHttpClient(ProxySelector ps) {
117         HttpClient.Builder builder = HttpClient
118                 .newBuilder()
119                 .sslContext(context)
120                 .executor(clientexec)
121                 .proxy(ps);
122         return builder.build();
123     }
124 
125     public void setUp() throws Exception {
126         try {
127             InetSocketAddress sa = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
128 
129             // HTTP/1.1
130             http1Server = HttpTestServer.create(HTTP_1_1, null, executor);
131             http1Server.addHandler(new HttpTestLargeHandler(), "/LargeResponseTest/http1/");
132             http1Server.start();
133             http1URI = new URI("http://" + http1Server.serverAuthority() + "/LargeResponseTest/http1/");
134 
135 
136             // HTTPS/1.1
137             HttpsServer sserver1 = HttpsServer.create(sa, 100);
138             sserver1.setExecutor(executor);
139             sserver1.setHttpsConfigurator(new HttpsConfigurator(context));
140             https1Server = HttpTestServer.of(sserver1);
141             https1Server.addHandler(new HttpTestLargeHandler(), "/LargeResponseTest/https1/");
142             https1Server.start();
143             https1URI = new URI("https://" + https1Server.serverAuthority() + "/LargeResponseTest/https1/");
144 
145             // HTTP/2.0
146             http2Server = HttpTestServer.create(HTTP_2);
147             http2Server.addHandler(new HttpTestLargeHandler(), "/LargeResponseTest/http2/");
148             http2Server.start();
149             http2URI = new URI("http://" + http2Server.serverAuthority() + "/LargeResponseTest/http2/");
150 
151             // HTTPS/2.0
152             https2Server = HttpTestServer.create(HTTP_2, SSLContext.getDefault());
153             https2Server.addHandler(new HttpTestLargeHandler(), "/LargeResponseTest/https2/");
154             https2Server.start();
155             https2URI = new URI("https://" + https2Server.serverAuthority() + "/LargeResponseTest/https2/");
156 
157             proxy = DigestEchoServer.createHttpsProxyTunnel(
158                     DigestEchoServer.HttpAuthSchemeType.NONE);
159             proxyAddress = proxy.getProxyAddress();
160             proxySelector = new HttpProxySelector(proxyAddress);
161             client = newHttpClient(proxySelector);
162             System.out.println("Setup: done");
163         } catch (Exception x) {
164             tearDown(); throw x;
165         } catch (Error e) {
166             tearDown(); throw e;
167         }
168     }
169 
170     public static void main(String[] args) throws Exception {
171         LargeResponseTest test = new LargeResponseTest();
172         test.setUp();
173         long start = System.nanoTime();
174         try {
175             test.run(args);
176         } finally {
177             try {
178                 long elapsed = System.nanoTime() - start;
179                 System.out.println("*** Elapsed: " + Duration.ofNanos(elapsed));
180             } finally {
181                 test.tearDown();
182             }
183         }
184     }
185 
186     public void run(String... args) throws Exception {
187         List<URI> serverURIs = List.of(http1URI, http2URI, https1URI, https2URI);
188         for (int i=0; i<5; i++) {
189             for (URI base : serverURIs) {
190                 if (base.getScheme().equalsIgnoreCase("https")) {
191                     URI proxy = i % 1 == 0 ? base.resolve(URI.create("proxy/foo?n="+requestCounter.incrementAndGet()))
192                     : base.resolve(URI.create("direct/foo?n="+requestCounter.incrementAndGet()));
193                     test(proxy);
194                 }
195             }
196             for (URI base : serverURIs) {
197                 URI direct = base.resolve(URI.create("direct/foo?n="+requestCounter.incrementAndGet()));
198                 test(direct);
199             }
200         }
201         CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join();
202     }
203 
204     public void test(URI uri) throws Exception {
205         System.out.println("Testing with " + uri);
206         pending.add(uri);
207         HttpRequest request = HttpRequest.newBuilder(uri).build();
208         CompletableFuture<HttpResponse<String>> resp =
209                 client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
210                 .whenComplete((r, t) -> this.requestCompleted(request, r, t));
211         futures.add(resp);
212     }
213 
214     private void requestCompleted(HttpRequest request, HttpResponse<?> r, Throwable t) {
215         responseCounter.incrementAndGet();
216         pending.remove(request.uri());
217         System.out.println(request + " -> " + (t == null ? r : t)
218                 + " [still pending: " + (requestCounter.get() - responseCounter.get()) +"]");
219         if (pending.size() < 10 && requestCounter.get() > 10) {
220             pending.forEach(u -> System.out.println("\tpending: " + u));
221         }
222     }
223 
224     public void tearDown() {
225         proxy = stop(proxy, DigestEchoServer.TunnelingProxy::stop);
226         http1Server = stop(http1Server, HttpTestServer::stop);
227         https1Server = stop(https1Server, HttpTestServer::stop);
228         http2Server = stop(http2Server, HttpTestServer::stop);
229         https2Server = stop(https2Server, HttpTestServer::stop);
230         client = null;
231         try {
232             executor.awaitTermination(2000, TimeUnit.MILLISECONDS);
233         } catch (Throwable x) {
234         } finally {
235             executor.shutdownNow();
236         }
237         try {
238             clientexec.awaitTermination(2000, TimeUnit.MILLISECONDS);
239         } catch (Throwable x) {
240         } finally {
241             clientexec.shutdownNow();
242         }
243         System.out.println("Teardown: done");
244     }
245 
246     private interface Stoppable<T> { public void stop(T service) throws Exception; }
247 
248     static <T>  T stop(T service, Stoppable<T> stop) {
249         try { if (service != null) stop.stop(service); } catch (Throwable x) { };
250         return null;
251     }
252 
253     static class HttpProxySelector extends ProxySelector {
254         private static final List<Proxy> NO_PROXY = List.of(Proxy.NO_PROXY);
255         private final List<Proxy> proxyList;
256         HttpProxySelector(InetSocketAddress proxyAddress) {
257             proxyList = List.of(new Proxy(Proxy.Type.HTTP, proxyAddress));
258         }
259 
260         @Override
261         public List<Proxy> select(URI uri) {
262             // our proxy only supports tunneling
263             if (uri.getScheme().equalsIgnoreCase("https")) {
264                 if (uri.getPath().contains("/proxy/")) {
265                     return proxyList;
266                 }
267             }
268             return NO_PROXY;
269         }
270 
271         @Override
272         public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
273             System.err.println("Connection to proxy failed: " + ioe);
274             System.err.println("Proxy: " + sa);
275             System.err.println("\tURI: " + uri);
276             ioe.printStackTrace();
277         }
278     }
279 
280     public static class HttpTestLargeHandler implements HttpTestHandler {
281         @Override
282         public void handle(HttpTestExchange t) throws IOException {
283             try (InputStream is = t.getRequestBody();
284                  OutputStream os = t.getResponseBody()) {
285                 byte[] bytes = is.readAllBytes();
286                 assert bytes.length == 0;
287                 URI u = t.getRequestURI();
288                 long responseID = Long.parseLong(u.getQuery().substring(2));
289                 System.out.println("Server " + t.getRequestURI() + " sending response " + responseID);
290                 t.sendResponseHeaders(200, DATA.length * 3);
291                 for (int i=0; i<3; i++) {
292                     os.write(DATA);
293                 }
294                 System.out.println("\tresp:" + responseID + ": done");
295             }
296         }
297     }
298 
299 }