src/java.net.http/share/classes/jdk/internal/net/http/Http1Exchange.java
author mbaesken
Thu, 28 Nov 2019 13:02:39 +0100
changeset 59323 ae2eb76c486d
parent 53300 54aa3ea04fe8
permissions -rw-r--r--
8234821: remove unused functions from libjli Reviewed-by: clanger, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     1
/*
53300
54aa3ea04fe8 8216974: HttpConnection not returned to the pool after 204 response
dfuchs
parents: 52554
diff changeset
     2
 * Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     4
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    10
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    15
 * accompanied this code).
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    16
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    20
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    23
 * questions.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    24
 */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    25
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    26
package jdk.internal.net.http;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    27
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    28
import java.io.IOException;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    29
import java.net.InetSocketAddress;
52554
5f1ca46703f9 8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents: 51364
diff changeset
    30
import java.net.http.HttpClient;
53300
54aa3ea04fe8 8216974: HttpConnection not returned to the pool after 204 response
dfuchs
parents: 52554
diff changeset
    31
import java.net.http.HttpResponse;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    32
import java.net.http.HttpResponse.BodyHandler;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    33
import java.net.http.HttpResponse.BodySubscriber;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    34
import java.nio.ByteBuffer;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    35
import java.util.Objects;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    36
import java.util.concurrent.CompletableFuture;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    37
import java.util.LinkedList;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    38
import java.util.List;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    39
import java.util.concurrent.ConcurrentLinkedDeque;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    40
import java.util.concurrent.Executor;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    41
import java.util.concurrent.Flow;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    42
import jdk.internal.net.http.common.Demand;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    43
import jdk.internal.net.http.common.Log;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    44
import jdk.internal.net.http.common.FlowTube;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    45
import jdk.internal.net.http.common.Logger;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    46
import jdk.internal.net.http.common.SequentialScheduler;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    47
import jdk.internal.net.http.common.MinimalFuture;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    48
import jdk.internal.net.http.common.Utils;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    49
import static java.net.http.HttpClient.Version.HTTP_1_1;
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
    50
import static jdk.internal.net.http.common.Utils.wrapWithExtraDetail;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    51
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    52
/**
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    53
 * Encapsulates one HTTP/1.1 request/response exchange.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    54
 */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    55
