Path: blob/master/test/jdk/java/net/httpclient/AbstractThrowingPublishers.java
41152 views
/*1* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import com.sun.net.httpserver.HttpServer;24import com.sun.net.httpserver.HttpsConfigurator;25import com.sun.net.httpserver.HttpsServer;26import jdk.test.lib.net.SimpleSSLContext;27import org.testng.ITestContext;28import org.testng.annotations.AfterClass;29import org.testng.annotations.AfterTest;30import org.testng.annotations.BeforeMethod;31import org.testng.annotations.BeforeTest;32import org.testng.annotations.DataProvider;33import org.testng.annotations.Test;3435import javax.net.ssl.SSLContext;36import java.io.IOException;37import java.io.InputStream;38import java.io.OutputStream;39import java.io.UncheckedIOException;40import java.net.InetAddress;41import java.net.InetSocketAddress;42import java.net.URI;43import java.net.http.HttpClient;44import java.net.http.HttpRequest;45import java.net.http.HttpRequest.BodyPublisher;46import java.net.http.HttpRequest.BodyPublishers;47import java.net.http.HttpResponse;48import java.net.http.HttpResponse.BodyHandler;49import java.net.http.HttpResponse.BodyHandlers;50import java.nio.ByteBuffer;51import java.nio.charset.StandardCharsets;52import java.util.EnumSet;53import java.util.List;54import java.util.Set;55import java.util.concurrent.CompletableFuture;56import java.util.concurrent.CompletionException;57import java.util.concurrent.ConcurrentHashMap;58import java.util.concurrent.ConcurrentMap;59import java.util.concurrent.ExecutionException;60import java.util.concurrent.Executor;61import java.util.concurrent.Executors;62import java.util.concurrent.Flow;63import java.util.concurrent.SubmissionPublisher;64import java.util.concurrent.atomic.AtomicLong;65import java.util.function.BiPredicate;66import java.util.function.Consumer;67import java.util.function.Supplier;68import java.util.stream.Collectors;69import java.util.stream.Stream;7071import static java.lang.String.format;72import static java.lang.System.out;73import static java.nio.charset.StandardCharsets.UTF_8;74import static org.testng.Assert.assertEquals;75import static org.testng.Assert.assertTrue;7677public abstract class AbstractThrowingPublishers implements HttpServerAdapters {7879SSLContext sslContext;80HttpTestServer httpTestServer; // HTTP/1.1 [ 4 servers ]81HttpTestServer httpsTestServer; // HTTPS/1.182HttpTestServer http2TestServer; // HTTP/2 ( h2c )83HttpTestServer https2TestServer; // HTTP/2 ( h2 )84String httpURI_fixed;85String httpURI_chunk;86String httpsURI_fixed;87String httpsURI_chunk;88String http2URI_fixed;89String http2URI_chunk;90String https2URI_fixed;91String https2URI_chunk;9293static final int ITERATION_COUNT = 1;94// a shared executor helps reduce the amount of threads created by the test95static final Executor executor = new TestExecutor(Executors.newCachedThreadPool());96static final ConcurrentMap<String, Throwable> FAILURES = new ConcurrentHashMap<>();97static volatile boolean tasksFailed;98static final AtomicLong serverCount = new AtomicLong();99static final AtomicLong clientCount = new AtomicLong();100static final long start = System.nanoTime();101public static String now() {102long now = System.nanoTime() - start;103long secs = now / 1000_000_000;104long mill = (now % 1000_000_000) / 1000_000;105long nan = now % 1000_000;106return String.format("[%d s, %d ms, %d ns] ", secs, mill, nan);107}108109final ReferenceTracker TRACKER = ReferenceTracker.INSTANCE;110private volatile HttpClient sharedClient;111112static class TestExecutor implements Executor {113final AtomicLong tasks = new AtomicLong();114Executor executor;115TestExecutor(Executor executor) {116this.executor = executor;117}118119@Override120public void execute(Runnable command) {121long id = tasks.incrementAndGet();122executor.execute(() -> {123try {124command.run();125} catch (Throwable t) {126tasksFailed = true;127System.out.printf(now() + "Task %s failed: %s%n", id, t);128System.err.printf(now() + "Task %s failed: %s%n", id, t);129FAILURES.putIfAbsent("Task " + id, t);130throw t;131}132});133}134}135136protected boolean stopAfterFirstFailure() {137return Boolean.getBoolean("jdk.internal.httpclient.debug");138}139140@BeforeMethod141void beforeMethod(ITestContext context) {142if (stopAfterFirstFailure() && context.getFailedTests().size() > 0) {143throw new RuntimeException("some tests failed");144}145}146147@AfterClass148static final void printFailedTests() {149out.println("\n=========================");150try {151out.printf("%n%sCreated %d servers and %d clients%n",152now(), serverCount.get(), clientCount.get());153if (FAILURES.isEmpty()) return;154out.println("Failed tests: ");155FAILURES.entrySet().forEach((e) -> {156out.printf("\t%s: %s%n", e.getKey(), e.getValue());157e.getValue().printStackTrace(out);158});159if (tasksFailed) {160System.out.println("WARNING: Some tasks failed");161}162} finally {163out.println("\n=========================\n");164}165}166167private String[] uris() {168return new String[] {169httpURI_fixed,170httpURI_chunk,171httpsURI_fixed,172httpsURI_chunk,173http2URI_fixed,174http2URI_chunk,175https2URI_fixed,176https2URI_chunk,177};178}179180@DataProvider(name = "sanity")181public Object[][] sanity() {182String[] uris = uris();183Object[][] result = new Object[uris.length * 2][];184//Object[][] result = new Object[uris.length][];185int i = 0;186for (boolean sameClient : List.of(false, true)) {187//if (!sameClient) continue;188for (String uri: uris()) {189result[i++] = new Object[] {uri + "/sanity", sameClient};190}191}192assert i == uris.length * 2;193// assert i == uris.length ;194return result;195}196197enum Where {198BEFORE_SUBSCRIBE, BEFORE_REQUEST, BEFORE_NEXT_REQUEST, BEFORE_CANCEL,199AFTER_SUBSCRIBE, AFTER_REQUEST, AFTER_NEXT_REQUEST, AFTER_CANCEL;200public Consumer<Where> select(Consumer<Where> consumer) {201return new Consumer<Where>() {202@Override203public void accept(Where where) {204if (Where.this == where) {205consumer.accept(where);206}207}208};209}210}211212private Object[][] variants(List<Thrower> throwers, Set<Where> whereValues) {213String[] uris = uris();214Object[][] result = new Object[uris.length * 2 * throwers.size()][];215//Object[][] result = new Object[(uris.length/2) * 2 * 2][];216int i = 0;217for (Thrower thrower : throwers) {218for (boolean sameClient : List.of(false, true)) {219for (String uri : uris()) {220// if (uri.contains("http2") || uri.contains("https2")) continue;221// if (!sameClient) continue;222result[i++] = new Object[]{uri, sameClient, thrower, whereValues};223}224}225}226assert i == uris.length * 2 * throwers.size();227//assert Stream.of(result).filter(o -> o != null).count() == result.length;228return result;229}230231@DataProvider(name = "subscribeProvider")232public Object[][] subscribeProvider(ITestContext context) {233if (stopAfterFirstFailure() && context.getFailedTests().size() > 0) {234return new Object[0][];235}236return variants(List.of(237new UncheckedCustomExceptionThrower(),238new UncheckedIOExceptionThrower()),239EnumSet.of(Where.BEFORE_SUBSCRIBE, Where.AFTER_SUBSCRIBE));240}241242@DataProvider(name = "requestProvider")243public Object[][] requestProvider(ITestContext context) {244if (stopAfterFirstFailure() && context.getFailedTests().size() > 0) {245return new Object[0][];246}247return variants(List.of(248new UncheckedCustomExceptionThrower(),249new UncheckedIOExceptionThrower()),250EnumSet.of(Where.BEFORE_REQUEST, Where.AFTER_REQUEST));251}252253@DataProvider(name = "nextRequestProvider")254public Object[][] nextRequestProvider(ITestContext context) {255if (stopAfterFirstFailure() && context.getFailedTests().size() > 0) {256return new Object[0][];257}258return variants(List.of(259new UncheckedCustomExceptionThrower(),260new UncheckedIOExceptionThrower()),261EnumSet.of(Where.BEFORE_NEXT_REQUEST, Where.AFTER_NEXT_REQUEST));262}263264@DataProvider(name = "beforeCancelProviderIO")265public Object[][] beforeCancelProviderIO(ITestContext context) {266if (stopAfterFirstFailure() && context.getFailedTests().size() > 0) {267return new Object[0][];268}269return variants(List.of(270new UncheckedIOExceptionThrower()),271EnumSet.of(Where.BEFORE_CANCEL));272}273274@DataProvider(name = "afterCancelProviderIO")275public Object[][] afterCancelProviderIO(ITestContext context) {276if (stopAfterFirstFailure() && context.getFailedTests().size() > 0) {277return new Object[0][];278}279return variants(List.of(280new UncheckedIOExceptionThrower()),281EnumSet.of(Where.AFTER_CANCEL));282}283284@DataProvider(name = "beforeCancelProviderCustom")285public Object[][] beforeCancelProviderCustom(ITestContext context) {286if (stopAfterFirstFailure() && context.getFailedTests().size() > 0) {287return new Object[0][];288}289return variants(List.of(290new UncheckedCustomExceptionThrower()),291EnumSet.of(Where.BEFORE_CANCEL));292}293294@DataProvider(name = "afterCancelProviderCustom")295public Object[][] afterCancelProvider(ITestContext context) {296if (stopAfterFirstFailure() && context.getFailedTests().size() > 0) {297return new Object[0][];298}299return variants(List.of(300new UncheckedCustomExceptionThrower()),301EnumSet.of(Where.AFTER_CANCEL));302}303304private HttpClient makeNewClient() {305clientCount.incrementAndGet();306return TRACKER.track(HttpClient.newBuilder()307.proxy(HttpClient.Builder.NO_PROXY)308.executor(executor)309.sslContext(sslContext)310.build());311}312313HttpClient newHttpClient(boolean share) {314if (!share) return makeNewClient();315HttpClient shared = sharedClient;316if (shared != null) return shared;317synchronized (this) {318shared = sharedClient;319if (shared == null) {320shared = sharedClient = makeNewClient();321}322return shared;323}324}325326final String BODY = "Some string | that ? can | be split ? several | ways.";327328//@Test(dataProvider = "sanity")329protected void testSanityImpl(String uri, boolean sameClient)330throws Exception {331HttpClient client = null;332out.printf("%n%s testSanity(%s, %b)%n", now(), uri, sameClient);333for (int i=0; i< ITERATION_COUNT; i++) {334if (!sameClient || client == null)335client = newHttpClient(sameClient);336337SubmissionPublisher<ByteBuffer> publisher338= new SubmissionPublisher<>(executor,10);339ThrowingBodyPublisher bodyPublisher = new ThrowingBodyPublisher((w) -> {},340BodyPublishers.fromPublisher(publisher));341CompletableFuture<Void> subscribedCF = bodyPublisher.subscribedCF();342subscribedCF.whenComplete((r,t) -> System.out.println(now() + " subscribe completed " + t))343.thenAcceptAsync((v) -> {344Stream.of(BODY.split("\\|"))345.forEachOrdered(s -> {346System.out.println("submitting \"" + s +"\"");347publisher.submit(ByteBuffer.wrap(s.getBytes(StandardCharsets.UTF_8)));348});349System.out.println("publishing done");350publisher.close();351},352executor);353354HttpRequest req = HttpRequest.newBuilder(URI.create(uri))355.POST(bodyPublisher)356.build();357BodyHandler<String> handler = BodyHandlers.ofString();358CompletableFuture<HttpResponse<String>> response = client.sendAsync(req, handler);359360String body = response.join().body();361assertEquals(body, Stream.of(BODY.split("\\|")).collect(Collectors.joining()));362}363}364365// @Test(dataProvider = "variants")366protected void testThrowingAsStringImpl(String uri,367boolean sameClient,368Thrower thrower,369Set<Where> whereValues)370throws Exception371{372String test = format("testThrowingAsString(%s, %b, %s, %s)",373uri, sameClient, thrower, whereValues);374List<byte[]> bytes = Stream.of(BODY.split("|"))375.map(s -> s.getBytes(UTF_8))376.collect(Collectors.toList());377testThrowing(test, uri, sameClient, () -> BodyPublishers.ofByteArrays(bytes),378this::shouldNotThrowInCancel, thrower,false, whereValues);379}380381private <T,U> void testThrowing(String name, String uri, boolean sameClient,382Supplier<BodyPublisher> publishers,383Finisher finisher, Thrower thrower,384boolean async, Set<Where> whereValues)385throws Exception386{387out.printf("%n%s%s%n", now(), name);388try {389testThrowing(uri, sameClient, publishers, finisher, thrower, async, whereValues);390} catch (Error | Exception x) {391FAILURES.putIfAbsent(name, x);392throw x;393}394}395396private void testThrowing(String uri, boolean sameClient,397Supplier<BodyPublisher> publishers,398Finisher finisher, Thrower thrower,399boolean async, Set<Where> whereValues)400throws Exception401{402HttpClient client = null;403for (Where where : whereValues) {404//if (where == Where.ON_SUBSCRIBE) continue;405//if (where == Where.ON_ERROR) continue;406if (!sameClient || client == null)407client = newHttpClient(sameClient);408409ThrowingBodyPublisher bodyPublisher =410new ThrowingBodyPublisher(where.select(thrower), publishers.get());411HttpRequest req = HttpRequest.412newBuilder(URI.create(uri))413.header("X-expect-exception", "true")414.POST(bodyPublisher)415.build();416BodyHandler<String> handler = BodyHandlers.ofString();417System.out.println("try throwing in " + where);418HttpResponse<String> response = null;419if (async) {420try {421response = client.sendAsync(req, handler).join();422} catch (Error | Exception x) {423Throwable cause = findCause(where, x, thrower);424if (cause == null) throw causeNotFound(where, x);425System.out.println(now() + "Got expected exception: " + cause);426}427} else {428try {429response = client.send(req, handler);430} catch (Error | Exception t) {431// synchronous send will rethrow exceptions432Throwable throwable = t.getCause();433assert throwable != null;434435if (thrower.test(where, throwable)) {436System.out.println(now() + "Got expected exception: " + throwable);437} else throw causeNotFound(where, t);438}439}440if (response != null) {441finisher.finish(where, response, thrower);442}443}444}445446// can be used to reduce the surface of the test when diagnosing447// some failure448Set<Where> whereValues() {449//return EnumSet.of(Where.BEFORE_CANCEL, Where.AFTER_CANCEL);450return EnumSet.allOf(Where.class);451}452453interface Thrower extends Consumer<Where>, BiPredicate<Where,Throwable> {454455}456457interface Finisher<T,U> {458U finish(Where w, HttpResponse<T> resp, Thrower thrower) throws IOException;459}460461final <T,U> U shouldNotThrowInCancel(Where w, HttpResponse<T> resp, Thrower thrower) {462switch (w) {463case BEFORE_CANCEL: return null;464case AFTER_CANCEL: return null;465default: break;466}467return shouldHaveThrown(w, resp, thrower);468}469470471final <T,U> U shouldHaveThrown(Where w, HttpResponse<T> resp, Thrower thrower) {472String msg = "Expected exception not thrown in " + w473+ "\n\tReceived: " + resp474+ "\n\tWith body: " + resp.body();475System.out.println(msg);476throw new RuntimeException(msg);477}478479480private static Throwable findCause(Where w,481Throwable x,482BiPredicate<Where, Throwable> filter) {483while (x != null && !filter.test(w,x)) x = x.getCause();484return x;485}486487static AssertionError causeNotFound(Where w, Throwable t) {488return new AssertionError("Expected exception not found in " + w, t);489}490491static boolean isConnectionClosedLocally(Throwable t) {492if (t instanceof CompletionException) t = t.getCause();493if (t instanceof ExecutionException) t = t.getCause();494if (t instanceof IOException) {495String msg = t.getMessage();496return msg == null ? false497: msg.contains("connection closed locally");498}499return false;500}501502static final class UncheckedCustomExceptionThrower implements Thrower {503@Override504public void accept(Where where) {505out.println(now() + "Throwing in " + where);506throw new UncheckedCustomException(where.name());507}508509@Override510public boolean test(Where w, Throwable throwable) {511switch (w) {512case AFTER_REQUEST:513case BEFORE_NEXT_REQUEST:514case AFTER_NEXT_REQUEST:515if (isConnectionClosedLocally(throwable)) return true;516break;517default:518break;519}520return UncheckedCustomException.class.isInstance(throwable);521}522523@Override524public String toString() {525return "UncheckedCustomExceptionThrower";526}527}528529static final class UncheckedIOExceptionThrower implements Thrower {530@Override531public void accept(Where where) {532out.println(now() + "Throwing in " + where);533throw new UncheckedIOException(new CustomIOException(where.name()));534}535536@Override537public boolean test(Where w, Throwable throwable) {538switch (w) {539case AFTER_REQUEST:540case BEFORE_NEXT_REQUEST:541case AFTER_NEXT_REQUEST:542if (isConnectionClosedLocally(throwable)) return true;543break;544default:545break;546}547return UncheckedIOException.class.isInstance(throwable)548&& CustomIOException.class.isInstance(throwable.getCause());549}550551@Override552public String toString() {553return "UncheckedIOExceptionThrower";554}555}556557static final class UncheckedCustomException extends RuntimeException {558UncheckedCustomException(String message) {559super(message);560}561UncheckedCustomException(String message, Throwable cause) {562super(message, cause);563}564}565566static final class CustomIOException extends IOException {567CustomIOException(String message) {568super(message);569}570CustomIOException(String message, Throwable cause) {571super(message, cause);572}573}574575576static final class ThrowingBodyPublisher implements BodyPublisher {577private final BodyPublisher publisher;578private final CompletableFuture<Void> subscribedCF = new CompletableFuture<>();579final Consumer<Where> throwing;580ThrowingBodyPublisher(Consumer<Where> throwing, BodyPublisher publisher) {581this.throwing = throwing;582this.publisher = publisher;583}584585@Override586public long contentLength() {587return publisher.contentLength();588}589590@Override591public void subscribe(Flow.Subscriber<? super ByteBuffer> subscriber) {592try {593throwing.accept(Where.BEFORE_SUBSCRIBE);594publisher.subscribe(new SubscriberWrapper(subscriber));595subscribedCF.complete(null);596throwing.accept(Where.AFTER_SUBSCRIBE);597} catch (Throwable t) {598subscribedCF.completeExceptionally(t);599throw t;600}601}602603CompletableFuture<Void> subscribedCF() {604return subscribedCF;605}606607class SubscriptionWrapper implements Flow.Subscription {608final Flow.Subscription subscription;609final AtomicLong requestCount = new AtomicLong();610SubscriptionWrapper(Flow.Subscription subscription) {611this.subscription = subscription;612}613@Override614public void request(long n) {615long count = requestCount.incrementAndGet();616System.out.printf("%s request-%d(%d)%n", now(), count, n);617if (count > 1) throwing.accept(Where.BEFORE_NEXT_REQUEST);618throwing.accept(Where.BEFORE_REQUEST);619subscription.request(n);620throwing.accept(Where.AFTER_REQUEST);621if (count > 1) throwing.accept(Where.AFTER_NEXT_REQUEST);622}623624@Override625public void cancel() {626throwing.accept(Where.BEFORE_CANCEL);627subscription.cancel();628throwing.accept(Where.AFTER_CANCEL);629}630}631632class SubscriberWrapper implements Flow.Subscriber<ByteBuffer> {633final Flow.Subscriber<? super ByteBuffer> subscriber;634SubscriberWrapper(Flow.Subscriber<? super ByteBuffer> subscriber) {635this.subscriber = subscriber;636}637@Override638public void onSubscribe(Flow.Subscription subscription) {639subscriber.onSubscribe(new SubscriptionWrapper(subscription));640}641@Override642public void onNext(ByteBuffer item) {643subscriber.onNext(item);644}645@Override646public void onComplete() {647subscriber.onComplete();648}649650@Override651public void onError(Throwable throwable) {652subscriber.onError(throwable);653}654}655}656657658@BeforeTest659public void setup() throws Exception {660sslContext = new SimpleSSLContext().get();661if (sslContext == null)662throw new AssertionError("Unexpected null sslContext");663664// HTTP/1.1665HttpTestHandler h1_fixedLengthHandler = new HTTP_FixedLengthHandler();666HttpTestHandler h1_chunkHandler = new HTTP_ChunkedHandler();667InetSocketAddress sa = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);668httpTestServer = HttpTestServer.of(HttpServer.create(sa, 0));669httpTestServer.addHandler(h1_fixedLengthHandler, "/http1/fixed");670httpTestServer.addHandler(h1_chunkHandler, "/http1/chunk");671httpURI_fixed = "http://" + httpTestServer.serverAuthority() + "/http1/fixed/x";672httpURI_chunk = "http://" + httpTestServer.serverAuthority() + "/http1/chunk/x";673674HttpsServer httpsServer = HttpsServer.create(sa, 0);675httpsServer.setHttpsConfigurator(new HttpsConfigurator(sslContext));676httpsTestServer = HttpTestServer.of(httpsServer);677httpsTestServer.addHandler(h1_fixedLengthHandler, "/https1/fixed");678httpsTestServer.addHandler(h1_chunkHandler, "/https1/chunk");679httpsURI_fixed = "https://" + httpsTestServer.serverAuthority() + "/https1/fixed/x";680httpsURI_chunk = "https://" + httpsTestServer.serverAuthority() + "/https1/chunk/x";681682// HTTP/2683HttpTestHandler h2_fixedLengthHandler = new HTTP_FixedLengthHandler();684HttpTestHandler h2_chunkedHandler = new HTTP_ChunkedHandler();685686http2TestServer = HttpTestServer.of(new Http2TestServer("localhost", false, 0));687http2TestServer.addHandler(h2_fixedLengthHandler, "/http2/fixed");688http2TestServer.addHandler(h2_chunkedHandler, "/http2/chunk");689http2URI_fixed = "http://" + http2TestServer.serverAuthority() + "/http2/fixed/x";690http2URI_chunk = "http://" + http2TestServer.serverAuthority() + "/http2/chunk/x";691692https2TestServer = HttpTestServer.of(new Http2TestServer("localhost", true, sslContext));693https2TestServer.addHandler(h2_fixedLengthHandler, "/https2/fixed");694https2TestServer.addHandler(h2_chunkedHandler, "/https2/chunk");695https2URI_fixed = "https://" + https2TestServer.serverAuthority() + "/https2/fixed/x";696https2URI_chunk = "https://" + https2TestServer.serverAuthority() + "/https2/chunk/x";697698serverCount.addAndGet(4);699httpTestServer.start();700httpsTestServer.start();701http2TestServer.start();702https2TestServer.start();703}704705@AfterTest706public void teardown() throws Exception {707String sharedClientName =708sharedClient == null ? null : sharedClient.toString();709sharedClient = null;710Thread.sleep(100);711AssertionError fail = TRACKER.check(500);712try {713httpTestServer.stop();714httpsTestServer.stop();715http2TestServer.stop();716https2TestServer.stop();717} finally {718if (fail != null) {719if (sharedClientName != null) {720System.err.println("Shared client name is: " + sharedClientName);721}722throw fail;723}724}725}726727static class HTTP_FixedLengthHandler implements HttpTestHandler {728@Override729public void handle(HttpTestExchange t) throws IOException {730out.println("HTTP_FixedLengthHandler received request to " + t.getRequestURI());731byte[] resp;732try (InputStream is = t.getRequestBody()) {733resp = is.readAllBytes();734}735t.sendResponseHeaders(200, resp.length); //fixed content length736try (OutputStream os = t.getResponseBody()) {737os.write(resp);738}739}740}741742static class HTTP_ChunkedHandler implements HttpTestHandler {743@Override744public void handle(HttpTestExchange t) throws IOException {745out.println("HTTP_ChunkedHandler received request to " + t.getRequestURI());746byte[] resp;747try (InputStream is = t.getRequestBody()) {748resp = is.readAllBytes();749}750t.sendResponseHeaders(200, -1); // chunked/variable751try (OutputStream os = t.getResponseBody()) {752os.write(resp);753}754}755}756757}758759760