1 /*
  2  * Copyright (c) 2017, 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 
 24 /*
 25  * @test
 26  * @summary Tests mapped response subscriber
 27  * @library /test/lib /test/jdk/java/net/httpclient/lib
 28  * @build jdk.test.lib.net.SimpleSSLContext jdk.httpclient.test.lib.http2.Http2TestServer
 29  * @run testng/othervm
 30  *       -Djdk.internal.httpclient.debug=true
 31  *      MappingResponseSubscriber
 32  */
 33 
 34 import java.io.IOException;
 35 import java.io.InputStream;
 36 import java.io.OutputStream;
 37 import java.net.InetAddress;
 38 import java.net.InetSocketAddress;
 39 import java.net.URI;
 40 import java.nio.ByteBuffer;
 41 import java.util.List;
 42 import java.util.concurrent.CompletionStage;
 43 import java.util.concurrent.Executor;
 44 import java.util.concurrent.Executors;
 45 import java.util.concurrent.Flow;
 46 import com.sun.net.httpserver.HttpExchange;
 47 import com.sun.net.httpserver.HttpHandler;
 48 import com.sun.net.httpserver.HttpServer;
 49 import com.sun.net.httpserver.HttpsConfigurator;
 50 import com.sun.net.httpserver.HttpsServer;
 51 import java.net.http.HttpClient;
 52 import java.net.http.HttpHeaders;
 53 import java.net.http.HttpRequest;
 54 import java.net.http.HttpResponse;
 55 import java.net.http.HttpResponse.BodyHandler;
 56 import java.net.http.HttpResponse.BodyHandlers;
 57 import java.net.http.HttpResponse.BodySubscribers;
 58 import  java.net.http.HttpResponse.BodySubscriber;
 59 import java.util.function.Function;
 60 import javax.net.ssl.SSLContext;
 61 
 62 import jdk.internal.net.http.common.OperationTrackers.Tracker;
 63 import jdk.httpclient.test.lib.http2.Http2TestServer;
 64 import jdk.httpclient.test.lib.http2.Http2TestExchange;
 65 import jdk.httpclient.test.lib.http2.Http2Handler;
 66 import jdk.test.lib.net.SimpleSSLContext;
 67 import org.testng.annotations.AfterTest;
 68 import org.testng.annotations.BeforeTest;
 69 import org.testng.annotations.DataProvider;
 70 import org.testng.annotations.Test;
 71 import static java.lang.System.out;
 72 import static java.nio.charset.StandardCharsets.UTF_8;
 73 import static org.testng.Assert.assertEquals;
 74 import static org.testng.Assert.assertTrue;
 75 
 76 public class MappingResponseSubscriber {
 77 
 78     SSLContext sslContext;
 79     HttpServer httpTestServer;         // HTTP/1.1    [ 4 servers ]
 80     HttpsServer httpsTestServer;       // HTTPS/1.1
 81     Http2TestServer http2TestServer;   // HTTP/2 ( h2c )
 82     Http2TestServer https2TestServer;  // HTTP/2 ( h2  )
 83     String httpURI_fixed;
 84     String httpURI_chunk;
 85     String httpsURI_fixed;
 86     String httpsURI_chunk;
 87     String http2URI_fixed;
 88     String http2URI_chunk;
 89     String https2URI_fixed;
 90     String https2URI_chunk;
 91 
 92     static final int ITERATION_COUNT = 3;
 93     // a shared executor helps reduce the amount of threads created by the test
 94     static final Executor executor = Executors.newCachedThreadPool();
 95 
 96     static final ReferenceTracker TRACKER = ReferenceTracker.INSTANCE;
 97 
 98     @DataProvider(name = "variants")
 99     public Object[][] variants() {
100         return new Object[][]{
101                 { httpURI_fixed,    false },
102                 { httpURI_chunk,    false },
103                 { httpsURI_fixed,   false },
104                 { httpsURI_chunk,   false },
105                 { http2URI_fixed,   false },
106                 { http2URI_chunk,   false },
107                 { https2URI_fixed,  false,},
108                 { https2URI_chunk,  false },
109 
110                 { httpURI_fixed,    true },
111                 { httpURI_chunk,    true },
112                 { httpsURI_fixed,   true },
113                 { httpsURI_chunk,   true },
114                 { http2URI_fixed,   true },
115                 { http2URI_chunk,   true },
116                 { https2URI_fixed,  true,},
117                 { https2URI_chunk,  true },
118         };
119     }
120 
121     HttpClient newHttpClient() {
122         return HttpClient.newBuilder()
123                          .executor(executor)
124                          .sslContext(sslContext)
125                          .build();
126     }
127 
128     @Test(dataProvider = "variants")
129     public void testAsBytes(String uri, boolean sameClient) throws Exception {
130         HttpClient client = null;
131         for (int i = 0; i < ITERATION_COUNT; i++) {
132             if (!sameClient || client == null)
133                 client = newHttpClient();
134 
135             HttpRequest req = HttpRequest.newBuilder(URI.create(uri))
136                     .build();
137             BodyHandler<byte[]> handler = new CRSBodyHandler();
138             HttpResponse<byte[]> response = client.send(req, handler);
139             byte[] body = response.body();
140             assertEquals(body, bytes);
141 
142             // if sameClient we will reuse the client for the next
143             // operation, so there's nothing more to do.
144             if (sameClient) continue;
145 
146             // if no error and not same client then wait for the
147             // client to be GC'ed before performing the nex operation
148             Tracker tracker = TRACKER.getTracker(client);
149             client = null;
150             System.gc();
151             AssertionError error = TRACKER.check(tracker, 1500);
152             if (error != null) throw error; // the client didn't shut down properly
153         }
154         if (sameClient) {
155             Tracker tracker = TRACKER.getTracker(client);
156             client = null;
157             System.gc();
158             AssertionError error = TRACKER.check(tracker,1500);
159             if (error != null) throw error; // the client didn't shut down properly
160         }
161     }
162 
163     static class CRSBodyHandler implements BodyHandler<byte[]> {
164         @Override
165         public BodySubscriber<byte[]> apply(HttpResponse.ResponseInfo rinfo) {
166             assertEquals(rinfo.statusCode(), 200);
167             return BodySubscribers.mapping(
168                 new CRSBodySubscriber(), (s) -> s.getBytes(UTF_8)
169             );
170         }
171     }
172 
173     static class CRSBodySubscriber implements BodySubscriber<String> {
174         private final BodySubscriber<String> ofString = BodySubscribers.ofString(UTF_8);
175         volatile boolean onSubscribeCalled;
176 
177         @Override
178         public void onSubscribe(Flow.Subscription subscription) {
179             //out.println("onSubscribe ");
180             onSubscribeCalled = true;
181             ofString.onSubscribe(subscription);
182         }
183 
184         @Override
185         public void onNext(List<ByteBuffer> item) {
186            // out.println("onNext " + item);
187             assertTrue(onSubscribeCalled);
188             ofString.onNext(item);
189         }
190 
191         @Override
192         public void onError(Throwable throwable) {
193             //out.println("onError");
194             assertTrue(onSubscribeCalled);
195             ofString.onError(throwable);
196         }
197 
198         @Override
199         public void onComplete() {
200             //out.println("onComplete");
201             assertTrue(onSubscribeCalled, "onComplete called before onSubscribe");
202             ofString.onComplete();
203         }
204 
205         @Override
206         public CompletionStage<String> getBody() {
207             return ofString.getBody();
208         }
209     }
210 
211     static String serverAuthority(HttpServer server) {
212         return InetAddress.getLoopbackAddress().getHostName() + ":"
213                 + server.getAddress().getPort();
214     }
215 
216     @BeforeTest
217     public void setup() throws Exception {
218         sslContext = new SimpleSSLContext().get();
219         if (sslContext == null)
220             throw new AssertionError("Unexpected null sslContext");
221 
222         // HTTP/1.1
223         HttpHandler h1_fixedLengthHandler = new HTTP1_FixedLengthHandler();
224         HttpHandler h1_chunkHandler = new HTTP1_ChunkedHandler();
225         InetSocketAddress sa = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
226         httpTestServer = HttpServer.create(sa, 0);
227         httpTestServer.createContext("/http1/fixed", h1_fixedLengthHandler);
228         httpTestServer.createContext("/http1/chunk", h1_chunkHandler);
229         httpURI_fixed = "http://" + serverAuthority(httpTestServer) + "/http1/fixed";
230         httpURI_chunk = "http://" + serverAuthority(httpTestServer) + "/http1/chunk";
231 
232         httpsTestServer = HttpsServer.create(sa, 0);
233         httpsTestServer.setHttpsConfigurator(new HttpsConfigurator(sslContext));
234         httpsTestServer.createContext("/https1/fixed", h1_fixedLengthHandler);
235         httpsTestServer.createContext("/https1/chunk", h1_chunkHandler);
236         httpsURI_fixed = "https://" + serverAuthority(httpsTestServer) + "/https1/fixed";
237         httpsURI_chunk = "https://" + serverAuthority(httpsTestServer) + "/https1/chunk";
238 
239         // HTTP/2
240         Http2Handler h2_fixedLengthHandler = new HTTP2_FixedLengthHandler();
241         Http2Handler h2_chunkedHandler = new HTTP2_VariableHandler();
242 
243         http2TestServer = new Http2TestServer("localhost", false, 0);
244         http2TestServer.addHandler(h2_fixedLengthHandler, "/http2/fixed");
245         http2TestServer.addHandler(h2_chunkedHandler, "/http2/chunk");
246         http2URI_fixed = "http://" + http2TestServer.serverAuthority() + "/http2/fixed";
247         http2URI_chunk = "http://" + http2TestServer.serverAuthority() + "/http2/chunk";
248 
249         https2TestServer = new Http2TestServer("localhost", true, sslContext);
250         https2TestServer.addHandler(h2_fixedLengthHandler, "/https2/fixed");
251         https2TestServer.addHandler(h2_chunkedHandler, "/https2/chunk");
252         https2URI_fixed = "https://" + https2TestServer.serverAuthority() + "/https2/fixed";
253         https2URI_chunk = "https://" + https2TestServer.serverAuthority() + "/https2/chunk";
254 
255         httpTestServer.start();
256         httpsTestServer.start();
257         http2TestServer.start();
258         https2TestServer.start();
259     }
260 
261     @AfterTest
262     public void teardown() throws Exception {
263         httpTestServer.stop(0);
264         httpsTestServer.stop(0);
265         http2TestServer.stop();
266         https2TestServer.stop();
267     }
268 
269     static byte[] bytes;
270 
271     static {
272         bytes = new byte[128 * 1024];
273         int b = 'A';
274 
275         for (int i=0; i< bytes.length; i++) {
276             bytes[i] = (byte)b;
277             b = b == 'Z'? 'A' : b + 1;
278         }
279     }
280 
281     static class HTTP1_FixedLengthHandler implements HttpHandler {
282         @Override
283         public void handle(HttpExchange t) throws IOException {
284             out.println("HTTP1_FixedLengthHandler received request to " + t.getRequestURI());
285             try (InputStream is = t.getRequestBody()) {
286                 is.readAllBytes();
287             }
288             t.sendResponseHeaders(200, bytes.length);  //no body
289             OutputStream os = t.getResponseBody();
290             os.write(bytes);
291             os.close();
292         }
293     }
294 
295     static class HTTP1_ChunkedHandler implements HttpHandler {
296         @Override
297         public void handle(HttpExchange t) throws IOException {
298             out.println("HTTP1_ChunkedHandler received request to " + t.getRequestURI());
299             try (InputStream is = t.getRequestBody()) {
300                 is.readAllBytes();
301             }
302             t.sendResponseHeaders(200, 0); // chunked
303             OutputStream os = t.getResponseBody();
304             os.write(bytes);
305             os.close();
306         }
307     }
308 
309     static class HTTP2_FixedLengthHandler implements Http2Handler {
310         @Override
311         public void handle(Http2TestExchange t) throws IOException {
312             out.println("HTTP2_FixedLengthHandler received request to " + t.getRequestURI());
313             try (InputStream is = t.getRequestBody()) {
314                 is.readAllBytes();
315             }
316             t.sendResponseHeaders(200, 0); // chunked
317             OutputStream os = t.getResponseBody();
318             os.write(bytes);
319             os.close();
320         }
321     }
322 
323     static class HTTP2_VariableHandler implements Http2Handler {
324         @Override
325         public void handle(Http2TestExchange t) throws IOException {
326             out.println("HTTP2_VariableHandler received request to " + t.getRequestURI());
327             try (InputStream is = t.getRequestBody()) {
328                 is.readAllBytes();
329             }
330             t.sendResponseHeaders(200, bytes.length);  //no body
331             OutputStream os = t.getResponseBody();
332             os.write(bytes);
333             os.close();
334         }
335     }
336 
337     // -- Compile only. Verifies generic signatures
338 
339     static final Function<CharSequence,Integer> f1 = subscriber -> 1;
340     static final Function<CharSequence,Number> f2 = subscriber -> 2;
341     static final Function<String,Integer> f3 = subscriber -> 3;
342     static final Function<String,Number> f4 = subscriber -> 4;
343 
344     public void compileOnly() throws Exception {
345         HttpClient client = null;
346         HttpRequest req = null;
347 
348         HttpResponse<Integer> r1 = client.send(req, (ri) ->
349                 BodySubscribers.mapping(BodySubscribers.ofString(UTF_8), s -> 1));
350         HttpResponse<Number>  r2 = client.send(req, (ri) ->
351                 BodySubscribers.mapping(BodySubscribers.ofString(UTF_8), s -> 1));
352         HttpResponse<String>  r3 = client.send(req, (ri) ->
353                 BodySubscribers.mapping(BodySubscribers.ofString(UTF_8), s -> "s"));
354         HttpResponse<CharSequence> r4 = client.send(req, (ri) ->
355                 BodySubscribers.mapping(BodySubscribers.ofString(UTF_8), s -> "s"));
356 
357         HttpResponse<Integer> x1 = client.send(req, (ri) ->
358                 BodySubscribers.mapping(BodySubscribers.ofString(UTF_8), f1));
359         HttpResponse<Number>  x2 = client.send(req, (ri) ->
360                 BodySubscribers.mapping(BodySubscribers.ofString(UTF_8), f1));
361         HttpResponse<Number>  x3 = client.send(req, (ri) ->
362                 BodySubscribers.mapping(BodySubscribers.ofString(UTF_8), f2));
363         HttpResponse<Integer> x4 = client.send(req, (ri) ->
364                 BodySubscribers.mapping(BodySubscribers.ofString(UTF_8), f3));
365         HttpResponse<Number>  x5 = client.send(req, (ri) ->
366                 BodySubscribers.mapping(BodySubscribers.ofString(UTF_8), f3));
367         HttpResponse<Number>  x7 = client.send(req, (ri) ->
368                 BodySubscribers.mapping(BodySubscribers.ofString(UTF_8), f4));
369     }
370 }