class Http1Exchange<T> extends ExchangeImpl<T> {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    56
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    57
    final Logger debug = Utils.getDebugLogger(this::dbgString, Utils.DEBUG);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    58
    final HttpRequestImpl request; // main request
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    59
    final Http1Request requestAction;
44639
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
    60
    private volatile Http1Response<T> response;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    61
    final HttpConnection connection;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    62
    final HttpClientImpl client;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    63
    final Executor executor;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    64
    private final Http1AsyncReceiver asyncReceiver;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    65
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    66
    /** Records a possible cancellation raised before any operation
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    67
     * has been initiated, or an error received while sending the request. */
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    68
    private Throwable failed;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    69
    private final List<CompletableFuture<?>> operations; // used for cancel
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    70
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    71
    /** Must be held when operating on any internal state or data. */
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    72
    private final Object lock = new Object();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    73
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    74
    /** Holds the outgoing data, either the headers or a request body part. Or
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    75
     * an error from the request body publisher. At most there can be ~2 pieces
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    76
     * of outgoing data ( onComplete|onError can be invoked without demand ).*/
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    77
    final ConcurrentLinkedDeque<DataPair> outgoing = new ConcurrentLinkedDeque<>();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    78
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    79
    /** The write publisher, responsible for writing the complete request ( both
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    80
     * headers and body ( if any ). */
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    81
    private final Http1Publisher writePublisher = new Http1Publisher();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    82
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    83
    /** Completed when the header have been published, or there is an error */
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    84
    private final CompletableFuture<ExchangeImpl<T>> headersSentCF  = new MinimalFuture<>();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    85
     /** Completed when the body has been published, or there is an error */
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    86
    private final CompletableFuture<ExchangeImpl<T>> bodySentCF = new MinimalFuture<>();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    87
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    88
    /** The subscriber to the request's body published. Maybe null. */
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    89
    private volatile Http1BodySubscriber bodySubscriber;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    90
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    91
    enum State { INITIAL,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    92
                 HEADERS,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    93
                 BODY,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    94
                 ERROR,          // terminal state
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    95
                 COMPLETING,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    96
                 COMPLETED }     // terminal state
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    97
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    98
    private State state = State.INITIAL;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    99
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   100
    /** A carrier for either data or an error. Used to carry data, and communicate
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   101
     * errors from the request ( both headers and body ) to the exchange. */
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   102
    static class DataPair {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   103
        Throwable throwable;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   104
        List<ByteBuffer> data;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   105
        DataPair(List<ByteBuffer> data, Throwable throwable){
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   106
            this.data = data;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   107
            this.throwable = throwable;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   108
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   109
        @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   110
        public String toString() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   111
            return "DataPair [data=" + data + ", throwable=" + throwable + "]";
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   112
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   113
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   114
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   115
    /** An abstract supertype for HTTP/1.1 body subscribers. There are two
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   116
     * concrete implementations: {@link Http1Request.StreamSubscriber}, and
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   117
     * {@link Http1Request.FixedContentSubscriber}, for receiving chunked and
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   118
     * fixed length bodies, respectively. */
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   119
    static abstract class Http1BodySubscriber implements Flow.Subscriber<ByteBuffer> {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   120
        final MinimalFuture<Flow.Subscription> whenSubscribed = new MinimalFuture<>();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   121
        private volatile Flow.Subscription subscription;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   122
        volatile boolean complete;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   123
        private final Logger debug;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   124
        Http1BodySubscriber(Logger debug) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   125
            assert debug != null;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   126
            this.debug = debug;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   127
        }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   128
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   129
        /** Final sentinel in the stream of request body. */
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   130
        static final List<ByteBuffer> COMPLETED = List.of(ByteBuffer.allocate(0));
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   131
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   132
        final void request(long n) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   133
            if (debug.on())
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   134
                debug.log("Http1BodySubscriber requesting %d, from %s",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   135
                          n, subscription);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   136
            subscription.request(n);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   137
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   138
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   139
        /** A current-state message suitable for inclusion in an exception detail message. */
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   140
        abstract String currentStateMessage();
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   141
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   142
        final boolean isSubscribed() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   143
            return subscription != null;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   144
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   145
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   146
        final void setSubscription(Flow.Subscription subscription) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   147
            this.subscription = subscription;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   148
            whenSubscribed.complete(subscription);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   149
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   150
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   151
        final void cancelSubscription() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   152
            try {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   153
                subscription.cancel();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   154
            } catch(Throwable t) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   155
                String msg = "Ignoring exception raised when canceling BodyPublisher subscription";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   156
                if (debug.on()) debug.log("%s: %s", msg, t);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   157
                Log.logError("{0}: {1}", msg, (Object)t);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   158
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   159
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   160
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   161
        static Http1BodySubscriber completeSubscriber(Logger debug) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   162
            return new Http1BodySubscriber(debug) {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   163
                @Override public void onSubscribe(Flow.Subscription subscription) { error(); }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   164
                @Override public void onNext(ByteBuffer item) { error(); }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   165
                @Override public void onError(Throwable throwable) { error(); }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   166
                @Override public void onComplete() { error(); }
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   167
                @Override String currentStateMessage() { return null; }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   168
                private void error() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   169
                    throw new InternalError("should not reach here");
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   170
                }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   171
            };
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   172
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   173
    }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   174
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   175
    @Override
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   176
    public String toString() {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   177
        return "HTTP/1.1 " + request.toString();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   178
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   179
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   180
    HttpRequestImpl request() {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   181
        return request;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   182
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   183
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   184
    Http1Exchange(Exchange<T> exchange, HttpConnection connection)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   185
        throws IOException
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   186
    {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   187
        super(exchange);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   188
        this.request = exchange.request();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   189
        this.client = exchange.client();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   190
        this.executor = exchange.executor();
44639
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   191
        this.operations = new LinkedList<>();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   192
        operations.add(headersSentCF);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   193
        operations.add(bodySentCF);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   194
        if (connection != null) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   195
            this.connection = connection;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   196
        } else {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   197
            InetSocketAddress addr = request.getAddress();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   198
            this.connection = HttpConnection.getConnection(addr, client, request, HTTP_1_1);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   199
        }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   200
        this.requestAction = new Http1Request(request, this);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   201
        this.asyncReceiver = new Http1AsyncReceiver(executor, this);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   202
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   203
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   204
    @Override
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   205
    HttpConnection connection() {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   206
        return connection;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   207
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   208
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   209
    private void connectFlows(HttpConnection connection) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   210
        FlowTube tube =  connection.getConnectionFlow();
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   211
        if (debug.on()) debug.log("%s connecting flows", tube);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   212
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   213
        // Connect the flow to our Http1TubeSubscriber:
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   214
        //   asyncReceiver.subscriber().
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   215
        tube.connectFlows(writePublisher,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   216
                          asyncReceiver.subscriber());
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   217
    }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   218
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   219
    @Override
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   220
    CompletableFuture<ExchangeImpl<T>> sendHeadersAsync() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   221
        // create the response before sending the request headers, so that
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   222
        // the response can set the appropriate receivers.
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   223
        if (debug.on()) debug.log("Sending headers only");
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   224
        // If the first attempt to read something triggers EOF, or
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   225
        // IOException("channel reset by peer"), we're going to retry.
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   226
        // Instruct the asyncReceiver to throw ConnectionExpiredException
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   227
        // to force a retry.
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   228
        asyncReceiver.setRetryOnError(true);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   229
        if (response == null) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   230
            response = new Http1Response<>(connection, this, asyncReceiver);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   231
        }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   232
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   233
        if (debug.on()) debug.log("response created in advance");
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   234
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   235
        CompletableFuture<Void> connectCF;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   236
        if (!connection.connected()) {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   237
            if (debug.on()) debug.log("initiating connect async");
51364
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 50985
diff changeset
   238
            connectCF = connection.connectAsync(exchange)
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 50985
diff changeset
   239
                    .thenCompose(unused -> connection.finishConnect());
49944
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   240
            Throwable cancelled;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   241
            synchronized (lock) {
49944
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   242
                if ((cancelled = failed) == null) {
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   243
                    operations.add(connectCF);
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   244
                }
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   245
            }
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   246
            if (cancelled != null) {
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   247
                if (client.isSelectorThread()) {
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   248
                    executor.execute(() ->
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   249
                        connectCF.completeExceptionally(cancelled));
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   250
                } else {
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   251
                    connectCF.completeExceptionally(cancelled);
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   252
                }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   253
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   254
        } else {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   255
            connectCF = new MinimalFuture<>();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   256
            connectCF.complete(null);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   257
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   258
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   259
        return connectCF
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   260
                .thenCompose(unused -> {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   261
                    CompletableFuture<Void> cf = new MinimalFuture<>();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   262
                    try {
50985
cd41f34e548c 8206001: Enable TLS1.3 by default in Http Client
michaelm
parents: 50681
diff changeset
   263
                        asyncReceiver.whenFinished.whenComplete((r,t) -> {
cd41f34e548c 8206001: Enable TLS1.3 by default in Http Client
michaelm
parents: 50681
diff changeset
   264
                            if (t != null) {
cd41f34e548c 8206001: Enable TLS1.3 by default in Http Client
michaelm
parents: 50681
diff changeset
   265
                                if (debug.on())
cd41f34e548c 8206001: Enable TLS1.3 by default in Http Client
michaelm
parents: 50681
diff changeset
   266
                                    debug.log("asyncReceiver finished (failed=%s)", (Object)t);
cd41f34e548c 8206001: Enable TLS1.3 by default in Http Client
michaelm
parents: 50681
diff changeset
   267
                                if (!headersSentCF.isDone())
cd41f34e548c 8206001: Enable TLS1.3 by default in Http Client
michaelm
parents: 50681
diff changeset
   268
                                    headersSentCF.completeAsync(() -> this, executor);
cd41f34e548c 8206001: Enable TLS1.3 by default in Http Client
michaelm
parents: 50681
diff changeset
   269
                            }
cd41f34e548c 8206001: Enable TLS1.3 by default in Http Client
michaelm
parents: 50681
diff changeset
   270
                        });
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   271
                        connectFlows(connection);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   272
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   273
                        if (debug.on()) debug.log("requestAction.headers");
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   274
                        List<ByteBuffer> data = requestAction.headers();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   275
                        synchronized (lock) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   276
                            state = State.HEADERS;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   277
                        }
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   278
                        if (debug.on()) debug.log("setting outgoing with headers");
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   279
                        assert outgoing.isEmpty() : "Unexpected outgoing:" + outgoing;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   280
                        appendToOutgoing(data);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   281
                        cf.complete(null);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   282
                        return cf;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   283
                    } catch (Throwable t) {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   284
                        if (debug.on()) debug.log("Failed to send headers: %s", t);
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   285
                        headersSentCF.completeExceptionally(t);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   286
                        bodySentCF.completeExceptionally(t);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   287
                        connection.close();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   288
                        cf.completeExceptionally(t);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   289
                        return cf;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   290
                    } })
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   291
                .thenCompose(unused -> headersSentCF);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   292
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   293
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   294
    private void cancelIfFailed(Flow.Subscription s) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   295
        asyncReceiver.whenFinished.whenCompleteAsync((r,t) -> {
50985
cd41f34e548c 8206001: Enable TLS1.3 by default in Http Client
michaelm
parents: 50681
diff changeset
   296
            if (debug.on())
cd41f34e548c 8206001: Enable TLS1.3 by default in Http Client
michaelm
parents: 50681
diff changeset
   297
                debug.log("asyncReceiver finished (failed=%s)", (Object)t);
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   298
            if (t != null) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   299
                s.cancel();
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   300
                // Don't complete exceptionally here as 't'
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   301
                // might not be the right exception: it will
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   302
                // not have been decorated yet.
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   303
                // t is an exception raised by the read side,
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   304
                // an EOFException or Broken Pipe...
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   305
                // We are cancelling the BodyPublisher subscription
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   306
                // and completing bodySentCF to allow the next step
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   307
                // to flow and call readHeaderAsync, which will
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   308
                // get the right exception from the asyncReceiver.
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   309
                bodySentCF.complete(this);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   310
            }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   311
        }, executor);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   312
    }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   313
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   314
    @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   315
    CompletableFuture<ExchangeImpl<T>> sendBodyAsync() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   316
        assert headersSentCF.isDone();
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   317
        if (debug.on()) debug.log("sendBodyAsync");
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   318
        try {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   319
            bodySubscriber = requestAction.continueRequest();
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   320
            if (debug.on()) debug.log("bodySubscriber is %s",
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   321
                    bodySubscriber == null ? null : bodySubscriber.getClass());
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   322
            if (bodySubscriber == null) {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   323
                bodySubscriber = Http1BodySubscriber.completeSubscriber(debug);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   324
                appendToOutgoing(Http1BodySubscriber.COMPLETED);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   325
            } else {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   326
                // start
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   327
                bodySubscriber.whenSubscribed
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   328
                        .thenAccept((s) -> cancelIfFailed(s))
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   329
                        .thenAccept((s) -> requestMoreBody());
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   330
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   331
        } catch (Throwable t) {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   332
            cancelImpl(t);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   333
            bodySentCF.completeExceptionally(t);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   334
        }
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   335
        return Utils.wrapForDebug(debug, "sendBodyAsync", bodySentCF);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   336
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   337
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   338
    @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   339
    CompletableFuture<Response> getResponseAsync(Executor executor) {
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   340
        if (debug.on()) debug.log("reading headers");
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   341
        CompletableFuture<Response> cf = response.readHeadersAsync(executor);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   342
        Throwable cause;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   343
        synchronized (lock) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   344
            operations.add(cf);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   345
            cause = failed;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   346
            failed = null;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   347
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   348
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   349
        if (cause != null) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   350
            Log.logTrace("Http1Exchange: request [{0}/timeout={1}ms]"
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   351
                            + "\n\tCompleting exceptionally with {2}\n",
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   352
                         request.uri(),
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   353
                         request.timeout().isPresent() ?
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   354
                            // calling duration.toMillis() can throw an exception.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   355
                            // this is just debugging, we don't care if it overflows.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   356
                            (request.timeout().get().getSeconds() * 1000
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   357
                             + request.timeout().get().getNano() / 1000000) : -1,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   358
                         cause);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   359
            boolean acknowledged = cf.completeExceptionally(cause);
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   360
            if (debug.on())
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   361
                debug.log(acknowledged ? ("completed response with " + cause)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   362
                          : ("response already completed, ignoring " + cause));
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   363
        }
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   364
        return Utils.wrapForDebug(debug, "getResponseAsync", cf);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   365
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   366
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   367
    @Override
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   368
    CompletableFuture<T> readBodyAsync(BodyHandler<T> handler,
43999
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 43984
diff changeset
   369
                                       boolean returnConnectionToPool,
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   370
                                       Executor executor)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   371
    {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   372
        BodySubscriber<T> bs = handler.apply(new ResponseInfoImpl(response.responseCode(),
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   373
                                                                  response.responseHeaders(),
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   374
                                                                  HTTP_1_1));
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   375
        CompletableFuture<T> bodyCF = response.readBody(bs,
43999
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 43984
diff changeset
   376
                                                        returnConnectionToPool,
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   377
                                                        executor);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   378
        return bodyCF;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   379
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   380
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   381
    @Override
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   382
    CompletableFuture<Void> ignoreBody() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   383
        return response.ignoreBody(executor);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   384
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   385
53300
54aa3ea04fe8 8216974: HttpConnection not returned to the pool after 204 response
dfuchs
parents: 52554
diff changeset
   386
    // Used for those response codes that have no body associated
54aa3ea04fe8 8216974: HttpConnection not returned to the pool after 204 response
dfuchs
parents: 52554
diff changeset
   387
    @Override
54aa3ea04fe8 8216974: HttpConnection not returned to the pool after 204 response
dfuchs
parents: 52554
diff changeset
   388
    public void nullBody(HttpResponse<T> resp, Throwable t) {
54aa3ea04fe8 8216974: HttpConnection not returned to the pool after 204 response
dfuchs
parents: 52554
diff changeset
   389
       response.nullBody(resp, t);
54aa3ea04fe8 8216974: HttpConnection not returned to the pool after 204 response
dfuchs
parents: 52554
diff changeset
   390
    }
54aa3ea04fe8 8216974: HttpConnection not returned to the pool after 204 response
dfuchs
parents: 52554
diff changeset
   391
54aa3ea04fe8 8216974: HttpConnection not returned to the pool after 204 response
dfuchs
parents: 52554
diff changeset
   392
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   393
    ByteBuffer drainLeftOverBytes() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   394
        synchronized (lock) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   395
            asyncReceiver.stop();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   396
            return asyncReceiver.drain(Utils.EMPTY_BYTEBUFFER);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   397
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   398
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   399
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   400
    void released() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   401
        Http1Response<T> resp = this.response;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   402
        if (resp != null) resp.completed();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   403
        asyncReceiver.clear();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   404
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   405
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   406
    void completed() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   407
        Http1Response<T> resp = this.response;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   408
        if (resp != null) resp.completed();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   409
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   410
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   411
    /**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   412
     * Cancel checks to see if request and responseAsync finished already.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   413
     * If not it closes the connection and completes all pending operations
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   414
     */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   415
    @Override
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   416
    void cancel() {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   417
        cancelImpl(new IOException("Request cancelled"));
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   418
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   419
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   420
    /**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   421
     * Cancel checks to see if request and responseAsync finished already.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   422
     * If not it closes the connection and completes all pending operations
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   423
     */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   424
    @Override
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   425
    void cancel(IOException cause) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   426
        cancelImpl(cause);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   427
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   428
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   429
    private void cancelImpl(Throwable cause) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   430
        LinkedList<CompletableFuture<?>> toComplete = null;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   431
        int count = 0;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   432
        Throwable error;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   433
        synchronized (lock) {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   434
            if ((error = failed) == null) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   435
                failed = error = cause;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   436
            }
49944
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   437
            if (debug.on()) {
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   438
                debug.log(request.uri() + ": " + error);
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   439
            }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   440
            if (requestAction != null && requestAction.finished()
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   441
                    && response != null && response.finished()) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   442
                return;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   443
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   444
            writePublisher.writeScheduler.stop();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   445
            if (operations.isEmpty()) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   446
                Log.logTrace("Http1Exchange: request [{0}/timeout={1}ms] no pending operation."
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   447
                                + "\n\tCan''t cancel yet with {2}",
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   448
                             request.uri(),
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   449
                             request.timeout().isPresent() ?
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   450
                                // calling duration.toMillis() can throw an exception.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   451
                                // this is just debugging, we don't care if it overflows.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   452
                                (request.timeout().get().getSeconds() * 1000
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   453
                                 + request.timeout().get().getNano() / 1000000) : -1,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   454
                             cause);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   455
            } else {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   456
                for (CompletableFuture<?> cf : operations) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   457
                    if (!cf.isDone()) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   458
                        if (toComplete == null) toComplete = new LinkedList<>();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   459
                        toComplete.add(cf);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   460
                        count++;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   461
                    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   462
                }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   463
                operations.clear();
44639
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   464
            }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   465
        }
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   466
        try {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   467
            Log.logError("Http1Exchange.cancel: count=" + count);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   468
            if (toComplete != null) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   469
                // We might be in the selector thread in case of timeout, when
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   470
                // the SelectorManager calls purgeTimeoutsAndReturnNextDeadline()
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   471
                // There may or may not be other places that reach here
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   472
                // from the SelectorManager thread, so just make sure we
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   473
                // don't complete any CF from within the selector manager
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   474
                // thread.
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   475
                Executor exec = client.isSelectorThread()
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   476
                        ? executor
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   477
                        : this::runInline;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   478
                Throwable x = error;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   479
                while (!toComplete.isEmpty()) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   480
                    CompletableFuture<?> cf = toComplete.poll();
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   481
                    exec.execute(() -> {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   482
                        if (cf.completeExceptionally(x)) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   483
                            if (debug.on())
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   484
                                debug.log("%s: completed cf with %s", request.uri(), x);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   485
                        }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   486
                    });
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   487
                }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   488
            }
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   489
        } finally {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   490
            connection.close();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   491
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   492
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   493
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   494
    private void runInline(Runnable run) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   495
        assert !client.isSelectorThread();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   496
        run.run();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   497
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   498
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   499
    /** Returns true if this exchange was canceled. */
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   500
    boolean isCanceled() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   501
        synchronized (lock) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   502
            return failed != null;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   503
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   504
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   505
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   506
    /** Returns the cause for which this exchange was canceled, if available. */
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   507
    Throwable getCancelCause() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   508
        synchronized (lock) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   509
            return failed;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   510
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   511
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   512
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   513
    /** Convenience for {@link #appendToOutgoing(DataPair)}, with just a Throwable. */
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   514
    void appendToOutgoing(Throwable throwable) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   515
        appendToOutgoing(new DataPair(null, throwable));
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   516
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   517
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   518
    /** Convenience for {@link #appendToOutgoing(DataPair)}, with just data. */
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   519
    void appendToOutgoing(List<ByteBuffer> item) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   520
        appendToOutgoing(new DataPair(item, null));
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   521
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   522
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   523
    private void appendToOutgoing(DataPair dp) {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   524
        if (debug.on()) debug.log("appending to outgoing " + dp);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   525
        outgoing.add(dp);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   526
        writePublisher.writeScheduler.runOrSchedule();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   527
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   528
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   529
    /** Tells whether, or not, there is any outgoing data that can be published,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   530
     * or if there is an error. */
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   531
    private boolean hasOutgoing() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   532
        return !outgoing.isEmpty();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   533
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   534
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   535
    private void requestMoreBody() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   536
        try {
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   537
            if (debug.on()) debug.log("requesting more request body from the subscriber");
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   538
            bodySubscriber.request(1);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   539
        } catch (Throwable t) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   540
            if (debug.on()) debug.log("Subscription::request failed", t);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   541
            cancelImpl(t);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   542
            bodySentCF.completeExceptionally(t);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   543
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   544
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   545
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   546
    // Invoked only by the publisher
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   547
    // ALL tasks should execute off the Selector-Manager thread
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   548
    /** Returns the next portion of the HTTP request, or the error. */
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   549
    private DataPair getOutgoing() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   550
        final Executor exec = client.theExecutor();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   551
        final DataPair dp = outgoing.pollFirst();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   552
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   553
        if (writePublisher.cancelled) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   554
            if (debug.on()) debug.log("cancelling upstream publisher");
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   555
            if (bodySubscriber != null) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   556
                exec.execute(bodySubscriber::cancelSubscription);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   557
            } else if (debug.on()) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   558
                debug.log("bodySubscriber is null");
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   559
            }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   560
            headersSentCF.completeAsync(() -> this, exec);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   561
            bodySentCF.completeAsync(() -> this, exec);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   562
            return null;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   563
        }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   564
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   565
        if (dp == null)  // publisher has not published anything yet
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   566
            return null;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   567
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   568
        if (dp.throwable != null) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   569
            synchronized (lock) {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   570
                state = State.ERROR;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   571
            }
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   572
            exec.execute(() -> {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   573
                headersSentCF.completeExceptionally(dp.throwable);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   574
                bodySentCF.completeExceptionally(dp.throwable);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   575
                connection.close();
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   576
            });
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   577
            return dp;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   578
        }
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   579
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   580
        switch (state) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   581
            case HEADERS:
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   582
                synchronized (lock) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   583
                    state = State.BODY;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   584
                }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   585
                // completeAsync, since dependent tasks should run in another thread
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   586
                if (debug.on()) debug.log("initiating completion of headersSentCF");
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   587
                headersSentCF.completeAsync(() -> this, exec);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   588
                break;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   589
            case BODY:
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   590
                if (dp.data == Http1BodySubscriber.COMPLETED) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   591
                    synchronized (lock) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   592
                        state = State.COMPLETING;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   593
                    }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   594
                    if (debug.on()) debug.log("initiating completion of bodySentCF");
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   595
                    bodySentCF.completeAsync(() -> this, exec);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   596
                } else {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   597
                    exec.execute(this::requestMoreBody);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   598
                }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   599
                break;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   600
            case INITIAL:
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   601
            case ERROR:
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   602
            case COMPLETING:
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   603
            case COMPLETED:
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   604
            default:
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   605
                assert false : "Unexpected state:" + state;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   606
        }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   607
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   608
        return dp;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   609
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   610
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   611
    /** A Publisher of HTTP/1.1 headers and request body. */
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   612
    final class Http1Publisher implements FlowTube.TubePublisher {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   613
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   614
        final Logger debug = Utils.getDebugLogger(this::dbgString);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   615
        volatile Flow.Subscriber<? super List<ByteBuffer>> subscriber;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   616
        volatile boolean cancelled;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   617
        final Http1WriteSubscription subscription = new Http1WriteSubscription();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   618
        final Demand demand = new Demand();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   619
        final SequentialScheduler writeScheduler =
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   620
                SequentialScheduler.synchronizedScheduler(new WriteTask());
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   621
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   622
        @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   623
        public void subscribe(Flow.Subscriber<? super List<ByteBuffer>> s) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   624
            assert state == State.INITIAL;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   625
            Objects.requireNonNull(s);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   626
            assert subscriber == null;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   627
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   628
            subscriber = s;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   629
            if (debug.on()) debug.log("got subscriber: %s", s);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   630
            s.onSubscribe(subscription);
44639
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   631
        }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   632
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   633
        volatile String dbgTag;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   634
        String dbgString() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   635
            String tag = dbgTag;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   636
            Object flow = connection.getConnectionFlow();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   637
            if (tag == null && flow != null) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   638
                dbgTag = tag = "Http1Publisher(" + flow + ")";
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   639
            } else if (tag == null) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   640
                tag = "Http1Publisher(?)";
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   641
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   642
            return tag;
