changeset 51315:4254bed3c09d

8204679: HTTP Client refresh Reviewed-by: chegar, dfuchs, michaelm Contributed-by: Chris Hegarty <chris.hegarty@oracle.com>, Daniel Fuchs <daniel.fuchs@oracle.com>, Michael McMahon <michael.x.mcmahon@oracle.com>, Pavel Rappo <pavel.rappo@oracle.com>
author chegar
date Wed, 20 Jun 2018 09:05:57 -0700
parents 818a23db260c
children 9663777e8d41
files src/java.net.http/share/classes/java/net/http/HttpClient.java src/java.net.http/share/classes/java/net/http/HttpHeaders.java src/java.net.http/share/classes/java/net/http/HttpRequest.java src/java.net.http/share/classes/java/net/http/HttpResponse.java src/java.net.http/share/classes/java/net/http/WebSocket.java src/java.net.http/share/classes/java/net/http/WebSocketHandshakeException.java src/java.net.http/share/classes/java/net/http/package-info.java src/java.net.http/share/classes/jdk/internal/net/http/AuthenticationFilter.java src/java.net.http/share/classes/jdk/internal/net/http/ConnectionPool.java src/java.net.http/share/classes/jdk/internal/net/http/CookieFilter.java src/java.net.http/share/classes/jdk/internal/net/http/Exchange.java src/java.net.http/share/classes/jdk/internal/net/http/ExchangeImpl.java src/java.net.http/share/classes/jdk/internal/net/http/Http1AsyncReceiver.java src/java.net.http/share/classes/jdk/internal/net/http/Http1Exchange.java src/java.net.http/share/classes/jdk/internal/net/http/Http1HeaderParser.java src/java.net.http/share/classes/jdk/internal/net/http/Http1Request.java src/java.net.http/share/classes/jdk/internal/net/http/Http1Response.java src/java.net.http/share/classes/jdk/internal/net/http/Http2ClientImpl.java src/java.net.http/share/classes/jdk/internal/net/http/Http2Connection.java src/java.net.http/share/classes/jdk/internal/net/http/HttpClientImpl.java src/java.net.http/share/classes/jdk/internal/net/http/HttpConnection.java src/java.net.http/share/classes/jdk/internal/net/http/HttpRequestBuilderImpl.java src/java.net.http/share/classes/jdk/internal/net/http/HttpRequestImpl.java src/java.net.http/share/classes/jdk/internal/net/http/ImmutableHeaders.java src/java.net.http/share/classes/jdk/internal/net/http/ImmutableHttpRequest.java src/java.net.http/share/classes/jdk/internal/net/http/MultiExchange.java src/java.net.http/share/classes/jdk/internal/net/http/PlainHttpConnection.java src/java.net.http/share/classes/jdk/internal/net/http/RedirectFilter.java src/java.net.http/share/classes/jdk/internal/net/http/Response.java src/java.net.http/share/classes/jdk/internal/net/http/ResponseContent.java src/java.net.http/share/classes/jdk/internal/net/http/SocketTube.java src/java.net.http/share/classes/jdk/internal/net/http/Stream.java src/java.net.http/share/classes/jdk/internal/net/http/common/ConnectionExpiredException.java src/java.net.http/share/classes/jdk/internal/net/http/common/HttpHeadersBuilder.java src/java.net.http/share/classes/jdk/internal/net/http/common/HttpHeadersImpl.java src/java.net.http/share/classes/jdk/internal/net/http/common/Log.java src/java.net.http/share/classes/jdk/internal/net/http/common/SSLFlowDelegate.java src/java.net.http/share/classes/jdk/internal/net/http/common/SSLTube.java src/java.net.http/share/classes/jdk/internal/net/http/common/SubscriberWrapper.java src/java.net.http/share/classes/jdk/internal/net/http/common/Utils.java src/java.net.http/share/classes/jdk/internal/net/http/frame/SettingsFrame.java src/java.net.http/share/classes/jdk/internal/net/http/hpack/Decoder.java src/java.net.http/share/classes/jdk/internal/net/http/hpack/Encoder.java src/java.net.http/share/classes/jdk/internal/net/http/hpack/HPACK.java src/java.net.http/share/classes/jdk/internal/net/http/hpack/HeaderTable.java src/java.net.http/share/classes/jdk/internal/net/http/hpack/ISO_8859_1.java src/java.net.http/share/classes/jdk/internal/net/http/hpack/QuickHuffman.java src/java.net.http/share/classes/jdk/internal/net/http/hpack/SimpleHeaderTable.java src/java.net.http/share/classes/jdk/internal/net/http/websocket/OpeningHandshake.java src/java.net.http/share/classes/jdk/internal/net/http/websocket/WebSocketImpl.java src/jdk.httpserver/share/classes/sun/net/httpserver/ExchangeImpl.java test/jdk/java/net/httpclient/AbstractNoBody.java test/jdk/java/net/httpclient/AbstractThrowingPublishers.java test/jdk/java/net/httpclient/AbstractThrowingPushPromises.java test/jdk/java/net/httpclient/AbstractThrowingSubscribers.java test/jdk/java/net/httpclient/AsFileDownloadTest.java test/jdk/java/net/httpclient/BasicRedirectTest.java test/jdk/java/net/httpclient/BodyProcessorInputStreamTest.java test/jdk/java/net/httpclient/ConcurrentResponses.java test/jdk/java/net/httpclient/ConnectExceptionTest.java test/jdk/java/net/httpclient/CookieHeaderTest.java test/jdk/java/net/httpclient/CustomRequestPublisher.java test/jdk/java/net/httpclient/CustomResponseSubscriber.java test/jdk/java/net/httpclient/DependentActionsTest.java test/jdk/java/net/httpclient/DependentPromiseActionsTest.java test/jdk/java/net/httpclient/DigestEchoServer.java test/jdk/java/net/httpclient/EncodedCharsInURI.java test/jdk/java/net/httpclient/EscapedOctetsInURI.java test/jdk/java/net/httpclient/FlowAdapterPublisherTest.java test/jdk/java/net/httpclient/FlowAdapterSubscriberTest.java test/jdk/java/net/httpclient/HandshakeFailureTest.java test/jdk/java/net/httpclient/HeadTest.java test/jdk/java/net/httpclient/HeadersTest.java test/jdk/java/net/httpclient/HttpClientBuilderTest.java test/jdk/java/net/httpclient/HttpHeadersOf.java test/jdk/java/net/httpclient/HttpInputStreamTest.java test/jdk/java/net/httpclient/HttpServerAdapters.java test/jdk/java/net/httpclient/ImmutableFlowItems.java test/jdk/java/net/httpclient/InvalidInputStreamSubscriptionRequest.java test/jdk/java/net/httpclient/InvalidSSLContextTest.java test/jdk/java/net/httpclient/InvalidSubscriptionRequest.java test/jdk/java/net/httpclient/LineBodyHandlerTest.java test/jdk/java/net/httpclient/MappingResponseSubscriber.java test/jdk/java/net/httpclient/MaxStreams.java test/jdk/java/net/httpclient/MethodsTest.java test/jdk/java/net/httpclient/NonAsciiCharsInURI.java test/jdk/java/net/httpclient/RedirectMethodChange.java test/jdk/java/net/httpclient/RedirectWithCookie.java test/jdk/java/net/httpclient/RequestBuilderTest.java test/jdk/java/net/httpclient/ResponseBodyBeforeError.java test/jdk/java/net/httpclient/ResponsePublisher.java test/jdk/java/net/httpclient/RetryPost.java test/jdk/java/net/httpclient/RetryWithCookie.java test/jdk/java/net/httpclient/ShortResponseBody.java test/jdk/java/net/httpclient/ShortResponseBodyWithRetry.java test/jdk/java/net/httpclient/SpecialHeadersTest.java test/jdk/java/net/httpclient/SplitResponse.java test/jdk/java/net/httpclient/SplitResponseAsync.java test/jdk/java/net/httpclient/SplitResponseKeepAlive.java test/jdk/java/net/httpclient/SplitResponseKeepAliveAsync.java test/jdk/java/net/httpclient/SplitResponseSSL.java test/jdk/java/net/httpclient/SplitResponseSSLAsync.java test/jdk/java/net/httpclient/SplitResponseSSLKeepAlive.java test/jdk/java/net/httpclient/SplitResponseSSLKeepAliveAsync.java test/jdk/java/net/httpclient/StreamingBody.java test/jdk/java/net/httpclient/ThrowingPublishers.java test/jdk/java/net/httpclient/ThrowingPublishersCustomAfterCancel.java test/jdk/java/net/httpclient/ThrowingPublishersCustomBeforeCancel.java test/jdk/java/net/httpclient/ThrowingPublishersIOAfterCancel.java test/jdk/java/net/httpclient/ThrowingPublishersIOBeforeCancel.java test/jdk/java/net/httpclient/ThrowingPublishersInNextRequest.java test/jdk/java/net/httpclient/ThrowingPublishersInRequest.java test/jdk/java/net/httpclient/ThrowingPublishersInSubscribe.java test/jdk/java/net/httpclient/ThrowingPublishersSanity.java test/jdk/java/net/httpclient/ThrowingPushPromises.java test/jdk/java/net/httpclient/ThrowingPushPromisesAsInputStreamCustom.java test/jdk/java/net/httpclient/ThrowingPushPromisesAsInputStreamIO.java test/jdk/java/net/httpclient/ThrowingPushPromisesAsLinesCustom.java test/jdk/java/net/httpclient/ThrowingPushPromisesAsLinesIO.java test/jdk/java/net/httpclient/ThrowingPushPromisesAsStringCustom.java test/jdk/java/net/httpclient/ThrowingPushPromisesAsStringIO.java test/jdk/java/net/httpclient/ThrowingPushPromisesSanity.java test/jdk/java/net/httpclient/ThrowingSubscribers.java test/jdk/java/net/httpclient/ThrowingSubscribersAsInputStream.java test/jdk/java/net/httpclient/ThrowingSubscribersAsInputStreamAsync.java test/jdk/java/net/httpclient/ThrowingSubscribersAsLines.java test/jdk/java/net/httpclient/ThrowingSubscribersAsLinesAsync.java test/jdk/java/net/httpclient/ThrowingSubscribersAsString.java test/jdk/java/net/httpclient/ThrowingSubscribersAsStringAsync.java test/jdk/java/net/httpclient/ThrowingSubscribersSanity.java test/jdk/java/net/httpclient/UnauthorizedTest.java test/jdk/java/net/httpclient/examples/JavadocExamples.java test/jdk/java/net/httpclient/http2/BadHeadersTest.java test/jdk/java/net/httpclient/http2/BasicTest.java test/jdk/java/net/httpclient/http2/ContinuationFrameTest.java test/jdk/java/net/httpclient/http2/ErrorTest.java test/jdk/java/net/httpclient/http2/ImplicitPushCancel.java test/jdk/java/net/httpclient/http2/ServerPush.java test/jdk/java/net/httpclient/http2/ServerPushWithDiffTypes.java test/jdk/java/net/httpclient/http2/java.net.http/jdk/internal/net/http/hpack/HeaderTableTest.java test/jdk/java/net/httpclient/http2/java.net.http/jdk/internal/net/http/hpack/HuffmanTest.java test/jdk/java/net/httpclient/http2/java.net.http/jdk/internal/net/http/hpack/SimpleHeaderTableTest.java test/jdk/java/net/httpclient/http2/server/Http2EchoHandler.java test/jdk/java/net/httpclient/http2/server/Http2RedirectHandler.java test/jdk/java/net/httpclient/http2/server/Http2TestExchange.java test/jdk/java/net/httpclient/http2/server/Http2TestExchangeImpl.java test/jdk/java/net/httpclient/http2/server/Http2TestExchangeSupplier.java test/jdk/java/net/httpclient/http2/server/Http2TestServer.java test/jdk/java/net/httpclient/http2/server/Http2TestServerConnection.java test/jdk/java/net/httpclient/http2/server/OutgoingPushPromise.java test/jdk/java/net/httpclient/http2/server/PushHandler.java test/jdk/java/net/httpclient/noPermissions.policy test/jdk/java/net/httpclient/offline/FixedHttpHeaders.java test/jdk/java/net/httpclient/offline/OfflineTesting.java test/jdk/java/net/httpclient/ssltest/CertificateTest.java test/jdk/java/net/httpclient/websocket/BlowupOutputQueue.java test/jdk/java/net/httpclient/websocket/MockListener.java test/jdk/java/net/httpclient/websocket/WebSocketTest.java test/jdk/java/net/httpclient/whitebox/ConnectionPoolTestDriver.java test/jdk/java/net/httpclient/whitebox/java.net.http/jdk/internal/net/http/AuthenticationFilterTest.java test/jdk/java/net/httpclient/whitebox/java.net.http/jdk/internal/net/http/ConnectionPoolTest.java test/jdk/java/net/httpclient/whitebox/java.net.http/jdk/internal/net/http/Http1HeaderParserTest.java test/jdk/java/net/httpclient/whitebox/java.net.http/jdk/internal/net/http/RawChannelTest.java test/jdk/java/net/httpclient/whitebox/java.net.http/jdk/internal/net/http/SelectorTest.java test/jdk/lib/testlibrary/jdk/testlibrary/testkeys
diffstat 165 files changed, 10112 insertions(+), 3946 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.net.http/share/classes/java/net/http/HttpClient.java	Wed Jun 20 17:15:16 2018 +0200
+++ b/src/java.net.http/share/classes/java/net/http/HttpClient.java	Wed Jun 20 09:05:57 2018 -0700
@@ -57,7 +57,7 @@
  * and can be used to send multiple requests.
  *
  * <p> An {@code HttpClient} provides configuration information, and resource
- * sharing, for all requests send through it.
+ * sharing, for all requests sent through it.
  *
  * <p> A {@link BodyHandler BodyHandler} must be supplied for each {@link
  * HttpRequest} sent. The {@code BodyHandler} determines how to handle the
@@ -232,11 +232,10 @@
          *
          * <p> If this method is not invoked prior to {@linkplain #build()
          * building}, a default executor is created for each newly built {@code
-         * HttpClient}. The default executor uses a {@linkplain
-         * Executors#newCachedThreadPool(ThreadFactory) cached thread pool},
-         * with a custom thread factory.
+         * HttpClient}.
          *
-         * @implNote If a security manager has been installed, the thread
+         * @implNote The default executor uses a thread pool, with a custom
+         * thread factory. If a security manager has been installed, the thread
          * factory creates threads that run with an access control context that
          * has no permissions.
          *
@@ -451,7 +450,7 @@
      * then the response, containing the  {@code 3XX} response code, is returned,
      * where it can be handled manually.
      *
-     * <p> {@code Redirect} policy is set via the {@linkplain
+     * <p> {@code Redirect} policy is set through the {@linkplain
      * HttpClient.Builder#followRedirects(Redirect) Builder.followRedirects}
      * method.
      *
--- a/src/java.net.http/share/classes/java/net/http/HttpHeaders.java	Wed Jun 20 17:15:16 2018 +0200
+++ b/src/java.net.http/share/classes/java/net/http/HttpHeaders.java	Wed Jun 20 09:05:57 2018 -0700
@@ -25,62 +25,68 @@
 
 package java.net.http;
 
+import java.util.ArrayList;
 import java.util.List;
+import java.util.Locale;
 import java.util.Map;
 import java.util.Optional;
 import java.util.OptionalLong;
-import static java.util.Collections.emptyList;
-import static java.util.Collections.unmodifiableList;
+import java.util.TreeMap;
+import java.util.TreeSet;
+import java.util.function.BiPredicate;
+import static java.lang.String.CASE_INSENSITIVE_ORDER;
+import static java.util.Collections.unmodifiableMap;
 import static java.util.Objects.requireNonNull;
 
 /**
  * A read-only view of a set of HTTP headers.
  *
- * <p> An {@code HttpHeaders} is not created directly, but rather returned from
- * an {@link HttpResponse HttpResponse}. Specific HTTP headers can be set for
- * {@linkplain HttpRequest requests} through the one of the request builder's
- * {@link HttpRequest.Builder#header(String, String) headers} methods.
+ * <p> An {@code HttpHeaders} is not typically created directly, but rather
+ * returned from an {@link HttpRequest#headers() HttpRequest} or an
+ * {@link HttpResponse#headers() HttpResponse}. Specific HTTP headers can be
+ * set for a {@linkplain HttpRequest request} through one of the request
+ * builder's {@link HttpRequest.Builder#header(String, String) headers} methods.
  *
  * <p> The methods of this class ( that accept a String header name ), and the
- * Map returned by the {@link #map() map} method, operate without regard to
- * case when retrieving the header value.
+ * {@code Map} returned by the {@link #map() map} method, operate without regard
+ * to case when retrieving the header value(s).
+ *
+ * <p> An HTTP header name may appear more than once in the HTTP protocol. As
+ * such, headers are represented as a name and a list of values. Each occurrence
+ * of a header value is added verbatim, to the appropriate header name list,
+ * without interpreting its value. In particular, {@code HttpHeaders} does not
+ * perform any splitting or joining of comma separated header value strings. The
+ * order of elements in a header value list is preserved when {@link
+ * HttpRequest.Builder#header(String, String) building} a request. For
+ * responses, the order of elements in a header value list is the order in which
+ * they were received. The {@code Map} returned by the {@code map} method,
+ * however, does not provide any guarantee with regard to the ordering of its
+ * entries.
  *
  * <p> {@code HttpHeaders} instances are immutable.
  *
  * @since 11
  */
-public abstract class HttpHeaders {
+public final class HttpHeaders {
 
     /**
-     * Creates an HttpHeaders.
-     */
-    protected HttpHeaders() {}
-
-    /**
-     * Returns an {@link Optional} containing the first value of the given named
-     * (and possibly multi-valued) header. If the header is not present, then
-     * the returned {@code Optional} is empty.
-     *
-     * @implSpec
-     * The default implementation invokes
-     * {@code allValues(name).stream().findFirst()}
+     * Returns an {@link Optional} containing the first header string value of
+     * the given named (and possibly multi-valued) header. If the header is not
+     * present, then the returned {@code Optional} is empty.
      *
      * @param name the header name
-     * @return an {@code Optional<String>} for the first named value
+     * @return an {@code Optional<String>} containing the first named header
+     *         string value, if present
      */
     public Optional<String> firstValue(String name) {
         return allValues(name).stream().findFirst();
     }
 
     /**
-     * Returns an {@link OptionalLong} containing the first value of the
-     * named header field. If the header is not present, then the Optional is
-     * empty. If the header is present but contains a value that does not parse
-     * as a {@code Long} value, then an exception is thrown.
-     *
-     * @implSpec
-     * The default implementation invokes
-     * {@code allValues(name).stream().mapToLong(Long::valueOf).findFirst()}
+     * Returns an {@link OptionalLong} containing the first header string value
+     * of the named header field. If the header is not present, then the
+     * Optional is empty. If the header is present but contains a value that
+     * does not parse as a {@code Long} value, then an exception is thrown.
      *
      * @param name the header name
      * @return  an {@code OptionalLong}
@@ -92,23 +98,19 @@
     }
 
     /**
-     * Returns an unmodifiable List of all of the values of the given named
-     * header. Always returns a List, which may be empty if the header is not
-     * present.
-     *
-     * @implSpec
-     * The default implementation invokes, among other things, the
-     * {@code map().get(name)} to retrieve the list of header values.
+     * Returns an unmodifiable List of all of the header string values of the
+     * given named header. Always returns a List, which may be empty if the
+     * header is not present.
      *
      * @param name the header name
-     * @return a List of String values
+     * @return a List of headers string values
      */
     public List<String> allValues(String name) {
         requireNonNull(name);
         List<String> values = map().get(name);
         // Making unmodifiable list out of empty in order to make a list which
         // throws UOE unconditionally
-        return values != null ? values : unmodifiableList(emptyList());
+        return values != null ? values : List.of();
     }
 
     /**
@@ -116,7 +118,9 @@
      *
      * @return the Map
      */
-    public abstract Map<String, List<String>> map();
+    public Map<String,List<String>> map() {
+        return headers;
+    }
 
     /**
      * Tests this HTTP headers instance for equality with the given object.
@@ -149,7 +153,11 @@
      * @return the hash-code value for this HTTP headers
      */
     public final int hashCode() {
-        return map().hashCode();
+        int h = 0;
+        for (Map.Entry<String, List<String>> e : map().entrySet()) {
+            h += entryHash(e);
+        }
+        return h;
     }
 
     /**
@@ -165,4 +173,93 @@
         sb.append(" }");
         return sb.toString();
     }
+
+    /**
+     * Returns an HTTP headers from the given map. The given map's key
+     * represents the header name, and its value the list of string header
+     * values for that header name.
+     *
+     * <p> An HTTP header name may appear more than once in the HTTP protocol.
+     * Such, <i>multi-valued</i>, headers must be represented by a single entry
+     * in the given map, whose entry value is a list that represents the
+     * multiple header string values. Leading and trailing whitespaces are
+     * removed from all string values retrieved from the given map and its lists
+     * before processing. Only headers that, after filtering, contain at least
+     * one, possibly empty string, value will be added to the HTTP headers.
+     *
+     * @apiNote The primary purpose of this method is for testing frameworks.
+     * Per-request headers can be set through one of the {@code HttpRequest}
+     * {@link HttpRequest.Builder#header(String, String) headers} methods.
+     *
+     * @param headerMap the map containing the header names and values
+     * @param filter a filter that can be used to inspect each
+     *               header-name-and-value pair in the given map to determine if
+     *               it should, or should not, be added to the to the HTTP
+     *               headers
+     * @return an HTTP headers instance containing the given headers
+     * @throws NullPointerException if any of: {@code headerMap}, a key or value
+     *        in the given map, or an entry in the map's value list, or
+     *        {@code filter}, is {@code null}
+     * @throws IllegalArgumentException if the given {@code headerMap} contains
+     *         any two keys that are equal ( without regard to case ); or if the
+     *         given map contains any key whose length, after trimming
+     *         whitespaces, is {@code 0}
+     */
+    public static HttpHeaders of(Map<String,List<String>> headerMap,
+                                 BiPredicate<String,String> filter) {
+        requireNonNull(headerMap);
+        requireNonNull(filter);
+        return headersOf(headerMap, filter);
+    }
+
+    // --
+
+    private static final HttpHeaders NO_HEADERS = new HttpHeaders(Map.of());
+
+    private final Map<String,List<String>> headers;
+
+    private HttpHeaders(Map<String,List<String>> headers) {
+        this.headers = headers;
+    }
+
+    private static final int entryHash(Map.Entry<String, List<String>> e) {
+        String key = e.getKey();
+        List<String> value = e.getValue();
+        // we know that by construction key and values can't be null
+        int keyHash = key.toLowerCase(Locale.ROOT).hashCode();
+        int valueHash = value.hashCode();
+        return keyHash ^ valueHash;
+    }
+
+    // Returns a new HTTP headers after performing a structural copy and filtering.
+    private static HttpHeaders headersOf(Map<String,List<String>> map,
+                                         BiPredicate<String,String> filter) {
+        TreeMap<String,List<String>> other = new TreeMap<>(CASE_INSENSITIVE_ORDER);
+        TreeSet<String> notAdded = new TreeSet<>(CASE_INSENSITIVE_ORDER);
+        ArrayList<String> tempList = new ArrayList<>();
+        map.forEach((key, value) -> {
+            String headerName = requireNonNull(key).trim();
+            if (headerName.isEmpty()) {
+                throw new IllegalArgumentException("empty key");
+            }
+            List<String> headerValues = requireNonNull(value);
+            headerValues.forEach(headerValue -> {
+                headerValue = requireNonNull(headerValue).trim();
+                if (filter.test(headerName, headerValue)) {
+                    tempList.add(headerValue);
+                }
+            });
+
+            if (tempList.isEmpty()) {
+                if (other.containsKey(headerName)
+                        || notAdded.contains(headerName.toLowerCase(Locale.ROOT)))
+                    throw new IllegalArgumentException("duplicate key: " + headerName);
+                notAdded.add(headerName.toLowerCase(Locale.ROOT));
+            } else if (other.put(headerName, List.copyOf(tempList)) != null) {
+                throw new IllegalArgumentException("duplicate key: " + headerName);
+            }
+            tempList.clear();
+        });
+        return other.isEmpty() ? NO_HEADERS : new HttpHeaders(unmodifiableMap(other));
+    }
 }
--- a/src/java.net.http/share/classes/java/net/http/HttpRequest.java	Wed Jun 20 17:15:16 2018 +0200
+++ b/src/java.net.http/share/classes/java/net/http/HttpRequest.java	Wed Jun 20 09:05:57 2018 -0700
@@ -89,10 +89,12 @@
      * <p> Instances of {@code HttpRequest.Builder} are created by calling {@link
      * HttpRequest#newBuilder(URI)} or {@link HttpRequest#newBuilder()}.
      *
-     * <p> Each of the setter methods modifies the state of the builder
-     * and returns the same instance. The methods are not synchronized and
-     * should not be called from multiple threads without external
-     * synchronization. The {@link #build() build} method returns a new
+     * <p> The builder can be used to configure per-request state, such as: the
+     * request URI, the request method (default is GET unless explicitly set),
+     * specific request headers, etc. Each of the setter methods modifies the
+     * state of the builder and returns the same instance. The methods are not
+     * synchronized and should not be called from multiple threads without
+     * external synchronization. The {@link #build() build} method returns a new
      * {@code HttpRequest} each time it is invoked. Once built an {@code
      * HttpRequest} is immutable, and can be sent multiple times.
      *
--- a/src/java.net.http/share/classes/java/net/http/HttpResponse.java	Wed Jun 20 17:15:16 2018 +0200
+++ b/src/java.net.http/share/classes/java/net/http/HttpResponse.java	Wed Jun 20 09:05:57 2018 -0700
@@ -830,13 +830,13 @@
      * BodySubscriber} provides implementations of many common body subscribers.
      *
      * <p> The object acts as a {@link Flow.Subscriber}&lt;{@link List}&lt;{@link
-     * ByteBuffer}&gt;&gt; to the HTTP client implementation, which publishes
-     * unmodifiable lists of read-only ByteBuffers containing the response body.
-     * The Flow of data, as well as the order of ByteBuffers in the Flow lists,
-     * is a strictly ordered representation of the response body. Both the Lists
-     * and the ByteBuffers, once passed to the subscriber, are no longer used by
-     * the HTTP client. The subscriber converts the incoming buffers of data to
-     * some higher-level Java type {@code T}.
+     * ByteBuffer}&gt;&gt; to the HTTP Client implementation, which publishes
+     * lists of ByteBuffers containing the response body. The Flow of data, as
+     * well as the order of ByteBuffers in the Flow lists, is a strictly ordered
+     * representation of the response body. Both the Lists and the ByteBuffers,
+     * once passed to the subscriber, are no longer used by the HTTP Client. The
+     * subscriber converts the incoming buffers of data to some higher-level
+     * Java type {@code T}.
      *
      * <p> The {@link #getBody()} method returns a
      * {@link CompletionStage}&lt;{@code T}&gt; that provides the response body
@@ -859,6 +859,9 @@
      * may cause the underlying HTTP connection to be closed and prevent it
      * from being reused for subsequent operations.
      *
+     * @implNote The flow of data containing the response body is immutable.
+     * Specifically, it is a flow of unmodifiable lists of read-only ByteBuffers.
+     *
      * @param <T> the response body type
      * @see BodySubscribers
      * @since 11
@@ -888,20 +891,20 @@
      *
      * <pre>{@code    // Streams the response body to a File
      *   HttpResponse<byte[]> response = client
-     *     .send(request, (statusCode, responseHeaders) -> BodySubscribers.ofByteArray());
+     *     .send(request, responseInfo -> BodySubscribers.ofByteArray());
      *
      *   // Accumulates the response body and returns it as a byte[]
      *   HttpResponse<byte[]> response = client
-     *     .send(request, (statusCode, responseHeaders) -> BodySubscribers.ofByteArray());
+     *     .send(request, responseInfo -> BodySubscribers.ofByteArray());
      *
      *   // Discards the response body
      *   HttpResponse<Void> response = client
-     *     .send(request, (statusCode, responseHeaders) -> BodySubscribers.discarding());
+     *     .send(request, responseInfo -> BodySubscribers.discarding());
      *
      *   // Accumulates the response body as a String then maps it to its bytes
      *   HttpResponse<byte[]> response = client
-     *     .send(request, (sc, hdrs) ->
-     *        BodySubscribers.mapping(BodySubscribers.ofString(), String::getBytes));
+     *     .send(request, responseInfo ->
+     *        BodySubscribers.mapping(BodySubscribers.ofString(UTF_8), String::getBytes));
      * }</pre>
      *
      * @since 11
--- a/src/java.net.http/share/classes/java/net/http/WebSocket.java	Wed Jun 20 17:15:16 2018 +0200
+++ b/src/java.net.http/share/classes/java/net/http/WebSocket.java	Wed Jun 20 09:05:57 2018 -0700
@@ -35,9 +35,9 @@
 /**
  * A WebSocket Client.
  *
- * <p> {@code WebSocket} instances can be created via {@link WebSocket.Builder}.
+ * <p> {@code WebSocket} instances are created through {@link WebSocket.Builder}.
  *
- * <p> WebSocket has an input and an output sides. These sides are independent
+ * <p> WebSocket has an input and an output side. These sides are independent
  * from each other. A side can either be open or closed. Once closed, the side
  * remains closed. WebSocket messages are sent through a {@code WebSocket} and
  * received through a {@code WebSocket.Listener} associated with it. Messages
@@ -55,21 +55,22 @@
  *
  * <p> A receive method is any of the {@code onText}, {@code onBinary},
  * {@code onPing}, {@code onPong} and {@code onClose} methods of
- * {@code Listener}. A receive method initiates a receive operation and returns
- * a {@code CompletionStage} which completes once the operation has completed.
+ * {@code Listener}. WebSocket initiates a receive operation by invoking a
+ * receive method on the listener. The listener then must return a
+ * {@code CompletionStage} which completes once the operation has completed.
  *
- * <p> A WebSocket maintains an <a id="counter">internal counter</a>.
- * This counter's value is a number of times the WebSocket has yet to invoke a
- * receive method. While this counter is zero the WebSocket does not invoke
- * receive methods. The counter is incremented by {@code n} when {@code
- * request(n)} is called. The counter is decremented by one when the WebSocket
- * invokes a receive method. {@code onOpen} and {@code onError} are not receive
- * methods. WebSocket invokes {@code onOpen} prior to any other methods on the
- * listener. WebSocket invokes {@code onOpen} at most once. WebSocket may invoke
- * {@code onError} at any given time. If the WebSocket invokes {@code onError}
- * or {@code onClose}, then no further listener's methods will be invoked, no
- * matter the value of the counter. For a newly built WebSocket the counter is
- * zero. A WebSocket invokes methods on the listener in a thread-safe manner.
+ * <p> To control receiving of messages, a WebSocket maintains an
+ * <a id="counter">internal counter</a>. This counter's value is a number of
+ * times the WebSocket has yet to invoke a receive method. While this counter is
+ * zero the WebSocket does not invoke receive methods. The counter is
+ * incremented by {@code n} when {@code request(n)} is called. The counter is
+ * decremented by one when the WebSocket invokes a receive method.
+ * {@code onOpen} and {@code onError} are not receive methods. WebSocket invokes
+ * {@code onOpen} prior to any other methods on the listener. WebSocket invokes
+ * {@code onOpen} at most once. WebSocket may invoke {@code onError} at any
+ * given time. If the WebSocket invokes {@code onError} or {@code onClose}, then
+ * no further listener's methods will be invoked, no matter the value of the
+ * counter. For a newly built WebSocket the counter is zero.
  *
  * <p> Unless otherwise stated, {@code null} arguments will cause methods
  * of {@code WebSocket} to throw {@code NullPointerException}, similarly,
@@ -105,13 +106,13 @@
     /**
      * A builder of {@linkplain WebSocket WebSocket Clients}.
      *
-     * <p> A builder can be created by invoking the
-     * {@link HttpClient#newWebSocketBuilder HttpClient.newWebSocketBuilder}
-     * method. The intermediate (setter-like) methods change the state of the
-     * builder and return the same builder they have been invoked on. If an
-     * intermediate method is not invoked, an appropriate default value (or
-     * behavior) will be assumed. A {@code Builder} is not safe for use by
-     * multiple threads without external synchronization.
+     * <p> Builders are created by invoking
+     * {@link HttpClient#newWebSocketBuilder HttpClient.newWebSocketBuilder}.
+     * The intermediate (setter-like) methods change the state of the builder
+     * and return the same builder they have been invoked on. If an intermediate
+     * method is not invoked, an appropriate default value (or behavior) will be
+     * assumed. A {@code Builder} is not safe for use by multiple threads
+     * without external synchronization.
      *
      * @since 11
      */
@@ -155,7 +156,7 @@
          * Sets a request for the given subprotocols.
          *
          * <p> After the {@code WebSocket} has been built, the actual
-         * subprotocol can be queried via
+         * subprotocol can be queried through
          * {@link WebSocket#getSubprotocol WebSocket.getSubprotocol()}.
          *
          * <p> Subprotocols are specified in the order of preference. The most
@@ -218,11 +219,17 @@
      * The receiving interface of {@code WebSocket}.
      *
      * <p> A {@code WebSocket} invokes methods of the associated listener
-     * passing itself as an argument. When data has been received, the
-     * {@code WebSocket} invokes a receive method. Methods {@code onText},
-     * {@code onBinary}, {@code onPing} and {@code onPong} must return a
-     * {@code CompletionStage} that completes once the message has been received
-     * by the listener.
+     * passing itself as an argument. These methods are invoked in a thread-safe
+     * manner, such that the next invocation may start only after the previous
+     * one has finished.
+     *
+     * <p> When data has been received, the {@code WebSocket} invokes a receive
+     * method. Methods {@code onText}, {@code onBinary}, {@code onPing} and
+     * {@code onPong} must return a {@code CompletionStage} that completes once
+     * the message has been received by the listener. If a listener's method
+     * returns {@code null} rather than a {@code CompletionStage},
+     * {@code WebSocket} will behave as if the listener returned a
+     * {@code CompletionStage} that is already completed normally.
      *
      * <p> An {@code IOException} raised in {@code WebSocket} will result in an
      * invocation of {@code onError} with that exception (if the input is not
@@ -231,11 +238,14 @@
      * exceptionally, the WebSocket will invoke {@code onError} with this
      * exception.
      *
-     * <p> If a listener's method returns {@code null} rather than a
-     * {@code CompletionStage}, {@code WebSocket} will behave as if the listener
-     * returned a {@code CompletionStage} that is already completed normally.
+     * @apiNote The strict sequential order of invocations from
+     * {@code WebSocket} to {@code Listener} means, in particular, that the
+     * {@code Listener}'s methods are treated as non-reentrant. This means that
+     * {@code Listener} implementations do not need to be concerned with
+     * possible recursion or the order in which they invoke
+     * {@code WebSocket.request} in relation to their processing logic.
      *
-     * @apiNote Careful attention may be required if a listener is associated
+     * <p> Careful attention may be required if a listener is associated
      * with more than a single {@code WebSocket}. In this case invocations
      * related to different instances of {@code WebSocket} may not be ordered
      * and may even happen concurrently.
--- a/src/java.net.http/share/classes/java/net/http/WebSocketHandshakeException.java	Wed Jun 20 17:15:16 2018 +0200
+++ b/src/java.net.http/share/classes/java/net/http/WebSocketHandshakeException.java	Wed Jun 20 09:05:57 2018 -0700
@@ -28,7 +28,7 @@
 import java.io.IOException;
 
 /**
- * An exception used to signal the opening handshake failed.
+ * Thrown when the opening handshake has failed.
  *
  * @since 11
  */
@@ -55,6 +55,10 @@
      * <p> The value may be unavailable ({@code null}) if this exception has
      * been serialized and then deserialized.
      *
+     * @apiNote The primary purpose of this method is to allow programmatic
+     * examination of the reasons behind the failure of the opening handshake.
+     * Some of these reasons might allow recovery.
+     *
      * @return server response
      */
     public HttpResponse<?> getResponse() {
--- a/src/java.net.http/share/classes/java/net/http/package-info.java	Wed Jun 20 17:15:16 2018 +0200
+++ b/src/java.net.http/share/classes/java/net/http/package-info.java	Wed Jun 20 09:05:57 2018 -0700
@@ -42,20 +42,24 @@
  * Hypertext Transfer Protocol (HTTP/1.1)</a>, and
  * <a href="https://tools.ietf.org/html/rfc6455">The WebSocket Protocol</a>.
  *
- * <p> Asynchronous tasks and dependent actions of returned {@link
- * java.util.concurrent.CompletableFuture} instances are executed on the threads
- * supplied by the client's {@link java.util.concurrent.Executor}, where
- * practical.
+ * <p> In general, asynchronous tasks execute in either the thread invoking
+ * the operation, e.g. {@linkplain HttpClient#send(HttpRequest, BodyHandler)
+ * sending} an HTTP request, or by the threads supplied by the client's {@link
+ * HttpClient#executor() executor}. Dependent tasks, those that are triggered by
+ * returned CompletionStages or CompletableFutures, that do not explicitly
+ * specify an executor, execute in the same {@link
+ * CompletableFuture#defaultExecutor() default executor} as that of {@code
+ * CompletableFuture}, or the invoking thread if the operation completes before
+ * the dependent task is registered.
  *
  * <p> {@code CompletableFuture}s returned by this API will throw {@link
- * java.lang.UnsupportedOperationException} for their {@link
- * java.util.concurrent.CompletableFuture#obtrudeValue(Object) obtrudeValue}
- * and {@link java.util.concurrent.CompletableFuture#obtrudeException(Throwable)
- * obtrudeException} methods. Invoking the {@link
- * java.util.concurrent.CompletableFuture#cancel cancel} method on a {@code
- * CompletableFuture} returned by this API will not interrupt the underlying
- * operation, but may be useful to complete, exceptionally, dependent stages
- * that have not already completed.
+ * UnsupportedOperationException} for their {@link
+ * CompletableFuture#obtrudeValue(Object) obtrudeValue}
+ * and {@link CompletableFuture#obtrudeException(Throwable)
+ * obtrudeException} methods. Invoking the {@link CompletableFuture#cancel
+ * cancel} method on a {@code CompletableFuture} returned by this API may not
+ * interrupt the underlying operation, but may be useful to complete,
+ * exceptionally, dependent stages that have not already completed.
  *
  * <p> Unless otherwise stated, {@code null} parameter values will cause methods
  * of all classes in this package to throw {@code NullPointerException}.
@@ -63,3 +67,9 @@
  * @since 11
  */
 package java.net.http;
+
+import java.lang.UnsupportedOperationException;
+import java.net.http.HttpClient;
+import java.net.http.HttpRequest;
+import java.net.http.HttpResponse.BodyHandler;
+import java.util.concurrent.CompletableFuture;
--- a/src/java.net.http/share/classes/jdk/internal/net/http/AuthenticationFilter.java	Wed Jun 20 17:15:16 2018 +0200
+++ b/src/java.net.http/share/classes/jdk/internal/net/http/AuthenticationFilter.java	Wed Jun 20 09:05:57 2018 -0700
@@ -34,7 +34,6 @@
 import java.net.URL;
 import java.util.Base64;
 import java.util.LinkedList;
-import java.util.List;
 import java.util.Objects;
 import java.util.WeakHashMap;
 import java.net.http.HttpHeaders;
@@ -59,9 +58,9 @@
     static final int UNAUTHORIZED = 401;
     static final int PROXY_UNAUTHORIZED = 407;
 
-    private static final List<String> BASIC_DUMMY =
-            List.of("Basic " + Base64.getEncoder()
-                    .encodeToString("o:o".getBytes(ISO_8859_1)));
+    private static final String BASIC_DUMMY =
+            "Basic " + Base64.getEncoder()
+                    .encodeToString("o:o".getBytes(ISO_8859_1));
 
     // A public no-arg constructor is required by FilterFactory
     public AuthenticationFilter() {}
@@ -182,14 +181,12 @@
         String value = "Basic " + s;
         if (proxy) {
             if (r.isConnect()) {
-                if (!Utils.PROXY_TUNNEL_FILTER
-                        .test(hdrname, List.of(value))) {
+                if (!Utils.PROXY_TUNNEL_FILTER.test(hdrname, value)) {
                     Log.logError("{0} disabled", hdrname);
                     return;
                 }
             } else if (r.proxy() != null) {
-                if (!Utils.PROXY_FILTER
-                        .test(hdrname, List.of(value))) {
+                if (!Utils.PROXY_FILTER.test(hdrname, value)) {
                     Log.logError("{0} disabled", hdrname);
                     return;
                 }
@@ -261,9 +258,16 @@
 
         boolean proxy = status == PROXY_UNAUTHORIZED;
         String authname = proxy ? "Proxy-Authenticate" : "WWW-Authenticate";
-        String authval = hdrs.firstValue(authname).orElseThrow(() -> {
-            return new IOException("Invalid auth header");
-        });
+        String authval = hdrs.firstValue(authname).orElse(null);
+        if (authval == null) {
+            if (exchange.client().authenticator().isPresent()) {
+                throw new IOException(authname + " header missing for response code " + status);
+            } else {
+                // No authenticator? let the caller deal with this.
+                return null;
+            }
+        }
+
         HeaderParser parser = new HeaderParser(authval);
         String scheme = parser.findKey(0);
 
--- a/src/java.net.http/share/classes/jdk/internal/net/http/ConnectionPool.java	Wed Jun 20 17:15:16 2018 +0200
+++ b/src/java.net.http/share/classes/jdk/internal/net/http/ConnectionPool.java	Wed Jun 20 09:05:57 2018 -0700
@@ -53,6 +53,8 @@
 
     static final long KEEP_ALIVE = Utils.getIntegerNetProperty(
             "jdk.httpclient.keepalive.timeout", 1200); // seconds
+    static final long MAX_POOL_SIZE = Utils.getIntegerNetProperty(
+            "jdk.httpclient.connectionPoolSize", 0); // unbounded
     final Logger debug = Utils.getDebugLogger(this::dbgString, Utils.DEBUG);
 
     // Pools of idle connections
@@ -160,6 +162,7 @@
         CleanupTrigger cleanup = registerCleanupTrigger(conn);
 
         // it's possible that cleanup may have been called.
+        HttpConnection toClose = null;
         synchronized(this) {
             if (cleanup.isDone()) {
                 return;
@@ -167,6 +170,10 @@
                 conn.close();
                 return;
             }
+            if (MAX_POOL_SIZE > 0 && expiryList.size() >= MAX_POOL_SIZE) {
+                toClose = expiryList.removeOldest();
+                if (toClose != null) removeFromPool(toClose);
+            }
             if (conn instanceof PlainHttpConnection) {
                 putConnection(conn, plainPool);
             } else {
@@ -175,6 +182,13 @@
             }
             expiryList.add(conn, now, keepAlive);
         }
+        if (toClose != null) {
+            if (debug.on()) {
+                debug.log("Maximum pool size reached: removing oldest connection %s",
+                          toClose.dbgString());
+            }
+            close(toClose);
+        }
         //System.out.println("Return to pool: " + conn);
     }
 
@@ -314,6 +328,8 @@
         private final LinkedList<ExpiryEntry> list = new LinkedList<>();
         private volatile boolean mayContainEntries;
 
+        int size() { return list.size(); }
+
         // A loosely accurate boolean whose value is computed
         // at the end of each operation performed on ExpiryList;
         // Does not require synchronizing on the ConnectionPool.
@@ -331,6 +347,13 @@
 
         // should only be called while holding a synchronization
         // lock on the ConnectionPool
+        HttpConnection removeOldest() {
+            ExpiryEntry entry = list.pollLast();
+            return entry == null ? null : entry.connection;
+        }
+
+        // should only be called while holding a synchronization
+        // lock on the ConnectionPool
         void add(HttpConnection conn) {
             add(conn, Instant.now(), KEEP_ALIVE);
         }
@@ -419,17 +442,38 @@
         }
     }
 
+    // Remove a connection from the pool.
+    // should only be called while holding a synchronization
+    // lock on the ConnectionPool
+    private void removeFromPool(HttpConnection c) {
+        assert Thread.holdsLock(this);
+        if (c instanceof PlainHttpConnection) {
+            removeFromPool(c, plainPool);
+        } else {
+            assert c.isSecure();
+            removeFromPool(c, sslPool);
+        }
+    }
+
+    // Used by tests
+    synchronized boolean contains(HttpConnection c) {
+        final CacheKey key = c.cacheKey();
+        List<HttpConnection> list;
+        if ((list = plainPool.get(key)) != null) {
+            if (list.contains(c)) return true;
+        }
+        if ((list = sslPool.get(key)) != null) {
+            if (list.contains(c)) return true;
+        }
+        return false;
+    }
+
     void cleanup(HttpConnection c, Throwable error) {
         if (debug.on())
             debug.log("%s : ConnectionPool.cleanup(%s)",
                     String.valueOf(c.getConnectionFlow()), error);
         synchronized(this) {
-            if (c instanceof PlainHttpConnection) {
-                removeFromPool(c, plainPool);
-            } else {
-                assert c.isSecure();
-                removeFromPool(c, sslPool);
-            }
+            removeFromPool(c);
             expiryList.remove(c);
         }
         c.close();
--- a/src/java.net.http/share/classes/jdk/internal/net/http/CookieFilter.java	Wed Jun 20 17:15:16 2018 +0200
+++ b/src/java.net.http/share/classes/jdk/internal/net/http/CookieFilter.java	Wed Jun 20 09:05:57 2018 -0700
@@ -31,7 +31,7 @@
 import java.util.Map;
 import java.util.Optional;
 import java.net.http.HttpHeaders;
-import jdk.internal.net.http.common.HttpHeadersImpl;
+import jdk.internal.net.http.common.HttpHeadersBuilder;
 import jdk.internal.net.http.common.Log;
 import jdk.internal.net.http.common.Utils;
 
@@ -50,7 +50,7 @@
             Map<String,List<String>> cookies = cookieHandler.get(r.uri(), userheaders);
 
             // add the returned cookies
-            HttpHeadersImpl systemHeaders = r.getSystemHeaders();
+            HttpHeadersBuilder systemHeadersBuilder = r.getSystemHeadersBuilder();
             if (cookies.isEmpty()) {
                 Log.logTrace("Request: no cookie to add for {0}", r.uri());
             } else {
@@ -65,7 +65,7 @@
                 if (values == null || values.isEmpty()) continue;
                 for (String val : values) {
                     if (Utils.isValidValue(val)) {
-                        systemHeaders.addHeader(hdrname, val);
+                        systemHeadersBuilder.addHeader(hdrname, val);
                     }
                 }
             }
--- a/src/java.net.http/share/classes/jdk/internal/net/http/Exchange.java	Wed Jun 20 17:15:16 2018 +0200
+++ b/src/java.net.http/share/classes/jdk/internal/net/http/Exchange.java	Wed Jun 20 09:05:57 2018 -0700
@@ -307,6 +307,7 @@
                                                     Function<ExchangeImpl<T>,CompletableFuture<Response>> andThen) {
         t = Utils.getCompletionCause(t);
         if (t instanceof ProxyAuthenticationRequired) {
+            if (debug.on()) debug.log("checkFor407: ProxyAuthenticationRequired: building synthetic response");
             bodyIgnored = MinimalFuture.completedFuture(null);
             Response proxyResponse = ((ProxyAuthenticationRequired)t).proxyResponse;
             HttpConnection c = ex == null ? null : ex.connection();
@@ -315,8 +316,10 @@
                     proxyResponse.version, true);
             return MinimalFuture.completedFuture(syntheticResponse);
         } else if (t != null) {
+            if (debug.on()) debug.log("checkFor407: no response - %s", t);
             return MinimalFuture.failedFuture(t);
         } else {
+            if (debug.on()) debug.log("checkFor407: all clear");
             return andThen.apply(ex);
         }
     }
@@ -332,6 +335,7 @@
             int rcode = r1.statusCode();
             if (rcode == 100) {
                 Log.logTrace("Received 100-Continue: sending body");
+                if (debug.on()) debug.log("Received 100-Continue for %s", r1);
                 CompletableFuture<Response> cf =
                         exchImpl.sendBodyAsync()
                                 .thenCompose(exIm -> exIm.getResponseAsync(parentExecutor));
@@ -341,6 +345,7 @@
             } else {
                 Log.logTrace("Expectation failed: Received {0}",
                         rcode);
+                if (debug.on()) debug.log("Expect-Continue failed (%d) for: %s", rcode, r1);
                 if (upgrading && rcode == 101) {
                     IOException failed = new IOException(
                             "Unable to handle 101 while waiting for 100");
@@ -357,6 +362,7 @@
     // send the request body and proceed.
     private CompletableFuture<Response> sendRequestBody(ExchangeImpl<T> ex) {
         assert !request.expectContinue();
+        if (debug.on()) debug.log("sendRequestBody");
         CompletableFuture<Response> cf = ex.sendBodyAsync()
                 .thenCompose(exIm -> exIm.getResponseAsync(parentExecutor));
         cf = wrapForUpgrade(cf);
--- a/src/java.net.http/share/classes/jdk/internal/net/http/ExchangeImpl.java	Wed Jun 20 17:15:16 2018 +0200
+++ b/src/java.net.http/share/classes/jdk/internal/net/http/ExchangeImpl.java	Wed Jun 20 09:05:57 2018 -0700
@@ -32,8 +32,8 @@
 import java.net.http.HttpResponse;
 import jdk.internal.net.http.common.Logger;
 import jdk.internal.net.http.common.MinimalFuture;
+import jdk.internal.net.http.common.Utils;
 import static java.net.http.HttpClient.Version.HTTP_1_1;
-import jdk.internal.net.http.common.Utils;
 
 /**
  * Splits request so that headers and body can be sent separately with optional
--- a/src/java.net.http/share/classes/jdk/internal/net/http/Http1AsyncReceiver.java	Wed Jun 20 17:15:16 2018 +0200
+++ b/src/java.net.http/share/classes/jdk/internal/net/http/Http1AsyncReceiver.java	Wed Jun 20 09:05:57 2018 -0700
@@ -27,7 +27,6 @@
 
 import java.io.EOFException;
 import java.io.IOException;
-import java.lang.System.Logger.Level;
 import java.nio.ByteBuffer;
 import java.util.Arrays;
 import java.util.HashSet;
@@ -42,7 +41,9 @@
 import java.util.function.Consumer;
 import jdk.internal.net.http.common.Demand;
 import jdk.internal.net.http.common.FlowTube.TubeSubscriber;
+import jdk.internal.net.http.common.Log;
 import jdk.internal.net.http.common.Logger;
+import jdk.internal.net.http.common.MinimalFuture;
 import jdk.internal.net.http.common.SequentialScheduler;
 import jdk.internal.net.http.common.ConnectionExpiredException;
 import jdk.internal.net.http.common.Utils;
@@ -166,6 +167,7 @@
             = new ConcurrentLinkedDeque<>();
     private final SequentialScheduler scheduler =
             SequentialScheduler.synchronizedScheduler(this::flush);
+    final MinimalFuture<Void> whenFinished;
     private final Executor executor;
     private final Http1TubeSubscriber subscriber = new Http1TubeSubscriber();
     private final AtomicReference<Http1AsyncDelegate> pendingDelegateRef;
@@ -184,6 +186,7 @@
     public Http1AsyncReceiver(Executor executor, Http1Exchange<?> owner) {
         this.pendingDelegateRef = new AtomicReference<>();
         this.executor = executor;
+        this.whenFinished = new MinimalFuture<>();
         this.owner = owner;
         this.client = owner.client;
     }
@@ -261,6 +264,14 @@
         }
     }
 
+    private String describe() {
+        Http1Exchange<?> exchange = owner;
+        if (exchange != null) {
+            return String.valueOf(exchange.request());
+        }
+        return "<uri unavailable>";
+    }
+
     /**
      * Must be called from within the scheduler main loop.
      * Handles any pending errors by calling delegate.onReadError().
@@ -284,6 +295,10 @@
                           + "\t\t queue.isEmpty: " + queue.isEmpty());
             scheduler.stop();
             delegate.onReadError(x);
+            whenFinished.completeExceptionally(x);
+            if (Log.channel()) {
+                Log.logChannel("HTTP/1 read subscriber stopped for: {0}", describe());
+            }
             if (stopRequested) {
                 // This is the special case where the subscriber
                 // has requested an illegal number of items.
@@ -464,27 +479,35 @@
                         // throw ConnectionExpiredException
                         // to try & force a retry of the request.
                         retry = false;
-                        ex = new ConnectionExpiredException(
-                                "subscription is finished", ex);
+                        ex = new ConnectionExpiredException(ex);
                     }
                 }
                 error = ex;
             }
+        }
             final Throwable t = (recorded == null ? ex : recorded);
             if (debug.on())
                 debug.log("recorded " + t + "\n\t delegate: " + delegate
                           + "\t\t queue.isEmpty: " + queue.isEmpty(), ex);
+        if (Log.errors()) {
+            Log.logError("HTTP/1 read subscriber recorded error: {0} - {1}", describe(), t);
         }
         if (queue.isEmpty() || pendingDelegateRef.get() != null || stopRequested) {
             // This callback is called from within the selector thread.
             // Use an executor here to avoid doing the heavy lifting in the
             // selector.
+            if (Log.errors()) {
+                Log.logError("HTTP/1 propagating recorded error: {0} - {1}", describe(), t);
+            }
             scheduler.runOrSchedule(executor);
         }
     }
 
     void stop() {
         if (debug.on()) debug.log("stopping");
+        if (Log.channel() && !scheduler.isStopped()) {
+            Log.logChannel("HTTP/1 read subscriber stopped for {0}", describe());
+        }
         scheduler.stop();
         // make sure ref count is handled properly by
         // closing the delegate.
@@ -492,6 +515,7 @@
         if (previous != null) previous.close(error);
         delegate = null;
         owner  = null;
+        whenFinished.complete(null);
     }
 
     /**
@@ -514,12 +538,18 @@
             // supports being called multiple time.
             // doesn't cancel the previous subscription, since that is
             // most probably the same as the new subscription.
+            if (debug.on()) debug.log("Received onSubscribed from upstream");
+            if (Log.channel()) {
+                Log.logChannel("HTTP/1 read subscriber got subscription from {0}", describe());
+            }
             assert this.subscription == null || dropped == false;
             this.subscription = subscription;
             dropped = false;
             canRequestMore.set(true);
             if (delegate != null) {
                 scheduler.runOrSchedule(executor);
+            } else {
+                if (debug.on()) debug.log("onSubscribe: read delegate not present yet");
             }
         }
 
--- a/src/java.net.http/share/classes/jdk/internal/net/http/Http1Exchange.java	Wed Jun 20 17:15:16 2018 +0200
+++ b/src/java.net.http/share/classes/jdk/internal/net/http/Http1Exchange.java	Wed Jun 20 09:05:57 2018 -0700
@@ -26,7 +26,6 @@
 package jdk.internal.net.http;
 
 import java.io.IOException;
-import java.lang.System.Logger.Level;
 import java.net.InetSocketAddress;
 import java.net.http.HttpResponse.BodyHandler;
 import java.net.http.HttpResponse.BodySubscriber;
@@ -46,6 +45,7 @@
 import jdk.internal.net.http.common.MinimalFuture;
 import jdk.internal.net.http.common.Utils;
 import static java.net.http.HttpClient.Version.HTTP_1_1;
+import static jdk.internal.net.http.common.Utils.wrapWithExtraDetail;
 
 /**
  * Encapsulates one HTTP/1.1 request/response exchange.
@@ -134,6 +134,9 @@
             subscription.request(n);
         }
 
+        /** A current-state message suitable for inclusion in an exception detail message. */
+        abstract String currentStateMessage();
+
         final boolean isSubscribed() {
             return subscription != null;
         }
@@ -159,6 +162,7 @@
                 @Override public void onNext(ByteBuffer item) { error(); }
                 @Override public void onError(Throwable throwable) { error(); }
                 @Override public void onComplete() { error(); }
+                @Override String currentStateMessage() { return null; }
                 private void error() {
                     throw new InternalError("should not reach here");
                 }
@@ -193,35 +197,6 @@
         }
         this.requestAction = new Http1Request(request, this);
         this.asyncReceiver = new Http1AsyncReceiver(executor, this);
-        asyncReceiver.subscribe(new InitialErrorReceiver());
-    }
-
-    /** An initial receiver that handles no data, but cancels the request if
-     * it receives an error. Will be replaced when reading response body. */
-    final class InitialErrorReceiver implements Http1AsyncReceiver.Http1AsyncDelegate {
-        volatile AbstractSubscription s;
-        @Override
-        public boolean tryAsyncReceive(ByteBuffer ref) {
-            return false;  // no data has been processed, leave it in the queue
-        }
-
-        @Override
-        public void onReadError(Throwable ex) {
-            cancelImpl(ex);
-        }
-
-        @Override
-        public void onSubscribe(AbstractSubscription s) {
-            this.s = s;
-        }
-
-        @Override
-        public AbstractSubscription subscription() {
-            return s;
-        }
-
-        @Override
-        public void close(Throwable error) {}
     }
 
     @Override
@@ -244,16 +219,16 @@
         // create the response before sending the request headers, so that
         // the response can set the appropriate receivers.
         if (debug.on()) debug.log("Sending headers only");
+        // If the first attempt to read something triggers EOF, or
+        // IOException("channel reset by peer"), we're going to retry.
+        // Instruct the asyncReceiver to throw ConnectionExpiredException
+        // to force a retry.
+        asyncReceiver.setRetryOnError(true);
         if (response == null) {
             response = new Http1Response<>(connection, this, asyncReceiver);
         }
 
         if (debug.on()) debug.log("response created in advance");
-        // If the first attempt to read something triggers EOF, or
-        // IOException("channel reset by peer"), we're going to retry.
-        // Instruct the asyncReceiver to throw ConnectionExpiredException
-        // to force a retry.
-        asyncReceiver.setRetryOnError(true);
 
         CompletableFuture<Void> connectCF;
         if (!connection.connected()) {
@@ -296,6 +271,8 @@
                         return cf;
                     } catch (Throwable t) {
                         if (debug.on()) debug.log("Failed to send headers: %s", t);
+                        headersSentCF.completeExceptionally(t);
+                        bodySentCF.completeExceptionally(t);
                         connection.close();
                         cf.completeExceptionally(t);
                         return cf;
@@ -303,28 +280,52 @@
                 .thenCompose(unused -> headersSentCF);
     }
 
+    private void cancelIfFailed(Flow.Subscription s) {
+        asyncReceiver.whenFinished.whenCompleteAsync((r,t) -> {
+            if (debug.on()) debug.log("asyncReceiver finished (failed=%s)", t);
+            if (t != null) {
+                s.cancel();
+                // Don't complete exceptionally here as 't'
+                // might not be the right exception: it will
+                // not have been decorated yet.
+                // t is an exception raised by the read side,
+                // an EOFException or Broken Pipe...
+                // We are cancelling the BodyPublisher subscription
+                // and completing bodySentCF to allow the next step
+                // to flow and call readHeaderAsync, which will
+                // get the right exception from the asyncReceiver.
+                bodySentCF.complete(this);
+            }
+        }, executor);
+    }
+
     @Override
     CompletableFuture<ExchangeImpl<T>> sendBodyAsync() {
         assert headersSentCF.isDone();
+        if (debug.on()) debug.log("sendBodyAsync");
         try {
             bodySubscriber = requestAction.continueRequest();
+            if (debug.on()) debug.log("bodySubscriber is %s",
+                    bodySubscriber == null ? null : bodySubscriber.getClass());
             if (bodySubscriber == null) {
                 bodySubscriber = Http1BodySubscriber.completeSubscriber(debug);
                 appendToOutgoing(Http1BodySubscriber.COMPLETED);
             } else {
                 // start
                 bodySubscriber.whenSubscribed
+                        .thenAccept((s) -> cancelIfFailed(s))
                         .thenAccept((s) -> requestMoreBody());
             }
         } catch (Throwable t) {
             cancelImpl(t);
             bodySentCF.completeExceptionally(t);
         }
-        return bodySentCF;
+        return Utils.wrapForDebug(debug, "sendBodyAsync", bodySentCF);
     }
 
     @Override
     CompletableFuture<Response> getResponseAsync(Executor executor) {
+        if (debug.on()) debug.log("reading headers");
         CompletableFuture<Response> cf = response.readHeadersAsync(executor);
         Throwable cause;
         synchronized (lock) {
@@ -348,7 +349,7 @@
                 debug.log(acknowledged ? ("completed response with " + cause)
                           : ("response already completed, ignoring " + cause));
         }
-        return cf;
+        return Utils.wrapForDebug(debug, "getResponseAsync", cf);
     }
 
     @Override
@@ -421,7 +422,6 @@
                     && response != null && response.finished()) {
                 return;
             }
-            connection.close();   // TODO: ensure non-blocking if holding the lock
             writePublisher.writeScheduler.stop();
             if (operations.isEmpty()) {
                 Log.logTrace("Http1Exchange: request [{0}/timeout={1}ms] no pending operation."
@@ -444,27 +444,31 @@
                 operations.clear();
             }
         }
-        Log.logError("Http1Exchange.cancel: count=" + count);
-        if (toComplete != null) {
-            // We might be in the selector thread in case of timeout, when
-            // the SelectorManager calls purgeTimeoutsAndReturnNextDeadline()
-            // There may or may not be other places that reach here
-            // from the SelectorManager thread, so just make sure we
-            // don't complete any CF from within the selector manager
-            // thread.
-            Executor exec = client.isSelectorThread()
-                            ? executor
-                            : this::runInline;
-            Throwable x = error;
-            while (!toComplete.isEmpty()) {
-                CompletableFuture<?> cf = toComplete.poll();
-                exec.execute(() -> {
-                    if (cf.completeExceptionally(x)) {
-                        if (debug.on())
-                            debug.log("%s: completed cf with %s", request.uri(), x);
-                    }
-                });
+        try {
+            Log.logError("Http1Exchange.cancel: count=" + count);
+            if (toComplete != null) {
+                // We might be in the selector thread in case of timeout, when
+                // the SelectorManager calls purgeTimeoutsAndReturnNextDeadline()
+                // There may or may not be other places that reach here
+                // from the SelectorManager thread, so just make sure we
+                // don't complete any CF from within the selector manager
+                // thread.
+                Executor exec = client.isSelectorThread()
+                        ? executor
+                        : this::runInline;
+                Throwable x = error;
+                while (!toComplete.isEmpty()) {
+                    CompletableFuture<?> cf = toComplete.poll();
+                    exec.execute(() -> {
+                        if (cf.completeExceptionally(x)) {
+                            if (debug.on())
+                                debug.log("%s: completed cf with %s", request.uri(), x);
+                        }
+                    });
+                }
             }
+        } finally {
+            connection.close();
         }
     }
 
@@ -511,7 +515,7 @@
 
     private void requestMoreBody() {
         try {
-            if (debug.on()) debug.log("requesting more body from the subscriber");
+            if (debug.on()) debug.log("requesting more request body from the subscriber");
             bodySubscriber.request(1);
         } catch (Throwable t) {
             if (debug.on()) debug.log("Subscription::request failed", t);
@@ -527,46 +531,62 @@
         final Executor exec = client.theExecutor();
         final DataPair dp = outgoing.pollFirst();
 
+        if (writePublisher.cancelled) {
+            if (debug.on()) debug.log("cancelling upstream publisher");
+            if (bodySubscriber != null) {
+                exec.execute(bodySubscriber::cancelSubscription);
+            } else if (debug.on()) {
+                debug.log("bodySubscriber is null");
+            }
+            headersSentCF.completeAsync(() -> this, exec);
+            bodySentCF.completeAsync(() -> this, exec);
+            return null;
+        }
+
         if (dp == null)  // publisher has not published anything yet
             return null;
 
-        synchronized (lock) {
-            if (dp.throwable != null) {
+        if (dp.throwable != null) {
+            synchronized (lock) {
                 state = State.ERROR;
-                exec.execute(() -> {
-                    headersSentCF.completeExceptionally(dp.throwable);
-                    bodySentCF.completeExceptionally(dp.throwable);
-                    connection.close();
-                });
-                return dp;
             }
-
-            switch (state) {
-                case HEADERS:
-                    state = State.BODY;
-                    // completeAsync, since dependent tasks should run in another thread
-                    if (debug.on()) debug.log("initiating completion of headersSentCF");
-                    headersSentCF.completeAsync(() -> this, exec);
-                    break;
-                case BODY:
-                    if (dp.data == Http1BodySubscriber.COMPLETED) {
-                        state = State.COMPLETING;
-                        if (debug.on()) debug.log("initiating completion of bodySentCF");
-                        bodySentCF.completeAsync(() -> this, exec);
-                    } else {
-                        exec.execute(this::requestMoreBody);
-                    }
-                    break;
-                case INITIAL:
-                case ERROR:
-                case COMPLETING:
-                case COMPLETED:
-                default:
-                    assert false : "Unexpected state:" + state;
-            }
-
+            exec.execute(() -> {
+                headersSentCF.completeExceptionally(dp.throwable);
+                bodySentCF.completeExceptionally(dp.throwable);
+                connection.close();
+            });
             return dp;
         }
+
+        switch (state) {
+            case HEADERS:
+                synchronized (lock) {
+                    state = State.BODY;
+                }
+                // completeAsync, since dependent tasks should run in another thread
+                if (debug.on()) debug.log("initiating completion of headersSentCF");
+                headersSentCF.completeAsync(() -> this, exec);
+                break;
+            case BODY:
+                if (dp.data == Http1BodySubscriber.COMPLETED) {
+                    synchronized (lock) {
+                        state = State.COMPLETING;
+                    }
+                    if (debug.on()) debug.log("initiating completion of bodySentCF");
+                    bodySentCF.completeAsync(() -> this, exec);
+                } else {
+                    exec.execute(this::requestMoreBody);
+                }
+                break;
+            case INITIAL:
+            case ERROR:
+            case COMPLETING:
+            case COMPLETED:
+            default:
+                assert false : "Unexpected state:" + state;
+        }
+
+        return dp;
     }
 
     /** A Publisher of HTTP/1.1 headers and request body. */
@@ -608,6 +628,14 @@
             public void run() {
                 assert state != State.COMPLETED : "Unexpected state:" + state;
                 if (debug.on()) debug.log("WriteTask");
+
+                if (cancelled) {
+                    if (debug.on()) debug.log("handling cancellation");
+                    writeScheduler.stop();
+                    getOutgoing();
+                    return;
+                }
+
                 if (subscriber == null) {
                     if (debug.on()) debug.log("no subscriber yet");
                     return;
@@ -615,6 +643,8 @@
                 if (debug.on()) debug.log(() -> "hasOutgoing = " + hasOutgoing());
                 while (hasOutgoing() && demand.tryDecrement()) {
                     DataPair dp = getOutgoing();
+                    if (dp == null)
+                        break;
 
                     if (dp.throwable != null) {
                         if (debug.on()) debug.log("onError");
@@ -661,7 +691,7 @@
                 if (cancelled)
                     return;  //no-op
                 cancelled = true;
-                writeScheduler.stop();
+                writeScheduler.runOrSchedule(client.theExecutor());
             }
         }
     }
--- a/src/java.net.http/share/classes/jdk/internal/net/http/Http1HeaderParser.java	Wed Jun 20 17:15:16 2018 +0200
+++ b/src/java.net.http/share/classes/jdk/internal/net/http/Http1HeaderParser.java	Wed Jun 20 09:05:57 2018 -0700
@@ -35,6 +35,7 @@
 import java.net.http.HttpHeaders;
 import static java.lang.String.format;
 import static java.util.Objects.requireNonNull;
+import static jdk.internal.net.http.common.Utils.ACCEPT_ALL;
 
 class Http1HeaderParser {
 
@@ -49,10 +50,13 @@
     private HttpHeaders headers;
     private Map<String,List<String>> privateMap = new HashMap<>();
 
-    enum State { STATUS_LINE,
+    enum State { INITIAL,
+                 STATUS_LINE,
                  STATUS_LINE_FOUND_CR,
+                 STATUS_LINE_FOUND_LF,
                  STATUS_LINE_END,
                  STATUS_LINE_END_CR,
+                 STATUS_LINE_END_LF,
                  HEADER,
                  HEADER_FOUND_CR,
                  HEADER_FOUND_LF,
@@ -60,7 +64,7 @@
                  HEADER_FOUND_CR_LF_CR,
                  FINISHED }
 
-    private State state = State.STATUS_LINE;
+    private State state = State.INITIAL;
 
     /** Returns the status-line. */
     String statusLine() { return statusLine; }
@@ -69,7 +73,29 @@
     int responseCode() { return responseCode; }
 
     /** Returns the headers, possibly empty. */
-    HttpHeaders headers() { assert state == State.FINISHED; return headers; }
+    HttpHeaders headers() {
+        assert state == State.FINISHED : "Unexpected state " + state;
+        return headers;
+    }
+
+    /** A current-state message suitable for inclusion in an exception detail message. */
+    public String currentStateMessage() {
+        String stateName = state.name();
+        String msg;
+        if (stateName.contains("INITIAL")) {
+            return format("HTTP/1.1 header parser received no bytes");
+        } else if (stateName.contains("STATUS")) {
+            msg = format("parsing HTTP/1.1 status line, receiving [%s]", sb.toString());
+        } else if (stateName.contains("HEADER")) {
+            String headerName = sb.toString();
+            if (headerName.indexOf(':') != -1)
+                headerName = headerName.substring(0, headerName.indexOf(':')+1) + "...";
+            msg = format("parsing HTTP/1.1 header, receiving [%s]", headerName);
+        } else {
+            msg =format("HTTP/1.1 parser receiving [%s]", state, sb.toString());
+        }
+        return format("%s, parser state [%s]", msg , state);
+    }
 
     /**
      * Parses HTTP/1.X status-line and headers from the given bytes. Must be
@@ -84,18 +110,25 @@
     boolean parse(ByteBuffer input) throws ProtocolException {
         requireNonNull(input, "null input");
 
-        while (input.hasRemaining() && state != State.FINISHED) {
+        while (canContinueParsing(input)) {
             switch (state) {
+                case INITIAL:
+                    state = State.STATUS_LINE;
+                    break;
                 case STATUS_LINE:
                     readResumeStatusLine(input);
                     break;
+                // fallthrough
                 case STATUS_LINE_FOUND_CR:
+                case STATUS_LINE_FOUND_LF:
                     readStatusLineFeed(input);
                     break;
                 case STATUS_LINE_END:
                     maybeStartHeaders(input);
                     break;
+                // fallthrough
                 case STATUS_LINE_END_CR:
+                case STATUS_LINE_END_LF:
                     maybeEndHeaders(input);
                     break;
                 case HEADER:
@@ -121,21 +154,35 @@
         return state == State.FINISHED;
     }
 
+    private boolean canContinueParsing(ByteBuffer buffer) {
+        // some states don't require any input to transition
+        // to the next state.
+        switch (state) {
+            case FINISHED: return false;
+            case STATUS_LINE_FOUND_LF: return true;
+            case STATUS_LINE_END_LF: return true;
+            case HEADER_FOUND_LF: return true;
+            default: return buffer.hasRemaining();
+        }
+    }
+
     private void readResumeStatusLine(ByteBuffer input) {
         char c = 0;
         while (input.hasRemaining() && (c =(char)input.get()) != CR) {
+            if (c == LF) break;
             sb.append(c);
         }
-
         if (c == CR) {
             state = State.STATUS_LINE_FOUND_CR;
+        } else if (c == LF) {
+            state = State.STATUS_LINE_FOUND_LF;
         }
     }
 
     private void readStatusLineFeed(ByteBuffer input) throws ProtocolException {
-        char c = (char)input.get();
+        char c = state == State.STATUS_LINE_FOUND_LF ? LF : (char)input.get();
         if (c != LF) {
-            throw protocolException("Bad trailing char, \"%s\", when parsing status-line, \"%s\"",
+            throw protocolException("Bad trailing char, \"%s\", when parsing status line, \"%s\"",
                                     c, sb.toString());
         }
 
@@ -158,6 +205,8 @@
         char c = (char)input.get();
         if (c == CR) {
             state = State.STATUS_LINE_END_CR;
+        } else if (c == LF) {
+            state = State.STATUS_LINE_END_LF;
         } else {
             sb.append(c);
             state = State.HEADER;
@@ -165,15 +214,15 @@
     }
 
     private void maybeEndHeaders(ByteBuffer input) throws ProtocolException {
-        assert state == State.STATUS_LINE_END_CR;
+        assert state == State.STATUS_LINE_END_CR || state == State.STATUS_LINE_END_LF;
         assert sb.length() == 0;
-        char c = (char)input.get();
+        char c = state == State.STATUS_LINE_END_LF ? LF : (char)input.get();
         if (c == LF) {
-            headers = ImmutableHeaders.of(privateMap);
+            headers = HttpHeaders.of(privateMap, ACCEPT_ALL);
             privateMap = null;
             state = State.FINISHED;  // no headers
         } else {
-            throw protocolException("Unexpected \"%s\", after status-line CR", c);
+            throw protocolException("Unexpected \"%s\", after status line CR", c);
         }
     }
 
@@ -212,8 +261,8 @@
 
     private void resumeOrLF(ByteBuffer input) {
         assert state == State.HEADER_FOUND_CR || state == State.HEADER_FOUND_LF;
-        char c = (char)input.get();
-        if (c == LF && state == State.HEADER_FOUND_CR) {
+        char c = state == State.HEADER_FOUND_LF ? LF : (char)input.get();
+        if (c == LF) {
             // header value will be flushed by
             // resumeOrSecondCR if next line does not
             // begin by SP or HT
@@ -231,7 +280,7 @@
     private void resumeOrSecondCR(ByteBuffer input) {
         assert state == State.HEADER_FOUND_CR_LF;
         char c = (char)input.get();
-        if (c == CR) {
+        if (c == CR || c == LF) {
             if (sb.length() > 0) {
                 // no continuation line - flush
                 // previous header value.
@@ -239,7 +288,13 @@
                 sb = new StringBuilder();
                 addHeaderFromString(headerString);
             }
-            state = State.HEADER_FOUND_CR_LF_CR;
+            if (c == CR) {
+                state = State.HEADER_FOUND_CR_LF_CR;
+            } else {
+                state = State.FINISHED;
+                headers = HttpHeaders.of(privateMap, ACCEPT_ALL);
+                privateMap = null;
+            }
         } else if (c == SP || c == HT) {
             assert sb.length() != 0;
             sb.append(SP); // continuation line
@@ -262,7 +317,7 @@
         char c = (char)input.get();
         if (c == LF) {
             state = State.FINISHED;
-            headers = ImmutableHeaders.of(privateMap);
+            headers = HttpHeaders.of(privateMap, ACCEPT_ALL);
             privateMap = null;
         } else {
             throw protocolException("Unexpected \"%s\", after CR LF CR", c);
--- a/src/java.net.http/share/classes/jdk/internal/net/http/Http1Request.java	Wed Jun 20 17:15:16 2018 +0200
+++ b/src/java.net.http/share/classes/jdk/internal/net/http/Http1Request.java	Wed Jun 20 09:05:57 2018 -0700
@@ -26,7 +26,6 @@
 package jdk.internal.net.http;
 
 import java.io.IOException;
-import java.lang.System.Logger.Level;
 import java.net.URI;
 import java.nio.ByteBuffer;
 import java.util.ArrayList;
@@ -39,11 +38,12 @@
 import java.net.http.HttpHeaders;
 import java.net.http.HttpRequest;
 import jdk.internal.net.http.Http1Exchange.Http1BodySubscriber;
-import jdk.internal.net.http.common.HttpHeadersImpl;
+import jdk.internal.net.http.common.HttpHeadersBuilder;
 import jdk.internal.net.http.common.Log;
 import jdk.internal.net.http.common.Logger;
 import jdk.internal.net.http.common.Utils;
 
+import static java.lang.String.format;
 import static java.nio.charset.StandardCharsets.US_ASCII;
 
 /**
@@ -52,7 +52,7 @@
 class Http1Request {
 
     private static final String COOKIE_HEADER = "Cookie";
-    private static final BiPredicate<String,List<String>> NOCOOKIES =
+    private static final BiPredicate<String,String> NOCOOKIES =
             (k,v) -> !COOKIE_HEADER.equalsIgnoreCase(k);
 
     private final HttpRequestImpl request;
@@ -60,7 +60,7 @@
     private final HttpConnection connection;
     private final HttpRequest.BodyPublisher requestPublisher;
     private final HttpHeaders userHeaders;
-    private final HttpHeadersImpl systemHeaders;
+    private final HttpHeadersBuilder systemHeadersBuilder;
     private volatile boolean streaming;
     private volatile long contentLength;
 
@@ -73,7 +73,7 @@
         this.connection = http1Exchange.connection();
         this.requestPublisher = request.requestPublisher;  // may be null
         this.userHeaders = request.getUserHeaders();
-        this.systemHeaders = request.getSystemHeaders();
+        this.systemHeadersBuilder = request.getSystemHeadersBuilder();
     }
 
     private void logHeaders(String completeHeaders) {
@@ -92,12 +92,13 @@
 
 
     private void collectHeaders0(StringBuilder sb) {
-        BiPredicate<String,List<String>> filter =
+        BiPredicate<String,String> filter =
                 connection.headerFilter(request);
 
         // Filter out 'Cookie:' headers, we will collect them at the end.
-        BiPredicate<String,List<String>> nocookies =
-                NOCOOKIES.and(filter);
+        BiPredicate<String,String> nocookies = NOCOOKIES.and(filter);
+
+        HttpHeaders systemHeaders = systemHeadersBuilder.build();
 
         // If we're sending this request through a tunnel,
         // then don't send any preemptive proxy-* headers that
@@ -112,8 +113,7 @@
 
         // Gather all 'Cookie:' headers and concatenate their
         // values in a single line.
-        collectCookies(sb, COOKIE_HEADER,
-                systemHeaders, userHeaders, filter);
+        collectCookies(sb, systemHeaders, userHeaders);
 
         // terminate headers
         sb.append('\r').append('\n');
@@ -142,20 +142,16 @@
     // any illegal character for header field values.
     //
     private void collectCookies(StringBuilder sb,
-                                String key,
                                 HttpHeaders system,
-                                HttpHeaders user,
-                                BiPredicate<String, List<String>> filter) {
-        List<String> systemList = system.allValues(key);
-        if (systemList != null && !filter.test(key, systemList)) systemList = null;
-        List<String> userList = user.allValues(key);
-        if (userList != null && !filter.test(key, userList)) userList = null;
+                                HttpHeaders user) {
+        List<String> systemList = system.allValues(COOKIE_HEADER);
+        List<String> userList = user.allValues(COOKIE_HEADER);
         boolean found = false;
         if (systemList != null) {
             for (String cookie : systemList) {
                 if (!found) {
                     found = true;
-                    sb.append(key).append(':').append(' ');
+                    sb.append(COOKIE_HEADER).append(':').append(' ');
                 } else {
                     sb.append(';').append(' ');
                 }
@@ -166,7 +162,7 @@
             for (String cookie : userList) {
                 if (!found) {
                     found = true;
-                    sb.append(key).append(':').append(' ');
+                    sb.append(COOKIE_HEADER).append(':').append(' ');
                 } else {
                     sb.append(';').append(' ');
                 }
@@ -176,13 +172,15 @@
         if (found) sb.append('\r').append('\n');
     }
 
-    private void collectHeaders1(StringBuilder sb, HttpHeaders headers,
-                                 BiPredicate<String, List<String>> filter) {
+    private void collectHeaders1(StringBuilder sb,
+                                 HttpHeaders headers,
+                                 BiPredicate<String,String> filter) {
         for (Map.Entry<String,List<String>> entry : headers.map().entrySet()) {
             String key = entry.getKey();
             List<String> values = entry.getValue();
-            if (!filter.test(key, values)) continue;
             for (String value : values) {
+                if (!filter.test(key, value))
+                    continue;
                 sb.append(key).append(':').append(' ')
                         .append(value)
                         .append('\r').append('\n');
@@ -279,7 +277,7 @@
 
         URI uri = request.uri();
         if (uri != null) {
-            systemHeaders.setHeader("Host", hostString());
+            systemHeadersBuilder.setHeader("Host", hostString());
         }
         if (requestPublisher == null) {
             // Not a user request, or maybe a method, e.g. GET, with no body.
@@ -289,13 +287,13 @@
         }
 
         if (contentLength == 0) {
-            systemHeaders.setHeader("Content-Length", "0");
+            systemHeadersBuilder.setHeader("Content-Length", "0");
         } else if (contentLength > 0) {
-            systemHeaders.setHeader("Content-Length", Long.toString(contentLength));
+            systemHeadersBuilder.setHeader("Content-Length", Long.toString(contentLength));
             streaming = false;
         } else {
             streaming = true;
-            systemHeaders.setHeader("Transfer-encoding", "chunked");
+            systemHeadersBuilder.setHeader("Transfer-encoding", "chunked");
         }
         collectHeaders0(sb);
         String hs = sb.toString();
@@ -350,6 +348,11 @@
         }
 
         @Override
+        public String currentStateMessage() {
+            return "streaming request body " + (complete ? "complete" : "incomplete");
+        }
+
+        @Override
         public void onError(Throwable throwable) {
             if (complete)
                 return;
@@ -417,6 +420,12 @@
         }
 
         @Override
+        public String currentStateMessage() {
+            return format("fixed content-length: %d, bytes sent: %d",
+                           contentLength, contentWritten);
+        }
+
+        @Override
         public void onError(Throwable throwable) {
             if (debug.on()) debug.log("onError");
             if (complete)  // TODO: error?
--- a/src/java.net.http/share/classes/jdk/internal/net/http/Http1Response.java	Wed Jun 20 17:15:16 2018 +0200
+++ b/src/java.net.http/share/classes/jdk/internal/net/http/Http1Response.java	Wed Jun 20 09:05:57 2018 -0700
@@ -44,9 +44,10 @@
 import jdk.internal.net.http.common.Logger;
 import jdk.internal.net.http.common.MinimalFuture;
 import jdk.internal.net.http.common.Utils;
-
 import static java.net.http.HttpClient.Version.HTTP_1_1;
 import static java.net.http.HttpResponse.BodySubscribers.discarding;
+import static jdk.internal.net.http.common.Utils.wrapWithExtraDetail;
+import static jdk.internal.net.http.RedirectFilter.HTTP_NOT_MODIFIED;
 
 /**
  * Handles a HTTP/1.1 response (headers + body).
@@ -76,6 +77,7 @@
     final Logger debug = Utils.getDebugLogger(this::dbgString, Utils.DEBUG);
     final static AtomicLong responseCount = new AtomicLong();
     final long id = responseCount.incrementAndGet();
+    private Http1HeaderParser hd;
 
     Http1Response(HttpConnection conn,
                   Http1Exchange<T> exchange,
@@ -87,6 +89,11 @@
         this.asyncReceiver = asyncReceiver;
         headersReader = new HeadersReader(this::advance);
         bodyReader = new BodyReader(this::advance);
+
+        hd = new Http1HeaderParser();
+        readProgress = State.READING_HEADERS;
+        headersReader.start(hd);
+        asyncReceiver.subscribe(headersReader);
     }
 
     String dbgTag;
@@ -150,19 +157,36 @@
         }
     }
 
+    private volatile boolean firstTimeAround = true;
+
     public CompletableFuture<Response> readHeadersAsync(Executor executor) {
         if (debug.on())
             debug.log("Reading Headers: (remaining: "
                       + asyncReceiver.remaining() +") "  + readProgress);
-        // with expect continue we will resume reading headers + body.
-        asyncReceiver.unsubscribe(bodyReader);
-        bodyReader.reset();
-        Http1HeaderParser hd = new Http1HeaderParser();
-        readProgress = State.READING_HEADERS;
-        headersReader.start(hd);
-        asyncReceiver.subscribe(headersReader);
+
+        if (firstTimeAround) {
+            if (debug.on()) debug.log("First time around");
+            firstTimeAround = false;
+        } else {
+            // with expect continue we will resume reading headers + body.
+            asyncReceiver.unsubscribe(bodyReader);
+            bodyReader.reset();
+
+            hd = new Http1HeaderParser();
+            readProgress = State.READING_HEADERS;
+            headersReader.reset();
+            headersReader.start(hd);
+            asyncReceiver.subscribe(headersReader);
+        }
+
         CompletableFuture<State> cf = headersReader.completion();
         assert cf != null : "parsing not started";
+        if (debug.on()) {
+            debug.log("headersReader is %s",
+                    cf == null ? "not yet started"
+                            : cf.isDone() ? "already completed"
+                            : "not yet completed");
+        }
 
         Function<State, Response> lambda = (State completed) -> {
                 assert completed == State.READING_HEADERS;
@@ -207,7 +231,7 @@
     }
 
     int fixupContentLen(int clen) {
-        if (request.method().equalsIgnoreCase("HEAD")) {
+        if (request.method().equalsIgnoreCase("HEAD") || responseCode == HTTP_NOT_MODIFIED) {
             return 0;
         }
         if (clen == -1) {
@@ -289,13 +313,19 @@
                     try {
                         userSubscriber.onComplete();
                     } catch (Throwable x) {
+                        // Simply propagate the error by calling
+                        // onError on the user subscriber, and let the
+                        // connection be reused since we should have received
+                        // and parsed all the bytes when we reach here.
+                        // If onError throws in turn, then we will simply
+                        // let that new exception flow up to the caller
+                        // and let it deal with it.
+                        // (i.e: log and close the connection)
+                        // Note that rethrowing here could introduce a
+                        // race that might cause the next send() operation to
+                        // fail as the connection has already been put back
+                        // into the cache when we reach here.
                         propagateError(t = withError = Utils.getCompletionCause(x));
-                        // rethrow and let the caller deal with it.
-                        // (i.e: log and close the connection)
-                        // arguably we could decide to not throw and let the
-                        // connection be reused since we should have received and
-                        // parsed all the bytes when we reach here.
-                        throw x;
                     }
                 } else {
                     propagateError(t);
@@ -613,6 +643,7 @@
 
         @Override
         public final void onReadError(Throwable t) {
+            t = wrapWithExtraDetail(t, parser::currentStateMessage);
             Http1Response.this.onReadError(t);
         }
 
@@ -692,6 +723,7 @@
 
         @Override
         public final void onReadError(Throwable t) {
+            t = wrapWithExtraDetail(t, parser::currentStateMessage);
             Http1Response.this.onReadError(t);
         }
 
--- a/src/java.net.http/share/classes/jdk/internal/net/http/Http2ClientImpl.java	Wed Jun 20 17:15:16 2018 +0200
+++ b/src/java.net.http/share/classes/jdk/internal/net/http/Http2ClientImpl.java	Wed Jun 20 09:05:57 2018 -0700
@@ -25,6 +25,9 @@
 
 package jdk.internal.net.http;
 
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.net.ConnectException;
 import java.net.InetSocketAddress;
 import java.net.URI;
 import java.util.Base64;
@@ -95,15 +98,20 @@
         synchronized (this) {
             Http2Connection connection = connections.get(key);
             if (connection != null) {
-                if (connection.closed || !connection.reserveStream(true)) {
-                    if (debug.on())
-                        debug.log("removing found closed or closing connection: %s", connection);
-                    deleteConnection(connection);
-                } else {
-                    // fast path if connection already exists
-                    if (debug.on())
-                        debug.log("found connection in the pool: %s", connection);
-                    return MinimalFuture.completedFuture(connection);
+                try {
+                    if (connection.closed || !connection.reserveStream(true)) {
+                        if (debug.on())
+                            debug.log("removing found closed or closing connection: %s", connection);
+                        deleteConnection(connection);
+                    } else {
+                        // fast path if connection already exists
+                        if (debug.on())
+                            debug.log("found connection in the pool: %s", connection);
+                        return MinimalFuture.completedFuture(connection);
+                    }
+                } catch (IOException e) {
+                    // thrown by connection.reserveStream()
+                    return MinimalFuture.failedFuture(e);
                 }
             }
 
@@ -119,6 +127,11 @@
                 .whenComplete((conn, t) -> {
                     synchronized (Http2ClientImpl.this) {
                         if (conn != null) {
+                            try {
+                                conn.reserveStream(true);
+                            } catch (IOException e) {
+                                throw new UncheckedIOException(e); // shouldn't happen
+                            }
                             offerConnection(conn);
                         } else {
                             Throwable cause = Utils.getCompletionCause(t);
--- a/src/java.net.http/share/classes/jdk/internal/net/http/Http2Connection.java	Wed Jun 20 17:15:16 2018 +0200
+++ b/src/java.net.http/share/classes/jdk/internal/net/http/Http2Connection.java	Wed Jun 20 09:05:57 2018 -0700
@@ -28,7 +28,6 @@
 import java.io.EOFException;
 import java.io.IOException;
 import java.io.UncheckedIOException;
-import java.lang.System.Logger.Level;
 import java.net.InetSocketAddress;
 import java.net.URI;
 import java.nio.ByteBuffer;
@@ -54,7 +53,7 @@
 import jdk.internal.net.http.HttpConnection.HttpPublisher;
 import jdk.internal.net.http.common.FlowTube;
 import jdk.internal.net.http.common.FlowTube.TubeSubscriber;
-import jdk.internal.net.http.common.HttpHeadersImpl;
+import jdk.internal.net.http.common.HttpHeadersBuilder;
 import jdk.internal.net.http.common.Log;
 import jdk.internal.net.http.common.Logger;
 import jdk.internal.net.http.common.MinimalFuture;
@@ -82,7 +81,6 @@
 import static java.nio.charset.StandardCharsets.UTF_8;
 import static jdk.internal.net.http.frame.SettingsFrame.*;
 
-
 /**
  * An Http2Connection. Encapsulates the socket(channel) and any SSLEngine used
  * over it. Contains an HttpConnection which hides the SocketChannel SSL stuff.
@@ -259,6 +257,8 @@
     // assigning a stream to a connection.
     private int lastReservedClientStreamid = 1;
     private int lastReservedServerStreamid = 0;
+    private int numReservedClientStreams = 0; // count of current streams
+    private int numReservedServerStreams = 0; // count of current streams
     private final Encoder hpackOut;
     private final Decoder hpackIn;
     final SettingsFrame clientSettings;
@@ -273,7 +273,7 @@
      */
     private final WindowController windowController = new WindowController();
     private final FramesController framesController = new FramesController();
-    private final Http2TubeSubscriber subscriber = new Http2TubeSubscriber();
+    private final Http2TubeSubscriber subscriber;
     final ConnectionWindowUpdateSender windowUpdater;
     private volatile Throwable cause;
     private volatile Supplier<ByteBuffer> initial;
@@ -290,6 +290,7 @@
                             String key) {
         this.connection = connection;
         this.client2 = client2;
+        this.subscriber = new Http2TubeSubscriber(client2.client());
         this.nextstreamid = nextstreamid;
         this.key = key;
         this.clientSettings = this.client2.getClientSettings();
@@ -310,7 +311,7 @@
 
     /**
      * Case 1) Create from upgraded HTTP/1.1 connection.
-     * Is ready to use. Can be SSL. exchange is the Exchange
+     * Is ready to use. Can't be SSL. exchange is the Exchange
      * that initiated the connection, whose response will be delivered
      * on a Stream.
      */
@@ -324,6 +325,7 @@
                 client2,
                 3, // stream 1 is registered during the upgrade
                 keyFor(connection));
+        reserveStream(true);
         Log.logTrace("Connection send window size {0} ", windowController.connectionWindowSize());
 
         Stream<?> initialStream = createStream(exchange);
@@ -407,7 +409,8 @@
     // call these before assigning a request/stream to a connection
     // if false returned then a new Http2Connection is required
     // if true, the the stream may be assigned to this connection
-    synchronized boolean reserveStream(boolean clientInitiated) {
+    // for server push, if false returned, then the stream should be cancelled
+    synchronized boolean reserveStream(boolean clientInitiated) throws IOException {
         if (finalStream) {
             return false;
         }
@@ -424,6 +427,19 @@
             lastReservedClientStreamid+=2;
         else
             lastReservedServerStreamid+=2;
+
+        assert numReservedClientStreams >= 0;
+        assert numReservedServerStreams >= 0;
+        if (clientInitiated && numReservedClientStreams >= getMaxConcurrentClientStreams()) {
+            throw new IOException("too many concurrent streams");
+        } else if (clientInitiated) {
+            numReservedClientStreams++;
+        }
+        if (!clientInitiated && numReservedServerStreams >= getMaxConcurrentServerStreams()) {
+            return false;
+        } else if (!clientInitiated) {
+            numReservedServerStreams++;
+        }
         return true;
     }
 
@@ -564,6 +580,14 @@
         return serverSettings.getParameter(INITIAL_WINDOW_SIZE);
     }
 
+    final int getMaxConcurrentClientStreams() {
+        return serverSettings.getParameter(MAX_CONCURRENT_STREAMS);
+    }
+
+    final int getMaxConcurrentServerStreams() {
+        return clientSettings.getParameter(MAX_CONCURRENT_STREAMS);
+    }
+
     void close() {
         Log.logTrace("Closing HTTP/2 connection: to {0}", connection.address());
         GoAwayFrame f = new GoAwayFrame(0,
@@ -637,13 +661,19 @@
             if (closed == true) return;
             closed = true;
         }
-        Log.logError(t);
+        if (Log.errors()) {
+            if (!(t instanceof EOFException) || isActive()) {
+                Log.logError(t);
+            } else if (t != null) {
+                Log.logError("Shutting down connection: {0}", t.getMessage());
+            }
+        }
         Throwable initialCause = this.cause;
         if (initialCause == null) this.cause = t;
         client2.deleteConnection(this);
         List<Stream<?>> c = new LinkedList<>(streams.values());
         for (Stream<?> s : c) {
-            s.cancelImpl(t);
+            s.connectionClosing(t);
         }
         connection.close();
     }
@@ -766,7 +796,7 @@
             nextPushStream += 2;
         }
 
-        HttpHeadersImpl headers = decoder.headers();
+        HttpHeaders headers = decoder.headers();
         HttpRequestImpl pushReq = HttpRequestImpl.createPushRequest(parentReq, headers);
         Exchange<T> pushExch = new Exchange<>(pushReq, parent.exchange.multi);
         Stream.PushedStream<T> pushStream = createPushStream(parent, pushExch);
@@ -797,16 +827,44 @@
     }
 
     void resetStream(int streamid, int code) throws IOException {
-        Log.logError(
-            "Resetting stream {0,number,integer} with error code {1,number,integer}",
-            streamid, code);
-        ResetFrame frame = new ResetFrame(streamid, code);
-        sendFrame(frame);
-        closeStream(streamid);
+        try {
+            if (connection.channel().isOpen()) {
+                // no need to try & send a reset frame if the
+                // connection channel is already closed.
+                Log.logError(
+                        "Resetting stream {0,number,integer} with error code {1,number,integer}",
+                        streamid, code);
+                ResetFrame frame = new ResetFrame(streamid, code);
+                sendFrame(frame);
+            } else if (debug.on()) {
+                debug.log("Channel already closed, no need to reset stream %d",
+                          streamid);
+            }
+        } finally {
+            decrementStreamsCount(streamid);
+            closeStream(streamid);
+        }
+    }
+
+    // reduce count of streams by 1 if stream still exists
+    synchronized void decrementStreamsCount(int streamid) {
+        Stream<?> s = streams.get(streamid);
+        if (s == null || !s.deRegister())
+            return;
+        if (streamid % 2 == 1) {
+            numReservedClientStreams--;
+            assert numReservedClientStreams >= 0 :
+                    "negative client stream count for stream=" + streamid;
+        } else {
+            numReservedServerStreams--;
+            assert numReservedServerStreams >= 0 :
+                    "negative server stream count for stream=" + streamid;
+        }
     }
 
     void closeStream(int streamid) {
         if (debug.on()) debug.log("Closed stream %d", streamid);
+        boolean isClient = (streamid % 2) == 1;
         Stream<?> s = streams.remove(streamid);
         if (s != null) {
             // decrement the reference count on the HttpClientImpl
@@ -1148,14 +1206,19 @@
      * A simple tube subscriber for reading from the connection flow.
      */
     final class Http2TubeSubscriber implements TubeSubscriber {
-        volatile Flow.Subscription subscription;
-        volatile boolean completed;
-        volatile boolean dropped;
-        volatile Throwable error;
-        final ConcurrentLinkedQueue<ByteBuffer> queue
+        private volatile Flow.Subscription subscription;
+        private volatile boolean completed;
+        private volatile boolean dropped;
+        private volatile Throwable error;
+        private final ConcurrentLinkedQueue<ByteBuffer> queue
                 = new ConcurrentLinkedQueue<>();
-        final SequentialScheduler scheduler =
+        private final SequentialScheduler scheduler =
                 SequentialScheduler.synchronizedScheduler(this::processQueue);
+        private final HttpClientImpl client;
+
+        Http2TubeSubscriber(HttpClientImpl client) {
+            this.client = Objects.requireNonNull(client);
+        }
 
         final void processQueue() {
             try {
@@ -1179,6 +1242,12 @@
             }
         }
 
+        private final void runOrSchedule() {
+            if (client.isSelectorThread()) {
+                scheduler.runOrSchedule(client.theExecutor());
+            } else scheduler.runOrSchedule();
+        }
+
         @Override
         public void onSubscribe(Flow.Subscription subscription) {
             // supports being called multiple time.
@@ -1202,7 +1271,7 @@
             if (debug.on()) debug.log(() -> "onNext: got " + Utils.remaining(item)
                     + " bytes in " + item.size() + " buffers");
             queue.addAll(item);
-            scheduler.runOrSchedule(client().theExecutor());
+            runOrSchedule();
         }
 
         @Override
@@ -1210,15 +1279,18 @@
             if (debug.on()) debug.log(() -> "onError: " + throwable);
             error = throwable;
             completed = true;
-            scheduler.runOrSchedule(client().theExecutor());
+            runOrSchedule();
         }
 
         @Override
         public void onComplete() {
-            if (debug.on()) debug.log("EOF");
-            error = new EOFException("EOF reached while reading");
+            String msg = isActive()
+                    ? "EOF reached while reading"
+                    : "Idle connection closed by HTTP/2 peer";
+            if (debug.on()) debug.log(msg);
+            error = new EOFException(msg);
             completed = true;
-            scheduler.runOrSchedule(client().theExecutor());
+            runOrSchedule();
         }
 
         @Override
@@ -1230,6 +1302,10 @@
         }
     }
 
+    synchronized boolean isActive() {
+        return numReservedClientStreams > 0 || numReservedServerStreams > 0;
+    }
+
     @Override
     public final String toString() {
         return dbgString();
@@ -1242,10 +1318,10 @@
 
     static class HeaderDecoder extends ValidatingHeadersConsumer {
 
-        HttpHeadersImpl headers;
+        HttpHeadersBuilder headersBuilder;
 
         HeaderDecoder() {
-            this.headers = new HttpHeadersImpl();
+            this.headersBuilder = new HttpHeadersBuilder();
         }
 
         @Override
@@ -1253,11 +1329,11 @@
             String n = name.toString();
             String v = value.toString();
             super.onDecoded(n, v);
-            headers.addHeader(n, v);
+            headersBuilder.addHeader(n, v);
         }
 
-        HttpHeadersImpl headers() {
-            return headers;
+        HttpHeaders headers() {
+            return headersBuilder.build();
         }
     }
 
--- a/src/java.net.http/share/classes/jdk/internal/net/http/HttpClientImpl.java	Wed Jun 20 17:15:16 2018 +0200
+++ b/src/java.net.http/share/classes/jdk/internal/net/http/HttpClientImpl.java	Wed Jun 20 09:05:57 2018 -0700
@@ -28,11 +28,14 @@
 import javax.net.ssl.SSLContext;
 import javax.net.ssl.SSLParameters;
 import java.io.IOException;
+import java.io.UncheckedIOException;
 import java.lang.ref.Reference;
 import java.lang.ref.WeakReference;
 import java.net.Authenticator;
+import java.net.ConnectException;
 import java.net.CookieHandler;
 import java.net.ProxySelector;
+import java.net.http.HttpTimeoutException;
 import java.nio.ByteBuffer;
 import java.nio.channels.CancelledKeyException;
 import java.nio.channels.ClosedChannelException;
@@ -56,13 +59,14 @@
 import java.util.Set;
 import java.util.TreeSet;
 import java.util.concurrent.CompletableFuture;
-import java.util.concurrent.ConcurrentLinkedQueue;
+import java.util.concurrent.CompletionException;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Executor;
 import java.util.concurrent.Executors;
 import java.util.concurrent.ThreadFactory;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.concurrent.atomic.AtomicLong;
+import java.util.function.BooleanSupplier;
 import java.util.stream.Stream;
 import java.net.http.HttpClient;
 import java.net.http.HttpRequest;
@@ -121,6 +125,34 @@
         }
     }
 
+    /**
+     * A DelegatingExecutor is an executor that delegates tasks to
+     * a wrapped executor when it detects that the current thread
+     * is the SelectorManager thread. If the current thread is not
+     * the selector manager thread the given task is executed inline.
+     */
+    final static class DelegatingExecutor implements Executor {
+        private final BooleanSupplier isInSelectorThread;
+        private final Executor delegate;
+        DelegatingExecutor(BooleanSupplier isInSelectorThread, Executor delegate) {
+            this.isInSelectorThread = isInSelectorThread;
+            this.delegate = delegate;
+        }
+
+        Executor delegate() {
+            return delegate;
+        }
+
+        @Override
+        public void execute(Runnable command) {
+            if (isInSelectorThread.getAsBoolean()) {
+                delegate.execute(command);
+            } else {
+                command.run();
+            }
+        }
+    }
+
     private final CookieHandler cookieHandler;
     private final Redirect followRedirects;
     private final Optional<ProxySelector> userProxySelector;
@@ -128,7 +160,7 @@
     private final Authenticator authenticator;
     private final Version version;
     private final ConnectionPool connections;
-    private final Executor executor;
+    private final DelegatingExecutor delegatingExecutor;
     private final boolean isDefaultExecutor;
     // Security parameters
     private final SSLContext sslContext;
@@ -240,12 +272,11 @@
             ex = Executors.newCachedThreadPool(new DefaultThreadFactory(id));
             isDefaultExecutor = true;
         } else {
-            ex = builder.executor;
             isDefaultExecutor = false;
         }
+        delegatingExecutor = new DelegatingExecutor(this::isSelectorThread, ex);
         facadeRef = new WeakReference<>(facadeFactory.createFacade(this));
         client2 = new Http2ClientImpl(this);
-        executor = ex;
         cookieHandler = builder.cookieHandler;
         followRedirects = builder.followRedirects == null ?
                 Redirect.NEVER : builder.followRedirects;
@@ -489,21 +520,38 @@
     send(HttpRequest req, BodyHandler<T> responseHandler)
         throws IOException, InterruptedException
     {
+        CompletableFuture<HttpResponse<T>> cf = null;
         try {
-            return sendAsync(req, responseHandler, null).get();
+            cf = sendAsync(req, responseHandler, null, null);
+            return cf.get();
+        } catch (InterruptedException ie) {
+            if (cf != null )
+                cf.cancel(true);
+            throw ie;
         } catch (ExecutionException e) {
-            Throwable t = e.getCause();
-            if (t instanceof Error)
-                throw (Error)t;
-            if (t instanceof RuntimeException)
-                throw (RuntimeException)t;
-            else if (t instanceof IOException)
-                throw Utils.getIOException(t);
-            else
-                throw new InternalError("Unexpected exception", t);
+            final Throwable throwable = e.getCause();
+            final String msg = throwable.getMessage();
+
+            if (throwable instanceof IllegalArgumentException) {
+                throw new IllegalArgumentException(msg, throwable);
+            } else if (throwable instanceof SecurityException) {
+                throw new SecurityException(msg, throwable);
+            } else if (throwable instanceof HttpTimeoutException) {
+                throw new HttpTimeoutException(msg);
+            } else if (throwable instanceof ConnectException) {
+                ConnectException ce = new ConnectException(msg);
+                ce.initCause(throwable);
+                throw ce;
+            } else if (throwable instanceof IOException) {
+                throw new IOException(msg, throwable);
+            } else {
+                throw new IOException(msg, throwable);
+            }
         }
     }
 
+    private static final Executor ASYNC_POOL = new CompletableFuture<Void>().defaultExecutor();
+
     @Override
     public <T> CompletableFuture<HttpResponse<T>>
     sendAsync(HttpRequest userRequest, BodyHandler<T> responseHandler)
@@ -511,13 +559,20 @@
         return sendAsync(userRequest, responseHandler, null);
     }
 
-
     @Override
     public <T> CompletableFuture<HttpResponse<T>>
     sendAsync(HttpRequest userRequest,
               BodyHandler<T> responseHandler,
-              PushPromiseHandler<T> pushPromiseHandler)
-    {
+              PushPromiseHandler<T> pushPromiseHandler) {
+        return sendAsync(userRequest, responseHandler, pushPromiseHandler, delegatingExecutor.delegate);
+    }
+
+    private <T> CompletableFuture<HttpResponse<T>>
+    sendAsync(HttpRequest userRequest,
+              BodyHandler<T> responseHandler,
+              PushPromiseHandler<T> pushPromiseHandler,
+              Executor exchangeExecutor)    {
+
         Objects.requireNonNull(userRequest);
         Objects.requireNonNull(responseHandler);
 
@@ -536,9 +591,17 @@
             if (debugelapsed.on())
                 debugelapsed.log("ClientImpl (async) send %s", userRequest);
 
-            Executor executor = acc == null
-                    ? this.executor
-                    : new PrivilegedExecutor(this.executor, acc);
+            // When using sendAsync(...) we explicitly pass the
+            // executor's delegate as exchange executor to force
+            // asynchronous scheduling of the exchange.
+            // When using send(...) we don't specify any executor
+            // and default to using the client's delegating executor
+            // which only spawns asynchronous tasks if it detects
+            // that the current thread is the selector manager
+            // thread. This will cause everything to execute inline
+            // until we need to schedule some event with the selector.
+            Executor executor = exchangeExecutor == null
+                    ? this.delegatingExecutor : exchangeExecutor;
 
             MultiExchange<T> mex = new MultiExchange<>(userRequest,
                                                             requestImpl,
@@ -547,15 +610,18 @@
                                                             pushPromiseHandler,
                                                             acc);
             CompletableFuture<HttpResponse<T>> res =
-                    mex.responseAsync().whenComplete((b,t) -> unreference());
+                    mex.responseAsync(executor).whenComplete((b,t) -> unreference());
             if (DEBUGELAPSED) {
                 res = res.whenComplete(
                         (b,t) -> debugCompleted("ClientImpl (async)", start, userRequest));
             }
 
-            // makes sure that any dependent actions happen in the executor
-            res = res.whenCompleteAsync((r, t) -> { /* do nothing */}, executor);
-
+            // makes sure that any dependent actions happen in the CF default
+            // executor. This is only needed for sendAsync(...), when
+            // exchangeExecutor is non-null.
+            if (exchangeExecutor != null) {
+                res = res.whenCompleteAsync((r, t) -> { /* do nothing */}, ASYNC_POOL);
+            }
             return res;
         } catch(Throwable t) {
             unreference();
@@ -654,6 +720,7 @@
         }
 
         synchronized void shutdown() {
+            Log.logTrace("{0}: shutting down", getName());
             if (debug.on()) debug.log("SelectorManager shutting down");
             closed = true;
             try {
@@ -670,6 +737,7 @@
             List<AsyncEvent> readyList = new ArrayList<>();
             List<Runnable> resetList = new ArrayList<>();
             try {
+                if (Log.channel()) Log.logChannel(getName() + ": starting");
                 while (!Thread.currentThread().isInterrupted()) {
                     synchronized (this) {
                         assert errorList.isEmpty();
@@ -706,7 +774,7 @@
                                     throw new IOException("Channel closed");
                                 }
                             } catch (IOException e) {
-                                Log.logTrace("HttpClientImpl: " + e);
+                                Log.logTrace("{0}: {1}", getName(), e);
                                 if (debug.on())
                                     debug.log("Got " + e.getClass().getName()
                                               + " while handling registration events");
@@ -738,7 +806,9 @@
                     // Check whether client is still alive, and if not,
                     // gracefully stop this thread
                     if (!owner.isReferenced()) {
-                        Log.logTrace("HttpClient no longer referenced. Exiting...");
+                        Log.logTrace("{0}: {1}",
+                                getName(),
+                                "HttpClient no longer referenced. Exiting...");
                         return;
                     }
 
@@ -780,7 +850,9 @@
                         // Check whether client is still alive, and if not,
                         // gracefully stop this thread
                         if (!owner.isReferenced()) {
-                            Log.logTrace("HttpClient no longer referenced. Exiting...");
+                            Log.logTrace("{0}: {1}",
+                                    getName(),
+                                    "HttpClient no longer referenced. Exiting...");
                             return;
                         }
                         owner.purgeTimeoutsAndReturnNextDeadline();
@@ -831,17 +903,18 @@
 
                 }
             } catch (Throwable e) {
-                //e.printStackTrace();
                 if (!closed) {
                     // This terminates thread. So, better just print stack trace
                     String err = Utils.stackTrace(e);
-                    Log.logError("HttpClientImpl: fatal error: " + err);
+                    Log.logError("{0}: {1}: {2}", getName(),
+                            "HttpClientImpl shutting down due to fatal error", err);
                 }
                 if (debug.on()) debug.log("shutting down", e);
                 if (Utils.ASSERTIONSENABLED && !debug.on()) {
                     e.printStackTrace(System.err); // always print the stack
                 }
             } finally {
+                if (Log.channel()) Log.logChannel(getName() + ": stopping");
                 shutdown();
             }
         }
@@ -1013,13 +1086,15 @@
         return Optional.ofNullable(authenticator);
     }
 
-    /*package-private*/ final Executor theExecutor() {
-        return executor;
+    /*package-private*/ final DelegatingExecutor theExecutor() {
+        return delegatingExecutor;
     }
 
     @Override
     public final Optional<Executor> executor() {
-        return isDefaultExecutor ? Optional.empty() : Optional.of(executor);
+        return isDefaultExecutor
+                ? Optional.empty()
+                : Optional.of(delegatingExecutor.delegate());
     }
 
     ConnectionPool connectionPool() {
--- a/src/java.net.http/share/classes/jdk/internal/net/http/HttpConnection.java	Wed Jun 20 17:15:16 2018 +0200
+++ b/src/java.net.http/share/classes/jdk/internal/net/http/HttpConnection.java	Wed Jun 20 09:05:57 2018 -0700
@@ -27,7 +27,6 @@
 
 import java.io.Closeable;
 import java.io.IOException;
-import java.lang.System.Logger.Level;
 import java.net.InetSocketAddress;
 import java.nio.ByteBuffer;
 import java.nio.channels.SocketChannel;
@@ -250,7 +249,7 @@
      * @param request
      * @return
      */
-    BiPredicate<String,List<String>> headerFilter(HttpRequestImpl request) {
+    BiPredicate<String,String> headerFilter(HttpRequestImpl request) {
         if (isTunnel()) {
             // talking to a server through a proxy tunnel
             // don't send proxy-* headers to a plain server
@@ -280,12 +279,12 @@
     // start with "proxy-"
     private static HttpHeaders proxyTunnelHeaders(HttpRequestImpl request) {
         Map<String, List<String>> combined = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
-        combined.putAll(request.getSystemHeaders().map());
+        combined.putAll(request.getSystemHeadersBuilder().map());
         combined.putAll(request.headers().map()); // let user override system
 
         // keep only proxy-* - and also strip authorization headers
         // for disabled schemes
-        return ImmutableHeaders.of(combined, Utils.PROXY_TUNNEL_FILTER);
+        return HttpHeaders.of(combined, Utils.PROXY_TUNNEL_FILTER);
     }
 
     /* Returns either a plain HTTP connection or a plain tunnelling connection
--- a/src/java.net.http/share/classes/jdk/internal/net/http/HttpRequestBuilderImpl.java	Wed Jun 20 17:15:16 2018 +0200
+++ b/src/java.net.http/share/classes/jdk/internal/net/http/HttpRequestBuilderImpl.java	Wed Jun 20 09:05:57 2018 -0700
@@ -32,9 +32,9 @@
 import java.net.http.HttpClient;
 import java.net.http.HttpRequest;
 import java.net.http.HttpRequest.BodyPublisher;
-import jdk.internal.net.http.common.HttpHeadersImpl;
+
+import jdk.internal.net.http.common.HttpHeadersBuilder;
 import jdk.internal.net.http.common.Utils;
-import static java.lang.String.format;
 import static java.util.Objects.requireNonNull;
 import static jdk.internal.net.http.common.Utils.isValidName;
 import static jdk.internal.net.http.common.Utils.isValidValue;
@@ -42,7 +42,7 @@
 
 public class HttpRequestBuilderImpl implements HttpRequest.Builder {
 
-    private HttpHeadersImpl userHeaders;
+    private HttpHeadersBuilder headersBuilder;
     private URI uri;
     private String method;
     private boolean expectContinue;
@@ -54,13 +54,13 @@
         requireNonNull(uri, "uri must be non-null");
         checkURI(uri);
         this.uri = uri;
-        this.userHeaders = new HttpHeadersImpl();
+        this.headersBuilder = new HttpHeadersBuilder();
         this.method = "GET"; // default, as per spec
         this.version = Optional.empty();
     }
 
     public HttpRequestBuilderImpl() {
-        this.userHeaders = new HttpHeadersImpl();
+        this.headersBuilder = new HttpHeadersBuilder();
         this.method = "GET"; // default, as per spec
         this.version = Optional.empty();
     }
@@ -90,7 +90,7 @@
     public HttpRequestBuilderImpl copy() {
         HttpRequestBuilderImpl b = new HttpRequestBuilderImpl();
         b.uri = this.uri;
-        b.userHeaders = this.userHeaders.deepCopy();
+        b.headersBuilder = this.headersBuilder.structuralCopy();
         b.method = this.method;
         b.expectContinue = this.expectContinue;
         b.bodyPublisher = bodyPublisher;
@@ -106,7 +106,7 @@
         if (!isValidName(name)) {
             throw newIAE("invalid header name: \"%s\"", name);
         }
-        if (!Utils.ALLOWED_HEADERS.test(name)) {
+        if (!Utils.ALLOWED_HEADERS.test(name, null)) {
             throw newIAE("restricted header name: \"%s\"", name);
         }
         if (!isValidValue(value)) {
@@ -117,14 +117,14 @@
     @Override
     public HttpRequestBuilderImpl setHeader(String name, String value) {
         checkNameAndValue(name, value);
-        userHeaders.setHeader(name, value);
+        headersBuilder.setHeader(name, value);
         return this;
     }
 
     @Override
     public HttpRequestBuilderImpl header(String name, String value) {
         checkNameAndValue(name, value);
-        userHeaders.addHeader(name, value);
+        headersBuilder.addHeader(name, value);
         return this;
     }
 
@@ -155,7 +155,7 @@
         return this;
     }
 
-    HttpHeadersImpl headers() {  return userHeaders; }
+    HttpHeadersBuilder headersBuilder() {  return headersBuilder; }
 
     URI uri() { return uri; }
 
@@ -216,6 +216,13 @@
         if (uri == null)
             throw new IllegalStateException("uri is null");
         assert method != null;
+        return new ImmutableHttpRequest(this);
+    }
+
+    public HttpRequestImpl buildForWebSocket() {
+        if (uri == null)
+            throw new IllegalStateException("uri is null");
+        assert method != null;
         return new HttpRequestImpl(this);
     }
 
--- a/src/java.net.http/share/classes/jdk/internal/net/http/HttpRequestImpl.java	Wed Jun 20 17:15:16 2018 +0200
+++ b/src/java.net.http/share/classes/jdk/internal/net/http/HttpRequestImpl.java	Wed Jun 20 09:05:57 2018 -0700
@@ -41,16 +41,16 @@
 import java.net.http.HttpClient;
 import java.net.http.HttpHeaders;
 import java.net.http.HttpRequest;
-import jdk.internal.net.http.common.HttpHeadersImpl;
+import jdk.internal.net.http.common.HttpHeadersBuilder;
 import jdk.internal.net.http.common.Utils;
 import jdk.internal.net.http.websocket.WebSocketRequest;
 
 import static jdk.internal.net.http.common.Utils.ALLOWED_HEADERS;
 
-class HttpRequestImpl extends HttpRequest implements WebSocketRequest {
+public class HttpRequestImpl extends HttpRequest implements WebSocketRequest {
 
     private final HttpHeaders userHeaders;
-    private final HttpHeadersImpl systemHeaders;
+    private final HttpHeadersBuilder systemHeadersBuilder;
     private final URI uri;
     private volatile Proxy proxy; // ensure safe publishing
     private final InetSocketAddress authority; // only used when URI not specified
@@ -78,8 +78,8 @@
     public HttpRequestImpl(HttpRequestBuilderImpl builder) {
         String method = builder.method();
         this.method = method == null ? "GET" : method;
-        this.userHeaders = ImmutableHeaders.of(builder.headers().map(), ALLOWED_HEADERS);
-        this.systemHeaders = new HttpHeadersImpl();
+        this.userHeaders = HttpHeaders.of(builder.headersBuilder().map(), ALLOWED_HEADERS);
+        this.systemHeadersBuilder = new HttpHeadersBuilder();
         this.uri = builder.uri();
         assert uri != null;
         this.proxy = null;
@@ -106,21 +106,23 @@
                 "uri must be non null");
         Duration timeout = request.timeout().orElse(null);
         this.method = method == null ? "GET" : method;
-        this.userHeaders = ImmutableHeaders.validate(request.headers());
+        this.userHeaders = HttpHeaders.of(request.headers().map(), Utils.VALIDATE_USER_HEADER);
         if (request instanceof HttpRequestImpl) {
             // all cases exception WebSocket should have a new system headers
             this.isWebSocket = ((HttpRequestImpl) request).isWebSocket;
             if (isWebSocket) {
-                this.systemHeaders = ((HttpRequestImpl) request).systemHeaders;
+                this.systemHeadersBuilder = ((HttpRequestImpl)request).systemHeadersBuilder;
             } else {
-                this.systemHeaders = new HttpHeadersImpl();
+                this.systemHeadersBuilder = new HttpHeadersBuilder();
             }
         } else {
             HttpRequestBuilderImpl.checkURI(requestURI);
             checkTimeout(timeout);
-            this.systemHeaders = new HttpHeadersImpl();
+            this.systemHeadersBuilder = new HttpHeadersBuilder();
         }
-        this.systemHeaders.setHeader("User-Agent", USER_AGENT);
+        if (!userHeaders.firstValue("User-Agent").isPresent()) {
+            this.systemHeadersBuilder.setHeader("User-Agent", USER_AGENT);
+        }
         this.uri = requestURI;
         if (isWebSocket) {
             // WebSocket determines and sets the proxy itself
@@ -169,8 +171,10 @@
         this.method = method == null? "GET" : method;
         this.userHeaders = other.userHeaders;
         this.isWebSocket = other.isWebSocket;
-        this.systemHeaders = new HttpHeadersImpl();
-        this.systemHeaders.setHeader("User-Agent", USER_AGENT);
+        this.systemHeadersBuilder = new HttpHeadersBuilder();
+        if (!userHeaders.firstValue("User-Agent").isPresent()) {
+            this.systemHeadersBuilder.setHeader("User-Agent", USER_AGENT);
+        }
         this.uri = uri;
         this.proxy = other.proxy;
         this.expectContinue = other.expectContinue;
@@ -189,8 +193,8 @@
         // to the connection pool (we might need to revisit this constructor later)
         assert "CONNECT".equalsIgnoreCase(method);
         this.method = method;
-        this.systemHeaders = new HttpHeadersImpl();
-        this.userHeaders = ImmutableHeaders.of(headers);
+        this.systemHeadersBuilder = new HttpHeadersBuilder();
+        this.userHeaders = headers;
         this.uri = URI.create("socket://" + authority.getHostString() + ":"
                               + Integer.toString(authority.getPort()) + "/");
         this.proxy = null;
@@ -218,14 +222,14 @@
      * parent.
      */
     static HttpRequestImpl createPushRequest(HttpRequestImpl parent,
-                                             HttpHeadersImpl headers)
+                                             HttpHeaders headers)
         throws IOException
     {
         return new HttpRequestImpl(parent, headers);
     }
 
     // only used for push requests
-    private HttpRequestImpl(HttpRequestImpl parent, HttpHeadersImpl headers)
+    private HttpRequestImpl(HttpRequestImpl parent, HttpHeaders headers)
         throws IOException
     {
         this.method = headers.firstValue(":method")
@@ -240,8 +244,8 @@
         sb.append(scheme).append("://").append(authority).append(path);
         this.uri = URI.create(sb.toString());
         this.proxy = null;
-        this.userHeaders = ImmutableHeaders.of(headers.map(), ALLOWED_HEADERS);
-        this.systemHeaders = parent.systemHeaders;
+        this.userHeaders = HttpHeaders.of(headers.map(), ALLOWED_HEADERS);
+        this.systemHeadersBuilder = parent.systemHeadersBuilder;
         this.expectContinue = parent.expectContinue;
         this.secure = parent.secure;
         this.requestPublisher = parent.requestPublisher;
@@ -264,9 +268,9 @@
     InetSocketAddress authority() { return authority; }
 
     void setH2Upgrade(Http2ClientImpl h2client) {
-        systemHeaders.setHeader("Connection", "Upgrade, HTTP2-Settings");
-        systemHeaders.setHeader("Upgrade", "h2c");
-        systemHeaders.setHeader("HTTP2-Settings", h2client.getSettingsString());
+        systemHeadersBuilder.setHeader("Connection", "Upgrade, HTTP2-Settings");
+        systemHeadersBuilder.setHeader("Upgrade", "h2c");
+        systemHeadersBuilder.setHeader("HTTP2-Settings", h2client.getSettingsString());
     }
 
     @Override
@@ -332,18 +336,18 @@
 
     HttpHeaders getUserHeaders() { return userHeaders; }
 
-    HttpHeadersImpl getSystemHeaders() { return systemHeaders; }
+    HttpHeadersBuilder getSystemHeadersBuilder() { return systemHeadersBuilder; }
 
     @Override
     public Optional<HttpClient.Version> version() { return version; }
 
     void addSystemHeader(String name, String value) {
-        systemHeaders.addHeader(name, value);
+        systemHeadersBuilder.addHeader(name, value);
     }
 
     @Override
     public void setSystemHeader(String name, String value) {
-        systemHeaders.setHeader(name, value);
+        systemHeadersBuilder.setHeader(name, value);
     }
 
     InetSocketAddress getAddress() {
--- a/src/java.net.http/share/classes/jdk/internal/net/http/ImmutableHeaders.java	Wed Jun 20 17:15:16 2018 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,116 +0,0 @@
-/*
- * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
- *
- * This code is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 only, as
- * published by the Free Software Foundation.  Oracle designates this
- * particular file as subject to the "Classpath" exception as provided
- * by Oracle in the LICENSE file that accompanied this code.
- *
- * This code is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
- * version 2 for more details (a copy is included in the LICENSE file that
- * accompanied this code).
- *
- * You should have received a copy of the GNU General Public License version
- * 2 along with this work; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
- * or visit www.oracle.com if you need additional information or have any
- * questions.
- */
-
-package jdk.internal.net.http;
-
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
-import java.util.Objects;
-import java.util.TreeMap;
-import java.util.function.BiPredicate;
-import java.util.function.Predicate;
-import java.net.http.HttpHeaders;
-import jdk.internal.net.http.common.HttpHeadersImpl;
-import jdk.internal.net.http.common.Utils;
-import static java.util.Collections.emptyMap;
-import static java.util.Collections.unmodifiableList;
-import static java.util.Collections.unmodifiableMap;
-import static java.util.Objects.requireNonNull;
-
-final class ImmutableHeaders extends HttpHeaders {
-
-    private final Map<String, List<String>> map;
-
-    public static ImmutableHeaders empty() {
-        return of(emptyMap());
-    }
-
-    public static ImmutableHeaders of(Map<String, List<String>> src) {
-        return of(src, x -> true);
-    }
-
-    public static ImmutableHeaders of(HttpHeaders headers) {
-        return (headers instanceof ImmutableHeaders)
-                ? (ImmutableHeaders)headers
-                : of(headers.map());
-    }
-
-    static ImmutableHeaders validate(HttpHeaders headers) {
-        if (headers instanceof ImmutableHeaders) {
-            return of(headers);
-        }
-        if (headers instanceof HttpHeadersImpl) {
-            return of(headers);
-        }
-        Map<String, List<String>> map = headers.map();
-        return new ImmutableHeaders(map, Utils.VALIDATE_USER_HEADER);
-    }
-
-    public static ImmutableHeaders of(Map<String, List<String>> src,
-                                      Predicate<? super String> keyAllowed) {
-        requireNonNull(src, "src");
-        requireNonNull(keyAllowed, "keyAllowed");
-        return new ImmutableHeaders(src, headerAllowed(keyAllowed));
-    }
-
-    public static ImmutableHeaders of(Map<String, List<String>> src,
-                                      BiPredicate<? super String, ? super List<String>> headerAllowed) {
-        requireNonNull(src, "src");
-        requireNonNull(headerAllowed, "headerAllowed");
-        return new ImmutableHeaders(src, headerAllowed);
-    }
-
-    private ImmutableHeaders(Map<String, List<String>> src,
-                             BiPredicate<? super String, ? super List<String>> headerAllowed) {
-        Map<String, List<String>> m = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
-        src.entrySet().stream()
-                .forEach(e -> addIfAllowed(e, headerAllowed, m));
-        this.map = unmodifiableMap(m);
-    }
-
-    private static void addIfAllowed(Map.Entry<String, List<String>> e,
-                                     BiPredicate<? super String, ? super List<String>> headerAllowed,
-                                     Map<String, List<String>> map) {
-        String key = e.getKey();
-        List<String> values = unmodifiableValues(e.getValue());
-        if (headerAllowed.test(key, values)) {
-            map.put(key, values);
-        }
-    }
-
-    private static List<String> unmodifiableValues(List<String> values) {
-        return unmodifiableList(new ArrayList<>(Objects.requireNonNull(values)));
-    }
-
-    private static BiPredicate<String, List<String>> headerAllowed(Predicate<? super String> keyAllowed) {
-        return (n,v) -> keyAllowed.test(n);
-    }
-
-    @Override
-    public Map<String, List<String>> map() {
-        return map;
-    }
-}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/java.net.http/share/classes/jdk/internal/net/http/ImmutableHttpRequest.java	Wed Jun 20 09:05:57 2018 -0700
@@ -0,0 +1,85 @@
+/*
+ * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package jdk.internal.net.http;
+
+import java.net.URI;
+import java.net.http.HttpHeaders;
+import java.net.http.HttpRequest;
+import java.time.Duration;
+import java.util.Objects;
+import java.util.Optional;
+import java.net.http.HttpClient.Version;
+import static jdk.internal.net.http.common.Utils.ALLOWED_HEADERS;
+
+final class ImmutableHttpRequest extends HttpRequest {
+
+    private final String method;
+    private final URI uri;
+    private final HttpHeaders headers;
+    private final Optional<BodyPublisher> requestPublisher;
+    private final boolean expectContinue;
+    private final Optional<Duration> timeout;
+    private final Optional<Version> version;
+
+    /** Creates an ImmutableHttpRequest from the given builder. */
+    ImmutableHttpRequest(HttpRequestBuilderImpl builder) {
+        this.method = Objects.requireNonNull(builder.method());
+        this.uri = Objects.requireNonNull(builder.uri());
+        this.headers = HttpHeaders.of(builder.headersBuilder().map(), ALLOWED_HEADERS);
+        this.requestPublisher = Optional.ofNullable(builder.bodyPublisher());
+        this.expectContinue = builder.expectContinue();
+        this.timeout = Optional.ofNullable(builder.timeout());
+        this.version = Objects.requireNonNull(builder.version());
+    }
+
+    @Override
+    public String method() { return method; }
+
+    @Override
+    public URI uri() { return uri; }
+
+    @Override
+    public HttpHeaders headers() {
+        return headers;
+    }
+
+    @Override
+    public Optional<BodyPublisher> bodyPublisher() { return requestPublisher; }
+
+    @Override
+    public boolean expectContinue() { return expectContinue; }
+
+    @Override
+    public Optional<Duration> timeout() { return timeout; }
+
+    @Override
+    public Optional<Version> version() { return version; }
+
+    @Override
+    public String toString() {
+        return uri.toString() + " " + method;
+    }
+}
--- a/src/java.net.http/share/classes/jdk/internal/net/http/MultiExchange.java	Wed Jun 20 17:15:16 2018 +0200
+++ b/src/java.net.http/share/classes/jdk/internal/net/http/MultiExchange.java	Wed Jun 20 09:05:57 2018 -0700
@@ -26,7 +26,7 @@
 package jdk.internal.net.http;
 
 import java.io.IOException;
-import java.lang.System.Logger.Level;
+import java.net.ConnectException;
 import java.time.Duration;
 import java.util.Iterator;
 import java.util.LinkedList;
@@ -69,7 +69,7 @@
     final AccessControlContext acc;
     final HttpClientImpl client;
     final HttpResponse.BodyHandler<T> responseHandler;
-    final Executor executor;
+    final HttpClientImpl.DelegatingExecutor executor;
     final AtomicInteger attempts = new AtomicInteger();
     HttpRequestImpl currentreq; // used for retries & redirect
     HttpRequestImpl previousreq; // used for retries & redirect
@@ -124,8 +124,8 @@
 
         if (pushPromiseHandler != null) {
             Executor executor = acc == null
-                    ? this.executor
-                    : new PrivilegedExecutor(this.executor, acc);
+                    ? this.executor.delegate()
+                    : new PrivilegedExecutor(this.executor.delegate(), acc);
             this.pushGroup = new PushGroup<>(pushPromiseHandler, request, executor);
         } else {
             pushGroup = null;
@@ -193,7 +193,7 @@
         getExchange().cancel(cause);
     }
 
-    public CompletableFuture<HttpResponse<T>> responseAsync() {
+    public CompletableFuture<HttpResponse<T>> responseAsync(Executor executor) {
         CompletableFuture<Void> start = new MinimalFuture<>();
         CompletableFuture<HttpResponse<T>> cf = responseAsync0(start);
         start.completeAsync( () -> null, executor); // trigger execution
@@ -285,13 +285,22 @@
 
     private static boolean retryPostValue() {
         String s = Utils.getNetProperty("jdk.httpclient.enableAllMethodRetry");
-        if (s == "" || "true".equals(s))
-            return true;
-        return false;
+        if (s == null)
+            return false;
+        return s.isEmpty() ? true : Boolean.parseBoolean(s);
+    }
+
+    private static boolean retryConnect() {
+        String s = Utils.getNetProperty("jdk.httpclient.disableRetryConnect");
+        if (s == null)
+            return false;
+        return s.isEmpty() ? true : Boolean.parseBoolean(s);
     }
 
     /** True if ALL ( even non-idempotent ) requests can be automatic retried. */
     private static final boolean RETRY_ALWAYS = retryPostValue();
+    /** True if ConnectException should cause a retry. Enabled by default */
+    private static final boolean RETRY_CONNECT = retryConnect();
 
     /** Returns true is given request has an idempotent method. */
     private static boolean isIdempotentRequest(HttpRequest request) {
@@ -307,13 +316,23 @@
 
     /** Returns true if the given request can be automatically retried. */
     private static boolean canRetryRequest(HttpRequest request) {
+        if (RETRY_ALWAYS)
+            return true;
         if (isIdempotentRequest(request))
             return true;
-        if (RETRY_ALWAYS)
-            return true;
         return false;
     }
 
+    private boolean retryOnFailure(Throwable t) {
+        return t instanceof ConnectionExpiredException
+                || (RETRY_CONNECT && (t instanceof ConnectException));
+    }
+
+    private Throwable retryCause(Throwable t) {
+        Throwable cause = t instanceof ConnectionExpiredException ? t.getCause() : t;
+        return cause == null ? t : cause;
+    }
+
     /**
      * Takes a Throwable and returns a suitable CompletableFuture that is
      * completed exceptionally, or null.
@@ -326,21 +345,20 @@
         }
         if (cancelled && t instanceof IOException) {
             t = new HttpTimeoutException("request timed out");
-        } else if (t instanceof ConnectionExpiredException) {
-            Throwable cause = t;
-            if (t.getCause() != null) {
-                cause = t.getCause(); // unwrap the ConnectionExpiredException
-            }
+        } else if (retryOnFailure(t)) {
+            Throwable cause = retryCause(t);
 
-            if (!canRetryRequest(currentreq)) {
-                return failedFuture(cause); // fails with original cause
+            if (!(t instanceof ConnectException)) {
+                if (!canRetryRequest(currentreq)) {
+                    return failedFuture(cause); // fails with original cause
+                }
             }
 
             // allow the retry mechanism to do its work
             retryCause = cause;
             if (!expiredOnce) {
                 if (debug.on())
-                    debug.log("ConnectionExpiredException (async): retrying...", t);
+                    debug.log(t.getClass().getSimpleName() + " (async): retrying...", t);
                 expiredOnce = true;
                 // The connection was abruptly closed.
                 // We return null to retry the same request a second time.
@@ -350,9 +368,11 @@
                 previousreq = currentreq;
                 return null;
             } else {
-                if (debug.on())
-                    debug.log("ConnectionExpiredException (async): already retried once.", t);
-                if (t.getCause() != null) t = t.getCause();
+                if (debug.on()) {
+                    debug.log(t.getClass().getSimpleName()
+                            + " (async): already retried once.", t);
+                }
+                t = cause;
             }
         }
         return failedFuture(t);
@@ -364,9 +384,10 @@
         }
         @Override
         public void handle() {
-            if (debug.on())
+            if (debug.on()) {
                 debug.log("Cancelling MultiExchange due to timeout for request %s",
-                          request);
+                        request);
+            }
             cancel(new HttpTimeoutException("request timed out"));
         }
     }
--- a/src/java.net.http/share/classes/jdk/internal/net/http/PlainHttpConnection.java	Wed Jun 20 17:15:16 2018 +0200
+++ b/src/java.net.http/share/classes/jdk/internal/net/http/PlainHttpConnection.java	Wed Jun 20 09:05:57 2018 -0700
@@ -26,10 +26,8 @@
 package jdk.internal.net.http;
 
 import java.io.IOException;
-import java.lang.System.Logger.Level;
 import java.net.InetSocketAddress;
 import java.net.StandardSocketOptions;
-import java.nio.ByteBuffer;
 import java.nio.channels.SelectableChannel;
 import java.nio.channels.SelectionKey;
 import java.nio.channels.SocketChannel;
@@ -91,14 +89,16 @@
                 // complete async since the event runs on the SelectorManager thread
                 cf.completeAsync(() -> null, client().theExecutor());
             } catch (Throwable e) {
-                client().theExecutor().execute( () -> cf.completeExceptionally(e));
+                Throwable t = Utils.toConnectException(e);
+                client().theExecutor().execute( () -> cf.completeExceptionally(t));
+                close();
             }
         }
 
         @Override
         public void abort(IOException ioe) {
+            client().theExecutor().execute( () -> cf.completeExceptionally(ioe));
             close();
-            client().theExecutor().execute( () -> cf.completeExceptionally(ioe));
         }
     }
 
@@ -114,7 +114,7 @@
             try {
                  finished = AccessController.doPrivileged(pa);
             } catch (PrivilegedActionException e) {
-                cf.completeExceptionally(e.getCause());
+               throw e.getCause();
             }
             if (finished) {
                 if (debug.on()) debug.log("connect finished without blocking");
@@ -125,7 +125,13 @@
                 client().registerEvent(new ConnectEvent(cf));
             }
         } catch (Throwable throwable) {
-            cf.completeExceptionally(throwable);
+            cf.completeExceptionally(Utils.toConnectException(throwable));
+            try {
+                close();
+            } catch (Exception x) {
+                if (debug.on())
+                    debug.log("Failed to close channel after unsuccessful connect");
+            }
         }
         return cf;
     }
--- a/src/java.net.http/share/classes/jdk/internal/net/http/RedirectFilter.java	Wed Jun 20 17:15:16 2018 +0200
+++ b/src/java.net.http/share/classes/jdk/internal/net/http/RedirectFilter.java	Wed Jun 20 09:05:57 2018 -0700
@@ -43,6 +43,13 @@
     static final int DEFAULT_MAX_REDIRECTS = 5;
     URI uri;
 
+    /*
+     * NOT_MODIFIED status code results from a conditional GET where
+     * the server does not (must not) return a response body because
+     * the condition specified in the request disallows it
+     */
+    static final int HTTP_NOT_MODIFIED = 304;
+
     static final int max_redirects = Utils.getIntegerNetProperty(
             "jdk.httpclient.redirects.retrylimit", DEFAULT_MAX_REDIRECTS
     );
@@ -91,6 +98,10 @@
         if (rcode == 200 || policy == HttpClient.Redirect.NEVER) {
             return null;
         }
+
+        if (rcode == HTTP_NOT_MODIFIED)
+            return null;
+
         if (rcode >= 300 && rcode <= 399) {
             URI redir = getRedirectedURI(r.headers());
             String newMethod = redirectedMethod(rcode, method);
--- a/src/java.net.http/share/classes/jdk/internal/net/http/Response.java	Wed Jun 20 17:15:16 2018 +0200
+++ b/src/java.net.http/share/classes/jdk/internal/net/http/Response.java	Wed Jun 20 09:05:57 2018 -0700
@@ -64,7 +64,7 @@
              int statusCode,
              HttpClient.Version version,
              boolean isConnectResponse) {
-        this.headers = ImmutableHeaders.of(headers);
+        this.headers = headers;
         this.request = req;
         this.version = version;
         this.exchange = exchange;
--- a/src/java.net.http/share/classes/jdk/internal/net/http/ResponseContent.java	Wed Jun 20 17:15:16 2018 +0200
+++ b/src/java.net.http/share/classes/jdk/internal/net/http/ResponseContent.java	Wed Jun 20 09:05:57 2018 -0700
@@ -26,7 +26,6 @@
 package jdk.internal.net.http;
 
 import java.io.IOException;
-import java.lang.System.Logger.Level;
 import java.nio.ByteBuffer;
 import java.util.ArrayList;
 import java.util.Collections;
@@ -34,9 +33,9 @@
 import java.util.function.Consumer;
 import java.net.http.HttpHeaders;
 import java.net.http.HttpResponse;
-
 import jdk.internal.net.http.common.Logger;
 import jdk.internal.net.http.common.Utils;
+import static java.lang.String.format;
 
 /**
  * Implements chunked/fixed transfer encodings of HTTP/1.1 responses.
@@ -95,6 +94,9 @@
 
     interface BodyParser extends Consumer<ByteBuffer> {
         void onSubscribe(AbstractSubscription sub);
+        // A current-state message suitable for inclusion in an exception
+        // detail message.
+        String currentStateMessage();
     }
 
     // Returns a parser that will take care of parsing the received byte
@@ -145,6 +147,11 @@
         }
 
         @Override
+        public String currentStateMessage() {
+            return format("chunked transfer encoding, state: %s", state);
+        }
+
+        @Override
         public void accept(ByteBuffer b) {
             if (closedExceptionally != null) {
                 if (debug.on())
@@ -425,6 +432,12 @@
         }
 
         @Override
+        public String currentStateMessage() {
+            return format("fixed content-length: %d, bytes received: %d",
+                          contentLength, contentLength - remaining);
+        }
+
+        @Override
         public void accept(ByteBuffer b) {
             if (closedExceptionally != null) {
                 if (debug.on())
--- a/src/java.net.http/share/classes/jdk/internal/net/http/SocketTube.java	Wed Jun 20 17:15:16 2018 +0200
+++ b/src/java.net.http/share/classes/jdk/internal/net/http/SocketTube.java	Wed Jun 20 09:05:57 2018 -0700
@@ -41,6 +41,7 @@
 import jdk.internal.net.http.common.BufferSupplier;
 import jdk.internal.net.http.common.Demand;
 import jdk.internal.net.http.common.FlowTube;
+import jdk.internal.net.http.common.Log;
 import jdk.internal.net.http.common.Logger;
 import jdk.internal.net.http.common.SequentialScheduler;
 import jdk.internal.net.http.common.SequentialScheduler.DeferredCompleter;
@@ -149,6 +150,10 @@
     void signalClosed() {
         // Ensures that the subscriber will be terminated and that future
         // subscribers will be notified when the connection is closed.
+        if (Log.channel()) {
+            Log.logChannel("Connection close signalled: connection closed locally ({0})",
+                    channelDescr());
+        }
         readPublisher.subscriptionImpl.signalError(
                 new IOException("connection closed locally"));
     }
@@ -364,6 +369,10 @@
         void startSubscription() {
             try {
                 if (debug.on()) debug.log("write: starting subscription");
+                if (Log.channel()) {
+                    Log.logChannel("Start requesting bytes for writing to channel: {0}",
+                            channelDescr());
+                }
                 assert client.isSelectorThread();
                 // make sure read registrations are handled before;
                 readPublisher.subscriptionImpl.handlePending();
@@ -409,6 +418,10 @@
 
         void signalError(Throwable error) {
             debug.log(() -> "write error: " + error);
+            if (Log.channel()) {
+                Log.logChannel("Failed to write to channel ({0}: {1})",
+                        channelDescr(), error);
+            }
             completed = true;
             readPublisher.signalError(error);
         }
@@ -455,6 +468,7 @@
 
             @Override
             public void cancel() {
+                if (debug.on()) debug.log("write: cancel");
                 dropSubscription();
                 upstreamSubscription.cancel();
             }
@@ -558,6 +572,10 @@
             if (!errorRef.compareAndSet(null, error)) {
                 return;
             }
+            if (Log.channel()) {
+                Log.logChannel("Error signalled on channel {0}: {1}",
+                        channelDescr(), error);
+            }
             subscriptionImpl.handleError();
         }
 
@@ -665,6 +683,7 @@
             final void handleSubscribeEvent() {
                 assert client.isSelectorThread();
                 debug.log("subscribe event raised");
+                if (Log.channel()) Log.logChannel("Start reading from {0}", channelDescr());
                 readScheduler.runOrSchedule();
                 if (readScheduler.isStopped() || completed) {
                     // if already completed or stopped we can handle any
@@ -702,6 +721,10 @@
             @Override
             public final void cancel() {
                 pauseReadEvent();
+                if (Log.channel()) {
+                    Log.logChannel("Read subscription cancelled for channel {0}",
+                            channelDescr());
+                }
                 readScheduler.stop();
             }
 
@@ -726,6 +749,10 @@
                     return;
                 }
                 if (debug.on()) debug.log("got read error: " + error);
+                if (Log.channel()) {
+                    Log.logChannel("Read error signalled on channel {0}: {1}",
+                            channelDescr(), error);
+                }
                 readScheduler.runOrSchedule();
             }
 
@@ -772,6 +799,10 @@
                             if (debug.on())
                                 debug.log("Sending error " + error
                                           + " to subscriber " + subscriber);
+                            if (Log.channel()) {
+                                Log.logChannel("Raising error with subscriber for {0}: {1}",
+                                        channelDescr(), error);
+                            }
                             current.errorRef.compareAndSet(null, error);
                             current.signalCompletion();
                             readScheduler.stop();
@@ -788,6 +819,10 @@
                                 if (bytes == EOF) {
                                     if (!completed) {
                                         if (debug.on()) debug.log("got read EOF");
+                                        if (Log.channel()) {
+                                            Log.logChannel("EOF read from channel: {0}",
+                                                        channelDescr());
+                                        }
                                         completed = true;
                                         // safe to pause here because we're finished
                                         // anyway.
@@ -849,6 +884,12 @@
                     if (debug.on()) debug.log("Unexpected exception in read loop", t);
                     signalError(t);
                 } finally {
+                    if (readScheduler.isStopped()) {
+                        if (debug.on()) debug.log("Read scheduler stopped");
+                        if (Log.channel()) {
+                            Log.logChannel("Stopped reading from channel {0}", channelDescr());
+                        }
+                    }
                     handlePending();
                 }
             }
@@ -1238,4 +1279,8 @@
     final String dbgString() {
         return "SocketTube("+id+")";
     }
+
+    final String channelDescr() {
+        return String.valueOf(channel);
+    }
 }
--- a/src/java.net.http/share/classes/jdk/internal/net/http/Stream.java	Wed Jun 20 17:15:16 2018 +0200
+++ b/src/java.net.http/share/classes/jdk/internal/net/http/Stream.java	Wed Jun 20 09:05:57 2018 -0700
@@ -25,9 +25,9 @@
 
 package jdk.internal.net.http;
 
+import java.io.EOFException;
 import java.io.IOException;
 import java.io.UncheckedIOException;
-import java.lang.System.Logger.Level;
 import java.net.URI;
 import java.nio.ByteBuffer;
 import java.util.ArrayList;
@@ -39,6 +39,7 @@
 import java.util.concurrent.Executor;
 import java.util.concurrent.Flow;
 import java.util.concurrent.Flow.Subscription;
+import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicReference;
 import java.util.function.BiPredicate;
 import java.net.http.HttpClient;
@@ -114,8 +115,8 @@
     final Http2Connection connection;
     final HttpRequestImpl request;
     final HeadersConsumer rspHeadersConsumer;
-    final HttpHeadersImpl responseHeaders;
-    final HttpHeadersImpl requestPseudoHeaders;
+    final HttpHeadersBuilder responseHeadersBuilder;
+    final HttpHeaders requestPseudoHeaders;
     volatile HttpResponse.BodySubscriber<T> responseSubscriber;
     final HttpRequest.BodyPublisher requestPublisher;
     volatile RequestSubscriber requestSubscriber;
@@ -133,6 +134,8 @@
     private volatile boolean closed;
     private volatile boolean endStreamSent;
 
+    final AtomicBoolean deRegistered = new AtomicBoolean(false);
+
     // state flags
     private boolean requestSent, responseReceived;
 
@@ -171,7 +174,7 @@
                 Http2Frame frame = inputQ.peek();
                 if (frame instanceof ResetFrame) {
                     inputQ.remove();
-                    handleReset((ResetFrame)frame);
+                    handleReset((ResetFrame)frame, subscriber);
                     return;
                 }
                 DataFrame df = (DataFrame)frame;
@@ -185,6 +188,7 @@
                     Log.logTrace("responseSubscriber.onComplete");
                     if (debug.on()) debug.log("incoming: onComplete");
                     sched.stop();
+                    connection.decrementStreamsCount(streamid);
                     subscriber.onComplete();
                     onCompleteCalled = true;
                     setEndStreamReceived();
@@ -198,6 +202,7 @@
                         Log.logTrace("responseSubscriber.onComplete");
                         if (debug.on()) debug.log("incoming: onComplete");
                         sched.stop();
+                        connection.decrementStreamsCount(streamid);
                         subscriber.onComplete();
                         onCompleteCalled = true;
                         setEndStreamReceived();
@@ -251,6 +256,10 @@
         return true; // end of stream
     }
 
+    boolean deRegister() {
+        return deRegistered.compareAndSet(false, true);
+    }
+
     @Override
     CompletableFuture<T> readBodyAsync(HttpResponse.BodyHandler<T> handler,
                                        boolean returnConnectionToPool,
@@ -258,6 +267,7 @@
     {
         try {
             Log.logTrace("Reading body on stream {0}", streamid);
+            debug.log("Getting BodySubscriber for: " + response);
             BodySubscriber<T> bodySubscriber = handler.apply(new ResponseInfoImpl(response));
             CompletableFuture<T> cf = receiveData(bodySubscriber, executor);
 
@@ -337,10 +347,9 @@
         this.windowController = windowController;
         this.request = e.request();
         this.requestPublisher = request.requestPublisher;  // may be null
-        responseHeaders = new HttpHeadersImpl();
-        rspHeadersConsumer = new HeadersConsumer();
-        this.requestPseudoHeaders = new HttpHeadersImpl();
-        // NEW
+        this.responseHeadersBuilder = new HttpHeadersBuilder();
+        this.rspHeadersConsumer = new HeadersConsumer();
+        this.requestPseudoHeaders = createPseudoHeaders(request);
         this.windowUpdater = new StreamWindowUpdateSender(connection);
     }
 
@@ -391,6 +400,7 @@
     }
 
     protected void handleResponse() throws IOException {
+        HttpHeaders responseHeaders = responseHeadersBuilder.build();
         responseCode = (int)responseHeaders
                 .firstValueAsLong(":status")
                 .orElseThrow(() -> new IOException("no statuscode in response"));
@@ -424,25 +434,57 @@
         } else if (closed) {
             Log.logTrace("Ignoring RST_STREAM frame received on closed stream {0}", streamid);
         } else {
-            // put it in the input queue in order to read all
-            // pending data frames first. Indeed, a server may send
-            // RST_STREAM after sending END_STREAM, in which case we should
-            // ignore it. However, we won't know if we have received END_STREAM
-            // or not until all pending data frames are read.
-            receiveResetFrame(frame);
-            // RST_STREAM was pushed to the queue. It will be handled by
-            // asyncReceive after all pending data frames have been
-            // processed.
-            Log.logTrace("RST_STREAM pushed in queue for stream {0}", streamid);
+            Flow.Subscriber<?> subscriber =
+                    responseSubscriber == null ? pendingResponseSubscriber : responseSubscriber;
+            if (response == null && subscriber == null) {
+                // we haven't receive the headers yet, and won't receive any!
+                // handle reset now.
+                handleReset(frame, subscriber);
+            } else {
+                // put it in the input queue in order to read all
+                // pending data frames first. Indeed, a server may send
+                // RST_STREAM after sending END_STREAM, in which case we should
+                // ignore it. However, we won't know if we have received END_STREAM
+                // or not until all pending data frames are read.
+                receiveResetFrame(frame);
+                // RST_STREAM was pushed to the queue. It will be handled by
+                // asyncReceive after all pending data frames have been
+                // processed.
+                Log.logTrace("RST_STREAM pushed in queue for stream {0}", streamid);
+            }
         }
     }
 
-    void handleReset(ResetFrame frame) {
+    void handleReset(ResetFrame frame, Flow.Subscriber<?> subscriber) {
         Log.logTrace("Handling RST_STREAM on stream {0}", streamid);
         if (!closed) {
-            close();
-            int error = frame.getErrorCode();
-            completeResponseExceptionally(new IOException(ErrorFrame.stringForCode(error)));
+            synchronized (this) {
+                if (closed) {
+                    if (debug.on()) debug.log("Stream already closed: ignoring RESET");
+                    return;
+                }
+                closed = true;
+            }
+            try {
+                int error = frame.getErrorCode();
+                IOException e = new IOException("Received RST_STREAM: "
+                        + ErrorFrame.stringForCode(error));
+                if (errorRef.compareAndSet(null, e)) {
+                    if (subscriber != null) {
+                        subscriber.onError(e);
+                    }
+                }
+                completeResponseExceptionally(e);
+                if (!requestBodyCF.isDone()) {
+                    requestBodyCF.completeExceptionally(errorRef.get()); // we may be sending the body..
+                }
+                if (responseBodyCF != null) {
+                    responseBodyCF.completeExceptionally(errorRef.get());
+                }
+            } finally {
+                connection.decrementStreamsCount(streamid);
+                connection.closeStream(streamid);
+            }
         } else {
             Log.logTrace("Ignoring RST_STREAM frame received on closed stream {0}", streamid);
         }
@@ -535,13 +577,12 @@
     }
 
     private OutgoingHeaders<Stream<T>> headerFrame(long contentLength) {
-        HttpHeadersImpl h = request.getSystemHeaders();
+        HttpHeadersBuilder h = request.getSystemHeadersBuilder();
         if (contentLength > 0) {
             h.setHeader("content-length", Long.toString(contentLength));
         }
-        setPseudoHeaderFields();
-        HttpHeaders sysh = filter(h);
-        HttpHeaders userh = filter(request.getUserHeaders());
+        HttpHeaders sysh = filterHeaders(h.build());
+        HttpHeaders userh = filterHeaders(request.getUserHeaders());
         OutgoingHeaders<Stream<T>> f = new OutgoingHeaders<>(sysh, userh, this);
         if (contentLength == 0) {
             f.setFlag(HeadersFrame.END_STREAM);
@@ -565,7 +606,7 @@
     // If nothing needs filtering then we can just use the
     // original headers.
     private boolean needsFiltering(HttpHeaders headers,
-                                   BiPredicate<String, List<String>> filter) {
+                                   BiPredicate<String, String> filter) {
         if (filter == Utils.PROXY_TUNNEL_FILTER || filter == Utils.PROXY_FILTER) {
             // we're either connecting or proxying
             // slight optimization: we only need to filter out
@@ -583,18 +624,17 @@
         }
     }
 
-    private HttpHeaders filter(HttpHeaders headers) {
+    private HttpHeaders filterHeaders(HttpHeaders headers) {
         HttpConnection conn = connection();
-        BiPredicate<String, List<String>> filter =
-                conn.headerFilter(request);
+        BiPredicate<String, String> filter = conn.headerFilter(request);
         if (needsFiltering(headers, filter)) {
-            return ImmutableHeaders.of(headers.map(), filter);
+            return HttpHeaders.of(headers.map(), filter);
         }
         return headers;
     }
 
-    private void setPseudoHeaderFields() {
-        HttpHeadersImpl hdrs = requestPseudoHeaders;
+    private static HttpHeaders createPseudoHeaders(HttpRequest request) {
+        HttpHeadersBuilder hdrs = new HttpHeadersBuilder();
         String method = request.method();
         hdrs.setHeader(":method", method);
         URI uri = request.uri();
@@ -615,9 +655,10 @@
             path += "?" + query;
         }
         hdrs.setHeader(":path", Utils.encode(path));
+        return hdrs.build();
     }
 
-    HttpHeadersImpl getRequestPseudoHeaders() {
+    HttpHeaders getRequestPseudoHeaders() {
         return requestPseudoHeaders;
     }
 
@@ -657,6 +698,7 @@
         if (streamid > 0) {
             if (debug.on()) debug.log("Released stream %d", streamid);
             // remove this stream from the Http2Connection map.
+            connection.decrementStreamsCount(streamid);
             connection.closeStream(streamid);
         } else {
             if (debug.on()) debug.log("Can't release stream %d", streamid);
@@ -945,14 +987,18 @@
                 if (!cf.isDone()) {
                     Log.logTrace("Completing response (streamid={0}): {1}",
                                  streamid, cf);
+                    if (debug.on())
+                        debug.log("Completing responseCF(%d) with response headers", i);
+                    response_cfs.remove(cf);
                     cf.complete(resp);
-                    response_cfs.remove(cf);
                     return;
                 } // else we found the previous response: just leave it alone.
             }
             cf = MinimalFuture.completedFuture(resp);
             Log.logTrace("Created completed future (streamid={0}): {1}",
                          streamid, cf);
+            if (debug.on())
+                debug.log("Adding completed responseCF(0) with response headers");
             response_cfs.add(cf);
         }
     }
@@ -983,8 +1029,8 @@
             for (int i = 0; i < response_cfs.size(); i++) {
                 CompletableFuture<Response> cf = response_cfs.get(i);
                 if (!cf.isDone()) {
+                    response_cfs.remove(i);
                     cf.completeExceptionally(t);
-                    response_cfs.remove(i);
                     return;
                 }
             }
@@ -1033,6 +1079,15 @@
         cancelImpl(cause);
     }
 
+    void connectionClosing(Throwable cause) {
+        Flow.Subscriber<?> subscriber =
+                responseSubscriber == null ? pendingResponseSubscriber : responseSubscriber;
+        errorRef.compareAndSet(null, cause);
+        if (subscriber != null && !sched.isStopped() && !inputQ.isEmpty()) {
+            sched.runOrSchedule();
+        } else cancelImpl(cause);
+    }
+
     // This method sends a RST_STREAM frame
     void cancelImpl(Throwable e) {
         errorRef.compareAndSet(null, e);
@@ -1062,7 +1117,14 @@
         try {
             // will send a RST_STREAM frame
             if (streamid != 0) {
-                connection.resetStream(streamid, ResetFrame.CANCEL);
+                connection.decrementStreamsCount(streamid);
+                e = Utils.getCompletionCause(e);
+                if (e instanceof EOFException) {
+                    // read EOF: no need to try & send reset
+                    connection.closeStream(streamid);
+                } else {
+                    connection.resetStream(streamid, ResetFrame.CANCEL);
+                }
             }
         } catch (IOException ex) {
             Log.logError(ex);
@@ -1184,6 +1246,7 @@
         // create and return the PushResponseImpl
         @Override
         protected void handleResponse() {
+            HttpHeaders responseHeaders = responseHeadersBuilder.build();
             responseCode = (int)responseHeaders
                 .firstValueAsLong(":status")
                 .orElse(-1);
@@ -1252,17 +1315,18 @@
 
         void reset() {
             super.reset();
-            responseHeaders.clear();
+            responseHeadersBuilder.clear();
+            debug.log("Response builder cleared, ready to receive new headers.");
         }
 
         @Override
         public void onDecoded(CharSequence name, CharSequence value)
-                throws UncheckedIOException
+            throws UncheckedIOException
         {
             String n = name.toString();
             String v = value.toString();
             super.onDecoded(n, v);
-            responseHeaders.addHeader(n, v);
+            responseHeadersBuilder.addHeader(n, v);
             if (Log.headers() && Log.trace()) {
                 Log.logTrace("RECEIVED HEADER (streamid={0}): {1}: {2}",
                              streamid, n, v);
--- a/src/java.net.http/share/classes/jdk/internal/net/http/common/ConnectionExpiredException.java	Wed Jun 20 17:15:16 2018 +0200
+++ b/src/java.net.http/share/classes/jdk/internal/net/http/common/ConnectionExpiredException.java	Wed Jun 20 09:05:57 2018 -0700
@@ -35,13 +35,12 @@
     private static final long serialVersionUID = 0;
 
     /**
-     * Constructs a {@code ConnectionExpiredException} with the specified detail
-     * message and cause.
+     * Constructs a {@code ConnectionExpiredException} with a detail message of
+     * "subscription is finished" and the given cause.
      *
-     * @param   s     the detail message
      * @param   cause the throwable cause
      */
-    public ConnectionExpiredException(String s, Throwable cause) {
-        super(s, cause);
+    public ConnectionExpiredException(Throwable cause) {
+        super("subscription is finished", cause);
     }
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/java.net.http/share/classes/jdk/internal/net/http/common/HttpHeadersBuilder.java	Wed Jun 20 09:05:57 2018 -0700
@@ -0,0 +1,85 @@
+/*
+ * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package jdk.internal.net.http.common;
+
+import java.net.http.HttpHeaders;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.TreeMap;
+import static jdk.internal.net.http.common.Utils.ACCEPT_ALL;
+
+/** A mutable builder for collecting and building HTTP headers. */
+public class HttpHeadersBuilder {
+
+    private final TreeMap<String, List<String>> headersMap;
+
+    public HttpHeadersBuilder() {
+        headersMap = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
+    }
+
+    public HttpHeadersBuilder structuralCopy() {
+        HttpHeadersBuilder builder = new HttpHeadersBuilder();
+        for (Map.Entry<String, List<String>> entry : headersMap.entrySet()) {
+            List<String> valuesCopy = new ArrayList<>(entry.getValue());
+            builder.headersMap.put(entry.getKey(), valuesCopy);
+        }
+        return builder;
+    }
+
+    public void addHeader(String name, String value) {
+        headersMap.computeIfAbsent(name, k -> new ArrayList<>(1))
+                  .add(value);
+    }
+
+    public void setHeader(String name, String value) {
+        // headers typically have one value
+        List<String> values = new ArrayList<>(1);
+        values.add(value);
+        headersMap.put(name, values);
+    }
+
+    public void clear() {
+        headersMap.clear();
+    }
+
+    public Map<String, List<String>> map() {
+        return headersMap;
+    }
+
+    public HttpHeaders build() {
+        return HttpHeaders.of(headersMap, ACCEPT_ALL);
+    }
+
+    @Override
+    public String toString() {
+        StringBuilder sb = new StringBuilder();
+        sb.append(super.toString()).append(" { ");
+        sb.append(map());
+        sb.append(" }");
+        return sb.toString();
+    }
+}
--- a/src/java.net.http/share/classes/jdk/internal/net/http/common/HttpHeadersImpl.java	Wed Jun 20 17:15:16 2018 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,89 +0,0 @@
-/*
- * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
- *
- * This code is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 only, as
- * published by the Free Software Foundation.  Oracle designates this
- * particular file as subject to the "Classpath" exception as provided
- * by Oracle in the LICENSE file that accompanied this code.
- *
- * This code is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
- * version 2 for more details (a copy is included in the LICENSE file that
- * accompanied this code).
- *
- * You should have received a copy of the GNU General Public License version
- * 2 along with this work; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
- * or visit www.oracle.com if you need additional information or have any
- * questions.
- */
-
-package jdk.internal.net.http.common;
-
-import java.net.http.HttpHeaders;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
-import java.util.Map;
-import java.util.TreeMap;
-
-/**
- * Implementation of HttpHeaders.
- *
- * The public HttpHeaders API provides a read-only view, while the
- * non-HttpHeaders members allow for implementation specific mutation, e.g.
- * during creation, etc.
- */
-public class HttpHeadersImpl extends HttpHeaders {
-
-    private final TreeMap<String, List<String>> headers;
-
-    public HttpHeadersImpl() {
-        headers = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
-    }
-
-    @Override
-    public Map<String, List<String>> map() {
-        return Collections.unmodifiableMap(headersMap());
-    }
-
-    // non-HttpHeaders private mutators
-
-    public HttpHeadersImpl deepCopy() {
-        HttpHeadersImpl h1 = newDeepCopy();
-        for (Map.Entry<String, List<String>> entry : headersMap().entrySet()) {
-            List<String> valuesCopy = new ArrayList<>(entry.getValue());
-            h1.headersMap().put(entry.getKey(), valuesCopy);
-        }
-        return h1;
-    }
-
-    public void addHeader(String name, String value) {
-        headersMap().computeIfAbsent(name, k -> new ArrayList<>(1))
-                    .add(value);
-    }
-
-    public void setHeader(String name, String value) {
-        // headers typically have one value
-        List<String> values = new ArrayList<>(1);
-        values.add(value);
-        headersMap().put(name, values);
-    }
-
-    public void clear() {
-        headersMap().clear();
-    }
-
-    protected HttpHeadersImpl newDeepCopy() {
-        return new HttpHeadersImpl();
-    }
-
-    protected Map<String, List<String>> headersMap() {
-        return headers;
-    }
-}
--- a/src/java.net.http/share/classes/jdk/internal/net/http/common/Log.java	Wed Jun 20 17:15:16 2018 +0200
+++ b/src/java.net.http/share/classes/jdk/internal/net/http/common/Log.java	Wed Jun 20 09:05:57 2018 -0700
@@ -43,7 +43,7 @@
 /**
  * -Djava.net.HttpClient.log=
  *          errors,requests,headers,
- *          frames[:control:data:window:all..],content,ssl,trace
+ *          frames[:control:data:window:all..],content,ssl,trace,channel
  *
  * Any of errors, requests, headers or content are optional.
  *
@@ -65,6 +65,7 @@
     public static final int FRAMES = 0x10;
     public static final int SSL = 0x20;
     public static final int TRACE = 0x40;
+    public static final int CHANNEL = 0x80;
     static int logging;
 
     // Frame types: "control", "data", "window", "all"
@@ -99,11 +100,15 @@
                     case "ssl":
                         logging |= SSL;
                         break;
+                    case "channel":
+                        logging |= CHANNEL;
+                        break;
                     case "trace":
                         logging |= TRACE;
                         break;
                     case "all":
-                        logging |= CONTENT|HEADERS|REQUESTS|FRAMES|ERRORS|TRACE|SSL;
+                        logging |= CONTENT|HEADERS|REQUESTS|FRAMES|ERRORS|TRACE|SSL| CHANNEL;
+                        frametypes |= ALL;
                         break;
                     default:
                         // ignore bad values
@@ -166,6 +171,10 @@
         return (logging & FRAMES) != 0;
     }
 
+    public static boolean channel() {
+        return (logging & CHANNEL) != 0;
+    }
+
     public static void logError(String s, Object... s1) {
         if (errors()) {
             logger.log(Level.INFO, "ERROR: " + s, s1);
@@ -191,9 +200,21 @@
         }
     }
 
+    public static void logChannel(String s, Object... s1) {
+        if (channel()) {
+            logger.log(Level.INFO, "CHANNEL: " + s, s1);
+        }
+    }
+
+    public static void logChannel(Supplier<String> msgSupplier) {
+        if (channel()) {
+            logger.log(Level.INFO, "CHANNEL: " + msgSupplier.get());
+        }
+    }
+
     public static void logTrace(String s, Object... s1) {
         if (trace()) {
-            String format = "TRACE: " + s;
+            String format = "MISC: " + s;
             logger.log(Level.INFO, format, s1);
         }
     }
--- a/src/java.net.http/share/classes/jdk/internal/net/http/common/SSLFlowDelegate.java	Wed Jun 20 17:15:16 2018 +0200
+++ b/src/java.net.http/share/classes/jdk/internal/net/http/common/SSLFlowDelegate.java	Wed Jun 20 09:05:57 2018 -0700
@@ -46,6 +46,7 @@
 import java.util.concurrent.Flow.Subscriber;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.function.Consumer;
+import java.util.function.IntBinaryOperator;
 
 /**
  * Implements SSL using two SubscriberWrappers.
@@ -87,13 +88,18 @@
     final Logger debug =
             Utils.getDebugLogger(this::dbgString, Utils.DEBUG);
 
+    private static final ByteBuffer SENTINEL = Utils.EMPTY_BYTEBUFFER;
+    private static final ByteBuffer HS_TRIGGER = ByteBuffer.allocate(0);
+    // When handshake is in progress trying to wrap may produce no bytes.
+    private static final ByteBuffer NOTHING = ByteBuffer.allocate(0);
+    private static final String monProp = Utils.getProperty("jdk.internal.httpclient.monitorFlowDelegate");
+
     final Executor exec;
     final Reader reader;
     final Writer writer;
     final SSLEngine engine;
     final String tubeName; // hack
     final CompletableFuture<String> alpnCF; // completes on initial handshake
-    final static ByteBuffer SENTINEL = Utils.EMPTY_BYTEBUFFER;
     volatile boolean close_notify_received;
     final CompletableFuture<Void> readerCF;
     final CompletableFuture<Void> writerCF;
@@ -146,7 +152,8 @@
         // Writer to the downWriter.
         connect(downReader, downWriter);
 
-        //Monitor.add(this::monitor);
+        if (monProp != null && (monProp.equals("") || monProp.equalsIgnoreCase("true")))
+            Monitor.add(this::monitor);
     }
 
     /**
@@ -245,13 +252,16 @@
         final Logger debugr = Utils.getDebugLogger(this::dbgString, Utils.DEBUG);
 
         private final class ReaderDownstreamPusher implements Runnable {
-            @Override public void run() { processData(); }
+            @Override
+            public void run() {
+                processData();
+            }
         }
 
         Reader() {
             super();
             scheduler = SequentialScheduler.synchronizedScheduler(
-                                                new ReaderDownstreamPusher());
+                    new ReaderDownstreamPusher());
             this.readBuf = ByteBuffer.allocate(1024);
             readBuf.limit(0); // keep in read mode
         }
@@ -276,7 +286,7 @@
         public void incoming(List<ByteBuffer> buffers, boolean complete) {
             if (debugr.on())
                 debugr.log("Adding %d bytes to read buffer",
-                           Utils.remaining(buffers));
+                        Utils.remaining(buffers));
             addToReadBuf(buffers, complete);
             scheduler.runOrSchedule(exec);
         }
@@ -289,7 +299,7 @@
 
         private void reallocReadBuf() {
             int sz = readBuf.capacity();
-            ByteBuffer newb = ByteBuffer.allocate(sz*2);
+            ByteBuffer newb = ByteBuffer.allocate(sz * 2);
             readBuf.flip();
             Utils.copy(readBuf, newb);
             readBuf = newb;
@@ -300,7 +310,7 @@
             if (readBuf.remaining() > TARGET_BUFSIZE) {
                 if (debugr.on())
                     debugr.log("readBuf has more than TARGET_BUFSIZE: %d",
-                               readBuf.remaining());
+                            readBuf.remaining());
                 return 0;
             } else {
                 return super.upstreamWindowUpdate(currentWindow, downstreamQsize);
@@ -309,6 +319,7 @@
 
         // readBuf is kept ready for reading outside of this method
         private void addToReadBuf(List<ByteBuffer> buffers, boolean complete) {
+            assert Utils.remaining(buffers) > 0 || buffers.isEmpty();
             synchronized (readBufferLock) {
                 for (ByteBuffer buf : buffers) {
                     readBuf.compact();
@@ -344,14 +355,15 @@
         // In this case we need to wait for more bytes than what
         // we had before calling unwrap() again.
         volatile int minBytesRequired;
+
         // work function where it all happens
         final void processData() {
             try {
                 if (debugr.on())
                     debugr.log("processData:"
-                           + " readBuf remaining:" + readBuf.remaining()
-                           + ", state:" + states(handshakeState)
-                           + ", engine handshake status:" + engine.getHandshakeStatus());
+                            + " readBuf remaining:" + readBuf.remaining()
+                            + ", state:" + states(handshakeState)
+                            + ", engine handshake status:" + engine.getHandshakeStatus());
                 int len;
                 boolean complete = false;
                 while (readBuf.remaining() > (len = minBytesRequired)) {
@@ -400,14 +412,13 @@
                             outgoing(Utils.EMPTY_BB_LIST, true);
                             return;
                         }
-                        if (result.handshaking() && !complete) {
+                        if (result.handshaking()) {
+                            handshaking = true;
                             if (debugr.on()) debugr.log("handshaking");
-                            if (doHandshake(result, READER)) {
-                                resumeActivity();
-                            }
-                            handshaking = true;
+                            if (doHandshake(result, READER)) continue; // need unwrap
+                            else break; // doHandshake will have triggered the write scheduler if necessary
                         } else {
-                            if ((handshakeState.getAndSet(NOT_HANDSHAKING)& ~DOING_TASKS) == HANDSHAKING) {
+                            if ((handshakeState.getAndSet(NOT_HANDSHAKING) & ~DOING_TASKS) == HANDSHAKING) {
                                 handshaking = false;
                                 applicationBufferSize = engine.getSession().getApplicationBufferSize();
                                 packetBufferSize = engine.getSession().getPacketBufferSize();
@@ -443,12 +454,19 @@
 
         EngineResult unwrapBuffer(ByteBuffer src) throws IOException {
             ByteBuffer dst = getAppBuffer();
+            int len = src.remaining();
             while (true) {
                 SSLEngineResult sslResult = engine.unwrap(src, dst);
                 switch (sslResult.getStatus()) {
                     case BUFFER_OVERFLOW:
-                        // may happen only if app size buffer was changed.
-                        // get it again if app buffer size changed
+                        // may happen if app size buffer was changed, or if
+                        // our 'adaptiveBufferSize' guess was too small for
+                        // the current payload. In that case, update the
+                        // value of applicationBufferSize, and allocate a
+                        // buffer of that size, which we are sure will be
+                        // big enough to decode whatever needs to be
+                        // decoded. We will later update adaptiveBufferSize
+                        // in OK: below.
                         int appSize = applicationBufferSize =
                                 engine.getSession().getApplicationBufferSize();
                         ByteBuffer b = ByteBuffer.allocate(appSize + dst.position());
@@ -457,11 +475,26 @@
                         dst = b;
                         break;
                     case CLOSED:
+                        assert dst.position() == 0;
                         return doClosure(new EngineResult(sslResult));
                     case BUFFER_UNDERFLOW:
                         // handled implicitly by compaction/reallocation of readBuf
+                        assert dst.position() == 0;
                         return new EngineResult(sslResult);
                     case OK:
+                        int size = dst.position();
+                        if (debug.on()) {
+                            debugr.log("Decoded " + size + " bytes out of " + len
+                                    + " into buffer of " + dst.capacity()
+                                    + " remaining to decode: " + src.remaining());
+                        }
+                        // if the record payload was bigger than what was originally
+                        // allocated, then sets the adaptiveAppBufferSize to size
+                        // and we will use that new size as a guess for the next app
+                        // buffer.
+                        if (size > adaptiveAppBufferSize) {
+                            adaptiveAppBufferSize = ((size + 7) >>> 3) << 3;
+                        }
                         dst.flip();
                         return new EngineResult(sslResult, dst);
                 }
@@ -662,8 +695,8 @@
                     }
                     cleanList(writeList); // tidy up the source list
                     sendResultBytes(result);
-                    if (handshaking && !completing) {
-                        if (needWrap()) {
+                    if (handshaking) {
+                        if (!completing && needWrap()) {
                             continue;
                         } else {
                             return;
@@ -687,11 +720,30 @@
             }
         }
 
+        // The SSLEngine insists on being given a buffer that is at least
+        // SSLSession.getPacketBufferSize() long (usually 16K). If given
+        // a smaller buffer it will go in BUFFER_OVERFLOW, even if it only
+        // has 6 bytes to wrap. Typical usage shows that for GET we
+        // usually produce an average of ~ 100 bytes.
+        // To avoid wasting space, and because allocating and zeroing
+        // 16K buffers for encoding 6 bytes is costly, we are reusing the
+        // same writeBuffer to interact with SSLEngine.wrap().
+        // If the SSLEngine produces less than writeBuffer.capacity() / 2,
+        // then we copy off the bytes to a smaller buffer that we send
+        // downstream. Otherwise, we send the writeBuffer downstream
+        // and will allocate a new one next time.
+        volatile ByteBuffer writeBuffer;
         @SuppressWarnings("fallthrough")
         EngineResult wrapBuffers(ByteBuffer[] src) throws SSLException {
+            long len = Utils.remaining(src);
             if (debugw.on())
-                debugw.log("wrapping " + Utils.remaining(src) + " bytes");
-            ByteBuffer dst = getNetBuffer();
+                debugw.log("wrapping " + len + " bytes");
+
+            ByteBuffer dst = writeBuffer;
+            if (dst == null) dst = writeBuffer = getNetBuffer();
+            assert dst.position() == 0 : "buffer position is " + dst.position();
+            assert dst.hasRemaining() : "buffer has no remaining space: capacity=" + dst.capacity();
+
             while (true) {
                 SSLEngineResult sslResult = engine.wrap(src, dst);
                 if (debugw.on()) debugw.log("SSLResult: " + sslResult);
@@ -702,7 +754,7 @@
                         if (debugw.on()) debugw.log("BUFFER_OVERFLOW");
                         int netSize = packetBufferSize
                                 = engine.getSession().getPacketBufferSize();
-                        ByteBuffer b = ByteBuffer.allocate(netSize + dst.position());
+                        ByteBuffer b = writeBuffer = ByteBuffer.allocate(netSize + dst.position());
                         dst.flip();
                         b.put(dst);
                         dst = b;
@@ -712,11 +764,27 @@
                         // fallthrough. There could be some remaining data in dst.
                         // CLOSED will be handled by the caller.
                     case OK:
-                        dst.flip();
-                        final ByteBuffer dest = dst;
+                        final ByteBuffer dest;
+                        if (dst.position() == 0) {
+                            dest = NOTHING; // can happen if handshake is in progress
+                        } else if (dst.position() < dst.capacity() / 2) {
+                            // less than half the buffer was used.
+                            // copy off the bytes to a smaller buffer, and keep
+                            // the writeBuffer for next time.
+                            dst.flip();
+                            dest = Utils.copyAligned(dst);
+                            dst.clear();
+                        } else {
+                            // more than half the buffer was used.
+                            // just send that buffer downstream, and we will
+                            // get a new writeBuffer next time it is needed.
+                            dst.flip();
+                            dest = dst;
+                            writeBuffer = null;
+                        }
                         if (debugw.on())
-                            debugw.log("OK => produced: %d, not wrapped: %d",
-                                       dest.remaining(),  Utils.remaining(src));
+                            debugw.log("OK => produced: %d bytes into %d, not wrapped: %d",
+                                       dest.remaining(),  dest.capacity(), Utils.remaining(src));
                         return new EngineResult(sslResult, dest);
                     case BUFFER_UNDERFLOW:
                         // Shouldn't happen.  Doesn't returns when wrap()
@@ -799,8 +867,12 @@
     private static final int NOT_HANDSHAKING = 0;
     private static final int HANDSHAKING = 1;
 
-    private static final int DOING_TASKS = 4; // bit added to above state
-    private static final ByteBuffer HS_TRIGGER = ByteBuffer.allocate(0);
+    // Bit flags
+    // a thread is currently executing tasks
+    private static final int DOING_TASKS = 4;
+    // a thread wants to execute tasks, while another thread is executing
+    private static final int REQUESTING_TASKS = 8;
+    private static final int TASK_BITS = 12; // Both bits
 
     private static final int READER = 1;
     private static final int WRITER = 2;
@@ -808,7 +880,7 @@
     private static String states(AtomicInteger state) {
         int s = state.get();
         StringBuilder sb = new StringBuilder();
-        int x = s & ~DOING_TASKS;
+        int x = s & ~TASK_BITS;
         switch (x) {
             case NOT_HANDSHAKING:
                 sb.append(" NOT_HANDSHAKING ");
@@ -821,6 +893,8 @@
         }
         if ((s & DOING_TASKS) > 0)
             sb.append("|DOING_TASKS");
+        if ((s & REQUESTING_TASKS) > 0)
+            sb.append("|REQUESTING_TASKS");
         return sb.toString();
     }
 
@@ -833,18 +907,37 @@
     final ConcurrentLinkedQueue<String> stateList =
             debug.on() ? new ConcurrentLinkedQueue<>() : null;
 
+    // Atomically executed to update task bits. Sets either DOING_TASKS or REQUESTING_TASKS
+    // depending on previous value
+    private static final IntBinaryOperator REQUEST_OR_DO_TASKS = (current, ignored) -> {
+        if ((current & DOING_TASKS) == 0)
+            return DOING_TASKS | (current & HANDSHAKING);
+        else
+            return DOING_TASKS | REQUESTING_TASKS | (current & HANDSHAKING);
+    };
+
+    // Atomically executed to update task bits. Sets DOING_TASKS if REQUESTING was set
+    // clears bits if not.
+    private static final IntBinaryOperator FINISH_OR_DO_TASKS = (current, ignored) -> {
+        if ((current & REQUESTING_TASKS) != 0)
+            return DOING_TASKS | (current & HANDSHAKING);
+        // clear both bits
+        return (current & HANDSHAKING);
+    };
+
     private boolean doHandshake(EngineResult r, int caller) {
-        // unconditionally sets the HANDSHAKING bit, while preserving DOING_TASKS
-        handshakeState.getAndAccumulate(HANDSHAKING, (current, update) -> update | (current & DOING_TASKS));
+        // unconditionally sets the HANDSHAKING bit, while preserving task bits
+        handshakeState.getAndAccumulate(0, (current, unused) -> HANDSHAKING | (current & TASK_BITS));
         if (stateList != null && debug.on()) {
             stateList.add(r.handshakeStatus().toString());
             stateList.add(Integer.toString(caller));
         }
         switch (r.handshakeStatus()) {
             case NEED_TASK:
-                int s = handshakeState.getAndUpdate((current) -> current | DOING_TASKS);
-                if ((s & DOING_TASKS) > 0) // someone else was doing tasks
+                int s = handshakeState.accumulateAndGet(0, REQUEST_OR_DO_TASKS);
+                if ((s & REQUESTING_TASKS) > 0) { // someone else is or will do tasks
                     return false;
+                }
 
                 if (debug.on()) debug.log("obtaining and initiating task execution");
                 List<Runnable> tasks = obtainTasks();
@@ -878,20 +971,25 @@
     }
 
     private void executeTasks(List<Runnable> tasks) {
-        if (tasks.isEmpty())
-            return;
         exec.execute(() -> {
             try {
                 List<Runnable> nextTasks = tasks;
+                if (debug.on()) debug.log("#tasks to execute: " + Integer.toString(nextTasks.size()));
                 do {
                     nextTasks.forEach(Runnable::run);
                     if (engine.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {
                         nextTasks = obtainTasks();
                     } else {
+                        int s = handshakeState.accumulateAndGet(0, FINISH_OR_DO_TASKS);
+                        if ((s & DOING_TASKS) != 0) {
+                            if (debug.on()) debug.log("re-running tasks (B)");
+                            nextTasks = obtainTasks();
+                            continue;
+                        }
                         break;
                     }
                 } while (true);
-                handshakeState.getAndUpdate((current) -> current & ~DOING_TASKS);
+                if (debug.on()) debug.log("finished task execution");
                 resumeActivity();
             } catch (Throwable t) {
                 handleError(t);
@@ -997,6 +1095,8 @@
         }
     }
 
+    // The maximum network buffer size negotiated during
+    // the handshake. Usually 16K.
     volatile int packetBufferSize;
     final ByteBuffer getNetBuffer() {
         int netSize = packetBufferSize;
@@ -1006,13 +1106,32 @@
         return ByteBuffer.allocate(netSize);
     }
 
+    // The maximum application buffer size negotiated during
+    // the handshake. Usually close to 16K.
     volatile int applicationBufferSize;
+    // Despite of the maximum applicationBufferSize negotiated
+    // above, TLS records usually have a much smaller payload.
+    // The adaptativeAppBufferSize records the max payload
+    // ever decoded, and we use that as a guess for how big
+    // a buffer we will need for the next payload.
+    // This avoids allocating and zeroing a 16K buffer for
+    // nothing...
+    volatile int adaptiveAppBufferSize;
     final ByteBuffer getAppBuffer() {
         int appSize = applicationBufferSize;
         if (appSize <= 0) {
-            applicationBufferSize = appSize = engine.getSession().getApplicationBufferSize();
+            applicationBufferSize = appSize
+                    = engine.getSession().getApplicationBufferSize();
         }
-        return ByteBuffer.allocate(appSize);
+        int size = adaptiveAppBufferSize;
+        if (size <= 0) {
+            size = 512; // start with 512 this is usually enough for handshaking / headers
+        } else if (size > appSize) {
+            size = appSize;
+        }
+        // will cause a BUFFER_OVERFLOW if not big enough, but
+        // that's OK.
+        return ByteBuffer.allocate(size);
     }
 
     final String dbgString() {
--- a/src/java.net.http/share/classes/jdk/internal/net/http/common/SSLTube.java	Wed Jun 20 17:15:16 2018 +0200
+++ b/src/java.net.http/share/classes/jdk/internal/net/http/common/SSLTube.java	Wed Jun 20 09:05:57 2018 -0700
@@ -309,7 +309,7 @@
             synchronized (this) {
                 previous = pendingDelegate.getAndSet(delegateWrapper);
                 subscription = readSubscription;
-                handleNow = this.errorRef.get() != null || finished;
+                handleNow = this.errorRef.get() != null || onCompleteReceived;
             }
             if (previous != null) {
                 previous.dropSubscription();
@@ -424,12 +424,20 @@
             // if onError is invoked concurrently with setDelegate.
             synchronized (this) {
                 failed = this.errorRef.get();
-                completed = finished;
+                completed = onCompleteReceived;
                 subscribed = subscriberImpl;
             }
+
             if (failed != null) {
+                if (debug.on())
+                    debug.log("onNewSubscription: subscriberImpl:%s, invoking onError:%s",
+                              subscriberImpl, failed);
                 subscriberImpl.onError(failed);
             } else if (completed) {
+                if (debug.on())
+                    debug.log("onNewSubscription: subscriberImpl:%s, invoking onCompleted",
+                              subscriberImpl);
+                finished = true;
                 subscriberImpl.onComplete();
             }
         }
@@ -490,7 +498,6 @@
         @Override
         public void onComplete() {
             assert !finished && !onCompleteReceived;
-            onCompleteReceived = true;
             DelegateWrapper subscriberImpl;
             synchronized(this) {
                 subscriberImpl = subscribed;
@@ -505,8 +512,10 @@
                 onErrorImpl(new SSLHandshakeException(
                         "Remote host terminated the handshake"));
             } else if (subscriberImpl != null) {
-                finished = true;
+                onCompleteReceived = finished = true;
                 subscriberImpl.onComplete();
+            } else {
+                onCompleteReceived = true;
             }
             // now if we have any pending subscriber, we should complete
             // them immediately as the read scheduler will already be stopped.
@@ -528,12 +537,17 @@
     final class SSLSubscriptionWrapper implements Flow.Subscription {
 
         volatile Flow.Subscription delegate;
+        private volatile boolean cancelled;
 
         void setSubscription(Flow.Subscription sub) {
             long demand = writeDemand.get(); // FIXME: isn't it a racy way of passing the demand?
             delegate = sub;
-            if (debug.on()) debug.log("setSubscription: demand=%d", demand);
-            if (demand > 0)
+            if (debug.on())
+                debug.log("setSubscription: demand=%d, cancelled:%s", demand, cancelled);
+
+            if (cancelled)
+                delegate.cancel();
+            else if (demand > 0)
                 sub.request(demand);
         }
 
@@ -549,7 +563,9 @@
 
         @Override
         public void cancel() {
-            // TODO:  no-op or error?
+            cancelled = true;
+            if (delegate != null)
+                delegate.cancel();
         }
     }
 
--- a/src/java.net.http/share/classes/jdk/internal/net/http/common/SubscriberWrapper.java	Wed Jun 20 17:15:16 2018 +0200
+++ b/src/java.net.http/share/classes/jdk/internal/net/http/common/SubscriberWrapper.java	Wed Jun 20 09:05:57 2018 -0700
@@ -260,6 +260,8 @@
             try {
                 run1();
             } catch (Throwable t) {
+                if (debug.on())
+                    debug.log("DownstreamPusher threw: " + t);
                 errorCommon(t);
             }
         }
@@ -292,6 +294,7 @@
                 pushScheduler.stop();
                 outputQ.clear();
                 downstreamSubscriber.onError(error);
+                cf.completeExceptionally(error);
                 return;
             }
 
@@ -383,9 +386,8 @@
                 (throwable = new AssertionError("null throwable")) != null;
         if (errorRef.compareAndSet(null, throwable)) {
             if (debug.on()) debug.log("error", throwable);
+            upstreamCompleted = true;
             pushScheduler.runOrSchedule();
-            upstreamCompleted = true;
-            cf.completeExceptionally(throwable);
             return true;
         }
         return false;
--- a/src/java.net.http/share/classes/jdk/internal/net/http/common/Utils.java	Wed Jun 20 17:15:16 2018 +0200
+++ b/src/java.net.http/share/classes/jdk/internal/net/http/common/Utils.java	Wed Jun 20 09:05:57 2018 -0700
@@ -30,19 +30,23 @@
 import sun.net.www.HeaderParser;
 
 import javax.net.ssl.ExtendedSSLSession;
+import javax.net.ssl.SSLException;
 import javax.net.ssl.SSLParameters;
 import javax.net.ssl.SSLSession;
 import java.io.ByteArrayOutputStream;
 import java.io.Closeable;
+import java.io.EOFException;
 import java.io.IOException;
 import java.io.PrintStream;
 import java.io.UncheckedIOException;
 import java.io.UnsupportedEncodingException;
 import java.lang.System.Logger.Level;
+import java.net.ConnectException;
 import java.net.InetSocketAddress;
 import java.net.URI;
 import java.net.URLPermission;
 import java.net.http.HttpHeaders;
+import java.net.http.HttpTimeoutException;
 import java.nio.ByteBuffer;
 import java.nio.CharBuffer;
 import java.nio.charset.CharacterCodingException;
@@ -58,9 +62,11 @@
 import java.util.List;
 import java.util.Set;
 import java.util.TreeSet;
+import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.CompletionException;
 import java.util.concurrent.ExecutionException;
 import java.util.function.BiPredicate;
+import java.util.function.Function;
 import java.util.function.Predicate;
 import java.util.function.Supplier;
 import java.util.stream.Collectors;
@@ -119,6 +125,8 @@
             "jdk.httpclient.bufsize", DEFAULT_BUFSIZE
     );
 
+    public static final BiPredicate<String,String> ACCEPT_ALL = (x,y) -> true;
+
     private static final Set<String> DISALLOWED_HEADERS_SET;
 
     static {
@@ -131,24 +139,21 @@
         DISALLOWED_HEADERS_SET = Collections.unmodifiableSet(treeSet);
     }
 
-    public static final Predicate<String>
-            ALLOWED_HEADERS = header -> !DISALLOWED_HEADERS_SET.contains(header);
+    public static final BiPredicate<String, String>
+            ALLOWED_HEADERS = (header, unused) -> !DISALLOWED_HEADERS_SET.contains(header);
 
-    public static final BiPredicate<String, List<String>> VALIDATE_USER_HEADER =
-            (name, lv) -> {
-                requireNonNull(name, "header name");
-                requireNonNull(lv, "header values");
+    public static final BiPredicate<String, String> VALIDATE_USER_HEADER =
+            (name, value) -> {
+                assert name != null : "null header name";
+                assert value != null : "null header value";
                 if (!isValidName(name)) {
                     throw newIAE("invalid header name: \"%s\"", name);
                 }
-                if (!Utils.ALLOWED_HEADERS.test(name)) {
+                if (!Utils.ALLOWED_HEADERS.test(name, null)) {
                     throw newIAE("restricted header name: \"%s\"", name);
                 }
-                for (String value : lv) {
-                    requireNonNull(value, "header value");
-                    if (!isValidValue(value)) {
-                        throw newIAE("invalid header value for %s: \"%s\"", name, value);
-                    }
+                if (!isValidValue(value)) {
+                    throw newIAE("invalid header value for %s: \"%s\"", name, value);
                 }
                 return true;
             };
@@ -180,9 +185,20 @@
                                 .collect(Collectors.toUnmodifiableSet());
     }
 
+    public static <T> CompletableFuture<T> wrapForDebug(Logger logger, String name, CompletableFuture<T> cf) {
+        if (logger.on()) {
+            return cf.handle((r,t) -> {
+                logger.log("%s completed %s", name, t == null ? "successfully" : t );
+                return cf;
+            }).thenCompose(Function.identity());
+        } else {
+            return cf;
+        }
+    }
+
     private static final String WSPACES = " \t\r\n";
     private static final boolean isAllowedForProxy(String name,
-                                                   List<String> value,
+                                                   String value,
                                                    Set<String> disabledSchemes,
                                                    Predicate<String> allowedKeys) {
         if (!allowedKeys.test(name)) return false;
@@ -191,21 +207,19 @@
             if (value.isEmpty()) return false;
             for (String scheme : disabledSchemes) {
                 int slen = scheme.length();
-                for (String v : value) {
-                    int vlen = v.length();
-                    if (vlen == slen) {
-                        if (v.equalsIgnoreCase(scheme)) {
+                int vlen = value.length();
+                if (vlen == slen) {
+                    if (value.equalsIgnoreCase(scheme)) {
+                        return false;
+                    }
+                } else if (vlen > slen) {
+                    if (value.substring(0,slen).equalsIgnoreCase(scheme)) {
+                        int c = value.codePointAt(slen);
+                        if (WSPACES.indexOf(c) > -1
+                                || Character.isSpaceChar(c)
+                                || Character.isWhitespace(c)) {
                             return false;
                         }
-                    } else if (vlen > slen) {
-                        if (v.substring(0,slen).equalsIgnoreCase(scheme)) {
-                            int c = v.codePointAt(slen);
-                            if (WSPACES.indexOf(c) > -1
-                                    || Character.isSpaceChar(c)
-                                    || Character.isWhitespace(c)) {
-                                return false;
-                            }
-                        }
                     }
                 }
             }
@@ -213,13 +227,13 @@
         return true;
     }
 
-    public static final BiPredicate<String, List<String>> PROXY_TUNNEL_FILTER =
+    public static final BiPredicate<String, String> PROXY_TUNNEL_FILTER =
             (s,v) -> isAllowedForProxy(s, v, PROXY_AUTH_TUNNEL_DISABLED_SCHEMES,
                     IS_PROXY_HEADER);
-    public static final BiPredicate<String, List<String>> PROXY_FILTER =
+    public static final BiPredicate<String, String> PROXY_FILTER =
             (s,v) -> isAllowedForProxy(s, v, PROXY_AUTH_DISABLED_SCHEMES,
                     ALL_HEADERS);
-    public static final BiPredicate<String, List<String>> NO_PROXY_HEADERS_FILTER =
+    public static final BiPredicate<String, String> NO_PROXY_HEADERS_FILTER =
             (n,v) -> Utils.NO_PROXY_HEADER.test(n);
 
 
@@ -256,6 +270,35 @@
         return new IOException(t);
     }
 
+    /**
+     * Adds a more specific exception detail message, based on the given
+     * exception type and the message supplier. This is primarily to present
+     * more descriptive messages in IOExceptions that may be visible to calling
+     * code.
+     *
+     * @return a possibly new exception that has as its detail message, the
+     *         message from the messageSupplier, and the given throwable as its
+     *         cause. Otherwise returns the given throwable
+     */
+    public static Throwable wrapWithExtraDetail(Throwable t,
+                                                Supplier<String> messageSupplier) {
+        if (!(t instanceof IOException))
+            return t;
+
+        String msg = messageSupplier.get();
+        if (msg == null)
+            return t;
+
+        if (t instanceof ConnectionExpiredException) {
+            IOException ioe = new IOException(msg, t.getCause());
+            t = new ConnectionExpiredException(ioe);
+        } else {
+            IOException ioe = new IOException(msg, t);
+            t = ioe;
+        }
+        return t;
+    }
+
     private Utils() { }
 
     /**
@@ -534,6 +577,16 @@
         return dst;
     }
 
+    public static ByteBuffer copyAligned(ByteBuffer src) {
+        int len = src.remaining();
+        int size = ((len + 7) >> 3) << 3;
+        assert size >= len;
+        ByteBuffer dst = ByteBuffer.allocate(size);
+        dst.put(src);
+        dst.flip();
+        return dst;
+    }
+
     public static String dump(Object... objects) {
         return Arrays.toString(objects);
     }
@@ -901,6 +954,20 @@
         return address;
     }
 
+    public static Throwable toConnectException(Throwable e) {
+        if (e == null) return null;
+        e = getCompletionCause(e);
+        if (e instanceof ConnectException) return e;
+        if (e instanceof SecurityException) return e;
+        if (e instanceof SSLException) return e;
+        if (e instanceof Error) return e;
+        if (e instanceof HttpTimeoutException) return e;
+        Throwable cause = e;
+        e = new ConnectException(e.getMessage());
+        e.initCause(cause);
+        return e;
+    }
+
     /**
      * Returns the smallest (closest to zero) positive number {@code m} (which
      * is also a power of 2) such that {@code n <= m}.
--- a/src/java.net.http/share/classes/jdk/internal/net/http/frame/SettingsFrame.java	Wed Jun 20 17:15:16 2018 +0200
+++ b/src/java.net.http/share/classes/jdk/internal/net/http/frame/SettingsFrame.java	Wed Jun 20 09:05:57 2018 -0700
@@ -110,14 +110,14 @@
         return TYPE;
     }
 
-    public int getParameter(int paramID) {
+    public synchronized int getParameter(int paramID) {
         if (paramID > MAX_PARAM) {
             throw new IllegalArgumentException("illegal parameter");
         }
         return parameters[paramID - 1];
     }
 
-    public SettingsFrame setParameter(int paramID, int value) {
+    public synchronized SettingsFrame setParameter(int paramID, int value) {
         if (paramID > MAX_PARAM) {
             throw new IllegalArgumentException("illegal parameter");
         }
@@ -166,7 +166,7 @@
         // TODO: check these values
         f.setParameter(ENABLE_PUSH, 1);
         f.setParameter(HEADER_TABLE_SIZE, 4 * K);
-        f.setParameter(MAX_CONCURRENT_STREAMS, 35);
+        f.setParameter(MAX_CONCURRENT_STREAMS, 100);
         f.setParameter(INITIAL_WINDOW_SIZE, 64 * K - 1);
         f.setParameter(MAX_FRAME_SIZE, 16 * K);
         return f;
--- a/src/java.net.http/share/classes/jdk/internal/net/http/hpack/Decoder.java	Wed Jun 20 17:15:16 2018 +0200
+++ b/src/java.net.http/share/classes/jdk/internal/net/http/hpack/Decoder.java	Wed Jun 20 09:05:57 2018 -0700
@@ -28,6 +28,7 @@
 
 import java.io.IOException;
 import java.nio.ByteBuffer;
+import java.util.List;
 import java.util.concurrent.atomic.AtomicLong;
 
 import static jdk.internal.net.http.hpack.HPACK.Logger.Level.EXTRA;
@@ -66,7 +67,8 @@
     private final Logger logger;
     private static final AtomicLong DECODERS_IDS = new AtomicLong();
 
-    private static final State[] states = new State[256];
+    /* An immutable list of states */
+    private static final List<State> states;
 
     static {
         // To be able to do a quick lookup, each of 256 possibilities are mapped
@@ -78,21 +80,23 @@
         // I do it mainly for better debugging (to not go each time step by step
         // through if...else tree). As for performance win for the decoding, I
         // believe is negligible.
-        for (int i = 0; i < states.length; i++) {
+        State[] s = new State[256];
+        for (int i = 0; i < s.length; i++) {
             if ((i & 0b1000_0000) == 0b1000_0000) {
-                states[i] = State.INDEXED;
+                s[i] = State.INDEXED;
             } else if ((i & 0b1100_0000) == 0b0100_0000) {
-                states[i] = State.LITERAL_WITH_INDEXING;
+                s[i] = State.LITERAL_WITH_INDEXING;
             } else if ((i & 0b1110_0000) == 0b0010_0000) {
-                states[i] = State.SIZE_UPDATE;
+                s[i] = State.SIZE_UPDATE;
             } else if ((i & 0b1111_0000) == 0b0001_0000) {
-                states[i] = State.LITERAL_NEVER_INDEXED;
+                s[i] = State.LITERAL_NEVER_INDEXED;
             } else if ((i & 0b1111_0000) == 0b0000_0000) {
-                states[i] = State.LITERAL;
+                s[i] = State.LITERAL;
             } else {
                 throw new InternalError(String.valueOf(i));
             }
         }
+        states = List.of(s);
     }
 
     private final long id;
@@ -278,7 +282,7 @@
 
     private void resumeReady(ByteBuffer input) {
         int b = input.get(input.position()) & 0xff; // absolute read
-        State s = states[b];
+        State s = states.get(b);
         if (logger.isLoggable(EXTRA)) {
             logger.log(EXTRA, () -> format("next binary representation %s (first byte 0x%02x)",
                                            s, b));
@@ -388,15 +392,17 @@
         try {
             if (firstValueIndex) {
                 if (logger.isLoggable(NORMAL)) {
-                    logger.log(NORMAL, () -> format("literal without indexing ('%s', '%s')",
-                                                    intValue, value));
+                    logger.log(NORMAL, () -> format(
+                            "literal without indexing (%s, '%s', huffman=%b)",
+                            intValue, value, valueHuffmanEncoded));
                 }
                 SimpleHeaderTable.HeaderField f = getHeaderFieldAt(intValue);
                 action.onLiteral(intValue, f.name, value, valueHuffmanEncoded);
             } else {
                 if (logger.isLoggable(NORMAL)) {
-                    logger.log(NORMAL, () -> format("literal without indexing ('%s', '%s')",
-                                                    name, value));
+                    logger.log(NORMAL, () -> format(
+                            "literal without indexing ('%s', huffman=%b, '%s', huffman=%b)",
+                            name, nameHuffmanEncoded, value, valueHuffmanEncoded));
                 }
                 action.onLiteral(name, nameHuffmanEncoded, value, valueHuffmanEncoded);
             }
@@ -445,8 +451,9 @@
             String v = value.toString();
             if (firstValueIndex) {
                 if (logger.isLoggable(NORMAL)) {
-                    logger.log(NORMAL, () -> format("literal with incremental indexing ('%s', '%s')",
-                                                    intValue, value));
+                    logger.log(NORMAL, () -> format(
+                            "literal with incremental indexing (%s, '%s', huffman=%b)",
+                            intValue, value, valueHuffmanEncoded));
                 }
                 SimpleHeaderTable.HeaderField f = getHeaderFieldAt(intValue);
                 n = f.name;
@@ -454,8 +461,9 @@
             } else {
                 n = name.toString();
                 if (logger.isLoggable(NORMAL)) {
-                    logger.log(NORMAL, () -> format("literal with incremental indexing ('%s', '%s')",
-                                                    n, value));
+                    logger.log(NORMAL, () -> format(
+                            "literal with incremental indexing ('%s', huffman=%b, '%s', huffman=%b)",
+                            n, nameHuffmanEncoded, value, valueHuffmanEncoded));
                 }
                 action.onLiteralWithIndexing(n, nameHuffmanEncoded, v, valueHuffmanEncoded);
             }
@@ -496,15 +504,17 @@
         try {
             if (firstValueIndex) {
                 if (logger.isLoggable(NORMAL)) {
-                    logger.log(NORMAL, () -> format("literal never indexed ('%s', '%s')",
-                                                    intValue, value));
+                    logger.log(NORMAL, () -> format(
+                            "literal never indexed (%s, '%s', huffman=%b)",
+                            intValue, value, valueHuffmanEncoded));
                 }
                 SimpleHeaderTable.HeaderField f = getHeaderFieldAt(intValue);
                 action.onLiteralNeverIndexed(intValue, f.name, value, valueHuffmanEncoded);
             } else {
                 if (logger.isLoggable(NORMAL)) {
-                    logger.log(NORMAL, () -> format("literal never indexed ('%s', '%s')",
-                                                    name, value));
+                    logger.log(NORMAL, () -> format(
+                            "literal never indexed ('%s', huffman=%b, '%s', huffman=%b)",
+                            name, nameHuffmanEncoded, value, valueHuffmanEncoded));
                 }
                 action.onLiteralNeverIndexed(name, nameHuffmanEncoded, value, valueHuffmanEncoded);
             }
--- a/src/java.net.http/share/classes/jdk/internal/net/http/hpack/Encoder.java	Wed Jun 20 17:15:16 2018 +0200
+++ b/src/java.net.http/share/classes/jdk/internal/net/http/hpack/Encoder.java	Wed Jun 20 09:05:57 2018 -0700
@@ -105,8 +105,8 @@
 
     private static final AtomicLong ENCODERS_IDS = new AtomicLong();
 
-    // TODO: enum: no huffman/smart huffman/always huffman
-    private static final boolean DEFAULT_HUFFMAN = true;
+    /* Used to calculate the number of bytes required for Huffman encoding */
+    private final QuickHuffman.Writer huffmanWriter = new QuickHuffman.Writer();
 
     private final Logger logger;
     private final long id;
@@ -241,21 +241,30 @@
         int index = t.indexOf(name, value);
         if (index > 0) {
             indexed(index);
-        } else if (index < 0) {
-            if (sensitive) {
-                literalNeverIndexed(-index, value, DEFAULT_HUFFMAN);
+        } else {
+            boolean huffmanValue = isHuffmanBetterFor(value);
+            if (index < 0) {
+                if (sensitive) {
+                    literalNeverIndexed(-index, value, huffmanValue);
+                } else {
+                    literal(-index, value, huffmanValue);
+                }
             } else {
-                literal(-index, value, DEFAULT_HUFFMAN);
-            }
-        } else {
-            if (sensitive) {
-                literalNeverIndexed(name, DEFAULT_HUFFMAN, value, DEFAULT_HUFFMAN);
-            } else {
-                literal(name, DEFAULT_HUFFMAN, value, DEFAULT_HUFFMAN);
+                boolean huffmanName = isHuffmanBetterFor(name);
+                if (sensitive) {
+                    literalNeverIndexed(name, huffmanName, value, huffmanValue);
+                } else {
+                    literal(name, huffmanName, value, huffmanValue);
+                }
             }
         }
     }
 
+    private boolean isHuffmanBetterFor(CharSequence value) {
+        // prefer Huffman encoding only if it is strictly smaller than Latin-1
+        return huffmanWriter.lengthOf(value) < value.length();
+    }
+
     /**
      * Sets a maximum capacity of the header table.
      *
@@ -299,7 +308,7 @@
         if (calculated < 0 || calculated > capacity) {
             throw new IllegalArgumentException(
                     format("0 <= calculated <= capacity: calculated=%s, capacity=%s",
-                            calculated, capacity));
+                           calculated, capacity));
         }
         capacityUpdate = true;
         // maxCapacity needs to be updated unconditionally, so the encoder
@@ -416,8 +425,9 @@
                                  boolean useHuffman)
             throws IndexOutOfBoundsException {
         if (logger.isLoggable(EXTRA)) {
-            logger.log(EXTRA, () -> format("literal without indexing ('%s', '%s')",
-                                           index, value));
+            logger.log(EXTRA, () -> format(
+                    "literal without indexing (%s, '%s', huffman=%b)",
+                    index, value, useHuffman));
         }
         checkEncoding();
         encoding = true;
@@ -428,10 +438,12 @@
     protected final void literal(CharSequence name,
                                  boolean nameHuffman,
                                  CharSequence value,
-                                 boolean valueHuffman) {
+                                 boolean valueHuffman)
+    {
         if (logger.isLoggable(EXTRA)) {
-            logger.log(EXTRA, () -> format("literal without indexing ('%s', '%s')",
-                                           name, value));
+            logger.log(EXTRA, () -> format(
+                    "literal without indexing ('%s', huffman=%b, '%s', huffman=%b)",
+                    name, nameHuffman, value, valueHuffman));
         }
         checkEncoding();
         encoding = true;
@@ -442,10 +454,12 @@
     protected final void literalNeverIndexed(int index,
                                              CharSequence value,
                                              boolean valueHuffman)
-            throws IndexOutOfBoundsException {
+            throws IndexOutOfBoundsException
+    {
         if (logger.isLoggable(EXTRA)) {
-            logger.log(EXTRA, () -> format("literal never indexed ('%s', '%s')",
-                                           index, value));
+            logger.log(EXTRA, () -> format(
+                    "literal never indexed (%s, '%s', huffman=%b)",
+                    index, value, valueHuffman));
         }
         checkEncoding();
         encoding = true;
@@ -458,8 +472,9 @@
                                              CharSequence value,
                                              boolean valueHuffman) {
         if (logger.isLoggable(EXTRA)) {
-            logger.log(EXTRA, () -> format("literal never indexed ('%s', '%s')",
-                                           name, value));
+            logger.log(EXTRA, () -> format(
+                    "literal never indexed ('%s', huffman=%b, '%s', huffman=%b)",
+                    name, nameHuffman, value, valueHuffman));
         }
         checkEncoding();
         encoding = true;
@@ -472,8 +487,9 @@
                                              boolean valueHuffman)
             throws IndexOutOfBoundsException {
         if (logger.isLoggable(EXTRA)) {
-            logger.log(EXTRA, () -> format("literal with incremental indexing ('%s', '%s')",
-                                           index, value));
+            logger.log(EXTRA, () -> format(
+                    "literal with incremental indexing (%s, '%s', huffman=%b)",
+                    index, value, valueHuffman));
         }
         checkEncoding();
         encoding = true;
@@ -485,9 +501,10 @@
                                              boolean nameHuffman,
                                              CharSequence value,
                                              boolean valueHuffman) {
-        if (logger.isLoggable(EXTRA)) { // TODO: include huffman info?
-            logger.log(EXTRA, () -> format("literal with incremental indexing ('%s', '%s')",
-                                           name, value));
+        if (logger.isLoggable(EXTRA)) {
+            logger.log(EXTRA, () -> format(
+                    "literal with incremental indexing ('%s', huffman=%b, '%s', huffman=%b)",
+                    name, nameHuffman, value, valueHuffman));
         }
         checkEncoding();
         encoding = true;
@@ -506,7 +523,7 @@
         if (capacity > this.maxCapacity) {
             throw new IllegalArgumentException(
                     format("capacity <= maxCapacity: capacity=%s, maxCapacity=%s",
-                            capacity, maxCapacity));
+                           capacity, maxCapacity));
         }
         writer = sizeUpdateWriter.maxHeaderTableSize(capacity);
     }
--- a/src/java.net.http/share/classes/jdk/internal/net/http/hpack/HPACK.java	Wed Jun 20 17:15:16 2018 +0200
+++ b/src/java.net.http/share/classes/jdk/internal/net/http/hpack/HPACK.java	Wed Jun 20 09:05:57 2018 -0700
@@ -27,6 +27,7 @@
 import jdk.internal.net.http.common.Utils;
 import jdk.internal.net.http.hpack.HPACK.Logger.Level;
 
+import java.nio.ByteBuffer;
 import java.security.AccessController;
 import java.security.PrivilegedAction;
 import java.util.Map;
@@ -171,4 +172,124 @@
         }
 
     }
+
+    // -- low-level utilities --
+
+    @FunctionalInterface
+    interface BufferUpdateConsumer {
+        void accept(long data, int len);
+    }
+
+    @SuppressWarnings("fallthrough")
+    public static int read(ByteBuffer source,
+                           long buffer,
+                           int bufferLen,
+                           BufferUpdateConsumer consumer)
+    {
+        // read as much as possible (up to 8 bytes)
+        int nBytes = Math.min((64 - bufferLen) >> 3, source.remaining());
+        switch (nBytes) {
+            case 0:
+                break;
+            case 3:
+                buffer |= ((source.get() & 0x00000000000000ffL) << (56 - bufferLen));
+                bufferLen += 8;
+            case 2:
+                buffer |= ((source.get() & 0x00000000000000ffL) << (56 - bufferLen));
+                bufferLen += 8;
+            case 1:
+                buffer |= ((source.get() & 0x00000000000000ffL) << (56 - bufferLen));
+                bufferLen += 8;
+                consumer.accept(buffer, bufferLen);
+                break;
+            case 7:
+                buffer |= ((source.get() & 0x00000000000000ffL) << (56 - bufferLen));
+                bufferLen += 8;
+            case 6:
+                buffer |= ((source.get() & 0x00000000000000ffL) << (56 - bufferLen));
+                bufferLen += 8;
+            case 5:
+                buffer |= ((source.get() & 0x00000000000000ffL) << (56 - bufferLen));
+                bufferLen += 8;
+            case 4:
+                buffer |= ((source.getInt() & 0x00000000ffffffffL) << (32 - bufferLen));
+                bufferLen += 32;
+                consumer.accept(buffer, bufferLen);
+                break;
+            case 8:
+                buffer = source.getLong();
+                bufferLen = 64;
+                consumer.accept(buffer, bufferLen);
+                break;
+            default:
+                throw new InternalError(String.valueOf(nBytes));
+        }
+        return nBytes;
+    }
+
+    // The number of bytes that can be written at once
+    // (calculating in bytes, not bits, since
+    //  destination.remaining() * 8 might overflow)
+    @SuppressWarnings("fallthrough")
+    public static int write(long buffer,
+                            int bufferLen,
+                            BufferUpdateConsumer consumer,
+                            ByteBuffer destination)
+    {
+        int nBytes = Math.min(bufferLen >> 3, destination.remaining());
+        switch (nBytes) {
+            case 0:
+                break;
+            case 3:
+                destination.put((byte) (buffer >>> 56));
+                buffer <<= 8;
+                bufferLen -= 8;
+            case 2:
+                destination.put((byte) (buffer >>> 56));
+                buffer <<= 8;
+                bufferLen -= 8;
+            case 1:
+                destination.put((byte) (buffer >>> 56));
+                buffer <<= 8;
+                bufferLen -= 8;
+                consumer.accept(buffer, bufferLen);
+                break;
+            case 7:
+                destination.put((byte) (buffer >>> 56));
+                buffer <<= 8;
+                bufferLen -= 8;
+            case 6:
+                destination.put((byte) (buffer >>> 56));
+                buffer <<= 8;
+                bufferLen -= 8;
+            case 5:
+                destination.put((byte) (buffer >>> 56));
+                buffer <<= 8;
+                bufferLen -= 8;
+            case 4:
+                destination.putInt((int) (buffer >>> 32));
+                buffer <<= 32;
+                bufferLen -= 32;
+                consumer.accept(buffer, bufferLen);
+                break;
+            case 8:
+                destination.putLong(buffer);
+                buffer = 0;
+                bufferLen = 0;
+                consumer.accept(buffer, bufferLen);
+                break;
+            default:
+                throw new InternalError(String.valueOf(nBytes));
+        }
+        return nBytes;
+    }
+
+    /*
+     * Returns the number of bytes the given number of bits constitute.
+     */
+    static int bytesForBits(int n) {
+        assert (n / 8 + (n % 8 != 0 ? 1 : 0)) == (n + 7) / 8
+                && (n + 7) / 8 == ((n + 7) >> 3) : n;
+        return (n + 7) >> 3;
+    }
 }
--- a/src/java.net.http/share/classes/jdk/internal/net/http/hpack/HeaderTable.java	Wed Jun 20 17:15:16 2018 +0200
+++ b/src/java.net.http/share/classes/jdk/internal/net/http/hpack/HeaderTable.java	Wed Jun 20 09:05:57 2018 -0700
@@ -28,7 +28,6 @@
 
 import java.util.Deque;
 import java.util.HashMap;
-import java.util.LinkedHashMap;
 import java.util.LinkedList;
 import java.util.Map;
 
@@ -104,16 +103,24 @@
     // Long.MAX_VALUE :-)
     //
 
-    private static final Map<String, LinkedHashMap<String, Integer>> staticIndexes;
+    /* An immutable map of static header fields' indexes */
+    private static final Map<String, Map<String, Integer>> staticIndexes;
 
     static {
-        staticIndexes = new HashMap<>(STATIC_TABLE_LENGTH); // TODO: Map.of
+        Map<String, Map<String, Integer>> map
+                = new HashMap<>(STATIC_TABLE_LENGTH);
         for (int i = 1; i <= STATIC_TABLE_LENGTH; i++) {
-            HeaderField f = staticTable[i];
-            Map<String, Integer> values = staticIndexes
-                    .computeIfAbsent(f.name, k -> new LinkedHashMap<>());
+            HeaderField f = staticTable.get(i);
+            Map<String, Integer> values
+                    = map.computeIfAbsent(f.name, k -> new HashMap<>());
             values.put(f.value, i);
         }
+        // create an immutable deep copy
+        Map<String, Map<String, Integer>> copy = new HashMap<>(map.size());
+        for (Map.Entry<String, Map<String, Integer>> e : map.entrySet()) {
+            copy.put(e.getKey(), Map.copyOf(e.getValue()));
+        }
+        staticIndexes = Map.copyOf(copy);
     }
 
     //                name  ->    (value ->    [index])
--- a/src/java.net.http/share/classes/jdk/internal/net/http/hpack/ISO_8859_1.java	Wed Jun 20 17:15:16 2018 +0200
+++ b/src/java.net.http/share/classes/jdk/internal/net/http/hpack/ISO_8859_1.java	Wed Jun 20 09:05:57 2018 -0700
@@ -46,29 +46,58 @@
 
     public static final class Reader {
 
+        private final HPACK.BufferUpdateConsumer UPDATER =
+                (buf, bufLen) -> {
+                    buffer = buf;
+                    bufferLen = bufLen;
+                };
+
+        private long buffer;
+        private int bufferLen;
+
         public void read(ByteBuffer source, Appendable destination)
-                throws IOException {
-            for (int i = 0, len = source.remaining(); i < len; i++) {
-                char c = (char) (source.get() & 0xff);
-                try {
-                    destination.append(c);
-                } catch (IOException e) {
-                    throw new IOException(
-                            "Error appending to the destination", e);
+                throws IOException
+        {
+            while (true) {
+                int nBytes = HPACK.read(source, buffer, bufferLen, UPDATER);
+                if (nBytes == 0) {
+                    return;
+                }
+                assert bufferLen % 8 == 0 : bufferLen;
+                while (bufferLen > 0) {
+                    char c = (char) (buffer >>> 56);
+                    try {
+                        destination.append(c);
+                    } catch (IOException e) {
+                        throw new IOException(
+                                "Error appending to the destination", e);
+                    }
+                    buffer <<= 8;
+                    bufferLen -= 8;
                 }
             }
         }
 
         public Reader reset() {
+            buffer = 0;
+            bufferLen = 0;
             return this;
         }
     }
 
     public static final class Writer {
 
+        private final HPACK.BufferUpdateConsumer UPDATER =
+                (buf, bufLen) -> {
+                    buffer = buf;
+                    bufferLen = bufLen;
+                };
+
         private CharSequence source;
         private int pos;
         private int end;
+        private long buffer;
+        private int bufferLen;
 
         public Writer configure(CharSequence source, int start, int end) {
             this.source = source;
@@ -78,25 +107,39 @@
         }
 
         public boolean write(ByteBuffer destination) {
-            for (; pos < end; pos++) {
-                char c = source.charAt(pos);
-                if (c > '\u00FF') {
-                    throw new IllegalArgumentException(
-                            "Illegal ISO-8859-1 char: " + (int) c);
+            while (true) {
+                while (true) { // stuff codes into long
+                    if (pos >= end) {
+                        break;
+                    }
+                    char c = source.charAt(pos);
+                    if (c > 255) {
+                        throw new IllegalArgumentException(Integer.toString((int) c));
+                    }
+                    if (bufferLen <= 56) {
+                        buffer |= (((long) c) << (56 - bufferLen)); // append
+                        bufferLen += 8;
+                        pos++;
+                    } else {
+                        break;
+                    }
                 }
-                if (destination.hasRemaining()) {
-                    destination.put((byte) c);
-                } else {
+                if (bufferLen == 0) {
+                    return true;
+                }
+                int nBytes = HPACK.write(buffer, bufferLen, UPDATER, destination);
+                if (nBytes == 0) {
                     return false;
                 }
             }
-            return true;
         }
 
         public Writer reset() {
             source = null;
             pos = -1;
             end = -1;
+            buffer = 0;
+            bufferLen = 0;
             return this;
         }
     }
--- a/src/java.net.http/share/classes/jdk/internal/net/http/hpack/QuickHuffman.java	Wed Jun 20 17:15:16 2018 +0200
+++ b/src/java.net.http/share/classes/jdk/internal/net/http/hpack/QuickHuffman.java	Wed Jun 20 09:05:57 2018 -0700
@@ -25,19 +25,21 @@
 
 package jdk.internal.net.http.hpack;
 
+import jdk.internal.net.http.hpack.HPACK.BufferUpdateConsumer;
+
 import java.io.IOException;
 import java.nio.ByteBuffer;
 import java.util.List;
 import java.util.Objects;
 
+import static jdk.internal.net.http.hpack.HPACK.bytesForBits;
+
 public final class QuickHuffman {
 
     /*
-     * Huffman codes for encoding.
-     *
-     * EOS will never be matched, since there is no symbol for it, thus no need
-     * to store it in the array. Thus, the length of the array is 256, not 257.
-     * Code information for each character is encoded as follows:
+     * Mapping of characters to their code information. Code information
+     * consists of a code value and a code length. Both components are packed in
+     * a single long as follows:
      *
      *     MSB                             LSB
      *     +----------------+----------------+
@@ -46,9 +48,14 @@
      *     |<----- 32 ----->|<----- 32 ----->|
      *     |<------------- 64 -------------->|
      *
-     * The leftmost 32 bits hold the code value. This value is aligned left
-     * (or MSB). The rightmost 32 bits hold the length of the code value.
-     * This length is aligned right (or LSB).
+     * The leftmost 32 bits hold the code value. This value is aligned left (or
+     * MSB). The rightmost 32 bits hold the code length. This length is aligned
+     * right (or LSB). Such structure is possible thanks to codes being up to 30
+     * bits long and their lengths being up to 5 bits long (length = 5..30).
+     * This allows both components to fit into long and, thus, better locality.
+     *
+     * Input strings never contain EOS. Thus there's no need to provide EOS
+     * mapping. Hence, the length of the array is 256, not 257.
      */
     private static final long[] codes = new long[256];
 
@@ -62,16 +69,19 @@
 
     private static final int EOS_LENGTH = 30;
     private static final int EOS_LSB = 0x3fffffff;
-    private static final long EOS_MSB = EOS_LSB << (64 - EOS_LENGTH);
+    // EOS_LSB is casted to long before the shift, to allow long shift (6 bits)
+    // instead of int shift (5 bits):
+    private static final long EOS_MSB = ((long) EOS_LSB) << (64 - EOS_LENGTH);
 
     /*
-     * Huffman codes for decoding.
+     * Huffman trie.
      *
-     * Root node contains 257 descendant nodes, including EOS.
+     * The root node leads to 257 descendant leaf nodes, of which 256 nodes
+     * correspond to characters and 1 node correspond to EOS.
      */
-    private static final Node root;
+    private static final Node root = buildTrie();
 
-    static {
+    private static Node buildTrie() {
         TemporaryNode tmpRoot = new TemporaryNode();
         addChar(tmpRoot,   0, 0x1ff8,     13);
         addChar(tmpRoot,   1, 0x7fffd8,   23);
@@ -333,8 +343,8 @@
 
         // The difference in performance can always be checked by not using
         // the immutable trie:
-        //     root = tmpRoot;
-        root = ImmutableNode.copyOf(tmpRoot);
+        //     return tmpRoot;
+        return ImmutableNode.copyOf(tmpRoot);
     }
 
     private QuickHuffman() { }
@@ -605,6 +615,12 @@
 
     static final class Reader implements Huffman.Reader {
 
+        private final BufferUpdateConsumer UPDATER =
+                (buf, bufLen) -> {
+                    buffer = buf;
+                    bufferLen = bufLen;
+                };
+
         private Node curr = root;  // current position in the trie
         private long buffer;       // bits left from the previous match (aligned to the left, or MSB)
         private int bufferLen;     // number of bits in the buffer
@@ -616,53 +632,9 @@
                          Appendable destination,
                          boolean isLast) throws IOException
         {
-            read(source, destination, true, isLast);
-        }
-
-        @Override
-        public void reset() {
-            curr = root;
-            len = 0;
-            buffer = 0;
-            bufferLen = 0;
-            done = false;
-        }
-
-        @SuppressWarnings("fallthrough")
-        void read(ByteBuffer source,
-                  Appendable destination,
-                  boolean reportEOS, /* reportEOS is exposed for tests */
-                  boolean isLast) throws IOException
-        {
             while (!done) {
-                // read as much as possible (up to 8 bytes)
                 int remaining = source.remaining();
-                int nBytes = Math.min((64 - bufferLen) >> 3, remaining);
-                switch (nBytes) {
-                    case 0:
-                        break;
-                    case 3:
-                        readByte(source);
-                    case 2:
-                        readByte(source);
-                    case 1:
-                        readByte(source);
-                        break;
-                    case 7:
-                        readByte(source);
-                    case 6:
-                        readByte(source);
-                    case 5:
-                        readByte(source);
-                    case 4:
-                        readInt(source);
-                        break;
-                    case 8:
-                        readLong(source);
-                        break;
-                    default:
-                        throw new InternalError(String.valueOf(nBytes));
-                }
+                int nBytes = HPACK.read(source, buffer, bufferLen, UPDATER);
                 // write as much as possible
                 while (true) {
                     if (bufferLen < 8) {
@@ -671,7 +643,7 @@
                         } else if (!isLast) { // exit the method to accept more input
                             return;
                         } else if (bufferLen > 0) { // no more data is expected, pad
-                            // (this padding may be done more than once)
+                                                    // (this padding may be done more than once)
                             buffer |= ((0xff00000000000000L >>> bufferLen)
                                     & 0xff00000000000000L);
                             // do not update bufferLen, since all those ones are
@@ -692,7 +664,7 @@
                             throw new IOException(
                                     "Not a EOS prefix padding or unexpected end of data");
                         }
-                        if (reportEOS && node.isEOSPath()) {
+                        if (node.isEOSPath()) {
                             throw new IOException("Encountered EOS");
                         }
                         destination.append(node.getSymbol());
@@ -715,27 +687,24 @@
             }
         }
 
-        private void readLong(ByteBuffer source) {
-            buffer = source.getLong();
-            bufferLen = 64;
-        }
-
-        private void readInt(ByteBuffer source) {
-            long b;
-            b = source.getInt() & 0x00000000ffffffffL;
-            buffer |= (b << (32 - bufferLen));
-            bufferLen += 32;
-        }
-
-        private void readByte(ByteBuffer source) {
-            long b = source.get() & 0x00000000000000ffL;
-            buffer |= (b << (56 - bufferLen));
-            bufferLen += 8;
+        @Override
+        public void reset() {
+            curr = root;
+            len = 0;
+            buffer = 0;
+            bufferLen = 0;
+            done = false;
         }
     }
 
     static final class Writer implements Huffman.Writer {
 
+        private final BufferUpdateConsumer UPDATER =
+                (buf, bufLen) -> {
+                    buffer = buf;
+                    bufferLen = bufLen;
+                };
+
         private CharSequence source;
         private boolean padded;
         private int pos;
@@ -752,43 +721,44 @@
             return this;
         }
 
-        @SuppressWarnings("fallthrough")
         @Override
         public boolean write(ByteBuffer destination) {
             while (true) {
-                while (bufferLen < 32 && pos < end) {
-                    char c = source.charAt(pos++);
-                    buffer |= (codeValueOf(c) >>> bufferLen); // append
-                    bufferLen += codeLengthOf(c);
+                while (true) { // stuff codes into long
+                    if (pos >= end) {
+                        break;
+                    }
+                    char c = source.charAt(pos);
+                    if (c > 255) {
+                        throw new IllegalArgumentException("char=" + ((int) c));
+                    }
+                    long len = codeLengthOf(c);
+                    if (bufferLen + len <= 64) {
+                        buffer |= (codeValueOf(c) >>> bufferLen); // append
+                        bufferLen += len;
+                        pos++;
+                    } else {
+                        break;
+                    }
                 }
                 if (bufferLen == 0) {
                     return true;
                 }
-                if (pos >= end && !padded) { // no more chars, pad
+                if (pos >= end && !padded) { // no more input chars are expected, pad
                     padded = true;
-                    buffer |= (EOS_MSB >>> bufferLen);
-                    bufferLen = bytesForBits(bufferLen) << 3;
+                    // A long shift to 64 will result in the same long, not
+                    // necessarily 0L. In which case padding will be performed
+                    // incorrectly. If bufferLen is equal to 64, the shift (and
+                    // padding) in not performed.
+                    // (see https://docs.oracle.com/javase/specs/jls/se10/html/jls-15.html#jls-15.19)
+                    if (bufferLen != 64) {
+                        buffer |= (EOS_MSB >>> bufferLen);
+                        bufferLen = bytesForBits(bufferLen) << 3;
+                    }
                 }
-                // The number of bytes that can be written at once
-                // (calculating in bytes, not bits, since
-                //  destination.remaining() * 8 might overflow)
-
-                int nBytes = Math.min(bytesForBits(bufferLen), destination.remaining()); // ceil?
-                switch (nBytes) {
-                    case 0:
-                        return false;
-                    case 1:
-                    case 2:
-                    case 3:
-                        destination.put((byte) (buffer >>> 56));
-                        buffer <<= 8;
-                        bufferLen -= 8;
-                        break;
-                    default:
-                        destination.putInt((int) (buffer >>> 32));
-                        buffer <<= 32;
-                        bufferLen -= 32;
-                        break;
+                int nBytes = HPACK.write(buffer, bufferLen, UPDATER, destination);
+                if (nBytes == 0) {
+                    return false;
                 }
             }
         }
@@ -814,13 +784,4 @@
             return bytesForBits(len);
         }
     }
-
-    /*
-     * Returns the number of bytes the given number of bits constitute.
-     */
-    private static int bytesForBits(int n) {
-        assert (n / 8 + (n % 8 != 0 ? 1 : 0)) == (n + 7) / 8
-                && (n + 7) / 8 == ((n + 7) >> 3) : n;
-        return (n + 7) >> 3;
-    }
 }
--- a/src/java.net.http/share/classes/jdk/internal/net/http/hpack/SimpleHeaderTable.java	Wed Jun 20 17:15:16 2018 +0200
+++ b/src/java.net.http/share/classes/jdk/internal/net/http/hpack/SimpleHeaderTable.java	Wed Jun 20 09:05:57 2018 -0700
@@ -26,6 +26,7 @@
 
 import jdk.internal.net.http.hpack.HPACK.Logger;
 
+import java.util.List;
 import java.util.NoSuchElementException;
 
 import static jdk.internal.net.http.common.Utils.pow2Size;
@@ -41,8 +42,9 @@
  */
 class SimpleHeaderTable {
 
-    protected static final HeaderField[] staticTable = {
-            null, // To make index 1-based, instead of 0-based
+    /* An immutable list of static header fields */
+    protected static final List<HeaderField> staticTable = List.of(
+            new HeaderField(""), // A dummy to make the list index 1-based, instead of 0-based
             new HeaderField(":authority"),
             new HeaderField(":method", "GET"),
             new HeaderField(":method", "POST"),
@@ -103,10 +105,9 @@
             new HeaderField("user-agent"),
             new HeaderField("vary"),
             new HeaderField("via"),
-            new HeaderField("www-authenticate")
-    };
+            new HeaderField("www-authenticate"));
 
-    protected static final int STATIC_TABLE_LENGTH = staticTable.length - 1;
+    protected static final int STATIC_TABLE_LENGTH = staticTable.size() - 1;
     protected static final int ENTRY_SIZE = 32;
 
     private final Logger logger;
@@ -134,7 +135,7 @@
     HeaderField get(int index) {
         checkIndex(index);
         if (index <= STATIC_TABLE_LENGTH) {
-            return staticTable[index];
+            return staticTable.get(index);
         } else {
             return buffer.get(index - STATIC_TABLE_LENGTH - 1);
         }
@@ -263,23 +264,6 @@
         public String toString() {
             return value.isEmpty() ? name : name + ": " + value;
         }
-
-        @Override // TODO: remove since used only for testing
-        public boolean equals(Object o) {
-            if (this == o) {
-                return true;
-            }
-            if (o == null || getClass() != o.getClass()) {
-                return false;
-            }
-            HeaderField that = (HeaderField) o;
-            return name.equals(that.name) && value.equals(that.value);
-        }
-
-        @Override // TODO: remove since used only for testing
-        public int hashCode() {
-            return 31 * name.hashCode() + value.hashCode();
-        }
     }
 
     private final CircularBuffer<HeaderField> buffer = new CircularBuffer<>(0);
--- a/src/java.net.http/share/classes/jdk/internal/net/http/websocket/OpeningHandshake.java	Wed Jun 20 17:15:16 2018 +0200
+++ b/src/java.net.http/share/classes/jdk/internal/net/http/websocket/OpeningHandshake.java	Wed Jun 20 09:05:57 2018 -0700
@@ -32,6 +32,9 @@
 import java.net.http.HttpResponse;
 import java.net.http.HttpResponse.BodyHandlers;
 import java.net.http.WebSocketHandshakeException;
+
+import jdk.internal.net.http.HttpRequestBuilderImpl;
+import jdk.internal.net.http.HttpRequestImpl;
 import jdk.internal.net.http.common.MinimalFuture;
 import jdk.internal.net.http.common.Pair;
 import jdk.internal.net.http.common.Utils;
@@ -104,7 +107,7 @@
         }
     }
 
-    private final HttpRequest request;
+    private final HttpRequestImpl request;
     private final Collection<String> subprotocols;
     private final String nonce;
 
@@ -114,7 +117,7 @@
         checkPermissions(b, proxy);
         this.client = b.getClient();
         URI httpURI = createRequestURI(b.getUri());
-        HttpRequest.Builder requestBuilder = HttpRequest.newBuilder(httpURI);
+        HttpRequestBuilderImpl requestBuilder = new HttpRequestBuilderImpl(httpURI);
         Duration connectTimeout = b.getConnectTimeout();
         if (connectTimeout != null) {
             requestBuilder.timeout(connectTimeout);
@@ -127,7 +130,7 @@
         }
         this.subprotocols = createRequestSubprotocols(b.getSubprotocols());
         if (!this.subprotocols.isEmpty()) {
-            String p = this.subprotocols.stream().collect(Collectors.joining(", "));
+            String p = String.join(", ", this.subprotocols);
             requestBuilder.header(HEADER_PROTOCOL, p);
         }
         requestBuilder.header(HEADER_VERSION, VERSION);
@@ -137,12 +140,12 @@
         // to upgrade from HTTP/2 to WebSocket (as of August 2016):
         //
         //     https://tools.ietf.org/html/draft-hirano-httpbis-websocket-over-http2-00
-        this.request = requestBuilder.version(Version.HTTP_1_1).GET().build();
-        WebSocketRequest r = (WebSocketRequest) this.request;
-        r.isWebSocket(true);
-        r.setSystemHeader(HEADER_UPGRADE, "websocket");
-        r.setSystemHeader(HEADER_CONNECTION, "Upgrade");
-        r.setProxy(proxy);
+        requestBuilder.version(Version.HTTP_1_1).GET();
+        request = requestBuilder.buildForWebSocket();
+        request.isWebSocket(true);
+        request.setSystemHeader(HEADER_UPGRADE, "websocket");
+        request.setSystemHeader(HEADER_CONNECTION, "Upgrade");
+        request.setProxy(proxy);
     }
 
     private static Collection<String> createRequestSubprotocols(
--- a/src/java.net.http/share/classes/jdk/internal/net/http/websocket/WebSocketImpl.java	Wed Jun 20 17:15:16 2018 +0200
+++ b/src/java.net.http/share/classes/jdk/internal/net/http/websocket/WebSocketImpl.java	Wed Jun 20 09:05:57 2018 -0700
@@ -34,7 +34,6 @@
 import jdk.internal.net.http.websocket.OpeningHandshake.Result;
 
 import java.io.IOException;
-import java.lang.System.Logger.Level;
 import java.lang.ref.Reference;
 import java.net.ProtocolException;
 import java.net.URI;
@@ -88,7 +87,7 @@
         PING,
         PONG,
         CLOSE,
-        ERROR;
+        ERROR
     }
 
     private final AtomicReference<ByteBuffer> lastAutomaticPong = new AtomicReference<>();
--- a/src/jdk.httpserver/share/classes/sun/net/httpserver/ExchangeImpl.java	Wed Jun 20 17:15:16 2018 +0200
+++ b/src/jdk.httpserver/share/classes/sun/net/httpserver/ExchangeImpl.java	Wed Jun 20 09:05:57 2018 -0700
@@ -227,8 +227,8 @@
             contentLen = -1;
         }
 
-        if (isHeadRequest()) {
-            /* HEAD requests should not set a content length by passing it
+        if (isHeadRequest() || rCode == 304) {
+            /* HEAD requests or 304 responses should not set a content length by passing it
              * through this API, but should instead manually set the required
              * headers.*/
             if (contentLen >= 0) {
@@ -239,7 +239,7 @@
             }
             noContentToSend = true;
             contentLen = 0;
-        } else { /* not a HEAD request */
+        } else { /* not a HEAD request or 304 response */
             if (contentLen == 0) {
                 if (http10) {
                     o.setWrappedStream (new UndefLengthOutputStream (this, ros));
--- a/test/jdk/java/net/httpclient/AbstractNoBody.java	Wed Jun 20 17:15:16 2018 +0200
+++ b/test/jdk/java/net/httpclient/AbstractNoBody.java	Wed Jun 20 09:05:57 2018 -0700
@@ -57,7 +57,7 @@
     String https2URI_chunk;
 
     static final String SIMPLE_STRING = "Hello world. Goodbye world";
-    static final int ITERATION_COUNT = 10;
+    static final int ITERATION_COUNT = 3;
     // a shared executor helps reduce the amount of threads created by the test
     static final Executor executor = Executors.newFixedThreadPool(ITERATION_COUNT * 2);
     static final ExecutorService serverExecutor = Executors.newFixedThreadPool(ITERATION_COUNT * 4);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/java/net/httpclient/AbstractThrowingPublishers.java	Wed Jun 20 09:05:57 2018 -0700
@@ -0,0 +1,724 @@
+/*
+ * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+import com.sun.net.httpserver.HttpServer;
+import com.sun.net.httpserver.HttpsConfigurator;
+import com.sun.net.httpserver.HttpsServer;
+import jdk.testlibrary.SimpleSSLContext;
+import org.testng.annotations.AfterClass;
+import org.testng.annotations.AfterTest;
+import org.testng.annotations.BeforeTest;
+import org.testng.annotations.DataProvider;
+import org.testng.annotations.Test;
+
+import javax.net.ssl.SSLContext;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.UncheckedIOException;
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
+import java.net.URI;
+import java.net.http.HttpClient;
+import java.net.http.HttpRequest;
+import java.net.http.HttpRequest.BodyPublisher;
+import java.net.http.HttpRequest.BodyPublishers;
+import java.net.http.HttpResponse;
+import java.net.http.HttpResponse.BodyHandler;
+import java.net.http.HttpResponse.BodyHandlers;
+import java.nio.ByteBuffer;
+import java.nio.charset.StandardCharsets;
+import java.util.EnumSet;
+import java.util.List;
+import java.util.Set;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.CompletionException;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Executor;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Flow;
+import java.util.concurrent.SubmissionPublisher;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.function.BiPredicate;
+import java.util.function.Consumer;
+import java.util.function.Supplier;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import static java.lang.String.format;
+import static java.lang.System.out;
+import static java.nio.charset.StandardCharsets.UTF_8;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertTrue;
+
+public abstract class AbstractThrowingPublishers implements HttpServerAdapters {
+
+    SSLContext sslContext;
+    HttpTestServer httpTestServer;    // HTTP/1.1    [ 4 servers ]
+    HttpTestServer httpsTestServer;   // HTTPS/1.1
+    HttpTestServer http2TestServer;   // HTTP/2 ( h2c )
+    HttpTestServer https2TestServer;  // HTTP/2 ( h2  )
+    String httpURI_fixed;
+    String httpURI_chunk;
+    String httpsURI_fixed;
+    String httpsURI_chunk;
+    String http2URI_fixed;
+    String http2URI_chunk;
+    String https2URI_fixed;
+    String https2URI_chunk;
+
+    static final int ITERATION_COUNT = 1;
+    // a shared executor helps reduce the amount of threads created by the test
+    static final Executor executor = new TestExecutor(Executors.newCachedThreadPool());
+    static final ConcurrentMap<String, Throwable> FAILURES = new ConcurrentHashMap<>();
+    static volatile boolean tasksFailed;
+    static final AtomicLong serverCount = new AtomicLong();
+    static final AtomicLong clientCount = new AtomicLong();
+    static final long start = System.nanoTime();
+    public static String now() {
+        long now = System.nanoTime() - start;
+        long secs = now / 1000_000_000;
+        long mill = (now % 1000_000_000) / 1000_000;
+        long nan = now % 1000_000;
+        return String.format("[%d s, %d ms, %d ns] ", secs, mill, nan);
+    }
+
+    final ReferenceTracker TRACKER = ReferenceTracker.INSTANCE;
+    private volatile HttpClient sharedClient;
+
+    static class TestExecutor implements Executor {
+        final AtomicLong tasks = new AtomicLong();
+        Executor executor;
+        TestExecutor(Executor executor) {
+            this.executor = executor;
+        }
+
+        @Override
+        public void execute(Runnable command) {
+            long id = tasks.incrementAndGet();
+            executor.execute(() -> {
+                try {
+                    command.run();
+                } catch (Throwable t) {
+                    tasksFailed = true;
+                    System.out.printf(now() + "Task %s failed: %s%n", id, t);
+                    System.err.printf(now() + "Task %s failed: %s%n", id, t);
+                    FAILURES.putIfAbsent("Task " + id, t);
+                    throw t;
+                }
+            });
+        }
+    }
+
+    @AfterClass
+    static final void printFailedTests() {
+        out.println("\n=========================");
+        try {
+            out.printf("%n%sCreated %d servers and %d clients%n",
+                    now(), serverCount.get(), clientCount.get());
+            if (FAILURES.isEmpty()) return;
+            out.println("Failed tests: ");
+            FAILURES.entrySet().forEach((e) -> {
+                out.printf("\t%s: %s%n", e.getKey(), e.getValue());
+                e.getValue().printStackTrace(out);
+            });
+            if (tasksFailed) {
+                System.out.println("WARNING: Some tasks failed");
+            }
+        } finally {
+            out.println("\n=========================\n");
+        }
+    }
+
+    private String[] uris() {
+        return new String[] {
+                httpURI_fixed,
+                httpURI_chunk,
+                httpsURI_fixed,
+                httpsURI_chunk,
+                http2URI_fixed,
+                http2URI_chunk,
+                https2URI_fixed,
+                https2URI_chunk,
+        };
+    }
+
+    @DataProvider(name = "sanity")
+    public Object[][] sanity() {
+        String[] uris = uris();
+        Object[][] result = new Object[uris.length * 2][];
+        //Object[][] result = new Object[uris.length][];
+        int i = 0;
+        for (boolean sameClient : List.of(false, true)) {
+            //if (!sameClient) continue;
+            for (String uri: uris()) {
+                result[i++] = new Object[] {uri + "/sanity", sameClient};
+            }
+        }
+        assert i == uris.length * 2;
+        // assert i == uris.length ;
+        return result;
+    }
+
+    enum Where {
+        BEFORE_SUBSCRIBE, BEFORE_REQUEST, BEFORE_NEXT_REQUEST, BEFORE_CANCEL,
+        AFTER_SUBSCRIBE, AFTER_REQUEST, AFTER_NEXT_REQUEST, AFTER_CANCEL;
+        public Consumer<Where> select(Consumer<Where> consumer) {
+            return new Consumer<Where>() {
+                @Override
+                public void accept(Where where) {
+                    if (Where.this == where) {
+                        consumer.accept(where);
+                    }
+                }
+            };
+        }
+    }
+
+    private Object[][] variants(List<Thrower> throwers, Set<Where> whereValues) {
+        String[] uris = uris();
+        Object[][] result = new Object[uris.length * 2 * throwers.size()][];
+        //Object[][] result = new Object[(uris.length/2) * 2 * 2][];
+        int i = 0;
+        for (Thrower thrower : throwers) {
+            for (boolean sameClient : List.of(false, true)) {
+                for (String uri : uris()) {
+                    // if (uri.contains("http2") || uri.contains("https2")) continue;
+                    // if (!sameClient) continue;
+                    result[i++] = new Object[]{uri, sameClient, thrower, whereValues};
+                }
+            }
+        }
+        assert i == uris.length * 2 * throwers.size();
+        //assert Stream.of(result).filter(o -> o != null).count() == result.length;
+        return result;
+    }
+
+    @DataProvider(name = "subscribeProvider")
+    public Object[][] subscribeProvider() {
+        return  variants(List.of(
+                new UncheckedCustomExceptionThrower(),
+                new UncheckedIOExceptionThrower()),
+                EnumSet.of(Where.BEFORE_SUBSCRIBE, Where.AFTER_SUBSCRIBE));
+    }
+
+    @DataProvider(name = "requestProvider")
+    public Object[][] requestProvider() {
+        return  variants(List.of(
+                new UncheckedCustomExceptionThrower(),
+                new UncheckedIOExceptionThrower()),
+                EnumSet.of(Where.BEFORE_REQUEST, Where.AFTER_REQUEST));
+    }
+
+    @DataProvider(name = "nextRequestProvider")
+    public Object[][] nextRequestProvider() {
+        return  variants(List.of(
+                new UncheckedCustomExceptionThrower(),
+                new UncheckedIOExceptionThrower()),
+                EnumSet.of(Where.BEFORE_NEXT_REQUEST, Where.AFTER_NEXT_REQUEST));
+    }
+
+    @DataProvider(name = "beforeCancelProviderIO")
+    public Object[][] beforeCancelProviderIO() {
+        return  variants(List.of(
+                new UncheckedIOExceptionThrower()),
+                EnumSet.of(Where.BEFORE_CANCEL));
+    }
+
+    @DataProvider(name = "afterCancelProviderIO")
+    public Object[][] afterCancelProviderIO() {
+        return  variants(List.of(
+                new UncheckedIOExceptionThrower()),
+                EnumSet.of(Where.AFTER_CANCEL));
+    }
+
+    @DataProvider(name = "beforeCancelProviderCustom")
+    public Object[][] beforeCancelProviderCustom() {
+        return  variants(List.of(
+                new UncheckedCustomExceptionThrower()),
+                EnumSet.of(Where.BEFORE_CANCEL));
+    }
+
+    @DataProvider(name = "afterCancelProviderCustom")
+    public Object[][] afterCancelProvider() {
+        return  variants(List.of(
+                new UncheckedCustomExceptionThrower()),
+                EnumSet.of(Where.AFTER_CANCEL));
+    }
+
+    private HttpClient makeNewClient() {
+        clientCount.incrementAndGet();
+        return TRACKER.track(HttpClient.newBuilder()
+                .proxy(HttpClient.Builder.NO_PROXY)
+                .executor(executor)
+                .sslContext(sslContext)
+                .build());
+    }
+
+    HttpClient newHttpClient(boolean share) {
+        if (!share) return makeNewClient();
+        HttpClient shared = sharedClient;
+        if (shared != null) return shared;
+        synchronized (this) {
+            shared = sharedClient;
+            if (shared == null) {
+                shared = sharedClient = makeNewClient();
+            }
+            return shared;
+        }
+    }
+
+    final String BODY = "Some string | that ? can | be split ? several | ways.";
+
+    //@Test(dataProvider = "sanity")
+    protected void testSanityImpl(String uri, boolean sameClient)
+            throws Exception {
+        HttpClient client = null;
+        out.printf("%n%s testSanity(%s, %b)%n", now(), uri, sameClient);
+        for (int i=0; i< ITERATION_COUNT; i++) {
+            if (!sameClient || client == null)
+                client = newHttpClient(sameClient);
+
+            SubmissionPublisher<ByteBuffer> publisher
+                    = new SubmissionPublisher<>(executor,10);
+            ThrowingBodyPublisher bodyPublisher = new ThrowingBodyPublisher((w) -> {},
+                    BodyPublishers.fromPublisher(publisher));
+            CompletableFuture<Void> subscribedCF = bodyPublisher.subscribedCF();
+            subscribedCF.whenComplete((r,t) -> System.out.println(now() + " subscribe completed " + t))
+                    .thenAcceptAsync((v) -> {
+                                Stream.of(BODY.split("\\|"))
+                                        .forEachOrdered(s -> {
+                                                System.out.println("submitting \"" + s +"\"");
+                                                publisher.submit(ByteBuffer.wrap(s.getBytes(StandardCharsets.UTF_8)));
+                                        });
+                                System.out.println("publishing done");
+                                publisher.close();
+                            },
+                    executor);
+
+            HttpRequest req = HttpRequest.newBuilder(URI.create(uri))
+                    .POST(bodyPublisher)
+                    .build();
+            BodyHandler<String> handler = BodyHandlers.ofString();
+            CompletableFuture<HttpResponse<String>> response = client.sendAsync(req, handler);
+
+            String body = response.join().body();
+            assertEquals(body, Stream.of(BODY.split("\\|")).collect(Collectors.joining()));
+        }
+    }
+
+    // @Test(dataProvider = "variants")
+    protected void testThrowingAsStringImpl(String uri,
+                                     boolean sameClient,
+                                     Thrower thrower,
+                                     Set<Where> whereValues)
+            throws Exception
+    {
+        String test = format("testThrowingAsString(%s, %b, %s, %s)",
+                             uri, sameClient, thrower, whereValues);
+        List<byte[]> bytes = Stream.of(BODY.split("|"))
+                .map(s -> s.getBytes(UTF_8))
+                .collect(Collectors.toList());
+        testThrowing(test, uri, sameClient, () -> BodyPublishers.ofByteArrays(bytes),
+                this::shouldNotThrowInCancel, thrower,false, whereValues);
+    }
+
+    private <T,U> void testThrowing(String name, String uri, boolean sameClient,
+                                    Supplier<BodyPublisher> publishers,
+                                    Finisher finisher, Thrower thrower,
+                                    boolean async, Set<Where> whereValues)
+            throws Exception
+    {
+        out.printf("%n%s%s%n", now(), name);
+        try {
+            testThrowing(uri, sameClient, publishers, finisher, thrower, async, whereValues);
+        } catch (Error | Exception x) {
+            FAILURES.putIfAbsent(name, x);
+            throw x;
+        }
+    }
+
+    private void testThrowing(String uri, boolean sameClient,
+                                    Supplier<BodyPublisher> publishers,
+                                    Finisher finisher, Thrower thrower,
+                                    boolean async, Set<Where> whereValues)
+            throws Exception
+    {
+        HttpClient client = null;
+        for (Where where : whereValues) {
+            //if (where == Where.ON_SUBSCRIBE) continue;
+            //if (where == Where.ON_ERROR) continue;
+            if (!sameClient || client == null)
+                client = newHttpClient(sameClient);
+
+            ThrowingBodyPublisher bodyPublisher =
+                    new ThrowingBodyPublisher(where.select(thrower), publishers.get());
+            HttpRequest req = HttpRequest.
+                    newBuilder(URI.create(uri))
+                    .header("X-expect-exception", "true")
+                    .POST(bodyPublisher)
+                    .build();
+            BodyHandler<String> handler = BodyHandlers.ofString();
+            System.out.println("try throwing in " + where);
+            HttpResponse<String> response = null;
+            if (async) {
+                try {
+                    response = client.sendAsync(req, handler).join();
+                } catch (Error | Exception x) {
+                    Throwable cause = findCause(where, x, thrower);
+                    if (cause == null) throw causeNotFound(where, x);
+                    System.out.println(now() + "Got expected exception: " + cause);
+                }
+            } else {
+                try {
+                    response = client.send(req, handler);
+                } catch (Error | Exception t) {
+                    // synchronous send will rethrow exceptions
+                    Throwable throwable = t.getCause();
+                    assert throwable != null;
+
+                    if (thrower.test(where, throwable)) {
+                        System.out.println(now() + "Got expected exception: " + throwable);
+                    } else throw causeNotFound(where, t);
+                }
+            }
+            if (response != null) {
+                finisher.finish(where, response, thrower);
+            }
+        }
+    }
+
+    // can be used to reduce the surface of the test when diagnosing
+    // some failure
+    Set<Where> whereValues() {
+        //return EnumSet.of(Where.BEFORE_CANCEL, Where.AFTER_CANCEL);
+        return EnumSet.allOf(Where.class);
+    }
+
+    interface Thrower extends Consumer<Where>, BiPredicate<Where,Throwable> {
+
+    }
+
+    interface Finisher<T,U> {
+        U finish(Where w, HttpResponse<T> resp, Thrower thrower) throws IOException;
+    }
+
+    final <T,U> U shouldNotThrowInCancel(Where w, HttpResponse<T> resp, Thrower thrower) {
+        switch (w) {
+            case BEFORE_CANCEL: return null;
+            case AFTER_CANCEL: return null;
+            default: break;
+        }
+        return shouldHaveThrown(w, resp, thrower);
+    }
+
+
+    final <T,U> U shouldHaveThrown(Where w, HttpResponse<T> resp, Thrower thrower) {
+        String msg = "Expected exception not thrown in " + w
+                + "\n\tReceived: " + resp
+                + "\n\tWith body: " + resp.body();
+        System.out.println(msg);
+        throw new RuntimeException(msg);
+    }
+
+
+    private static Throwable findCause(Where w,
+                                       Throwable x,
+                                       BiPredicate<Where, Throwable> filter) {
+        while (x != null && !filter.test(w,x)) x = x.getCause();
+        return x;
+    }
+
+    static AssertionError causeNotFound(Where w, Throwable t) {
+        return new AssertionError("Expected exception not found in " + w, t);
+    }
+
+    static boolean isConnectionClosedLocally(Throwable t) {
+        if (t instanceof CompletionException) t = t.getCause();
+        if (t instanceof ExecutionException) t = t.getCause();
+        if (t instanceof IOException) {
+            String msg = t.getMessage();
+            return msg == null ? false
+                    : msg.contains("connection closed locally");
+        }
+        return false;
+    }
+
+    static final class UncheckedCustomExceptionThrower implements Thrower {
+        @Override
+        public void accept(Where where) {
+            out.println(now() + "Throwing in " + where);
+            throw new UncheckedCustomException(where.name());
+        }
+
+        @Override
+        public boolean test(Where w, Throwable throwable) {
+            switch (w) {
+                case AFTER_REQUEST:
+                case BEFORE_NEXT_REQUEST:
+                case AFTER_NEXT_REQUEST:
+                    if (isConnectionClosedLocally(throwable)) return true;
+                    break;
+                default:
+                    break;
+            }
+            return UncheckedCustomException.class.isInstance(throwable);
+        }
+
+        @Override
+        public String toString() {
+            return "UncheckedCustomExceptionThrower";
+        }
+    }
+
+    static final class UncheckedIOExceptionThrower implements Thrower {
+        @Override
+        public void accept(Where where) {
+            out.println(now() + "Throwing in " + where);
+            throw new UncheckedIOException(new CustomIOException(where.name()));
+        }
+
+        @Override
+        public boolean test(Where w, Throwable throwable) {
+            switch (w) {
+                case AFTER_REQUEST:
+                case BEFORE_NEXT_REQUEST:
+                case AFTER_NEXT_REQUEST:
+                    if (isConnectionClosedLocally(throwable)) return true;
+                    break;
+                default:
+                    break;
+            }
+            return UncheckedIOException.class.isInstance(throwable)
+                    && CustomIOException.class.isInstance(throwable.getCause());
+        }
+
+        @Override
+        public String toString() {
+            return "UncheckedIOExceptionThrower";
+        }
+    }
+
+    static final class UncheckedCustomException extends RuntimeException {
+        UncheckedCustomException(String message) {
+            super(message);
+        }
+        UncheckedCustomException(String message, Throwable cause) {
+            super(message, cause);
+        }
+    }
+
+    static final class CustomIOException extends IOException {
+        CustomIOException(String message) {
+            super(message);
+        }
+        CustomIOException(String message, Throwable cause) {
+            super(message, cause);
+        }
+    }
+
+
+    static final class ThrowingBodyPublisher implements BodyPublisher {
+        private final BodyPublisher publisher;
+        private final CompletableFuture<Void> subscribedCF = new CompletableFuture<>();
+        final Consumer<Where> throwing;
+        ThrowingBodyPublisher(Consumer<Where> throwing, BodyPublisher publisher) {
+            this.throwing = throwing;
+            this.publisher = publisher;
+        }
+
+        @Override
+        public long contentLength() {
+            return publisher.contentLength();
+        }
+
+        @Override
+        public void subscribe(Flow.Subscriber<? super ByteBuffer> subscriber) {
+            try {
+                throwing.accept(Where.BEFORE_SUBSCRIBE);
+                publisher.subscribe(new SubscriberWrapper(subscriber));
+                subscribedCF.complete(null);
+                throwing.accept(Where.AFTER_SUBSCRIBE);
+            } catch (Throwable t) {
+                subscribedCF.completeExceptionally(t);
+                throw t;
+            }
+        }
+
+        CompletableFuture<Void> subscribedCF() {
+            return subscribedCF;
+        }
+
+        class SubscriptionWrapper implements Flow.Subscription {
+            final Flow.Subscription subscription;
+            final AtomicLong requestCount = new AtomicLong();
+            SubscriptionWrapper(Flow.Subscription subscription) {
+                this.subscription = subscription;
+            }
+            @Override
+            public void request(long n) {
+                long count = requestCount.incrementAndGet();
+                System.out.printf("%s request-%d(%d)%n", now(), count, n);
+                if (count > 1) throwing.accept(Where.BEFORE_NEXT_REQUEST);
+                throwing.accept(Where.BEFORE_REQUEST);
+                subscription.request(n);
+                throwing.accept(Where.AFTER_REQUEST);
+                if (count > 1) throwing.accept(Where.AFTER_NEXT_REQUEST);
+            }
+
+            @Override
+            public void cancel() {
+                throwing.accept(Where.BEFORE_CANCEL);
+                subscription.cancel();
+                throwing.accept(Where.AFTER_CANCEL);
+            }
+        }
+
+        class SubscriberWrapper implements Flow.Subscriber<ByteBuffer> {
+            final Flow.Subscriber<? super ByteBuffer> subscriber;
+            SubscriberWrapper(Flow.Subscriber<? super ByteBuffer> subscriber) {
+                this.subscriber = subscriber;
+            }
+            @Override
+            public void onSubscribe(Flow.Subscription subscription) {
+                subscriber.onSubscribe(new SubscriptionWrapper(subscription));
+            }
+            @Override
+            public void onNext(ByteBuffer item) {
+                subscriber.onNext(item);
+            }
+            @Override
+            public void onComplete() {
+                subscriber.onComplete();
+            }
+
+            @Override
+            public void onError(Throwable throwable) {
+                subscriber.onError(throwable);
+            }
+        }
+    }
+
+
+    @BeforeTest
+    public void setup() throws Exception {
+        sslContext = new SimpleSSLContext().get();
+        if (sslContext == null)
+            throw new AssertionError("Unexpected null sslContext");
+
+        // HTTP/1.1
+        HttpTestHandler h1_fixedLengthHandler = new HTTP_FixedLengthHandler();
+        HttpTestHandler h1_chunkHandler = new HTTP_ChunkedHandler();
+        InetSocketAddress sa = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
+        httpTestServer = HttpTestServer.of(HttpServer.create(sa, 0));
+        httpTestServer.addHandler(h1_fixedLengthHandler, "/http1/fixed");
+        httpTestServer.addHandler(h1_chunkHandler, "/http1/chunk");
+        httpURI_fixed = "http://" + httpTestServer.serverAuthority() + "/http1/fixed/x";
+        httpURI_chunk = "http://" + httpTestServer.serverAuthority() + "/http1/chunk/x";
+
+        HttpsServer httpsServer = HttpsServer.create(sa, 0);
+        httpsServer.setHttpsConfigurator(new HttpsConfigurator(sslContext));
+        httpsTestServer = HttpTestServer.of(httpsServer);
+        httpsTestServer.addHandler(h1_fixedLengthHandler, "/https1/fixed");
+        httpsTestServer.addHandler(h1_chunkHandler, "/https1/chunk");
+        httpsURI_fixed = "https://" + httpsTestServer.serverAuthority() + "/https1/fixed/x";
+        httpsURI_chunk = "https://" + httpsTestServer.serverAuthority() + "/https1/chunk/x";
+
+        // HTTP/2
+        HttpTestHandler h2_fixedLengthHandler = new HTTP_FixedLengthHandler();
+        HttpTestHandler h2_chunkedHandler = new HTTP_ChunkedHandler();
+
+        http2TestServer = HttpTestServer.of(new Http2TestServer("localhost", false, 0));
+        http2TestServer.addHandler(h2_fixedLengthHandler, "/http2/fixed");
+        http2TestServer.addHandler(h2_chunkedHandler, "/http2/chunk");
+        http2URI_fixed = "http://" + http2TestServer.serverAuthority() + "/http2/fixed/x";
+        http2URI_chunk = "http://" + http2TestServer.serverAuthority() + "/http2/chunk/x";
+
+        https2TestServer = HttpTestServer.of(new Http2TestServer("localhost", true, sslContext));
+        https2TestServer.addHandler(h2_fixedLengthHandler, "/https2/fixed");
+        https2TestServer.addHandler(h2_chunkedHandler, "/https2/chunk");
+        https2URI_fixed = "https://" + https2TestServer.serverAuthority() + "/https2/fixed/x";
+        https2URI_chunk = "https://" + https2TestServer.serverAuthority() + "/https2/chunk/x";
+
+        serverCount.addAndGet(4);
+        httpTestServer.start();
+        httpsTestServer.start();
+        http2TestServer.start();
+        https2TestServer.start();
+    }
+
+    @AfterTest
+    public void teardown() throws Exception {
+        String sharedClientName =
+                sharedClient == null ? null : sharedClient.toString();
+        sharedClient = null;
+        Thread.sleep(100);
+        AssertionError fail = TRACKER.check(500);
+        try {
+            httpTestServer.stop();
+            httpsTestServer.stop();
+            http2TestServer.stop();
+            https2TestServer.stop();
+        } finally {
+            if (fail != null) {
+                if (sharedClientName != null) {
+                    System.err.println("Shared client name is: " + sharedClientName);
+                }
+                throw fail;
+            }
+        }
+    }
+
+    static class HTTP_FixedLengthHandler implements HttpTestHandler {
+        @Override
+        public void handle(HttpTestExchange t) throws IOException {
+            out.println("HTTP_FixedLengthHandler received request to " + t.getRequestURI());
+            byte[] resp;
+            try (InputStream is = t.getRequestBody()) {
+                resp = is.readAllBytes();
+            }
+            t.sendResponseHeaders(200, resp.length);  //fixed content length
+            try (OutputStream os = t.getResponseBody()) {
+                os.write(resp);
+            }
+        }
+    }
+
+    static class HTTP_ChunkedHandler implements HttpTestHandler {
+        @Override
+        public void handle(HttpTestExchange t) throws IOException {
+            out.println("HTTP_ChunkedHandler received request to " + t.getRequestURI());
+            byte[] resp;
+            try (InputStream is = t.getRequestBody()) {
+                resp = is.readAllBytes();
+            }
+            t.sendResponseHeaders(200, -1); // chunked/variable
+            try (OutputStream os = t.getResponseBody()) {
+                os.write(resp);
+            }
+        }
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/java/net/httpclient/AbstractThrowingPushPromises.java	Wed Jun 20 09:05:57 2018 -0700
@@ -0,0 +1,776 @@
+/*
+ * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @summary Tests what happens when push promise handlers and their
+ *          response body handlers and subscribers throw unexpected exceptions.
+ * @library /lib/testlibrary http2/server
+ * @build jdk.testlibrary.SimpleSSLContext HttpServerAdapters
+  *       ReferenceTracker AbstractThrowingPushPromises
+ * @modules java.base/sun.net.www.http
+ *          java.net.http/jdk.internal.net.http.common
+ *          java.net.http/jdk.internal.net.http.frame
+ *          java.net.http/jdk.internal.net.http.hpack
+ * @run testng/othervm -Djdk.internal.httpclient.debug=true AbstractThrowingPushPromises
+ */
+
+import jdk.testlibrary.SimpleSSLContext;
+import org.testng.annotations.AfterTest;
+import org.testng.annotations.AfterClass;
+import org.testng.annotations.BeforeTest;
+import org.testng.annotations.DataProvider;
+import org.testng.annotations.Test;
+
+import javax.net.ssl.SSLContext;
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.io.UncheckedIOException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.http.HttpClient;
+import java.net.http.HttpHeaders;
+import java.net.http.HttpRequest;
+import java.net.http.HttpResponse;
+import java.net.http.HttpResponse.BodyHandler;
+import java.net.http.HttpResponse.BodyHandlers;
+import java.net.http.HttpResponse.BodySubscriber;
+import java.net.http.HttpResponse.PushPromiseHandler;
+import java.nio.ByteBuffer;
+import java.nio.charset.StandardCharsets;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.CompletionException;
+import java.util.concurrent.CompletionStage;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.Executor;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Flow;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.function.BiPredicate;
+import java.util.function.Consumer;
+import java.util.function.Function;
+import java.util.function.Predicate;
+import java.util.function.Supplier;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import static java.lang.System.out;
+import static java.lang.System.err;
+import static java.lang.String.format;
+import static java.nio.charset.StandardCharsets.UTF_8;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertTrue;
+
+public abstract class AbstractThrowingPushPromises implements HttpServerAdapters {
+
+    SSLContext sslContext;
+    HttpTestServer http2TestServer;   // HTTP/2 ( h2c )
+    HttpTestServer https2TestServer;  // HTTP/2 ( h2  )
+    String http2URI_fixed;
+    String http2URI_chunk;
+    String https2URI_fixed;
+    String https2URI_chunk;
+
+    static final int ITERATION_COUNT = 1;
+    // a shared executor helps reduce the amount of threads created by the test
+    static final Executor executor = new TestExecutor(Executors.newCachedThreadPool());
+    static final ConcurrentMap<String, Throwable> FAILURES = new ConcurrentHashMap<>();
+    static volatile boolean tasksFailed;
+    static final AtomicLong serverCount = new AtomicLong();
+    static final AtomicLong clientCount = new AtomicLong();
+    static final long start = System.nanoTime();
+    public static String now() {
+        long now = System.nanoTime() - start;
+        long secs = now / 1000_000_000;
+        long mill = (now % 1000_000_000) / 1000_000;
+        long nan = now % 1000_000;
+        return String.format("[%d s, %d ms, %d ns] ", secs, mill, nan);
+    }
+
+    final ReferenceTracker TRACKER = ReferenceTracker.INSTANCE;
+    private volatile HttpClient sharedClient;
+
+    static class TestExecutor implements Executor {
+        final AtomicLong tasks = new AtomicLong();
+        Executor executor;
+        TestExecutor(Executor executor) {
+            this.executor = executor;
+        }
+
+        @Override
+        public void execute(Runnable command) {
+            long id = tasks.incrementAndGet();
+            executor.execute(() -> {
+                try {
+                    command.run();
+                } catch (Throwable t) {
+                    tasksFailed = true;
+                    out.printf(now() + "Task %s failed: %s%n", id, t);
+                    err.printf(now() + "Task %s failed: %s%n", id, t);
+                    FAILURES.putIfAbsent("Task " + id, t);
+                    throw t;
+                }
+            });
+        }
+    }
+
+    @AfterClass
+    static final void printFailedTests() {
+        out.println("\n=========================");
+        try {
+            out.printf("%n%sCreated %d servers and %d clients%n",
+                    now(), serverCount.get(), clientCount.get());
+            if (FAILURES.isEmpty()) return;
+            out.println("Failed tests: ");
+            FAILURES.entrySet().forEach((e) -> {
+                out.printf("\t%s: %s%n", e.getKey(), e.getValue());
+                e.getValue().printStackTrace(out);
+                e.getValue().printStackTrace();
+            });
+            if (tasksFailed) {
+                out.println("WARNING: Some tasks failed");
+            }
+        } finally {
+            out.println("\n=========================\n");
+        }
+    }
+
+    private String[] uris() {
+        return new String[] {
+                http2URI_fixed,
+                http2URI_chunk,
+                https2URI_fixed,
+                https2URI_chunk,
+        };
+    }
+
+    @DataProvider(name = "sanity")
+    public Object[][] sanity() {
+        String[] uris = uris();
+        Object[][] result = new Object[uris.length * 2][];
+
+        int i = 0;
+        for (boolean sameClient : List.of(false, true)) {
+            for (String uri: uris()) {
+                result[i++] = new Object[] {uri, sameClient};
+            }
+        }
+        assert i == uris.length * 2;
+        return result;
+    }
+
+    enum Where {
+        BODY_HANDLER, ON_SUBSCRIBE, ON_NEXT, ON_COMPLETE, ON_ERROR, GET_BODY, BODY_CF,
+        BEFORE_ACCEPTING, AFTER_ACCEPTING;
+        public Consumer<Where> select(Consumer<Where> consumer) {
+            return new Consumer<Where>() {
+                @Override
+                public void accept(Where where) {
+                    if (Where.this == where) {
+                        consumer.accept(where);
+                    }
+                }
+            };
+        }
+    }
+
+    private Object[][] variants(List<Thrower> throwers) {
+        String[] uris = uris();
+        Object[][] result = new Object[uris.length * 2 * throwers.size()][];
+        int i = 0;
+        for (Thrower thrower : throwers) {
+            for (boolean sameClient : List.of(false, true)) {
+                for (String uri : uris()) {
+                    result[i++] = new Object[]{uri, sameClient, thrower};
+                }
+            }
+        }
+        assert i == uris.length * 2 * throwers.size();
+        return result;
+    }
+
+    @DataProvider(name = "ioVariants")
+    public Object[][] ioVariants() {
+        return variants(List.of(
+                new UncheckedIOExceptionThrower()));
+    }
+
+    @DataProvider(name = "customVariants")
+    public Object[][] customVariants() {
+        return variants(List.of(
+                new UncheckedCustomExceptionThrower()));
+    }
+
+    private HttpClient makeNewClient() {
+        clientCount.incrementAndGet();
+        return TRACKER.track(HttpClient.newBuilder()
+                .proxy(HttpClient.Builder.NO_PROXY)
+                .executor(executor)
+                .sslContext(sslContext)
+                .build());
+    }
+
+    HttpClient newHttpClient(boolean share) {
+        if (!share) return makeNewClient();
+        HttpClient shared = sharedClient;
+        if (shared != null) return shared;
+        synchronized (this) {
+            shared = sharedClient;
+            if (shared == null) {
+                shared = sharedClient = makeNewClient();
+            }
+            return shared;
+        }
+    }
+
+    // @Test(dataProvider = "sanity")
+    protected void testSanityImpl(String uri, boolean sameClient)
+            throws Exception {
+        HttpClient client = null;
+        out.printf("%ntestNoThrows(%s, %b)%n", uri, sameClient);
+        for (int i=0; i< ITERATION_COUNT; i++) {
+            if (!sameClient || client == null)
+                client = newHttpClient(sameClient);
+
+            HttpRequest req = HttpRequest.newBuilder(URI.create(uri))
+                    .build();
+            BodyHandler<Stream<String>> handler =
+                    new ThrowingBodyHandler((w) -> {},
+                                            BodyHandlers.ofLines());
+            Map<HttpRequest, CompletableFuture<HttpResponse<Stream<String>>>> pushPromises =
+                    new ConcurrentHashMap<>();
+            PushPromiseHandler<Stream<String>> pushHandler = new PushPromiseHandler<>() {
+                @Override
+                public void applyPushPromise(HttpRequest initiatingRequest,
+                                             HttpRequest pushPromiseRequest,
+                                             Function<BodyHandler<Stream<String>>,
+                                                     CompletableFuture<HttpResponse<Stream<String>>>>
+                                                     acceptor) {
+                    pushPromises.putIfAbsent(pushPromiseRequest, acceptor.apply(handler));
+                }
+            };
+            HttpResponse<Stream<String>> response =
+                    client.sendAsync(req, BodyHandlers.ofLines(), pushHandler).get();
+            String body = response.body().collect(Collectors.joining("|"));
+            assertEquals(URI.create(body).getPath(), URI.create(uri).getPath());
+            for (HttpRequest promised : pushPromises.keySet()) {
+                out.printf("%s Received promise: %s%n\tresponse: %s%n",
+                        now(), promised, pushPromises.get(promised).get());
+                String promisedBody = pushPromises.get(promised).get().body()
+                        .collect(Collectors.joining("|"));
+                assertEquals(promisedBody, promised.uri().toASCIIString());
+            }
+            assertEquals(3, pushPromises.size());
+        }
+    }
+
+    // @Test(dataProvider = "variants")
+    protected void testThrowingAsStringImpl(String uri,
+                                     boolean sameClient,
+                                     Thrower thrower)
+            throws Exception
+    {
+        String test = format("testThrowingAsString(%s, %b, %s)",
+                             uri, sameClient, thrower);
+        testThrowing(test, uri, sameClient, BodyHandlers::ofString,
+                this::checkAsString, thrower);
+    }
+
+    //@Test(dataProvider = "variants")
+    protected void testThrowingAsLinesImpl(String uri,
+                                    boolean sameClient,
+                                    Thrower thrower)
+            throws Exception
+    {
+        String test =  format("testThrowingAsLines(%s, %b, %s)",
+                uri, sameClient, thrower);
+        testThrowing(test, uri, sameClient, BodyHandlers::ofLines,
+                this::checkAsLines, thrower);
+    }
+
+    //@Test(dataProvider = "variants")
+    protected void testThrowingAsInputStreamImpl(String uri,
+                                          boolean sameClient,
+                                          Thrower thrower)
+            throws Exception
+    {
+        String test = format("testThrowingAsInputStream(%s, %b, %s)",
+                uri, sameClient, thrower);
+        testThrowing(test, uri, sameClient, BodyHandlers::ofInputStream,
+                this::checkAsInputStream,  thrower);
+    }
+
+    private <T,U> void testThrowing(String name, String uri, boolean sameClient,
+                                    Supplier<BodyHandler<T>> handlers,
+                                    Finisher finisher, Thrower thrower)
+            throws Exception
+    {
+        out.printf("%n%s%s%n", now(), name);
+        try {
+            testThrowing(uri, sameClient, handlers, finisher, thrower);
+        } catch (Error | Exception x) {
+            FAILURES.putIfAbsent(name, x);
+            throw x;
+        }
+    }
+
+    private <T,U> void testThrowing(String uri, boolean sameClient,
+                                    Supplier<BodyHandler<T>> handlers,
+                                    Finisher finisher, Thrower thrower)
+            throws Exception
+    {
+        HttpClient client = null;
+        for (Where where : Where.values()) {
+            if (where == Where.ON_ERROR) continue;
+            if (!sameClient || client == null)
+                client = newHttpClient(sameClient);
+
+            HttpRequest req = HttpRequest.
+                    newBuilder(URI.create(uri))
+                    .build();
+            ConcurrentMap<HttpRequest, CompletableFuture<HttpResponse<T>>> promiseMap =
+                    new ConcurrentHashMap<>();
+            Supplier<BodyHandler<T>> throwing = () ->
+                    new ThrowingBodyHandler(where.select(thrower), handlers.get());
+            PushPromiseHandler<T> pushHandler = new ThrowingPromiseHandler<>(
+                    where.select(thrower),
+                    PushPromiseHandler.of((r) -> throwing.get(), promiseMap));
+            out.println("try throwing in " + where);
+            HttpResponse<T> response = null;
+            try {
+                response = client.sendAsync(req, handlers.get(), pushHandler).join();
+            } catch (Error | Exception x) {
+                throw x;
+            }
+            if (response != null) {
+                finisher.finish(where, req.uri(), response, thrower, promiseMap);
+            }
+        }
+    }
+
+    interface Thrower extends Consumer<Where>, Predicate<Throwable> {
+
+    }
+
+    interface Finisher<T,U> {
+        U finish(Where w, URI requestURI, HttpResponse<T> resp, Thrower thrower,
+                 Map<HttpRequest, CompletableFuture<HttpResponse<T>>> promises);
+    }
+
+    final <T,U> U shouldHaveThrown(Where w, HttpResponse<T> resp, Thrower thrower) {
+        String msg = "Expected exception not thrown in " + w
+                + "\n\tReceived: " + resp
+                + "\n\tWith body: " + resp.body();
+        System.out.println(msg);
+        throw new RuntimeException(msg);
+    }
+
+    final List<String> checkAsString(Where w, URI reqURI,
+                                    HttpResponse<String> resp,
+                                    Thrower thrower,
+                                    Map<HttpRequest, CompletableFuture<HttpResponse<String>>> promises) {
+        Function<HttpResponse<String>, List<String>> extractor =
+                (r) -> List.of(r.body());
+        return check(w, reqURI, resp, thrower, promises, extractor);
+    }
+
+    final List<String> checkAsLines(Where w, URI reqURI,
+                                    HttpResponse<Stream<String>> resp,
+                                    Thrower thrower,
+                                    Map<HttpRequest, CompletableFuture<HttpResponse<Stream<String>>>> promises) {
+        Function<HttpResponse<Stream<String>>, List<String>> extractor =
+                (r) -> r.body().collect(Collectors.toList());
+        return check(w, reqURI, resp, thrower, promises, extractor);
+    }
+
+    final List<String> checkAsInputStream(Where w, URI reqURI,
+                                          HttpResponse<InputStream> resp,
+                                          Thrower thrower,
+                                          Map<HttpRequest, CompletableFuture<HttpResponse<InputStream>>> promises)
+    {
+        Function<HttpResponse<InputStream>, List<String>> extractor = (r) -> {
+            List<String> result;
+            try (InputStream is = r.body()) {
+                result = new BufferedReader(new InputStreamReader(is))
+                        .lines().collect(Collectors.toList());
+            } catch (Throwable t) {
+                throw new CompletionException(t);
+            }
+            return result;
+        };
+        return check(w, reqURI, resp, thrower, promises, extractor);
+    }
+
+    private final <T> List<String> check(Where w, URI reqURI,
+                                 HttpResponse<T> resp,
+                                 Thrower thrower,
+                                 Map<HttpRequest, CompletableFuture<HttpResponse<T>>> promises,
+                                 Function<HttpResponse<T>, List<String>> extractor)
+    {
+        List<String> result = extractor.apply(resp);
+        for (HttpRequest req : promises.keySet()) {
+            switch (w) {
+                case BEFORE_ACCEPTING:
+                    throw new RuntimeException("No push promise should have been received" +
+                            " for " + reqURI + " in " + w + ": got " + promises.keySet());
+                default:
+                    break;
+            }
+            HttpResponse<T> presp;
+            try {
+                presp = promises.get(req).join();
+            } catch (Error | Exception x) {
+                Throwable cause = findCause(x, thrower);
+                if (cause != null) {
+                    out.println(now() + "Got expected exception in "
+                            + w + ": " + cause);
+                    continue;
+                }
+                throw x;
+            }
+            switch (w) {
+                case BEFORE_ACCEPTING:
+                case AFTER_ACCEPTING:
+                case BODY_HANDLER:
+                case GET_BODY:
+                case BODY_CF:
+                    return shouldHaveThrown(w, presp, thrower);
+                default:
+                    break;
+            }
+            List<String> presult = null;
+            try {
+                presult = extractor.apply(presp);
+            } catch (Error | Exception x) {
+                Throwable cause = findCause(x, thrower);
+                if (cause != null) {
+                    out.println(now() + "Got expected exception for "
+                            + req + " in " + w + ": " + cause);
+                    continue;
+                }
+                throw x;
+            }
+            throw new RuntimeException("Expected exception not thrown for "
+                    + req + " in " + w);
+        }
+        final int expectedCount;
+        switch (w) {
+            case BEFORE_ACCEPTING:
+                expectedCount = 0;
+                break;
+            default:
+                expectedCount = 3;
+        }
+        assertEquals(promises.size(), expectedCount,
+                "bad promise count for " + reqURI + " with " + w);
+        assertEquals(result, List.of(reqURI.toASCIIString()));
+        return result;
+    }
+
+    private static Throwable findCause(Throwable x,
+                                       Predicate<Throwable> filter) {
+        while (x != null && !filter.test(x)) x = x.getCause();
+        return x;
+    }
+
+    static final class UncheckedCustomExceptionThrower implements Thrower {
+        @Override
+        public void accept(Where where) {
+            out.println(now() + "Throwing in " + where);
+            throw new UncheckedCustomException(where.name());
+        }
+
+        @Override
+        public boolean test(Throwable throwable) {
+            return UncheckedCustomException.class.isInstance(throwable);
+        }
+
+        @Override
+        public String toString() {
+            return "UncheckedCustomExceptionThrower";
+        }
+    }
+
+    static final class UncheckedIOExceptionThrower implements Thrower {
+        @Override
+        public void accept(Where where) {
+            out.println(now() + "Throwing in " + where);
+            throw new UncheckedIOException(new CustomIOException(where.name()));
+        }
+
+        @Override
+        public boolean test(Throwable throwable) {
+            return UncheckedIOException.class.isInstance(throwable)
+                    && CustomIOException.class.isInstance(throwable.getCause());
+        }
+
+        @Override
+        public String toString() {
+            return "UncheckedIOExceptionThrower";
+        }
+    }
+
+    static final class UncheckedCustomException extends RuntimeException {
+        UncheckedCustomException(String message) {
+            super(message);
+        }
+        UncheckedCustomException(String message, Throwable cause) {
+            super(message, cause);
+        }
+    }
+
+    static final class CustomIOException extends IOException {
+        CustomIOException(String message) {
+            super(message);
+        }
+        CustomIOException(String message, Throwable cause) {
+            super(message, cause);
+        }
+    }
+
+    static final class ThrowingPromiseHandler<T> implements PushPromiseHandler<T> {
+        final Consumer<Where> throwing;
+        final PushPromiseHandler<T> pushHandler;
+        ThrowingPromiseHandler(Consumer<Where> throwing, PushPromiseHandler<T> pushHandler) {
+            this.throwing = throwing;
+            this.pushHandler = pushHandler;
+        }
+
+        @Override
+        public void applyPushPromise(HttpRequest initiatingRequest,
+                                     HttpRequest pushPromiseRequest,
+                                     Function<BodyHandler<T>,
+                                             CompletableFuture<HttpResponse<T>>> acceptor) {
+            throwing.accept(Where.BEFORE_ACCEPTING);
+            pushHandler.applyPushPromise(initiatingRequest, pushPromiseRequest, acceptor);
+            throwing.accept(Where.AFTER_ACCEPTING);
+        }
+    }
+
+    static final class ThrowingBodyHandler<T> implements BodyHandler<T> {
+        final Consumer<Where> throwing;
+        final BodyHandler<T> bodyHandler;
+        ThrowingBodyHandler(Consumer<Where> throwing, BodyHandler<T> bodyHandler) {
+            this.throwing = throwing;
+            this.bodyHandler = bodyHandler;
+        }
+        @Override
+        public BodySubscriber<T> apply(HttpResponse.ResponseInfo rinfo) {
+            throwing.accept(Where.BODY_HANDLER);
+            BodySubscriber<T> subscriber = bodyHandler.apply(rinfo);
+            return new ThrowingBodySubscriber(throwing, subscriber);
+        }
+    }
+
+    static final class ThrowingBodySubscriber<T> implements BodySubscriber<T> {
+        private final BodySubscriber<T> subscriber;
+        volatile boolean onSubscribeCalled;
+        final Consumer<Where> throwing;
+        ThrowingBodySubscriber(Consumer<Where> throwing, BodySubscriber<T> subscriber) {
+            this.throwing = throwing;
+            this.subscriber = subscriber;
+        }
+
+        @Override
+        public void onSubscribe(Flow.Subscription subscription) {
+            //out.println("onSubscribe ");
+            onSubscribeCalled = true;
+            throwing.accept(Where.ON_SUBSCRIBE);
+            subscriber.onSubscribe(subscription);
+        }
+
+        @Override
+        public void onNext(List<ByteBuffer> item) {
+           // out.println("onNext " + item);
+            assertTrue(onSubscribeCalled);
+            throwing.accept(Where.ON_NEXT);
+            subscriber.onNext(item);
+        }
+
+        @Override
+        public void onError(Throwable throwable) {
+            //out.println("onError");
+            assertTrue(onSubscribeCalled);
+            throwing.accept(Where.ON_ERROR);
+            subscriber.onError(throwable);
+        }
+
+        @Override
+        public void onComplete() {
+            //out.println("onComplete");
+            assertTrue(onSubscribeCalled, "onComplete called before onSubscribe");
+            throwing.accept(Where.ON_COMPLETE);
+            subscriber.onComplete();
+        }
+
+        @Override
+        public CompletionStage<T> getBody() {
+            throwing.accept(Where.GET_BODY);
+            try {
+                throwing.accept(Where.BODY_CF);
+            } catch (Throwable t) {
+                return CompletableFuture.failedFuture(t);
+            }
+            return subscriber.getBody();
+        }
+    }
+
+
+    @BeforeTest
+    public void setup() throws Exception {
+        sslContext = new SimpleSSLContext().get();
+        if (sslContext == null)
+            throw new AssertionError("Unexpected null sslContext");
+
+        // HTTP/2
+        HttpTestHandler h2_fixedLengthHandler = new HTTP_FixedLengthHandler();
+        HttpTestHandler h2_chunkedHandler = new HTTP_ChunkedHandler();
+
+        http2TestServer = HttpTestServer.of(new Http2TestServer("localhost", false, 0));
+        http2TestServer.addHandler(h2_fixedLengthHandler, "/http2/fixed");
+        http2TestServer.addHandler(h2_chunkedHandler, "/http2/chunk");
+        http2URI_fixed = "http://" + http2TestServer.serverAuthority() + "/http2/fixed/x";
+        http2URI_chunk = "http://" + http2TestServer.serverAuthority() + "/http2/chunk/x";
+
+        https2TestServer = HttpTestServer.of(new Http2TestServer("localhost", true, sslContext));
+        https2TestServer.addHandler(h2_fixedLengthHandler, "/https2/fixed");
+        https2TestServer.addHandler(h2_chunkedHandler, "/https2/chunk");
+        https2URI_fixed = "https://" + https2TestServer.serverAuthority() + "/https2/fixed/x";
+        https2URI_chunk = "https://" + https2TestServer.serverAuthority() + "/https2/chunk/x";
+
+        serverCount.addAndGet(2);
+        http2TestServer.start();
+        https2TestServer.start();
+    }
+
+    @AfterTest
+    public void teardown() throws Exception {
+        String sharedClientName =
+                sharedClient == null ? null : sharedClient.toString();
+        sharedClient = null;
+        Thread.sleep(100);
+        AssertionError fail = TRACKER.check(500);
+        try {
+            http2TestServer.stop();
+            https2TestServer.stop();
+        } finally {
+            if (fail != null) {
+                if (sharedClientName != null) {
+                    System.err.println("Shared client name is: " + sharedClientName);
+                }
+                throw fail;
+            }
+        }
+    }
+
+    static final BiPredicate<String,String> ACCEPT_ALL = (x, y) -> true;
+
+    private static void pushPromiseFor(HttpTestExchange t,
+                                       URI requestURI,
+                                       String pushPath,
+                                       boolean fixed)
+        throws IOException
+    {
+        try {
+            URI promise = new URI(requestURI.getScheme(),
+                    requestURI.getAuthority(),
+                    pushPath, null, null);
+            byte[] promiseBytes = promise.toASCIIString().getBytes(UTF_8);
+            out.printf("TestServer: %s Pushing promise: %s%n", now(), promise);
+            err.printf("TestServer: %s Pushing promise: %s%n", now(), promise);
+            HttpHeaders headers;
+            if (fixed) {
+                String length = String.valueOf(promiseBytes.length);
+                headers = HttpHeaders.of(Map.of("Content-Length", List.of(length)),
+                                         ACCEPT_ALL);
+            } else {
+                headers = HttpHeaders.of(Map.of(), ACCEPT_ALL); // empty
+            }
+            t.serverPush(promise, headers, promiseBytes);
+        } catch (URISyntaxException x) {
+            throw new IOException(x.getMessage(), x);
+        }
+    }
+
+    static class HTTP_FixedLengthHandler implements HttpTestHandler {
+        @Override
+        public void handle(HttpTestExchange t) throws IOException {
+            out.println("HTTP_FixedLengthHandler received request to " + t.getRequestURI());
+            try (InputStream is = t.getRequestBody()) {
+                is.readAllBytes();
+            }
+            URI requestURI = t.getRequestURI();
+            for (int i = 1; i<2; i++) {
+                String path = requestURI.getPath() + "/before/promise-" + i;
+                pushPromiseFor(t, requestURI, path, true);
+            }
+            byte[] resp = t.getRequestURI().toString().getBytes(StandardCharsets.UTF_8);
+            t.sendResponseHeaders(200, resp.length);  //fixed content length
+            try (OutputStream os = t.getResponseBody()) {
+                int bytes = resp.length/3;
+                for (int i = 0; i<2; i++) {
+                    String path = requestURI.getPath() + "/after/promise-" + (i + 2);
+                    os.write(resp, i * bytes, bytes);
+                    os.flush();
+                    pushPromiseFor(t, requestURI, path, true);
+                }
+                os.write(resp, 2*bytes, resp.length - 2*bytes);
+            }
+        }
+
+    }
+
+    static class HTTP_ChunkedHandler implements HttpTestHandler {
+        @Override
+        public void handle(HttpTestExchange t) throws IOException {
+            out.println("HTTP_ChunkedHandler received request to " + t.getRequestURI());
+            byte[] resp = t.getRequestURI().toString().getBytes(StandardCharsets.UTF_8);
+            try (InputStream is = t.getRequestBody()) {
+                is.readAllBytes();
+            }
+            URI requestURI = t.getRequestURI();
+            for (int i = 1; i<2; i++) {
+                String path = requestURI.getPath() + "/before/promise-" + i;
+                pushPromiseFor(t, requestURI, path, false);
+            }
+            t.sendResponseHeaders(200, -1); // chunked/variable
+            try (OutputStream os = t.getResponseBody()) {
+                int bytes = resp.length/3;
+                for (int i = 0; i<2; i++) {
+                    String path = requestURI.getPath() + "/after/promise-" + (i + 2);
+                    os.write(resp, i * bytes, bytes);
+                    os.flush();
+                    pushPromiseFor(t, requestURI, path, false);
+                }
+                os.write(resp, 2*bytes, resp.length - 2*bytes);
+            }
+        }
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/java/net/httpclient/AbstractThrowingSubscribers.java	Wed Jun 20 09:05:57 2018 -0700
@@ -0,0 +1,733 @@
+/*
+ * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+import com.sun.net.httpserver.HttpServer;
+import com.sun.net.httpserver.HttpsConfigurator;
+import com.sun.net.httpserver.HttpsServer;
+import jdk.testlibrary.SimpleSSLContext;
+import org.testng.annotations.AfterTest;
+import org.testng.annotations.AfterClass;
+import org.testng.annotations.BeforeTest;
+import org.testng.annotations.DataProvider;
+import org.testng.annotations.Test;
+
+import javax.net.ssl.SSLContext;
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.io.UncheckedIOException;
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
+import java.net.URI;
+import java.net.http.HttpClient;
+import java.net.http.HttpHeaders;
+import java.net.http.HttpRequest;
+import java.net.http.HttpResponse;
+import java.net.http.HttpResponse.BodyHandler;
+import java.net.http.HttpResponse.BodyHandlers;
+import java.net.http.HttpResponse.BodySubscriber;
+import java.nio.ByteBuffer;
+import java.nio.charset.StandardCharsets;
+import java.util.EnumSet;
+import java.util.List;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.CompletionStage;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.Executor;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Flow;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.function.Consumer;
+import java.util.function.Predicate;
+import java.util.function.Supplier;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import static java.lang.System.out;
+import static java.lang.String.format;
+import static java.nio.charset.StandardCharsets.UTF_8;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertTrue;
+
+public abstract class AbstractThrowingSubscribers implements HttpServerAdapters {
+
+    SSLContext sslContext;
+    HttpTestServer httpTestServer;    // HTTP/1.1    [ 4 servers ]
+    HttpTestServer httpsTestServer;   // HTTPS/1.1
+    HttpTestServer http2TestServer;   // HTTP/2 ( h2c )
+    HttpTestServer https2TestServer;  // HTTP/2 ( h2  )
+    String httpURI_fixed;
+    String httpURI_chunk;
+    String httpsURI_fixed;
+    String httpsURI_chunk;
+    String http2URI_fixed;
+    String http2URI_chunk;
+    String https2URI_fixed;
+    String https2URI_chunk;
+
+    static final int ITERATION_COUNT = 1;
+    // a shared executor helps reduce the amount of threads created by the test
+    static final Executor executor = new TestExecutor(Executors.newCachedThreadPool());
+    static final ConcurrentMap<String, Throwable> FAILURES = new ConcurrentHashMap<>();
+    static volatile boolean tasksFailed;
+    static final AtomicLong serverCount = new AtomicLong();
+    static final AtomicLong clientCount = new AtomicLong();
+    static final long start = System.nanoTime();
+    public static String now() {
+        long now = System.nanoTime() - start;
+        long secs = now / 1000_000_000;
+        long mill = (now % 1000_000_000) / 1000_000;
+        long nan = now % 1000_000;
+        return String.format("[%d s, %d ms, %d ns] ", secs, mill, nan);
+    }
+
+    final ReferenceTracker TRACKER = ReferenceTracker.INSTANCE;
+    private volatile HttpClient sharedClient;
+
+    static class TestExecutor implements Executor {
+        final AtomicLong tasks = new AtomicLong();
+        Executor executor;
+        TestExecutor(Executor executor) {
+            this.executor = executor;
+        }
+
+        @Override
+        public void execute(Runnable command) {
+            long id = tasks.incrementAndGet();
+            executor.execute(() -> {
+                try {
+                    command.run();
+                } catch (Throwable t) {
+                    tasksFailed = true;
+                    System.out.printf(now() + "Task %s failed: %s%n", id, t);
+                    System.err.printf(now() + "Task %s failed: %s%n", id, t);
+                    FAILURES.putIfAbsent("Task " + id, t);
+                    throw t;
+                }
+            });
+        }
+    }
+
+    @AfterClass
+    static final void printFailedTests() {
+        out.println("\n=========================");
+        try {
+            out.printf("%n%sCreated %d servers and %d clients%n",
+                    now(), serverCount.get(), clientCount.get());
+            if (FAILURES.isEmpty()) return;
+            out.println("Failed tests: ");
+            FAILURES.entrySet().forEach((e) -> {
+                out.printf("\t%s: %s%n", e.getKey(), e.getValue());
+                e.getValue().printStackTrace(out);
+                e.getValue().printStackTrace();
+            });
+            if (tasksFailed) {
+                System.out.println("WARNING: Some tasks failed");
+            }
+        } finally {
+            out.println("\n=========================\n");
+        }
+    }
+
+    private String[] uris() {
+        return new String[] {
+                httpURI_fixed,
+                httpURI_chunk,
+                httpsURI_fixed,
+                httpsURI_chunk,
+                http2URI_fixed,
+                http2URI_chunk,
+                https2URI_fixed,
+                https2URI_chunk,
+        };
+    }
+
+    static AtomicLong URICOUNT = new AtomicLong();
+
+    @DataProvider(name = "sanity")
+    public Object[][] sanity() {
+        String[] uris = uris();
+        Object[][] result = new Object[uris.length * 2][];
+        int i = 0;
+        for (boolean sameClient : List.of(false, true)) {
+            for (String uri: uris()) {
+                result[i++] = new Object[] {uri, sameClient};
+            }
+        }
+        assert i == uris.length * 2;
+        return result;
+    }
+
+    @DataProvider(name = "variants")
+    public Object[][] variants() {
+        String[] uris = uris();
+        Object[][] result = new Object[uris.length * 2 * 2][];
+        int i = 0;
+        for (Thrower thrower : List.of(
+                new UncheckedIOExceptionThrower(),
+                new UncheckedCustomExceptionThrower())) {
+            for (boolean sameClient : List.of(false, true)) {
+                for (String uri : uris()) {
+                    result[i++] = new Object[]{uri, sameClient, thrower};
+                }
+            }
+        }
+        assert i == uris.length * 2 * 2;
+        return result;
+    }
+
+    private HttpClient makeNewClient() {
+        clientCount.incrementAndGet();
+        HttpClient client =  HttpClient.newBuilder()
+                .proxy(HttpClient.Builder.NO_PROXY)
+                .executor(executor)
+                .sslContext(sslContext)
+                .build();
+        return TRACKER.track(client);
+    }
+
+    HttpClient newHttpClient(boolean share) {
+        if (!share) return makeNewClient();
+        HttpClient shared = sharedClient;
+        if (shared != null) return shared;
+        synchronized (this) {
+            shared = sharedClient;
+            if (shared == null) {
+                shared = sharedClient = makeNewClient();
+            }
+            return shared;
+        }
+    }
+
+    enum SubscriberType {
+        INLINE,  // In line subscribers complete their CF on ON_COMPLETE
+                 // e.g. BodySubscribers::ofString
+        OFFLINE; // Off line subscribers complete their CF immediately
+                 // but require the client to pull the data after the
+                 // CF completes (e.g. BodySubscribers::ofInputStream)
+    }
+
+    static EnumSet<Where> excludes(SubscriberType type) {
+        EnumSet<Where> set = EnumSet.noneOf(Where.class);
+
+        if (type == SubscriberType.OFFLINE) {
+            // Throwing on onSubscribe needs some more work
+            // for the case of InputStream, where the body has already
+            // completed by the time the subscriber is subscribed.
+            // The only way we have at that point to relay the exception
+            // is to call onError on the subscriber, but should we if
+            // Subscriber::onSubscribed has thrown an exception and
+            // not completed normally?
+            set.add(Where.ON_SUBSCRIBE);
+        }
+
+        // Don't know how to make the stack reliably cause onError
+        // to be called without closing the connection.
+        // And how do we get the exception if onError throws anyway?
+        set.add(Where.ON_ERROR);
+
+        return set;
+    }
+
+    //@Test(dataProvider = "sanity")
+    protected void testSanityImpl(String uri, boolean sameClient)
+            throws Exception {
+        HttpClient client = null;
+        String uri2 = uri + "-" + URICOUNT.incrementAndGet() + "/sanity";
+        out.printf("%ntestSanity(%s, %b)%n", uri2, sameClient);
+        for (int i=0; i< ITERATION_COUNT; i++) {
+            if (!sameClient || client == null)
+                client = newHttpClient(sameClient);
+
+            HttpRequest req = HttpRequest.newBuilder(URI.create(uri2))
+                    .build();
+            BodyHandler<String> handler =
+                    new ThrowingBodyHandler((w) -> {},
+                                            BodyHandlers.ofString());
+            HttpResponse<String> response = client.send(req, handler);
+            String body = response.body();
+            assertEquals(URI.create(body).getPath(), URI.create(uri2).getPath());
+        }
+    }
+
+    //@Test(dataProvider = "variants")
+    protected void testThrowingAsStringImpl(String uri,
+                                     boolean sameClient,
+                                     Thrower thrower)
+            throws Exception
+    {
+        uri = uri + "-" + URICOUNT.incrementAndGet();
+        String test = format("testThrowingAsString(%s, %b, %s)",
+                             uri, sameClient, thrower);
+        testThrowing(test, uri, sameClient, BodyHandlers::ofString,
+                this::shouldHaveThrown, thrower,false,
+                excludes(SubscriberType.INLINE));
+    }
+
+    //@Test(dataProvider = "variants")
+    protected void testThrowingAsLinesImpl(String uri,
+                                    boolean sameClient,
+                                    Thrower thrower)
+            throws Exception
+    {
+        uri = uri + "-" + URICOUNT.incrementAndGet();
+        String test =  format("testThrowingAsLines(%s, %b, %s)",
+                uri, sameClient, thrower);
+        testThrowing(test, uri, sameClient, BodyHandlers::ofLines,
+                this::checkAsLines, thrower,false,
+                excludes(SubscriberType.OFFLINE));
+    }
+
+    //@Test(dataProvider = "variants")
+    protected void testThrowingAsInputStreamImpl(String uri,
+                                          boolean sameClient,
+                                          Thrower thrower)
+            throws Exception
+    {
+        uri = uri + "-" + URICOUNT.incrementAndGet();
+        String test = format("testThrowingAsInputStream(%s, %b, %s)",
+                uri, sameClient, thrower);
+        testThrowing(test, uri, sameClient, BodyHandlers::ofInputStream,
+                this::checkAsInputStream,  thrower,false,
+                excludes(SubscriberType.OFFLINE));
+    }
+
+    //@Test(dataProvider = "variants")
+    protected void testThrowingAsStringAsyncImpl(String uri,
+                                          boolean sameClient,
+                                          Thrower thrower)
+            throws Exception
+    {
+        uri = uri + "-" + URICOUNT.incrementAndGet();
+        String test = format("testThrowingAsStringAsync(%s, %b, %s)",
+                uri, sameClient, thrower);
+        testThrowing(test, uri, sameClient, BodyHandlers::ofString,
+                     this::shouldHaveThrown, thrower, true,
+                excludes(SubscriberType.INLINE));
+    }
+
+    //@Test(dataProvider = "variants")
+    protected void testThrowingAsLinesAsyncImpl(String uri,
+                                         boolean sameClient,
+                                         Thrower thrower)
+            throws Exception
+    {
+        uri = uri + "-" + URICOUNT.incrementAndGet();
+        String test = format("testThrowingAsLinesAsync(%s, %b, %s)",
+                uri, sameClient, thrower);
+        testThrowing(test, uri, sameClient, BodyHandlers::ofLines,
+                this::checkAsLines, thrower,true,
+                excludes(SubscriberType.OFFLINE));
+    }
+
+    //@Test(dataProvider = "variants")
+    protected void testThrowingAsInputStreamAsyncImpl(String uri,
+                                               boolean sameClient,
+                                               Thrower thrower)
+            throws Exception
+    {
+        uri = uri + "-" + URICOUNT.incrementAndGet();
+        String test = format("testThrowingAsInputStreamAsync(%s, %b, %s)",
+                uri, sameClient, thrower);
+        testThrowing(test, uri, sameClient, BodyHandlers::ofInputStream,
+                this::checkAsInputStream, thrower,true,
+                excludes(SubscriberType.OFFLINE));
+    }
+
+    private <T,U> void testThrowing(String name, String uri, boolean sameClient,
+                                    Supplier<BodyHandler<T>> handlers,
+                                    Finisher finisher, Thrower thrower,
+                                    boolean async, EnumSet<Where> excludes)
+            throws Exception
+    {
+        out.printf("%n%s%s%n", now(), name);
+        try {
+            testThrowing(uri, sameClient, handlers, finisher, thrower, async, excludes);
+        } catch (Error | Exception x) {
+            FAILURES.putIfAbsent(name, x);
+            throw x;
+        }
+    }
+
+    private <T,U> void testThrowing(String uri, boolean sameClient,
+                                    Supplier<BodyHandler<T>> handlers,
+                                    Finisher finisher, Thrower thrower,
+                                    boolean async,
+                                    EnumSet<Where> excludes)
+            throws Exception
+    {
+        HttpClient client = null;
+        for (Where where : EnumSet.complementOf(excludes)) {
+
+            if (!sameClient || client == null)
+                client = newHttpClient(sameClient);
+            String uri2 = uri + "-" + where;
+            HttpRequest req = HttpRequest.
+                    newBuilder(URI.create(uri2))
+                    .build();
+            BodyHandler<T> handler =
+                    new ThrowingBodyHandler(where.select(thrower), handlers.get());
+            System.out.println("try throwing in " + where);
+            HttpResponse<T> response = null;
+            if (async) {
+                try {
+                    response = client.sendAsync(req, handler).join();
+                } catch (Error | Exception x) {
+                    Throwable cause = findCause(x, thrower);
+                    if (cause == null) throw causeNotFound(where, x);
+                    System.out.println(now() + "Got expected exception: " + cause);
+                }
+            } else {
+                try {
+                    response = client.send(req, handler);
+                } catch (Error | Exception t) {
+                    // synchronous send will rethrow exceptions
+                    Throwable throwable = t.getCause();
+                    assert throwable != null;
+
+                    if (thrower.test(throwable)) {
+                        System.out.println(now() + "Got expected exception: " + throwable);
+                    } else throw causeNotFound(where, t);
+                }
+            }
+            if (response != null) {
+                finisher.finish(where, response, thrower);
+            }
+        }
+    }
+
+    enum Where {
+        BODY_HANDLER, ON_SUBSCRIBE, ON_NEXT, ON_COMPLETE, ON_ERROR, GET_BODY, BODY_CF;
+        public Consumer<Where> select(Consumer<Where> consumer) {
+            return new Consumer<Where>() {
+                @Override
+                public void accept(Where where) {
+                    if (Where.this == where) {
+                        consumer.accept(where);
+                    }
+                }
+            };
+        }
+    }
+
+    static AssertionError causeNotFound(Where w, Throwable t) {
+        return new AssertionError("Expected exception not found in " + w, t);
+    }
+
+    interface Thrower extends Consumer<Where>, Predicate<Throwable> {
+
+    }
+
+    interface Finisher<T,U> {
+        U finish(Where w, HttpResponse<T> resp, Thrower thrower) throws IOException;
+    }
+
+    final <T,U> U shouldHaveThrown(Where w, HttpResponse<T> resp, Thrower thrower) {
+        String msg = "Expected exception not thrown in " + w
+                + "\n\tReceived: " + resp
+                + "\n\tWith body: " + resp.body();
+        System.out.println(msg);
+        throw new RuntimeException(msg);
+    }
+
+    final List<String> checkAsLines(Where w, HttpResponse<Stream<String>> resp, Thrower thrower) {
+        switch(w) {
+            case BODY_HANDLER: return shouldHaveThrown(w, resp, thrower);
+            case GET_BODY: return shouldHaveThrown(w, resp, thrower);
+            case BODY_CF: return shouldHaveThrown(w, resp, thrower);
+            default: break;
+        }
+        List<String> result = null;
+        try {
+            result = resp.body().collect(Collectors.toList());
+        } catch (Error | Exception x) {
+            Throwable cause = findCause(x, thrower);
+            if (cause != null) {
+                out.println(now() + "Got expected exception in " + w + ": " + cause);
+                return result;
+            }
+            throw causeNotFound(w, x);
+        }
+        return shouldHaveThrown(w, resp, thrower);
+    }
+
+    final List<String> checkAsInputStream(Where w, HttpResponse<InputStream> resp,
+                                    Thrower thrower)
+            throws IOException
+    {
+        switch(w) {
+            case BODY_HANDLER: return shouldHaveThrown(w, resp, thrower);
+            case GET_BODY: return shouldHaveThrown(w, resp, thrower);
+            case BODY_CF: return shouldHaveThrown(w, resp, thrower);
+            default: break;
+        }
+        List<String> result = null;
+        try (InputStreamReader r1 = new InputStreamReader(resp.body(), UTF_8);
+             BufferedReader r = new BufferedReader(r1)) {
+            try {
+                result = r.lines().collect(Collectors.toList());
+            } catch (Error | Exception x) {
+                Throwable cause = findCause(x, thrower);
+                if (cause != null) {
+                    out.println(now() + "Got expected exception in " + w + ": " + cause);
+                    return result;
+                }
+                throw causeNotFound(w, x);
+            }
+        }
+        return shouldHaveThrown(w, resp, thrower);
+    }
+
+    private static Throwable findCause(Throwable x,
+                                       Predicate<Throwable> filter) {
+        while (x != null && !filter.test(x)) x = x.getCause();
+        return x;
+    }
+
+    static final class UncheckedCustomExceptionThrower implements Thrower {
+        @Override
+        public void accept(Where where) {
+            out.println(now() + "Throwing in " + where);
+            throw new UncheckedCustomException(where.name());
+        }
+
+        @Override
+        public boolean test(Throwable throwable) {
+            return UncheckedCustomException.class.isInstance(throwable);
+        }
+
+        @Override
+        public String toString() {
+            return "UncheckedCustomExceptionThrower";
+        }
+    }
+
+    static final class UncheckedIOExceptionThrower implements Thrower {
+        @Override
+        public void accept(Where where) {
+            out.println(now() + "Throwing in " + where);
+            throw new UncheckedIOException(new CustomIOException(where.name()));
+        }
+
+        @Override
+        public boolean test(Throwable throwable) {
+            return UncheckedIOException.class.isInstance(throwable)
+                    && CustomIOException.class.isInstance(throwable.getCause());
+        }
+
+        @Override
+        public String toString() {
+            return "UncheckedIOExceptionThrower";
+        }
+    }
+
+    static final class UncheckedCustomException extends RuntimeException {
+        UncheckedCustomException(String message) {
+            super(message);
+        }
+        UncheckedCustomException(String message, Throwable cause) {
+            super(message, cause);
+        }
+    }
+
+    static final class CustomIOException extends IOException {
+        CustomIOException(String message) {
+            super(message);
+        }
+        CustomIOException(String message, Throwable cause) {
+            super(message, cause);
+        }
+    }
+
+    static final class ThrowingBodyHandler<T> implements BodyHandler<T> {
+        final Consumer<Where> throwing;
+        final BodyHandler<T> bodyHandler;
+        ThrowingBodyHandler(Consumer<Where> throwing, BodyHandler<T> bodyHandler) {
+            this.throwing = throwing;
+            this.bodyHandler = bodyHandler;
+        }
+        @Override
+        public BodySubscriber<T> apply(HttpResponse.ResponseInfo rinfo) {
+            throwing.accept(Where.BODY_HANDLER);
+            BodySubscriber<T> subscriber = bodyHandler.apply(rinfo);
+            return new ThrowingBodySubscriber(throwing, subscriber);
+        }
+    }
+
+    static final class ThrowingBodySubscriber<T> implements BodySubscriber<T> {
+        private final BodySubscriber<T> subscriber;
+        volatile boolean onSubscribeCalled;
+        final Consumer<Where> throwing;
+        ThrowingBodySubscriber(Consumer<Where> throwing, BodySubscriber<T> subscriber) {
+            this.throwing = throwing;
+            this.subscriber = subscriber;
+        }
+
+        @Override
+        public void onSubscribe(Flow.Subscription subscription) {
+            //out.println("onSubscribe ");
+            onSubscribeCalled = true;
+            throwing.accept(Where.ON_SUBSCRIBE);
+            subscriber.onSubscribe(subscription);
+        }
+
+        @Override
+        public void onNext(List<ByteBuffer> item) {
+           // out.println("onNext " + item);
+            assertTrue(onSubscribeCalled);
+            throwing.accept(Where.ON_NEXT);
+            subscriber.onNext(item);
+        }
+
+        @Override
+        public void onError(Throwable throwable) {
+            //out.println("onError");
+            assertTrue(onSubscribeCalled);
+            throwing.accept(Where.ON_ERROR);
+            subscriber.onError(throwable);
+        }
+
+        @Override
+        public void onComplete() {
+            //out.println("onComplete");
+            assertTrue(onSubscribeCalled, "onComplete called before onSubscribe");
+            throwing.accept(Where.ON_COMPLETE);
+            subscriber.onComplete();
+        }
+
+        @Override
+        public CompletionStage<T> getBody() {
+            throwing.accept(Where.GET_BODY);
+            try {
+                throwing.accept(Where.BODY_CF);
+            } catch (Throwable t) {
+                return CompletableFuture.failedFuture(t);
+            }
+            return subscriber.getBody();
+        }
+    }
+
+
+    @BeforeTest
+    public void setup() throws Exception {
+        sslContext = new SimpleSSLContext().get();
+        if (sslContext == null)
+            throw new AssertionError("Unexpected null sslContext");
+
+        // HTTP/1.1
+        HttpTestHandler h1_fixedLengthHandler = new HTTP_FixedLengthHandler();
+        HttpTestHandler h1_chunkHandler = new HTTP_ChunkedHandler();
+        InetSocketAddress sa = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
+        httpTestServer = HttpTestServer.of(HttpServer.create(sa, 0));
+        httpTestServer.addHandler(h1_fixedLengthHandler, "/http1/fixed");
+        httpTestServer.addHandler(h1_chunkHandler, "/http1/chunk");
+        httpURI_fixed = "http://" + httpTestServer.serverAuthority() + "/http1/fixed/x";
+        httpURI_chunk = "http://" + httpTestServer.serverAuthority() + "/http1/chunk/x";
+
+        HttpsServer httpsServer = HttpsServer.create(sa, 0);
+        httpsServer.setHttpsConfigurator(new HttpsConfigurator(sslContext));
+        httpsTestServer = HttpTestServer.of(httpsServer);
+        httpsTestServer.addHandler(h1_fixedLengthHandler, "/https1/fixed");
+        httpsTestServer.addHandler(h1_chunkHandler, "/https1/chunk");
+        httpsURI_fixed = "https://" + httpsTestServer.serverAuthority() + "/https1/fixed/x";
+        httpsURI_chunk = "https://" + httpsTestServer.serverAuthority() + "/https1/chunk/x";
+
+        // HTTP/2
+        HttpTestHandler h2_fixedLengthHandler = new HTTP_FixedLengthHandler();
+        HttpTestHandler h2_chunkedHandler = new HTTP_ChunkedHandler();
+
+        http2TestServer = HttpTestServer.of(new Http2TestServer("localhost", false, 0));
+        http2TestServer.addHandler(h2_fixedLengthHandler, "/http2/fixed");
+        http2TestServer.addHandler(h2_chunkedHandler, "/http2/chunk");
+        http2URI_fixed = "http://" + http2TestServer.serverAuthority() + "/http2/fixed/x";
+        http2URI_chunk = "http://" + http2TestServer.serverAuthority() + "/http2/chunk/x";
+
+        https2TestServer = HttpTestServer.of(new Http2TestServer("localhost", true, sslContext));
+        https2TestServer.addHandler(h2_fixedLengthHandler, "/https2/fixed");
+        https2TestServer.addHandler(h2_chunkedHandler, "/https2/chunk");
+        https2URI_fixed = "https://" + https2TestServer.serverAuthority() + "/https2/fixed/x";
+        https2URI_chunk = "https://" + https2TestServer.serverAuthority() + "/https2/chunk/x";
+
+        serverCount.addAndGet(4);
+        httpTestServer.start();
+        httpsTestServer.start();
+        http2TestServer.start();
+        https2TestServer.start();
+    }
+
+    @AfterTest
+    public void teardown() throws Exception {
+        String sharedClientName =
+                sharedClient == null ? null : sharedClient.toString();
+        sharedClient = null;
+        Thread.sleep(100);
+        AssertionError fail = TRACKER.check(500);
+        try {
+            httpTestServer.stop();
+            httpsTestServer.stop();
+            http2TestServer.stop();
+            https2TestServer.stop();
+        } finally {
+            if (fail != null) {
+                if (sharedClientName != null) {
+                    System.err.println("Shared client name is: " + sharedClientName);
+                }
+                throw fail;
+            }
+        }
+    }
+
+    static class HTTP_FixedLengthHandler implements HttpTestHandler {
+        @Override
+        public void handle(HttpTestExchange t) throws IOException {
+            out.println("HTTP_FixedLengthHandler received request to " + t.getRequestURI());
+            try (InputStream is = t.getRequestBody()) {
+                is.readAllBytes();
+            }
+            byte[] resp = t.getRequestURI().toString().getBytes(StandardCharsets.UTF_8);
+            t.sendResponseHeaders(200, resp.length);  //fixed content length
+            try (OutputStream os = t.getResponseBody()) {
+                os.write(resp);
+            }
+        }
+    }
+
+    static class HTTP_ChunkedHandler implements HttpTestHandler {
+        @Override
+        public void handle(HttpTestExchange t) throws IOException {
+            out.println("HTTP_ChunkedHandler received request to " + t.getRequestURI());
+            byte[] resp = t.getRequestURI().toString().getBytes(StandardCharsets.UTF_8);
+            try (InputStream is = t.getRequestBody()) {
+                is.readAllBytes();
+            }
+            t.sendResponseHeaders(200, -1); // chunked/variable
+            try (OutputStream os = t.getResponseBody()) {
+                os.write(resp);
+            }
+        }
+    }
+
+}
--- a/test/jdk/java/net/httpclient/AsFileDownloadTest.java	Wed Jun 20 17:15:16 2018 +0200
+++ b/test/jdk/java/net/httpclient/AsFileDownloadTest.java	Wed Jun 20 09:05:57 2018 -0700
@@ -53,6 +53,7 @@
 import java.net.InetSocketAddress;
 import java.net.URI;
 import java.net.http.HttpClient;
+import java.net.http.HttpHeaders;
 import java.net.http.HttpRequest;
 import java.net.http.HttpRequest.BodyPublishers;
 import java.net.http.HttpResponse;
@@ -63,9 +64,8 @@
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
-import java.util.logging.ConsoleHandler;
-import java.util.logging.Level;
-import java.util.logging.Logger;
+import java.util.Locale;
+import java.util.Map;
 import javax.net.ssl.SSLContext;
 import jdk.testlibrary.SimpleSSLContext;
 import jdk.test.lib.util.FileUtils;
@@ -188,6 +188,10 @@
         assertEquals(response.headers().firstValue("Content-Disposition").get(),
                      contentDispositionValue);
         assertEquals(fileContents, "May the luck of the Irish be with you!");
+
+        // additional checks unrelated to file download
+        caseInsensitivityOfHeaders(request.headers());
+        caseInsensitivityOfHeaders(response.headers());
     }
 
     // --- Negative
@@ -299,7 +303,7 @@
         http2TestServer.addHandler(new Http2FileDispoHandler(), "/http2/afdt");
         http2URI = "http://" + http2TestServer.serverAuthority() + "/http2/afdt";
 
-        https2TestServer = new Http2TestServer("localhost", true, 0);
+        https2TestServer = new Http2TestServer("localhost", true, sslContext);
         https2TestServer.addHandler(new Http2FileDispoHandler(), "/https2/afdt");
         https2URI = "https://" + https2TestServer.serverAuthority() + "/https2/afdt";
 
@@ -372,4 +376,30 @@
             }
         }
     }
+
+    // ---
+
+    // Asserts case-insensitivity of headers (nothing to do with file
+    // download, just convenient as we have a couple of header instances. )
+    static void caseInsensitivityOfHeaders(HttpHeaders headers) {
+        try {
+            for (Map.Entry<String, List<String>> entry : headers.map().entrySet()) {
+                String headerName = entry.getKey();
+                List<String> headerValue = entry.getValue();
+
+                for (String name : List.of(headerName.toUpperCase(Locale.ROOT),
+                                           headerName.toLowerCase(Locale.ROOT))) {
+                    assertTrue(headers.firstValue(name).isPresent());
+                    assertEquals(headers.firstValue(name).get(), headerValue.get(0));
+                    assertEquals(headers.allValues(name).size(), headerValue.size());
+                    assertEquals(headers.allValues(name), headerValue);
+                    assertEquals(headers.map().get(name).size(), headerValue.size());
+                    assertEquals(headers.map().get(name), headerValue);
+                }
+            }
+        } catch (Throwable t) {
+            System.out.println("failure in caseInsensitivityOfHeaders with:" + headers);
+            throw t;
+        }
+    }
 }
--- a/test/jdk/java/net/httpclient/BasicRedirectTest.java	Wed Jun 20 17:15:16 2018 +0200
+++ b/test/jdk/java/net/httpclient/BasicRedirectTest.java	Wed Jun 20 09:05:57 2018 -0700
@@ -228,7 +228,7 @@
         http2TestServer = HttpTestServer.of(new Http2TestServer("localhost", false, 0));
         http2TestServer.addHandler(new BasicHttpRedirectHandler(), "/http2/same/");
         http2URI = "http://" + http2TestServer.serverAuthority() + "/http2/same/redirect";
-        https2TestServer = HttpTestServer.of(new Http2TestServer("localhost", true, 0));
+        https2TestServer = HttpTestServer.of(new Http2TestServer("localhost", true, sslContext));
         https2TestServer.addHandler(new BasicHttpRedirectHandler(), "/https2/same/");
         https2URI = "https://" + https2TestServer.serverAuthority() + "/https2/same/redirect";
 
--- a/test/jdk/java/net/httpclient/BodyProcessorInputStreamTest.java	Wed Jun 20 17:15:16 2018 +0200
+++ b/test/jdk/java/net/httpclient/BodyProcessorInputStreamTest.java	Wed Jun 20 09:05:57 2018 -0700
@@ -41,7 +41,7 @@
  * @test
  * @bug 8187503
  * @summary An example on how to read a response body with InputStream.
- * @run main/manual -Dtest.debug=true BodyProcessorInputStreamTest
+ * @run main/othervm/manual -Dtest.debug=true BodyProcessorInputStreamTest
  * @author daniel fuchs
  */
 public class BodyProcessorInputStreamTest {
--- a/test/jdk/java/net/httpclient/ConcurrentResponses.java	Wed Jun 20 17:15:16 2018 +0200
+++ b/test/jdk/java/net/httpclient/ConcurrentResponses.java	Wed Jun 20 09:05:57 2018 -0700
@@ -35,7 +35,9 @@
  * @library /lib/testlibrary http2/server
  * @build Http2TestServer
  * @build jdk.testlibrary.SimpleSSLContext
- * @run testng/othervm -Djdk.internal.httpclient.debug=true ConcurrentResponses
+ * @run testng/othervm
+ *      -Djdk.httpclient.HttpClient.log=headers,errors,channel
+ *      ConcurrentResponses
  */
 
 import java.io.IOException;
@@ -280,7 +282,7 @@
         http2TestServer.addHandler(new Http2VariableHandler(), "/http2/variable");
         http2VariableURI = "http://" + http2TestServer.serverAuthority() + "/http2/variable";
 
-        https2TestServer = new Http2TestServer("localhost", true, 0);
+        https2TestServer = new Http2TestServer("localhost", true, sslContext);
         https2TestServer.addHandler(new Http2FixedHandler(), "/https2/fixed");
         https2FixedURI = "https://" + https2TestServer.serverAuthority() + "/https2/fixed";
         https2TestServer.addHandler(new Http2VariableHandler(), "/https2/variable");
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/java/net/httpclient/ConnectExceptionTest.java	Wed Jun 20 09:05:57 2018 -0700
@@ -0,0 +1,167 @@
+/*
+ * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @summary Expect ConnectException for all non-security related connect errors
+ * @bug 8204864
+ * @run testng/othervm ConnectExceptionTest
+ * @run testng/othervm/java.security.policy=noPermissions.policy ConnectExceptionTest
+ */
+
+import java.io.IOException;
+import java.net.ConnectException;
+import java.net.InetSocketAddress;
+import java.net.Proxy;
+import java.net.ProxySelector;
+import java.net.SocketAddress;
+import java.net.URI;
+import java.net.http.HttpClient;
+import java.net.http.HttpRequest;
+import java.net.http.HttpRequest.BodyPublishers;
+import java.net.http.HttpResponse;
+import java.net.http.HttpResponse.BodyHandlers;
+import java.util.List;
+import java.util.concurrent.ExecutionException;
+import org.testng.annotations.DataProvider;
+import org.testng.annotations.Test;
+import static java.lang.System.out;
+import static org.testng.Assert.assertTrue;
+import static org.testng.Assert.fail;
+
+public class ConnectExceptionTest {
+
+    static final ProxySelector INVALID_PROXY = new ProxySelector() {
+        final List<Proxy> proxy = List.of(new Proxy(Proxy.Type.HTTP,
+                InetSocketAddress.createUnresolved("proxy.invalid", 8080)));
+        @Override public List<Proxy> select(URI uri) { return proxy; }
+        @Override public void connectFailed(URI uri, SocketAddress sa, IOException ioe) { }
+        @Override public String toString() { return "INVALID_PROXY"; }
+    };
+
+    static final ProxySelector NO_PROXY = new ProxySelector() {
+        @Override public List<Proxy> select(URI uri) { return List.of(Proxy.NO_PROXY); }
+        @Override public void connectFailed(URI uri, SocketAddress sa, IOException ioe) { }
+        @Override public String toString() { return "NO_PROXY"; }
+    };
+
+    @DataProvider(name = "uris")
+    public Object[][] uris() {
+        return new Object[][]{
+            { "http://test.invalid/",  NO_PROXY       },
+            { "https://test.invalid/", NO_PROXY       },
+            { "http://test.invalid/",  INVALID_PROXY  },
+            { "https://test.invalid/", INVALID_PROXY  },
+        };
+    }
+
+    @Test(dataProvider = "uris")
+    void testSynchronousGET(String uriString, ProxySelector proxy) throws Exception {
+        out.printf("%n---%ntestSynchronousGET starting uri:%s, proxy:%s%n", uriString, proxy);
+        HttpClient client = HttpClient.newBuilder().proxy(proxy).build();
+
+        URI uri = URI.create(uriString);
+        HttpRequest request = HttpRequest.newBuilder(uri).build();
+        try {
+            HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
+            fail("UNEXPECTED response: " + response + ", body:" + response.body());
+        } catch (ConnectException ioe) {
+            out.println("Caught expected: " + ioe);
+            //ioe.printStackTrace(out);
+        } catch (SecurityException expectedIfSMIsSet) {
+            out.println("Caught expected: " + expectedIfSMIsSet);
+            assertTrue(System.getSecurityManager() != null);
+        }
+    }
+
+    @Test(dataProvider = "uris")
+    void testSynchronousPOST(String uriString, ProxySelector proxy) throws Exception {
+        out.printf("%n---%ntestSynchronousPOST starting uri:%s, proxy:%s%n", uriString, proxy);
+        HttpClient client = HttpClient.newBuilder().proxy(proxy).build();
+
+        URI uri = URI.create(uriString);
+        HttpRequest request = HttpRequest.newBuilder(uri)
+                .POST(BodyPublishers.ofString("Does not matter"))
+                .build();
+        try {
+            HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
+            fail("UNEXPECTED response: " + response + ", body:" + response.body());
+        } catch (ConnectException ioe) {
+            out.println("Caught expected: " + ioe);
+            //ioe.printStackTrace(out);
+        } catch (SecurityException expectedIfSMIsSet) {
+            out.println("Caught expected: " + expectedIfSMIsSet);
+            assertTrue(System.getSecurityManager() != null);
+        }
+    }
+
+    @Test(dataProvider = "uris")
+    void testAsynchronousGET(String uriString, ProxySelector proxy) throws Exception {
+        out.printf("%n---%ntestAsynchronousGET starting uri:%s, proxy:%s%n", uriString, proxy);
+        HttpClient client = HttpClient.newBuilder().proxy(proxy).build();
+
+        URI uri = URI.create(uriString);
+        HttpRequest request = HttpRequest.newBuilder(uri).build();
+        try {
+            HttpResponse<String> response = client.sendAsync(request, BodyHandlers.ofString()).get();
+            fail("UNEXPECTED response: " + response + ", body:" + response.body());
+        } catch (ExecutionException ee) {
+            Throwable t = ee.getCause();
+            if (t instanceof ConnectException) {
+                out.println("Caught expected: " + t);
+            } else if (t instanceof SecurityException) {
+                out.println("Caught expected: " + t);
+                assertTrue(System.getSecurityManager() != null);
+            } else {
+                t.printStackTrace(out);
+                fail("Unexpected exception: " + t);
+            }
+        }
+    }
+
+    @Test(dataProvider = "uris")
+    void testAsynchronousPOST(String uriString, ProxySelector proxy) throws Exception {
+        out.printf("%n---%ntestAsynchronousPOST starting uri:%s, proxy:%s%n", uriString, proxy);
+        HttpClient client = HttpClient.newBuilder().proxy(proxy).build();
+
+        URI uri = URI.create(uriString);
+        HttpRequest request = HttpRequest.newBuilder(uri)
+                .POST(BodyPublishers.ofString("Does not matter"))
+                .build();
+        try {
+            HttpResponse<String> response = client.sendAsync(request, BodyHandlers.ofString()).get();
+            fail("UNEXPECTED response: " + response + ", body:" + response.body());
+        } catch (ExecutionException ee) {
+            Throwable t = ee.getCause();
+            if (t instanceof ConnectException) {
+                out.println("Caught expected: " + t);
+            } else if (t instanceof SecurityException) {
+                out.println("Caught expected: " + t);
+                assertTrue(System.getSecurityManager() != null);
+            } else {
+                t.printStackTrace(out);
+                fail("Unexpected exception: " + t);
+            }
+        }
+    }
+}
--- a/test/jdk/java/net/httpclient/CookieHeaderTest.java	Wed Jun 20 17:15:16 2018 +0200
+++ b/test/jdk/java/net/httpclient/CookieHeaderTest.java	Wed Jun 20 09:05:57 2018 -0700
@@ -205,7 +205,7 @@
         http2TestServer = HttpTestServer.of(new Http2TestServer("localhost", false, 0));
         http2TestServer.addHandler(new CookieValidationHandler(), "/http2/cookie/");
         http2URI = "http://" + http2TestServer.serverAuthority() + "/http2/cookie/retry";
-        https2TestServer = HttpTestServer.of(new Http2TestServer("localhost", true, 0));
+        https2TestServer = HttpTestServer.of(new Http2TestServer("localhost", true, sslContext));
         https2TestServer.addHandler(new CookieValidationHandler(), "/https2/cookie/");
         https2URI = "https://" + https2TestServer.serverAuthority() + "/https2/cookie/retry";
 
--- a/test/jdk/java/net/httpclient/CustomRequestPublisher.java	Wed Jun 20 17:15:16 2018 +0200
+++ b/test/jdk/java/net/httpclient/CustomRequestPublisher.java	Wed Jun 20 09:05:57 2018 -0700
@@ -340,7 +340,7 @@
         http2TestServer.addHandler(new Http2EchoHandler(), "/http2/echo");
         http2URI = "http://" + http2TestServer.serverAuthority() + "/http2/echo";
 
-        https2TestServer = new Http2TestServer("localhost", true, 0);
+        https2TestServer = new Http2TestServer("localhost", true, sslContext);
         https2TestServer.addHandler(new Http2EchoHandler(), "/https2/echo");
         https2URI = "https://" + https2TestServer.serverAuthority() + "/https2/echo";
 
--- a/test/jdk/java/net/httpclient/CustomResponseSubscriber.java	Wed Jun 20 17:15:16 2018 +0200
+++ b/test/jdk/java/net/httpclient/CustomResponseSubscriber.java	Wed Jun 20 09:05:57 2018 -0700
@@ -217,7 +217,7 @@
         http2URI_fixed = "http://" + http2TestServer.serverAuthority() + "/http2/fixed";
         http2URI_chunk = "http://" + http2TestServer.serverAuthority() + "/http2/chunk";
 
-        https2TestServer = new Http2TestServer("localhost", true, 0);
+        https2TestServer = new Http2TestServer("localhost", true, sslContext);
         https2TestServer.addHandler(h2_fixedLengthHandler, "/https2/fixed");
         https2TestServer.addHandler(h2_chunkedHandler, "/https2/chunk");
         https2URI_fixed = "https://" + https2TestServer.serverAuthority() + "/https2/fixed";
--- a/test/jdk/java/net/httpclient/DependentActionsTest.java	Wed Jun 20 17:15:16 2018 +0200
+++ b/test/jdk/java/net/httpclient/DependentActionsTest.java	Wed Jun 20 09:05:57 2018 -0700
@@ -27,7 +27,7 @@
  *          completes are executed either asynchronously in an executor when the
  *          CF later completes, or in the user thread that joins.
  * @library /lib/testlibrary http2/server
- * @build jdk.testlibrary.SimpleSSLContext HttpServerAdapters ThrowingPublishers
+ * @build jdk.testlibrary.SimpleSSLContext HttpServerAdapters DependentActionsTest
  * @modules java.base/sun.net.www.http
  *          java.net.http/jdk.internal.net.http.common
  *          java.net.http/jdk.internal.net.http.frame
@@ -82,12 +82,14 @@
 import java.util.concurrent.atomic.AtomicLong;
 import java.util.concurrent.atomic.AtomicReference;
 import java.util.function.Consumer;
+import java.util.function.Predicate;
 import java.util.function.Supplier;
 import java.util.stream.Collectors;
 import java.util.stream.Stream;
 
 import static java.lang.System.out;
 import static java.lang.String.format;
+import static java.util.stream.Collectors.toList;
 import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.assertTrue;
 
@@ -374,13 +376,13 @@
     }
 
     final List<String> extractStream(HttpResponse<Stream<String>> resp) {
-        return resp.body().collect(Collectors.toList());
+        return resp.body().collect(toList());
     }
 
     final List<String> extractInputStream(HttpResponse<InputStream> resp) {
         try (InputStream is = resp.body()) {
             return new BufferedReader(new InputStreamReader(is))
-                    .lines().collect(Collectors.toList());
+                    .lines().collect(toList());
         } catch (IOException x) {
             throw new CompletionException(x);
         }
@@ -399,43 +401,27 @@
                 .findFirst();
     }
 
+    static final Predicate<StackFrame> DAT = sfe ->
+            sfe.getClassName().startsWith("DependentActionsTest");
+    static final Predicate<StackFrame> JUC = sfe ->
+            sfe.getClassName().startsWith("java.util.concurrent");
+    static final Predicate<StackFrame> JLT = sfe ->
+            sfe.getClassName().startsWith("java.lang.Thread");
+    static final Predicate<StackFrame> NotDATorJUCorJLT = Predicate.not(DAT.or(JUC).or(JLT));
+
+
     <T> void checkThreadAndStack(Thread thread,
                                  AtomicReference<RuntimeException> failed,
                                  T result,
                                  Throwable error) {
-        if (Thread.currentThread() == thread) {
-            //failed.set(new RuntimeException("Dependant action was executed in " + thread));
-            List<StackFrame> httpStack = WALKER.walk(s -> s.filter(f -> f.getDeclaringClass()
-                    .getModule().equals(HttpClient.class.getModule()))
-                    .collect(Collectors.toList()));
-            if (!httpStack.isEmpty()) {
-                System.out.println("Found unexpected trace: ");
-                httpStack.forEach(f -> System.out.printf("\t%s%n", f));
-                failed.set(new RuntimeException("Dependant action has unexpected frame in " +
-                        Thread.currentThread() + ": " + httpStack.get(0)));
+        //failed.set(new RuntimeException("Dependant action was executed in " + thread));
+        List<StackFrame> otherFrames = WALKER.walk(s -> s.filter(NotDATorJUCorJLT).collect(toList()));
+        if (!otherFrames.isEmpty()) {
+            System.out.println("Found unexpected trace: ");
+            otherFrames.forEach(f -> System.out.printf("\t%s%n", f));
+            failed.set(new RuntimeException("Dependant action has unexpected frame in " +
+                       Thread.currentThread() + ": " + otherFrames.get(0)));
 
-            }
-            return;
-        } else if (System.getSecurityManager() != null) {
-            Optional<StackFrame> sf = WALKER.walk(s -> findFrame(s, "PrivilegedRunnable"));
-            if (!sf.isPresent()) {
-                failed.set(new RuntimeException("Dependant action does not have expected frame in "
-                        + Thread.currentThread()));
-                return;
-            } else {
-                System.out.println("Found expected frame: " + sf.get());
-            }
-        } else {
-            List<StackFrame> httpStack = WALKER.walk(s -> s.filter(f -> f.getDeclaringClass()
-                    .getModule().equals(HttpClient.class.getModule()))
-                    .collect(Collectors.toList()));
-            if (!httpStack.isEmpty()) {
-                System.out.println("Found unexpected trace: ");
-                httpStack.forEach(f -> System.out.printf("\t%s%n", f));
-                failed.set(new RuntimeException("Dependant action has unexpected frame in " +
-                        Thread.currentThread() + ": " + httpStack.get(0)));
-
-            }
         }
     }
 
@@ -620,7 +606,7 @@
         http2URI_fixed = "http://" + http2TestServer.serverAuthority() + "/http2/fixed/x";
         http2URI_chunk = "http://" + http2TestServer.serverAuthority() + "/http2/chunk/x";
 
-        https2TestServer = HttpTestServer.of(new Http2TestServer("localhost", true, 0));
+        https2TestServer = HttpTestServer.of(new Http2TestServer("localhost", true, sslContext));
         https2TestServer.addHandler(h2_fixedLengthHandler, "/https2/fixed");
         https2TestServer.addHandler(h2_chunkedHandler, "/https2/chunk");
         https2URI_fixed = "https://" + https2TestServer.serverAuthority() + "/https2/fixed/x";
--- a/test/jdk/java/net/httpclient/DependentPromiseActionsTest.java	Wed Jun 20 17:15:16 2018 +0200
+++ b/test/jdk/java/net/httpclient/DependentPromiseActionsTest.java	Wed Jun 20 09:05:57 2018 -0700
@@ -27,7 +27,7 @@
  *          completes are executed either asynchronously in an executor when the
  *          CF later completes, or in the user thread that joins.
  * @library /lib/testlibrary http2/server
- * @build jdk.testlibrary.SimpleSSLContext HttpServerAdapters ThrowingPublishers
+ * @build jdk.testlibrary.SimpleSSLContext HttpServerAdapters DependentPromiseActionsTest
  * @modules java.base/sun.net.www.http
  *          java.net.http/jdk.internal.net.http.common
  *          java.net.http/jdk.internal.net.http.frame
@@ -40,7 +40,6 @@
 import java.io.BufferedReader;
 import java.io.InputStreamReader;
 import java.lang.StackWalker.StackFrame;
-import jdk.internal.net.http.common.HttpHeadersImpl;
 import jdk.testlibrary.SimpleSSLContext;
 import org.testng.annotations.AfterTest;
 import org.testng.annotations.AfterClass;
@@ -79,6 +78,7 @@
 import java.util.concurrent.Semaphore;
 import java.util.concurrent.atomic.AtomicLong;
 import java.util.concurrent.atomic.AtomicReference;
+import java.util.function.BiPredicate;
 import java.util.function.Consumer;
 import java.util.function.Function;
 import java.util.function.Supplier;
@@ -672,7 +672,7 @@
         http2URI_fixed = "http://" + http2TestServer.serverAuthority() + "/http2/fixed/y";
         http2URI_chunk = "http://" + http2TestServer.serverAuthority() + "/http2/chunk/y";
 
-        https2TestServer = HttpTestServer.of(new Http2TestServer("localhost", true, 0));
+        https2TestServer = HttpTestServer.of(new Http2TestServer("localhost", true, sslContext));
         https2TestServer.addHandler(h2_fixedLengthHandler, "/https2/fixed");
         https2TestServer.addHandler(h2_chunkedHandler, "/https2/chunk");
         https2URI_fixed = "https://" + https2TestServer.serverAuthority() + "/https2/fixed/y";
@@ -690,8 +690,13 @@
         https2TestServer.stop();
     }
 
-    private static void pushPromiseFor(HttpTestExchange t, URI requestURI, String pushPath, boolean fixed)
-            throws IOException
+    static final BiPredicate<String,String> ACCEPT_ALL = (x, y) -> true;
+
+    private static void pushPromiseFor(HttpTestExchange t,
+                                       URI requestURI,
+                                       String pushPath,
+                                       boolean fixed)
+        throws IOException
     {
         try {
             URI promise = new URI(requestURI.getScheme(),
@@ -700,9 +705,13 @@
             byte[] promiseBytes = promise.toASCIIString().getBytes(UTF_8);
             out.printf("TestServer: %s Pushing promise: %s%n", now(), promise);
             err.printf("TestServer: %s Pushing promise: %s%n", now(), promise);
-            HttpTestHeaders headers =  HttpTestHeaders.of(new HttpHeadersImpl());
+            HttpHeaders headers;
             if (fixed) {
-                headers.addHeader("Content-length", String.valueOf(promiseBytes.length));
+                String length = String.valueOf(promiseBytes.length);
+                headers = HttpHeaders.of(Map.of("Content-Length", List.of(length)),
+                                         ACCEPT_ALL);
+            } else {
+                headers = HttpHeaders.of(Map.of(), ACCEPT_ALL); // empty
             }
             t.serverPush(promise, headers, promiseBytes);
         } catch (URISyntaxException x) {
--- a/test/jdk/java/net/httpclient/DigestEchoServer.java	Wed Jun 20 17:15:16 2018 +0200
+++ b/test/jdk/java/net/httpclient/DigestEchoServer.java	Wed Jun 20 09:05:57 2018 -0700
@@ -170,7 +170,7 @@
         }
     }
 
-    private static String toString(HttpTestHeaders headers) {
+    private static String toString(HttpTestRequestHeaders headers) {
         return headers.entrySet().stream()
                 .map((e) -> e.getKey() + ": " + e.getValue())
                 .collect(Collectors.joining("\n"));
@@ -847,9 +847,9 @@
         }
 
         public static String computeDigest(boolean isRequest,
-                                            String reqMethod,
-                                            char[] password,
-                                            DigestResponse params)
+                                           String reqMethod,
+                                           char[] password,
+                                           DigestResponse params)
             throws NoSuchAlgorithmException
         {
 
@@ -970,11 +970,13 @@
 
         @Override
         protected void requestAuthentication(HttpTestExchange he)
-            throws IOException {
-            he.getResponseHeaders().addHeader(getAuthenticate(),
-                 "Basic realm=\"" + auth.getRealm() + "\"");
-            System.out.println(type + ": Requesting Basic Authentication "
-                 + he.getResponseHeaders().firstValue(getAuthenticate()));
+            throws IOException
+        {
+            String headerName = getAuthenticate();
+            String headerValue = "Basic realm=\"" + auth.getRealm() + "\"";
+            he.getResponseHeaders().addHeader(headerName, headerValue);
+            System.out.println(type + ": Requesting Basic Authentication, "
+                               + headerName + " : "+ headerValue);
         }
 
         @Override
@@ -1061,14 +1063,13 @@
             } else {
                 throw new InternalError(String.valueOf(v));
             }
-            he.getResponseHeaders().addHeader(getAuthenticate(),
-                 "Digest realm=\"" + auth.getRealm() + "\","
-                 + separator + "qop=\"auth\","
-                 + separator + "nonce=\"" + ns +"\"");
-            System.out.println(type + ": Requesting Digest Authentication "
-                 + he.getResponseHeaders()
-                    .firstValue(getAuthenticate())
-                    .orElse("null"));
+            String headerName = getAuthenticate();
+            String headerValue = "Digest realm=\"" + auth.getRealm() + "\","
+                    + separator + "qop=\"auth\","
+                    + separator + "nonce=\"" + ns +"\"";
+            he.getResponseHeaders().addHeader(headerName, headerValue);
+            System.out.println(type + ": Requesting Digest Authentication, "
+                               + headerName + " : " + headerValue);
         }
 
         @Override
--- a/test/jdk/java/net/httpclient/EncodedCharsInURI.java	Wed Jun 20 17:15:16 2018 +0200
+++ b/test/jdk/java/net/httpclient/EncodedCharsInURI.java	Wed Jun 20 09:05:57 2018 -0700
@@ -33,7 +33,8 @@
  *          java.net.http/jdk.internal.net.http.frame
  *          java.net.http/jdk.internal.net.http.hpack
  * @run testng/othervm
- *        -Djdk.httpclient.HttpClient.log=headers EncodedCharsInURI
+ *        -Djdk.internal.httpclient.debug=true
+ *        -Djdk.httpclient.HttpClient.log=headers,errors EncodedCharsInURI
  */
 //*        -Djdk.internal.httpclient.debug=true
 
@@ -48,21 +49,14 @@
 import org.testng.annotations.Test;
 
 import javax.net.ServerSocketFactory;
-import javax.net.SocketFactory;
 import javax.net.ssl.SSLContext;
-import javax.net.ssl.SSLServerSocket;
-import javax.net.ssl.SSLServerSocketFactory;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
-import java.io.OutputStreamWriter;
-import java.io.PrintWriter;
-import java.io.Writer;
 import java.net.InetAddress;
 import java.net.InetSocketAddress;
 import java.net.ServerSocket;
 import java.net.Socket;
-import java.net.SocketAddress;
 import java.net.URI;
 import java.net.http.HttpClient;
 import java.net.http.HttpRequest;
@@ -71,8 +65,6 @@
 import java.net.http.HttpResponse;
 import java.net.http.HttpResponse.BodyHandler;
 import java.net.http.HttpResponse.BodyHandlers;
-import java.nio.file.Path;
-import java.nio.file.Paths;
 import java.util.List;
 import java.util.Locale;
 import java.util.StringTokenizer;
@@ -87,6 +79,7 @@
 import static java.lang.String.format;
 import static java.lang.System.in;
 import static java.lang.System.out;
+import static java.nio.charset.StandardCharsets.US_ASCII;
 import static java.nio.charset.StandardCharsets.UTF_8;
 import static java.net.http.HttpClient.Builder.NO_PROXY;
 import static org.testng.Assert.assertEquals;
@@ -292,7 +285,7 @@
         http2URI_fixed = "http://" + http2TestServer.serverAuthority() + "/http2/fixed/x";
         http2URI_chunk = "http://" + http2TestServer.serverAuthority() + "/http2/chunk/x";
 
-        https2TestServer = HttpTestServer.of(new Http2TestServer("localhost", true, 0));
+        https2TestServer = HttpTestServer.of(new Http2TestServer("localhost", true, sslContext));
         https2TestServer.addHandler(h2_fixedLengthHandler, "/https2/fixed");
         https2TestServer.addHandler(h2_chunkedHandler, "/https2/chunk");
         https2URI_fixed = "https://" + https2TestServer.serverAuthority() + "/https2/fixed/x";
@@ -402,9 +395,6 @@
                     Socket targetConnection = null;
                     InputStream  ccis = clientConnection.getInputStream();
                     OutputStream ccos = clientConnection.getOutputStream();
-                    Writer w = new OutputStreamWriter(
-                            clientConnection.getOutputStream(), "UTF-8");
-                    PrintWriter pw = new PrintWriter(w);
                     System.out.println(now() + getName() + ": Reading request line");
                     String requestLine = readLine(ccis);
                     System.out.println(now() + getName() + ": Request line: " + requestLine);
@@ -459,11 +449,13 @@
                     // Then send the 200 OK response to the client
                     System.out.println(now() + getName() + ": Sending "
                             + response);
-                    pw.print(response);
-                    pw.flush();
+                    ccos.write(response.toString().getBytes(UTF_8));
+                    ccos.flush();
+                    System.out.println(now() + getName() + ": sent response headers");
                     ccos.write(b);
                     ccos.flush();
                     ccos.close();
+                    System.out.println(now() + getName() + ": sent " + b.length + " body bytes");
                     connections.remove(clientConnection);
                     clientConnection.close();
                 }
--- a/test/jdk/java/net/httpclient/EscapedOctetsInURI.java	Wed Jun 20 17:15:16 2018 +0200
+++ b/test/jdk/java/net/httpclient/EscapedOctetsInURI.java	Wed Jun 20 09:05:57 2018 -0700
@@ -201,7 +201,7 @@
         http2TestServer.addHandler(new HttpASCIIUriStringHandler(), "/http2");
         http2URI = "http://" + http2TestServer.serverAuthority() + "/http2";
 
-        https2TestServer = new Http2TestServer("localhost", true, 0);
+        https2TestServer = new Http2TestServer("localhost", true, sslContext);
         https2TestServer.addHandler(new HttpASCIIUriStringHandler(), "/https2");
         https2URI = "https://" + https2TestServer.serverAuthority() + "/https2";
 
--- a/test/jdk/java/net/httpclient/FlowAdapterPublisherTest.java	Wed Jun 20 17:15:16 2018 +0200
+++ b/test/jdk/java/net/httpclient/FlowAdapterPublisherTest.java	Wed Jun 20 09:05:57 2018 -0700
@@ -359,7 +359,7 @@
         http2TestServer.addHandler(new Http2EchoHandler(), "/http2/echo");
         http2URI = "http://" + http2TestServer.serverAuthority() + "/http2/echo";
 
-        https2TestServer = new Http2TestServer("localhost", true, 0);
+        https2TestServer = new Http2TestServer("localhost", true, sslContext);
         https2TestServer.addHandler(new Http2EchoHandler(), "/https2/echo");
         https2URI = "https://" + https2TestServer.serverAuthority() + "/https2/echo";
 
--- a/test/jdk/java/net/httpclient/FlowAdapterSubscriberTest.java	Wed Jun 20 17:15:16 2018 +0200
+++ b/test/jdk/java/net/httpclient/FlowAdapterSubscriberTest.java	Wed Jun 20 09:05:57 2018 -0700
@@ -534,7 +534,7 @@
         http2TestServer.addHandler(new Http2EchoHandler(), "/http2/echo");
         http2URI = "http://" + http2TestServer.serverAuthority() + "/http2/echo";
 
-        https2TestServer = new Http2TestServer("localhost", true, 0);
+        https2TestServer = new Http2TestServer("localhost", true, sslContext);
         https2TestServer.addHandler(new Http2EchoHandler(), "/https2/echo");
         https2URI = "https://" + https2TestServer.serverAuthority() + "/https2/echo";
 
--- a/test/jdk/java/net/httpclient/HandshakeFailureTest.java	Wed Jun 20 17:15:16 2018 +0200
+++ b/test/jdk/java/net/httpclient/HandshakeFailureTest.java	Wed Jun 20 09:05:57 2018 -0700
@@ -23,6 +23,7 @@
 
 import javax.net.ServerSocketFactory;
 import javax.net.ssl.SSLContext;
+import javax.net.ssl.SSLParameters;
 import javax.net.ssl.SSLHandshakeException;
 import javax.net.ssl.SSLSocket;
 import java.io.DataInputStream;
@@ -45,7 +46,7 @@
 
 /**
  * @test
- * @run main/othervm HandshakeFailureTest
+ * @run main/othervm -Djdk.internal.httpclient.debug=true HandshakeFailureTest
  * @summary Verify SSLHandshakeException is received when the handshake fails,
  * either because the server closes ( EOF ) the connection during handshaking
  * or no cipher suite ( or similar ) can be negotiated.
@@ -80,9 +81,17 @@
         }
     }
 
+    static HttpClient getClient() {
+        SSLParameters params = new SSLParameters();
+        params.setProtocols(new String[] {"TLSv1.2"});
+        return HttpClient.newBuilder()
+                .sslParameters(params)
+                .build();
+    }
+
     void testSyncSameClient(URI uri, Version version) throws Exception {
         out.printf("%n--- testSyncSameClient %s ---%n", version);
-        HttpClient client = HttpClient.newHttpClient();
+        HttpClient client = getClient();
         for (int i = 0; i < TIMES; i++) {
             out.printf("iteration %d%n", i);
             HttpRequest request = HttpRequest.newBuilder(uri)
@@ -92,8 +101,9 @@
                 HttpResponse<Void> response = client.send(request, discarding());
                 String msg = String.format("UNEXPECTED response=%s%n", response);
                 throw new RuntimeException(msg);
-            } catch (SSLHandshakeException expected) {
+            } catch (IOException expected) {
                 out.printf("Client: caught expected exception: %s%n", expected);
+                checkExceptionOrCause(SSLHandshakeException.class, expected);
             }
         }
     }
@@ -103,7 +113,7 @@
         for (int i = 0; i < TIMES; i++) {
             out.printf("iteration %d%n", i);
             // a new client each time
-            HttpClient client = HttpClient.newHttpClient();
+            HttpClient client = getClient();
             HttpRequest request = HttpRequest.newBuilder(uri)
                                              .version(version)
                                              .build();
@@ -111,15 +121,16 @@
                 HttpResponse<Void> response = client.send(request, discarding());
                 String msg = String.format("UNEXPECTED response=%s%n", response);
                 throw new RuntimeException(msg);
-            } catch (SSLHandshakeException expected) {
+            } catch (IOException expected) {
                 out.printf("Client: caught expected exception: %s%n", expected);
+                checkExceptionOrCause(SSLHandshakeException.class, expected);
             }
         }
     }
 
     void testAsyncSameClient(URI uri, Version version) throws Exception {
         out.printf("%n--- testAsyncSameClient %s ---%n", version);
-        HttpClient client = HttpClient.newHttpClient();
+        HttpClient client = getClient();
         for (int i = 0; i < TIMES; i++) {
             out.printf("iteration %d%n", i);
             HttpRequest request = HttpRequest.newBuilder(uri)
@@ -132,12 +143,9 @@
                 String msg = String.format("UNEXPECTED response=%s%n", response);
                 throw new RuntimeException(msg);
             } catch (CompletionException ce) {
-                if (ce.getCause() instanceof SSLHandshakeException) {
-                    out.printf("Client: caught expected exception: %s%n", ce.getCause());
-                } else {
-                    out.printf("Client: caught UNEXPECTED exception: %s%n", ce.getCause());
-                    throw ce;
-                }
+                Throwable expected = ce.getCause();
+                out.printf("Client: caught expected exception: %s%n", expected);
+                checkExceptionOrCause(SSLHandshakeException.class, expected);
             }
         }
     }
@@ -147,7 +155,7 @@
         for (int i = 0; i < TIMES; i++) {
             out.printf("iteration %d%n", i);
             // a new client each time
-            HttpClient client = HttpClient.newHttpClient();
+            HttpClient client = getClient();
             HttpRequest request = HttpRequest.newBuilder(uri)
                                              .version(version)
                                              .build();
@@ -158,16 +166,26 @@
                 String msg = String.format("UNEXPECTED response=%s%n", response);
                 throw new RuntimeException(msg);
             } catch (CompletionException ce) {
-                if (ce.getCause() instanceof SSLHandshakeException) {
-                    out.printf("Client: caught expected exception: %s%n", ce.getCause());
-                } else {
-                    out.printf("Client: caught UNEXPECTED exception: %s%n", ce.getCause());
-                    throw ce;
-                }
+                ce.printStackTrace(out);
+                Throwable expected = ce.getCause();
+                out.printf("Client: caught expected exception: %s%n", expected);
+                checkExceptionOrCause(SSLHandshakeException.class, expected);
             }
         }
     }
 
+    static void checkExceptionOrCause(Class<? extends Throwable> clazz, Throwable t) {
+        final Throwable original = t;
+        do {
+            if (clazz.isInstance(t)) {
+                System.out.println("Found expected exception/cause: " + t);
+                return; // found
+            }
+        } while ((t = t.getCause()) != null);
+        original.printStackTrace(System.out);
+        throw new RuntimeException("Expected " + clazz + "in " + original);
+    }
+
     /** Common supertype for PlainServer and SSLServer. */
     static abstract class AbstractServer extends Thread implements AutoCloseable {
         protected final ServerSocket ss;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/java/net/httpclient/HeadTest.java	Wed Jun 20 09:05:57 2018 -0700
@@ -0,0 +1,234 @@
+/*
+ * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 8203433
+ * @summary (httpclient) Add tests for HEAD and 304 responses.
+ * @modules java.base/sun.net.www.http
+ *          java.net.http/jdk.internal.net.http.common
+ *          java.net.http/jdk.internal.net.http.frame
+ *          java.net.http/jdk.internal.net.http.hpack
+ *          java.logging
+ *          jdk.httpserver
+ * @library /lib/testlibrary /test/lib http2/server
+ * @build Http2TestServer
+ * @build jdk.testlibrary.SimpleSSLContext
+ * @run testng/othervm
+ *       -Djdk.httpclient.HttpClient.log=trace,headers,requests
+ *       HeadTest
+ */
+
+import com.sun.net.httpserver.HttpServer;
+import com.sun.net.httpserver.HttpsConfigurator;
+import com.sun.net.httpserver.HttpsServer;
+import jdk.testlibrary.SimpleSSLContext;
+import org.testng.annotations.AfterTest;
+import org.testng.annotations.BeforeTest;
+import org.testng.annotations.DataProvider;
+import org.testng.annotations.Test;
+
+import javax.net.ServerSocketFactory;
+import javax.net.ssl.SSLContext;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.PrintWriter;
+import java.io.Writer;
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.net.URI;
+import java.net.http.HttpClient;
+import java.net.http.HttpClient.Redirect;
+import java.net.http.HttpRequest;
+import java.net.http.HttpResponse;
+import java.net.http.HttpResponse.BodyHandlers;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.StringTokenizer;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentLinkedQueue;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import static java.lang.System.out;
+import static java.nio.charset.StandardCharsets.UTF_8;
+import static java.net.HttpURLConnection.HTTP_OK;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertTrue;
+
+public class HeadTest implements HttpServerAdapters {
+
+    SSLContext sslContext;
+    HttpTestServer httpTestServer;        // HTTP/1.1
+    HttpTestServer httpsTestServer;       // HTTPS/1.1
+    HttpTestServer http2TestServer;       // HTTP/2 ( h2c )
+    HttpTestServer https2TestServer;      // HTTP/2 ( h2  )
+    String httpURI;
+    String httpsURI;
+    String http2URI;
+    String https2URI;
+
+    static final String MESSAGE = "Basic HeadTest message body";
+    static final int ITERATIONS = 3;
+    static final String CONTENT_LEN = "300";
+
+    /*
+     * NOT_MODIFIED status code results from a conditional GET where
+     * the server does not (must not) return a response body because
+     * the condition specified in the request disallows it
+     */
+    static final int HTTP_NOT_MODIFIED = 304;
+    static final int HTTP_OK = 200;
+
+
+    @DataProvider(name = "positive")
+    public Object[][] positive() {
+        return new Object[][] {
+                { httpURI, "GET", HTTP_NOT_MODIFIED, HttpClient.Version.HTTP_1_1  },
+                { httpsURI, "GET", HTTP_NOT_MODIFIED, HttpClient.Version.HTTP_1_1  },
+                { httpURI, "GET", HTTP_NOT_MODIFIED, HttpClient.Version.HTTP_2  },
+                { httpsURI, "GET", HTTP_NOT_MODIFIED, HttpClient.Version.HTTP_2  },
+                { httpURI, "HEAD", HTTP_OK, HttpClient.Version.HTTP_1_1  },
+                { httpsURI, "HEAD", HTTP_OK, HttpClient.Version.HTTP_1_1  },
+                { httpURI, "HEAD", HTTP_OK, HttpClient.Version.HTTP_2  },
+                { httpsURI, "HEAD", HTTP_OK, HttpClient.Version.HTTP_2  },
+                { httpURI + "transfer/", "GET", HTTP_NOT_MODIFIED, HttpClient.Version.HTTP_1_1  },
+                { httpsURI + "transfer/", "GET", HTTP_NOT_MODIFIED, HttpClient.Version.HTTP_1_1  },
+                { httpURI + "transfer/", "GET", HTTP_NOT_MODIFIED, HttpClient.Version.HTTP_2  },
+                { httpsURI + "transfer/", "GET", HTTP_NOT_MODIFIED, HttpClient.Version.HTTP_2  },
+                { httpURI + "transfer/", "HEAD", HTTP_OK, HttpClient.Version.HTTP_1_1  },
+                { httpsURI + "transfer/", "HEAD", HTTP_OK, HttpClient.Version.HTTP_1_1  },
+                { httpURI + "transfer/", "HEAD", HTTP_OK, HttpClient.Version.HTTP_2  },
+                { httpsURI + "transfer/", "HEAD", HTTP_OK, HttpClient.Version.HTTP_2  }
+        };
+    }
+
+    static final AtomicLong requestCounter = new AtomicLong();
+
+    @Test(dataProvider = "positive")
+    void test(String uriString, String method,
+                        int expResp, HttpClient.Version version) throws Exception {
+        out.printf("%n---- starting (%s) ----%n", uriString);
+        HttpClient client = HttpClient.newBuilder()
+                .followRedirects(Redirect.ALWAYS)
+                .sslContext(sslContext)
+                .build();
+
+        URI uri = URI.create(uriString);
+
+        HttpRequest.Builder requestBuilder = HttpRequest
+                .newBuilder(uri)
+                .method(method, HttpRequest.BodyPublishers.noBody());
+
+        if (version != null) {
+            requestBuilder.version(version);
+        }
+        HttpRequest request = requestBuilder.build();
+        out.println("Initial request: " + request.uri());
+
+        HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
+
+        out.println("  Got response: " + response);
+
+        assertEquals(response.statusCode(), expResp);
+        assertEquals(response.body(), "");
+        assertEquals(response.headers().firstValue("Content-length").get(), CONTENT_LEN);
+    }
+
+    // -- Infrastructure
+
+    @BeforeTest
+    public void setup() throws Exception {
+        sslContext = new SimpleSSLContext().get();
+        if (sslContext == null)
+            throw new AssertionError("Unexpected null sslContext");
+
+        InetSocketAddress sa = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
+
+        httpTestServer = HttpTestServer.of(HttpServer.create(sa, 0));
+        httpTestServer.addHandler(new HeadHandler(), "/");
+        httpURI = "http://" + httpTestServer.serverAuthority() + "/";
+        HttpsServer httpsServer = HttpsServer.create(sa, 0);
+        httpsServer.setHttpsConfigurator(new HttpsConfigurator(sslContext));
+        httpsTestServer = HttpTestServer.of(httpsServer);
+        httpsTestServer.addHandler(new HeadHandler(),"/");
+        httpsURI = "https://" + httpsTestServer.serverAuthority() + "/";
+
+        http2TestServer = HttpTestServer.of(new Http2TestServer("localhost", false, 0));
+        http2TestServer.addHandler(new HeadHandler(), "/");
+        http2URI = "http://" + http2TestServer.serverAuthority() + "/";
+        https2TestServer = HttpTestServer.of(new Http2TestServer("localhost", true, 0));
+        https2TestServer.addHandler(new HeadHandler(), "/");
+        https2URI = "https://" + https2TestServer.serverAuthority() + "/";
+
+
+        httpTestServer.start();
+        httpsTestServer.start();
+        http2TestServer.start();
+        https2TestServer.start();
+    }
+
+    @AfterTest
+    public void teardown() throws Exception {
+        httpTestServer.stop();
+        httpsTestServer.stop();
+        http2TestServer.stop();
+        https2TestServer.stop();
+    }
+
+    static class HeadHandler implements HttpTestHandler {
+
+        @Override
+        public void handle(HttpTestExchange t) throws IOException {
+            readAllRequestData(t); // shouldn't be any
+            String method = t.getRequestMethod();
+            String path = t.getRequestURI().getPath();
+            HttpTestResponseHeaders rsph = t.getResponseHeaders();
+            if (path.contains("transfer"))
+                rsph.addHeader("Transfer-Encoding", "chunked");
+            rsph.addHeader("Content-length", CONTENT_LEN);
+            if (method.equals("HEAD")) {
+                t.sendResponseHeaders(HTTP_OK, -1);
+            } else if (method.equals("GET")) {
+                t.sendResponseHeaders(HTTP_NOT_MODIFIED, -1);
+            }
+            t.close();
+        }
+    }
+
+    static void readAllRequestData(HttpTestExchange t) throws IOException {
+        try (InputStream is = t.getRequestBody()) {
+            is.readAllBytes();
+        }
+    }
+}
--- a/test/jdk/java/net/httpclient/HeadersTest.java	Wed Jun 20 17:15:16 2018 +0200
+++ b/test/jdk/java/net/httpclient/HeadersTest.java	Wed Jun 20 09:05:57 2018 -0700
@@ -32,6 +32,7 @@
 import java.util.List;
 import java.util.Map;
 import java.util.Optional;
+import java.util.function.BiPredicate;
 import static java.net.http.HttpClient.Builder.NO_PROXY;
 
 /**
@@ -41,20 +42,11 @@
  */
 public class HeadersTest {
 
+    static final BiPredicate<String,String> ACCEPT_ALL = (x, y) -> true;
+
     static final URI TEST_URI = URI.create("http://www.foo.com/");
     static final HttpClient client = HttpClient.newBuilder().proxy(NO_PROXY).build();
 
-    static final class Htt