44639
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   643
        }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   644
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   645
        final class WriteTask implements Runnable {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   646
            @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   647
            public void run() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   648
                assert state != State.COMPLETED : "Unexpected state:" + state;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   649
                if (debug.on()) debug.log("WriteTask");
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   650
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   651
                if (cancelled) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   652
                    if (debug.on()) debug.log("handling cancellation");
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   653
                    writeScheduler.stop();
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   654
                    getOutgoing();
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   655
                    return;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   656
                }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   657
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   658
                if (subscriber == null) {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   659
                    if (debug.on()) debug.log("no subscriber yet");
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   660
                    return;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   661
                }
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   662
                if (debug.on()) debug.log(() -> "hasOutgoing = " + hasOutgoing());
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   663
                while (hasOutgoing() && demand.tryDecrement()) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   664
                    DataPair dp = getOutgoing();
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   665
                    if (dp == null)
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   666
                        break;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   667
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   668
                    if (dp.throwable != null) {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   669
                        if (debug.on()) debug.log("onError");
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   670
                        // Do not call the subscriber's onError, it is not required.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   671
                        writeScheduler.stop();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   672
                    } else {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   673
                        List<ByteBuffer> data = dp.data;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   674
                        if (data == Http1BodySubscriber.COMPLETED) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   675
                            synchronized (lock) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   676
                                assert state == State.COMPLETING : "Unexpected state:" + state;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   677
                                state = State.COMPLETED;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   678
                            }
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   679
                            if (debug.on())
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   680
                                debug.log("completed, stopping %s", writeScheduler);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   681
                            writeScheduler.stop();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   682
                            // Do nothing more. Just do not publish anything further.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   683
                            // The next Subscriber will eventually take over.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   684
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   685
                        } else {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   686
                            if (debug.on())
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   687
                                debug.log("onNext with " + Utils.remaining(data) + " bytes");
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   688
                            subscriber.onNext(data);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   689
                        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   690
                    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   691
                }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   692
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   693
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   694
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   695
        final class Http1WriteSubscription implements Flow.Subscription {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   696
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   697
            @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   698
            public void request(long n) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   699
                if (cancelled)
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   700
                    return;  //no-op
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   701
                demand.increase(n);
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   702
                if (debug.on())
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   703
                    debug.log("subscription request(%d), demand=%s", n, demand);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   704
                writeScheduler.runOrSchedule(client.theExecutor());
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   705
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   706
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   707
            @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   708
            public void cancel() {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   709
                if (debug.on()) debug.log("subscription cancelled");
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   710
                if (cancelled)
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   711
                    return;  //no-op
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   712
                cancelled = true;
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   713
                writeScheduler.runOrSchedule(client.theExecutor());
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   714
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   715
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   716
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   717
52554
5f1ca46703f9 8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents: 51364
diff changeset
   718
    HttpClient client() {
5f1ca46703f9 8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents: 51364
diff changeset
   719
        return client;
5f1ca46703f9 8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents: 51364
diff changeset
   720
    }
5f1ca46703f9 8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents: 51364
diff changeset
   721
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   722
    String dbgString() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   723
        return "Http1Exchange";
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   724
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   725
}