src/java.net.http/share/classes/jdk/internal/net/http/Http1Response.java
author chegar
Mon, 16 Apr 2018 16:44:12 +0100
branchhttp-client-branch
changeset 56437 f8b3f053cfbb
parent 56423 ba64c30666cc
child 56451 9585061fdb04
permissions -rw-r--r--
http-client-branch: review comment - update debug statements as per new internal DebugLogger
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
/*
56079
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents: 55973
diff changeset
     2
 * Copyright (c) 2015, 2018, 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
56092
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
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
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    28
import java.io.EOFException;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    29
import java.lang.System.Logger.Level;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    30
import java.nio.ByteBuffer;
56392
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
    31
import java.util.List;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    32
import java.util.concurrent.CompletableFuture;
56392
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
    33
import java.util.concurrent.CompletionStage;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    34
import java.util.concurrent.Executor;
56392
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
    35
import java.util.concurrent.Flow;
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
    36
import java.util.concurrent.atomic.AtomicBoolean;
56165
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
    37
import java.util.concurrent.atomic.AtomicLong;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    38
import java.util.function.Consumer;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    39
import java.util.function.Function;
56089
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56082
diff changeset
    40
import java.net.http.HttpHeaders;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56082
diff changeset
    41
import java.net.http.HttpResponse;
56092
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    42
import jdk.internal.net.http.ResponseContent.BodyParser;
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    43
import jdk.internal.net.http.common.Log;
56437
f8b3f053cfbb http-client-branch: review comment - update debug statements as per new internal DebugLogger
chegar
parents: 56423
diff changeset
    44
import jdk.internal.net.http.common.Logger;
56092
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    45
import jdk.internal.net.http.common.MinimalFuture;
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    46
import jdk.internal.net.http.common.Utils;
56392
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
    47
56089
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56082
diff changeset
    48
import static java.net.http.HttpClient.Version.HTTP_1_1;
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56165
diff changeset
    49
import static java.net.http.HttpResponse.BodySubscribers.discarding;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    50
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    51
/**
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    52
 * Handles a HTTP/1.1 response (headers + body).
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    53
 * There can be more than one of these per Http 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 Http1Response<T> {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    56
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    57
    private volatile ResponseContent content;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    58
    private final HttpRequestImpl request;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    59
    private Response response;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    60
    private final HttpConnection connection;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    61
    private HttpHeaders headers;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    62
    private int responseCode;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    63
    private final Http1Exchange<T> exchange;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    64
    private boolean return2Cache; // return connection to cache when finished
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    65
    private final HeadersReader headersReader; // used to read the headers
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    66
    private final BodyReader bodyReader; // used to read the body
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    67
    private final Http1AsyncReceiver asyncReceiver;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    68
    private volatile EOFException eof;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    69
    // max number of bytes of (fixed length) body to ignore on redirect
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    70
    private final static int MAX_IGNORE = 1024;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    71
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    72
    // Revisit: can we get rid of this?
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    73
    static enum State {INITIAL, READING_HEADERS, READING_BODY, DONE}
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    74
    private volatile State readProgress = State.INITIAL;
56437
f8b3f053cfbb http-client-branch: review comment - update debug statements as per new internal DebugLogger
chegar
parents: 56423
diff changeset
    75
f8b3f053cfbb http-client-branch: review comment - update debug statements as per new internal DebugLogger
chegar
parents: 56423
diff changeset
    76
    final Logger debug = Utils.getDebugLogger(this::dbgString, Utils.DEBUG);
56165
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
    77
    final static AtomicLong responseCount = new AtomicLong();
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
    78
    final long id = responseCount.incrementAndGet();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    79
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    80
    Http1Response(HttpConnection conn,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    81
                  Http1Exchange<T> exchange,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    82
                  Http1AsyncReceiver asyncReceiver) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    83
        this.readProgress = State.INITIAL;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    84
        this.request = exchange.request();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    85
        this.exchange = exchange;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    86
        this.connection = conn;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    87
        this.asyncReceiver = asyncReceiver;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    88
        headersReader = new HeadersReader(this::advance);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    89
        bodyReader = new BodyReader(this::advance);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    90
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    91
56165
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
    92
    String dbgTag;
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
    93
    private String dbgString() {
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
    94
        String dbg = dbgTag;
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
    95
        if (dbg == null) {
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
    96
            String cdbg = connection.dbgTag;
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
    97
            if (cdbg != null) {
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
    98
                dbgTag = dbg = "Http1Response(id=" + id + ", " + cdbg + ")";
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
    99
            } else {
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
   100
                dbg = "Http1Response(id=" + id + ")";
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
   101
            }
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
   102
        }
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
   103
        return dbg;
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
   104
    }
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
   105
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
   106
    // The ClientRefCountTracker is used to track the state
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
   107
    // of a pending operation. Altough there usually is a single
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
   108
    // point where the operation starts, it may terminate at
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
   109
    // different places.
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
   110
    private final class ClientRefCountTracker {
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
   111
        final HttpClientImpl client = connection.client();
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
   112
        // state & 0x01 != 0 => acquire called
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
   113
        // state & 0x02 != 0 => tryRelease called
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
   114
        byte state;
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
   115
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
   116
        public synchronized void acquire() {
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
   117
            if (state == 0) {
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
   118
                // increment the reference count on the HttpClientImpl
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
   119
                // to prevent the SelectorManager thread from exiting
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
   120
                // until our operation is complete.
56437
f8b3f053cfbb http-client-branch: review comment - update debug statements as per new internal DebugLogger
chegar
parents: 56423
diff changeset
   121
                if (debug.on())
f8b3f053cfbb http-client-branch: review comment - update debug statements as per new internal DebugLogger
chegar
parents: 56423
diff changeset
   122
                    debug.log("Operation started: incrementing ref count for %s", client);
56165
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
   123
                client.reference();
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
   124
                state = 0x01;
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
   125
            } else {
56437
f8b3f053cfbb http-client-branch: review comment - update debug statements as per new internal DebugLogger
chegar
parents: 56423
diff changeset
   126
                if (debug.on())
f8b3f053cfbb http-client-branch: review comment - update debug statements as per new internal DebugLogger
chegar
parents: 56423
diff changeset
   127
                    debug.log("Operation ref count for %s is already %s",
f8b3f053cfbb http-client-branch: review comment - update debug statements as per new internal DebugLogger
chegar
parents: 56423
diff changeset
   128
                              client, ((state & 0x2) == 0x2) ? "released." : "incremented!" );
56165
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
   129
                assert (state & 0x01) == 0 : "reference count already incremented";
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
   130
            }
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
   131
        }
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
   132
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
   133
        public synchronized void tryRelease() {
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
   134
            if (state == 0x01) {
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
   135
                // decrement the reference count on the HttpClientImpl
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
   136
                // to allow the SelectorManager thread to exit if no
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
   137
                // other operation is pending and the facade is no
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
   138
                // longer referenced.
56437
f8b3f053cfbb http-client-branch: review comment - update debug statements as per new internal DebugLogger
chegar
parents: 56423
diff changeset
   139
                if (debug.on())
f8b3f053cfbb http-client-branch: review comment - update debug statements as per new internal DebugLogger
chegar
parents: 56423
diff changeset
   140
                    debug.log("Operation finished: decrementing ref count for %s", client);
56165
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
   141
                client.unreference();
56423
ba64c30666cc http-client-branch: add test coverage and better diagnosis for HttpClientImpl operations ref count
dfuchs
parents: 56399
diff changeset
   142
            } else if (state == 0) {
56437
f8b3f053cfbb http-client-branch: review comment - update debug statements as per new internal DebugLogger
chegar
parents: 56423
diff changeset
   143
                if (debug.on())
f8b3f053cfbb http-client-branch: review comment - update debug statements as per new internal DebugLogger
chegar
parents: 56423
diff changeset
   144
                    debug.log("Operation finished: releasing ref count for %s", client);
56423
ba64c30666cc http-client-branch: add test coverage and better diagnosis for HttpClientImpl operations ref count
dfuchs
parents: 56399
diff changeset
   145
            } else if ((state & 0x02) == 0x02) {
56437
f8b3f053cfbb http-client-branch: review comment - update debug statements as per new internal DebugLogger
chegar
parents: 56423
diff changeset
   146
                if (debug.on())
f8b3f053cfbb http-client-branch: review comment - update debug statements as per new internal DebugLogger
chegar
parents: 56423
diff changeset
   147
                    debug.log("ref count for %s already released", client);
56165
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
   148
            }
56423
ba64c30666cc http-client-branch: add test coverage and better diagnosis for HttpClientImpl operations ref count
dfuchs
parents: 56399
diff changeset
   149
            state |= 0x02;
56165
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
   150
        }
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
   151
    }
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
   152
56437
f8b3f053cfbb http-client-branch: review comment - update debug statements as per new internal DebugLogger
chegar
parents: 56423
diff changeset
   153
    public CompletableFuture<Response> readHeadersAsync(Executor executor) {
f8b3f053cfbb http-client-branch: review comment - update debug statements as per new internal DebugLogger
chegar
parents: 56423
diff changeset
   154
        if (debug.on())
f8b3f053cfbb http-client-branch: review comment - update debug statements as per new internal DebugLogger
chegar
parents: 56423
diff changeset
   155
            debug.log("Reading Headers: (remaining: "
f8b3f053cfbb http-client-branch: review comment - update debug statements as per new internal DebugLogger
chegar
parents: 56423
diff changeset
   156
                      + asyncReceiver.remaining() +") "  + readProgress);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   157
        // with expect continue we will resume reading headers + body.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   158
        asyncReceiver.unsubscribe(bodyReader);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   159
        bodyReader.reset();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   160
        Http1HeaderParser hd = new Http1HeaderParser();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   161
        readProgress = State.READING_HEADERS;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   162
        headersReader.start(hd);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   163
        asyncReceiver.subscribe(headersReader);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   164
        CompletableFuture<State> cf = headersReader.completion();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   165
        assert cf != null : "parsing not started";
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   166
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   167
        Function<State, Response> lambda = (State completed) -> {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   168
                assert completed == State.READING_HEADERS;
56437
f8b3f053cfbb http-client-branch: review comment - update debug statements as per new internal DebugLogger
chegar
parents: 56423
diff changeset
   169
                if (debug.on())
f8b3f053cfbb http-client-branch: review comment - update debug statements as per new internal DebugLogger
chegar
parents: 56423
diff changeset
   170
                    debug.log("Reading Headers: creating Response object;"
f8b3f053cfbb http-client-branch: review comment - update debug statements as per new internal DebugLogger
chegar
parents: 56423
diff changeset
   171
                              + " state is now " + readProgress);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   172
                asyncReceiver.unsubscribe(headersReader);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   173
                responseCode = hd.responseCode();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   174
                headers = hd.headers();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   175
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   176
                response = new Response(request,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   177
                                        exchange.getExchange(),
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   178
                                        headers,
56126
86e628130926 http-client-branch: fixed TLS hostname checking issue, SSL session reuse, and changed HttpResponse to return SSLSession
michaelm
parents: 56092
diff changeset
   179
                                        connection,
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   180
                                        responseCode,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   181
                                        HTTP_1_1);
56253
875dbf6234f2 http-client-branch: set correct request returned by response when redirecting, and add additional test coverage
chegar
parents: 56167
diff changeset
   182
875dbf6234f2 http-client-branch: set correct request returned by response when redirecting, and add additional test coverage
chegar
parents: 56167
diff changeset
   183
                if (Log.headers()) {
875dbf6234f2 http-client-branch: set correct request returned by response when redirecting, and add additional test coverage
chegar
parents: 56167
diff changeset
   184
                    StringBuilder sb = new StringBuilder("RESPONSE HEADERS:\n");
875dbf6234f2 http-client-branch: set correct request returned by response when redirecting, and add additional test coverage
chegar
parents: 56167
diff changeset
   185
                    Log.dumpHeaders(sb, "    ", headers);
875dbf6234f2 http-client-branch: set correct request returned by response when redirecting, and add additional test coverage
chegar
parents: 56167
diff changeset
   186
                    Log.logHeaders(sb.toString());
875dbf6234f2 http-client-branch: set correct request returned by response when redirecting, and add additional test coverage
chegar
parents: 56167
diff changeset
   187
                }
875dbf6234f2 http-client-branch: set correct request returned by response when redirecting, and add additional test coverage
chegar
parents: 56167
diff changeset
   188
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   189
                return response;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   190
            };
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   191
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   192
        if (executor != null) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   193
            return cf.thenApplyAsync(lambda, executor);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   194
        } else {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   195
            return cf.thenApply(lambda);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   196
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   197
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   198
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   199
    private boolean finished;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   200
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   201
    synchronized void completed() {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   202
        finished = true;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   203
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   204
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   205
    synchronized boolean finished() {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   206
        return finished;
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
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   209
    int fixupContentLen(int clen) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   210
        if (request.method().equalsIgnoreCase("HEAD")) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   211
            return 0;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   212
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   213
        if (clen == -1) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   214
            if (headers.firstValue("Transfer-encoding").orElse("")
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   215
                       .equalsIgnoreCase("chunked")) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   216
                return -1;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   217
            }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   218
            return 0;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   219
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   220
        return clen;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   221
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   222
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   223
    /**
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   224
     * Read up to MAX_IGNORE bytes discarding
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   225
     */
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   226
    public CompletableFuture<Void> ignoreBody(Executor executor) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   227
        int clen = (int)headers.firstValueAsLong("Content-Length").orElse(-1);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   228
        if (clen == -1 || clen > MAX_IGNORE) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   229
            connection.close();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   230
            return MinimalFuture.completedFuture(null); // not treating as error
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   231
        } else {
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56165
diff changeset
   232
            return readBody(discarding(), true, executor);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   233
        }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   234
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   235
56392
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   236
    static final Flow.Subscription NOP = new Flow.Subscription() {
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   237
        @Override
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   238
        public void request(long n) { }
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   239
        public void cancel() { }
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   240
    };
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   241
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   242
    /**
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   243
     * The Http1AsyncReceiver ensures that all calls to
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   244
     * the subscriber, including onSubscribe, occur sequentially.
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   245
     * There could however be some race conditions that could happen
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   246
     * in case of unexpected errors thrown at unexpected places, which
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   247
     * may cause onError to be called multiple times.
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   248
     * The Http1BodySubscriber will ensure that the user subscriber
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   249
     * is actually completed only once - and only after it is
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   250
     * subscribed.
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   251
     * @param <U> The type of response.
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   252
     */
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   253
    final static class Http1BodySubscriber<U> implements HttpResponse.BodySubscriber<U> {
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   254
        final HttpResponse.BodySubscriber<U> userSubscriber;
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   255
        final AtomicBoolean completed = new AtomicBoolean();
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   256
        volatile Throwable withError;
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   257
        volatile boolean subscribed;
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   258
        Http1BodySubscriber(HttpResponse.BodySubscriber<U> userSubscriber) {
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   259
            this.userSubscriber = userSubscriber;
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   260
        }
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   261
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   262
        // propagate the error to the user subscriber, even if not
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   263
        // subscribed yet.
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   264
        private void propagateError(Throwable t) {
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   265
            assert t != null;
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   266
            try {
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   267
                // if unsubscribed at this point, it will not
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   268
                // get subscribed later - so do it now and
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   269
                // propagate the error
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   270
                if (subscribed == false) {
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   271
                    subscribed = true;
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   272
                    userSubscriber.onSubscribe(NOP);
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   273
                }
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   274
            } finally  {
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   275
                // if onError throws then there is nothing to do
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   276
                // here: let the caller deal with it by logging
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   277
                // and closing the connection.
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   278
                userSubscriber.onError(t);
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   279
            }
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   280
        }
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   281
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   282
        // complete the subscriber, either normally or exceptionally
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   283
        // ensure that the subscriber is completed only once.
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   284
        private void complete(Throwable t) {
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   285
            if (completed.compareAndSet(false, true)) {
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   286
                t  = withError = Utils.getCompletionCause(t);
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   287
                if (t == null) {
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   288
                    assert subscribed;
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   289
                    try {
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   290
                        userSubscriber.onComplete();
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   291
                    } catch (Throwable x) {
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   292
                        propagateError(t = withError = Utils.getCompletionCause(x));
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   293
                        // rethrow and let the caller deal with it.
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   294
                        // (i.e: log and close the connection)
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   295
                        // arguably we could decide to not throw and let the
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   296
                        // connection be reused since we should have received and
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   297
                        // parsed all the bytes when we reach here.
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   298
                        throw x;
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   299
                    }
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   300
                } else {
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   301
                    propagateError(t);
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   302
                }
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   303
            }
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   304
        }
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   305
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   306
        @Override
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   307
        public CompletionStage<U> getBody() {
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   308
            return userSubscriber.getBody();
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   309
        }
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   310
        @Override
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   311
        public void onSubscribe(Flow.Subscription subscription) {
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   312
            if (!subscribed) {
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   313
                subscribed = true;
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   314
                userSubscriber.onSubscribe(subscription);
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   315
            } else {
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   316
                // could be already subscribed and completed
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   317
                // if an unexpected error occurred before the actual
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   318
                // subscription - though that's not supposed
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   319
                // happen.
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   320
                assert completed.get();
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   321
            }
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   322
        }
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   323
        @Override
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   324
        public void onNext(List<ByteBuffer> item) {
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   325
            assert !completed.get();
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   326
            userSubscriber.onNext(item);
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   327
        }
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   328
        @Override
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   329
        public void onError(Throwable throwable) {
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   330
            complete(throwable);
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   331
        }
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   332
        @Override
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   333
        public void onComplete() {
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   334
            complete(null);
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   335
        }
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   336
    }
56165
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
   337
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   338
    public <U> CompletableFuture<U> readBody(HttpResponse.BodySubscriber<U> p,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   339
                                         boolean return2Cache,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   340
                                         Executor executor) {
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   341
        this.return2Cache = return2Cache;
56392
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   342
        final Http1BodySubscriber<U> subscriber = new Http1BodySubscriber<>(p);
56082
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents: 56079
diff changeset
   343
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents: 56079
diff changeset
   344
        final CompletableFuture<U> cf = new MinimalFuture<>();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   345
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   346
        int clen0 = (int)headers.firstValueAsLong("Content-Length").orElse(-1);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   347
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   348
        final int clen = fixupContentLen(clen0);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   349
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   350
        // expect-continue reads headers and body twice.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   351
        // if we reach here, we must reset the headersReader state.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   352
        asyncReceiver.unsubscribe(headersReader);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   353
        headersReader.reset();
56165
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
   354
        ClientRefCountTracker refCountTracker = new ClientRefCountTracker();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   355
56392
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   356
        // We need to keep hold on the client facade until the
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   357
        // tracker has been incremented.
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   358
        connection.client().reference();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   359
        executor.execute(() -> {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   360
            try {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   361
                content = new ResponseContent(
56392
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   362
                        connection, clen, headers, subscriber,
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   363
                        this::onFinished
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   364
                );
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   365
                if (cf.isCompletedExceptionally()) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   366
                    // if an error occurs during subscription
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   367
                    connection.close();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   368
                    return;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   369
                }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   370
                // increment the reference count on the HttpClientImpl
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   371
                // to prevent the SelectorManager thread from exiting until
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   372
                // the body is fully read.
56165
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
   373
                refCountTracker.acquire();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   374
                bodyReader.start(content.getBodyParser(
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   375
                    (t) -> {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   376
                        try {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   377
                            if (t != null) {
56392
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   378
                                subscriber.onError(t);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   379
                                connection.close();
56392
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   380
                                cf.completeExceptionally(t);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   381
                            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   382
                        } finally {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   383
                            bodyReader.onComplete(t);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   384
                        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   385
                    }));
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   386
                CompletableFuture<State> bodyReaderCF = bodyReader.completion();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   387
                asyncReceiver.subscribe(bodyReader);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   388
                assert bodyReaderCF != null : "parsing not started";
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   389
                // Make sure to keep a reference to asyncReceiver from
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   390
                // within this
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   391
                CompletableFuture<?> trailingOp = bodyReaderCF.whenComplete((s,t) ->  {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   392
                    t = Utils.getCompletionCause(t);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   393
                    try {
56165
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
   394
                        if (t == null) {
56437
f8b3f053cfbb http-client-branch: review comment - update debug statements as per new internal DebugLogger
chegar
parents: 56423
diff changeset
   395
                            if (debug.on()) debug.log("Finished reading body: " + s);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   396
                            assert s == State.READING_BODY;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   397
                        }
56392
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   398
                        if (t != null) {
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   399
                            subscriber.onError(t);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   400
                            cf.completeExceptionally(t);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   401
                        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   402
                    } catch (Throwable x) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   403
                        // not supposed to happen
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   404
                        asyncReceiver.onReadError(x);
56165
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
   405
                    } finally {
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
   406
                        // we're done: release the ref count for
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
   407
                        // the current operation.
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
   408
                        refCountTracker.tryRelease();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   409
                    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   410
                });
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   411
                connection.addTrailingOperation(trailingOp);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   412
            } catch (Throwable t) {
56437
f8b3f053cfbb http-client-branch: review comment - update debug statements as per new internal DebugLogger
chegar
parents: 56423
diff changeset
   413
               if (debug.on()) debug.log("Failed reading body: " + t);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   414
                try {
56392
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   415
                    subscriber.onError(t);
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   416
                    cf.completeExceptionally(t);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   417
                } finally {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   418
                    asyncReceiver.onReadError(t);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   419
                }
56392
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   420
            } finally {
9120556e7163 http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part I: HTTP/1.1)
dfuchs
parents: 56288
diff changeset
   421
                connection.client().unreference();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   422
            }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   423
        });
56165
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
   424
        try {
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
   425
            p.getBody().whenComplete((U u, Throwable t) -> {
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
   426
                if (t == null)
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
   427
                    cf.complete(u);
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
   428
                else
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
   429
                    cf.completeExceptionally(t);
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
   430
            });
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
   431
        } catch (Throwable t) {
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
   432
            cf.completeExceptionally(t);
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
   433
            asyncReceiver.setRetryOnError(false);
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
   434
            asyncReceiver.onReadError(t);
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
   435
        }
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
   436
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
   437
        return cf.whenComplete((s,t) -> {
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
   438
            if (t != null) {
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
   439
                // If an exception occurred, release the
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
   440
                // ref count for the current operation, as
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
   441
                // it may never be triggered otherwise
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
   442
                // (BodySubscriber ofInputStream)
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
   443
                // If there was no exception then the
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
   444
                // ref count will be/have been released when
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
   445
                // the last byte of the response is/was received
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
   446
                refCountTracker.tryRelease();
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56126
diff changeset
   447
            }
56082
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents: 56079
diff changeset
   448
        });
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   449
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   450
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   451
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   452
    private void onFinished() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   453
        asyncReceiver.clear();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   454
        if (return2Cache) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   455
            Log.logTrace("Attempting to return connection to the pool: {0}", connection);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   456
            // TODO: need to do something here?
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   457
            // connection.setAsyncCallbacks(null, null, null);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   458
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   459
            // don't return the connection to the cache if EOF happened.
56437
f8b3f053cfbb http-client-branch: review comment - update debug statements as per new internal DebugLogger
chegar
parents: 56423
diff changeset
   460
            if (debug.on())
f8b3f053cfbb http-client-branch: review comment - update debug statements as per new internal DebugLogger
chegar
parents: 56423
diff changeset
   461
                debug.log(connection.getConnectionFlow() + ": return to HTTP/1.1 pool");
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   462
            connection.closeOrReturnToCache(eof == null ? headers : null);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   463
        }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   464
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   465
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   466
    HttpHeaders responseHeaders() {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   467
        return headers;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   468
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   469
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   470
    int responseCode() {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   471
        return responseCode;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   472
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   473
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   474
// ================ Support for plugging into Http1Receiver   =================
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   475
// ============================================================================
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   476
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   477
    // Callback: Error receiver: Consumer of Throwable.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   478
    void onReadError(Throwable t) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   479
        Log.logError(t);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   480
        Receiver<?> receiver = receiver(readProgress);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   481
        if (t instanceof EOFException) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   482
            debug.log(Level.DEBUG, "onReadError: received EOF");
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   483
            eof = (EOFException) t;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   484
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   485
        CompletableFuture<?> cf = receiver == null ? null : receiver.completion();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   486
        debug.log(Level.DEBUG, () -> "onReadError: cf is "
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   487
                + (cf == null  ? "null"
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   488
                : (cf.isDone() ? "already completed"
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   489
                               : "not yet completed")));
56437
f8b3f053cfbb http-client-branch: review comment - update debug statements as per new internal DebugLogger
chegar
parents: 56423
diff changeset
   490
        if (cf != null) {
f8b3f053cfbb http-client-branch: review comment - update debug statements as per new internal DebugLogger
chegar
parents: 56423
diff changeset
   491
            cf.completeExceptionally(t);
f8b3f053cfbb http-client-branch: review comment - update debug statements as per new internal DebugLogger
chegar
parents: 56423
diff changeset
   492
        } else {
f8b3f053cfbb http-client-branch: review comment - update debug statements as per new internal DebugLogger
chegar
parents: 56423
diff changeset
   493
            debug.log(Level.DEBUG, "onReadError", t);
f8b3f053cfbb http-client-branch: review comment - update debug statements as per new internal DebugLogger
chegar
parents: 56423
diff changeset
   494
        }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   495
        debug.log(Level.DEBUG, () -> "closing connection: cause is " + t);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   496
        connection.close();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   497
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   498
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   499
    // ========================================================================
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   500
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   501
    private State advance(State previous) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   502
        assert readProgress == previous;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   503
        switch(previous) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   504
            case READING_HEADERS:
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   505
                asyncReceiver.unsubscribe(headersReader);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   506
                return readProgress = State.READING_BODY;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   507
            case READING_BODY:
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   508
                asyncReceiver.unsubscribe(bodyReader);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   509
                return readProgress = State.DONE;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   510
            default:
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   511
                throw new InternalError("can't advance from " + previous);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   512
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   513
    }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   514
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   515
    Receiver<?> receiver(State state) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   516
        switch(state) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   517
            case READING_HEADERS: return headersReader;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   518
            case READING_BODY: return bodyReader;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   519
            default: return null;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   520
        }
48083
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
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   524
    static abstract class Receiver<T>
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   525
            implements Http1AsyncReceiver.Http1AsyncDelegate {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   526
        abstract void start(T parser);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   527
        abstract CompletableFuture<State> completion();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   528
        // accepts a buffer from upstream.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   529
        // this should be implemented as a simple call to
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   530
        // accept(ref, parser, cf)
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   531
        public abstract boolean tryAsyncReceive(ByteBuffer buffer);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   532
        public abstract void onReadError(Throwable t);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   533
        // handle a byte buffer received from upstream.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   534
        // this method should set the value of Http1Response.buffer
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   535
        // to ref.get() before beginning parsing.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   536
        abstract void handle(ByteBuffer buf, T parser,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   537
                             CompletableFuture<State> cf);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   538
        // resets this objects state so that it can be reused later on
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   539
        // typically puts the reference to parser and completion to null
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   540
        abstract void reset();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   541
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   542
        // accepts a byte buffer received from upstream
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   543
        // returns true if the buffer is fully parsed and more data can
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   544
        // be accepted, false otherwise.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   545
        final boolean accept(ByteBuffer buf, T parser,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   546
                CompletableFuture<State> cf) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   547
            if (cf == null || parser == null || cf.isDone()) return false;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   548
            handle(buf, parser, cf);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   549
            return !cf.isDone();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   550
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   551
        public abstract void onSubscribe(AbstractSubscription s);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   552
        public abstract AbstractSubscription subscription();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   553
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   554
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   555
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   556
    // Invoked with each new ByteBuffer when reading headers...
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   557
    final class HeadersReader extends Receiver<Http1HeaderParser> {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   558
        final Consumer<State> onComplete;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   559
        volatile Http1HeaderParser parser;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   560
        volatile CompletableFuture<State> cf;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   561
        volatile long count; // bytes parsed (for debug)
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   562
        volatile AbstractSubscription subscription;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   563
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   564
        HeadersReader(Consumer<State> onComplete) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   565
            this.onComplete = onComplete;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   566
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   567
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   568
        @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   569
        public AbstractSubscription subscription() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   570
            return subscription;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   571
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   572
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   573
        @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   574
        public void onSubscribe(AbstractSubscription s) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   575
            this.subscription = s;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   576
            s.request(1);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   577
        }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   578
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   579
        @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   580
        void reset() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   581
            cf = null;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   582
            parser = null;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   583
            count = 0;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   584
            subscription = null;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   585
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   586
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   587
        // Revisit: do we need to support restarting?
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   588
        @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   589
        final void start(Http1HeaderParser hp) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   590
            count = 0;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   591
            cf = new MinimalFuture<>();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   592
            parser = hp;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   593
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   594
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   595
        @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   596
        CompletableFuture<State> completion() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   597
            return cf;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   598
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   599
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   600
        @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   601
        public final boolean tryAsyncReceive(ByteBuffer ref) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   602
            boolean hasDemand = subscription.demand().tryDecrement();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   603
            assert hasDemand;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   604
            boolean needsMore = accept(ref, parser, cf);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   605
            if (needsMore) subscription.request(1);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   606
            return needsMore;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   607
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   608
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   609
        @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   610
        public final void onReadError(Throwable t) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   611
            Http1Response.this.onReadError(t);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   612
        }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   613
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   614
        @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   615
        final void handle(ByteBuffer b,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   616
                          Http1HeaderParser parser,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   617
                          CompletableFuture<State> cf) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   618
            assert cf != null : "parsing not started";
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   619
            assert parser != null : "no parser";
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   620
            try {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   621
                count += b.remaining();
56437
f8b3f053cfbb http-client-branch: review comment - update debug statements as per new internal DebugLogger
chegar
parents: 56423
diff changeset
   622
                if (debug.on())
f8b3f053cfbb http-client-branch: review comment - update debug statements as per new internal DebugLogger
chegar
parents: 56423
diff changeset
   623
                    debug.log("Sending " + b.remaining() + "/" + b.capacity()
f8b3f053cfbb http-client-branch: review comment - update debug statements as per new internal DebugLogger
chegar
parents: 56423
diff changeset
   624
                              + " bytes to header parser");
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   625
                if (parser.parse(b)) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   626
                    count -= b.remaining();
56437
f8b3f053cfbb http-client-branch: review comment - update debug statements as per new internal DebugLogger
chegar
parents: 56423
diff changeset
   627
                    if (debug.on())
f8b3f053cfbb http-client-branch: review comment - update debug statements as per new internal DebugLogger
chegar
parents: 56423
diff changeset
   628
                        debug.log("Parsing headers completed. bytes=" + count);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   629
                    onComplete.accept(State.READING_HEADERS);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   630
                    cf.complete(State.READING_HEADERS);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   631
                }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   632
            } catch (Throwable t) {
56437
f8b3f053cfbb http-client-branch: review comment - update debug statements as per new internal DebugLogger
chegar
parents: 56423
diff changeset
   633
                if (debug.on())
f8b3f053cfbb http-client-branch: review comment - update debug statements as per new internal DebugLogger
chegar
parents: 56423
diff changeset
   634
                    debug.log("Header parser failed to handle buffer: " + t);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   635
                cf.completeExceptionally(t);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   636
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   637
        }
56423
ba64c30666cc http-client-branch: add test coverage and better diagnosis for HttpClientImpl operations ref count
dfuchs
parents: 56399
diff changeset
   638
ba64c30666cc http-client-branch: add test coverage and better diagnosis for HttpClientImpl operations ref count
dfuchs
parents: 56399
diff changeset
   639
        @Override
ba64c30666cc http-client-branch: add test coverage and better diagnosis for HttpClientImpl operations ref count
dfuchs
parents: 56399
diff changeset
   640
        public void close(Throwable error) {
ba64c30666cc http-client-branch: add test coverage and better diagnosis for HttpClientImpl operations ref count
dfuchs
parents: 56399
diff changeset
   641
            // if there's no error nothing to do: the cf should/will
ba64c30666cc http-client-branch: add test coverage and better diagnosis for HttpClientImpl operations ref count
dfuchs
parents: 56399
diff changeset
   642
            // be completed.
ba64c30666cc http-client-branch: add test coverage and better diagnosis for HttpClientImpl operations ref count
dfuchs
parents: 56399
diff changeset
   643
            if (error != null) {
ba64c30666cc http-client-branch: add test coverage and better diagnosis for HttpClientImpl operations ref count
dfuchs
parents: 56399
diff changeset
   644
                CompletableFuture<State> cf = this.cf;
ba64c30666cc http-client-branch: add test coverage and better diagnosis for HttpClientImpl operations ref count
dfuchs
parents: 56399
diff changeset
   645
                if (cf != null) {
56437
f8b3f053cfbb http-client-branch: review comment - update debug statements as per new internal DebugLogger
chegar
parents: 56423
diff changeset
   646
                    if (debug.on())
f8b3f053cfbb http-client-branch: review comment - update debug statements as per new internal DebugLogger
chegar
parents: 56423
diff changeset
   647
                        debug.log("close: completing header parser CF with " + error);
56423
ba64c30666cc http-client-branch: add test coverage and better diagnosis for HttpClientImpl operations ref count
dfuchs
parents: 56399
diff changeset
   648
                    cf.completeExceptionally(error);
ba64c30666cc http-client-branch: add test coverage and better diagnosis for HttpClientImpl operations ref count
dfuchs
parents: 56399
diff changeset
   649
                }
ba64c30666cc http-client-branch: add test coverage and better diagnosis for HttpClientImpl operations ref count
dfuchs
parents: 56399
diff changeset
   650
            }
ba64c30666cc http-client-branch: add test coverage and better diagnosis for HttpClientImpl operations ref count
dfuchs
parents: 56399
diff changeset
   651
        }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   652
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   653
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   654
    // Invoked with each new ByteBuffer when reading bodies...
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   655
    final class BodyReader extends Receiver<BodyParser> {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   656
        final Consumer<State> onComplete;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   657
        volatile BodyParser parser;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   658
        volatile CompletableFuture<State> cf;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   659
        volatile AbstractSubscription subscription;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   660
        BodyReader(Consumer<State> onComplete) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   661
            this.onComplete = onComplete;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   662
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   663
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   664
        @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   665
        void reset() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   666
            parser = null;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   667
            cf = null;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   668
            subscription = null;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   669
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   670
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   671
        // Revisit: do we need to support restarting?
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   672
        @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   673
        final void start(BodyParser parser) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   674
            cf = new MinimalFuture<>();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   675
            this.parser = parser;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   676
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   677
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   678
        @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   679
        CompletableFuture<State> completion() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   680
            return cf;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   681
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   682
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   683
        @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   684
        public final boolean tryAsyncReceive(ByteBuffer b) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   685
            return accept(b, parser, cf);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   686
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   687
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   688
        @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   689
        public final void onReadError(Throwable t) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   690
            Http1Response.this.onReadError(t);
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
        @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   694
        public AbstractSubscription subscription() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   695
            return 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
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   698
        @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   699
        public void onSubscribe(AbstractSubscription s) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   700
            this.subscription = s;
56288
2de1aa88cf06 http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents: 56253
diff changeset
   701
            try {
2de1aa88cf06 http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents: 56253
diff changeset
   702
                parser.onSubscribe(s);
2de1aa88cf06 http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents: 56253
diff changeset
   703
            } catch (Throwable t) {
2de1aa88cf06 http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents: 56253
diff changeset
   704
                cf.completeExceptionally(t);
2de1aa88cf06 http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents: 56253
diff changeset
   705
                throw t;
2de1aa88cf06 http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents: 56253
diff changeset
   706
            }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   707
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   708
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   709
        @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   710
        final void handle(ByteBuffer b,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   711
                          BodyParser parser,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   712
                          CompletableFuture<State> cf) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   713
            assert cf != null : "parsing not started";
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   714
            assert parser != null : "no parser";
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   715
            try {
56437
f8b3f053cfbb http-client-branch: review comment - update debug statements as per new internal DebugLogger
chegar
parents: 56423
diff changeset
   716
                if (debug.on())
f8b3f053cfbb http-client-branch: review comment - update debug statements as per new internal DebugLogger
chegar
parents: 56423
diff changeset
   717
                    debug.log("Sending " + b.remaining() + "/" + b.capacity()
f8b3f053cfbb http-client-branch: review comment - update debug statements as per new internal DebugLogger
chegar
parents: 56423
diff changeset
   718
                              + " bytes to body parser");
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   719
                parser.accept(b);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   720
            } catch (Throwable t) {
56437
f8b3f053cfbb http-client-branch: review comment - update debug statements as per new internal DebugLogger
chegar
parents: 56423
diff changeset
   721
                if (debug.on())
f8b3f053cfbb http-client-branch: review comment - update debug statements as per new internal DebugLogger
chegar
parents: 56423
diff changeset
   722
                    debug.log("Body parser failed to handle buffer: " + t);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   723
                if (!cf.isDone()) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   724
                    cf.completeExceptionally(t);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   725
                }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   726
            }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   727
        }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   728
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   729
        final void onComplete(Throwable closedExceptionally) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   730
            if (cf.isDone()) return;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   731
            if (closedExceptionally != null) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   732
                cf.completeExceptionally(closedExceptionally);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   733
            } else {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   734
                onComplete.accept(State.READING_BODY);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   735
                cf.complete(State.READING_BODY);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   736
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   737
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   738
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   739
        @Override
56423
ba64c30666cc http-client-branch: add test coverage and better diagnosis for HttpClientImpl operations ref count
dfuchs
parents: 56399
diff changeset
   740
        public final void close(Throwable error) {
ba64c30666cc http-client-branch: add test coverage and better diagnosis for HttpClientImpl operations ref count
dfuchs
parents: 56399
diff changeset
   741
            CompletableFuture<State> cf = this.cf;
ba64c30666cc http-client-branch: add test coverage and better diagnosis for HttpClientImpl operations ref count
dfuchs
parents: 56399
diff changeset
   742
            if (cf != null && !cf.isDone()) {
ba64c30666cc http-client-branch: add test coverage and better diagnosis for HttpClientImpl operations ref count
dfuchs
parents: 56399
diff changeset
   743
                // we want to make sure dependent actions are triggered
ba64c30666cc http-client-branch: add test coverage and better diagnosis for HttpClientImpl operations ref count
dfuchs
parents: 56399
diff changeset
   744
                // in order to make sure the client reference count
ba64c30666cc http-client-branch: add test coverage and better diagnosis for HttpClientImpl operations ref count
dfuchs
parents: 56399
diff changeset
   745
                // is decremented
ba64c30666cc http-client-branch: add test coverage and better diagnosis for HttpClientImpl operations ref count
dfuchs
parents: 56399
diff changeset
   746
                if (error != null) {
56437
f8b3f053cfbb http-client-branch: review comment - update debug statements as per new internal DebugLogger
chegar
parents: 56423
diff changeset
   747
                    if (debug.on())
f8b3f053cfbb http-client-branch: review comment - update debug statements as per new internal DebugLogger
chegar
parents: 56423
diff changeset
   748
                        debug.log("close: completing body parser CF with " + error);
56423
ba64c30666cc http-client-branch: add test coverage and better diagnosis for HttpClientImpl operations ref count
dfuchs
parents: 56399
diff changeset
   749
                    cf.completeExceptionally(error);
ba64c30666cc http-client-branch: add test coverage and better diagnosis for HttpClientImpl operations ref count
dfuchs
parents: 56399
diff changeset
   750
                } else {
56437
f8b3f053cfbb http-client-branch: review comment - update debug statements as per new internal DebugLogger
chegar
parents: 56423
diff changeset
   751
                    if (debug.on())
f8b3f053cfbb http-client-branch: review comment - update debug statements as per new internal DebugLogger
chegar
parents: 56423
diff changeset
   752
                        debug.log("close: completing body parser CF");
56423
ba64c30666cc http-client-branch: add test coverage and better diagnosis for HttpClientImpl operations ref count
dfuchs
parents: 56399
diff changeset
   753
                    cf.complete(State.READING_BODY);
ba64c30666cc http-client-branch: add test coverage and better diagnosis for HttpClientImpl operations ref count
dfuchs
parents: 56399
diff changeset
   754
                }
ba64c30666cc http-client-branch: add test coverage and better diagnosis for HttpClientImpl operations ref count
dfuchs
parents: 56399
diff changeset
   755
            }
ba64c30666cc http-client-branch: add test coverage and better diagnosis for HttpClientImpl operations ref count
dfuchs
parents: 56399
diff changeset
   756
        }
ba64c30666cc http-client-branch: add test coverage and better diagnosis for HttpClientImpl operations ref count
dfuchs
parents: 56399
diff changeset
   757
ba64c30666cc http-client-branch: add test coverage and better diagnosis for HttpClientImpl operations ref count
dfuchs
parents: 56399
diff changeset
   758
        @Override
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   759
        public String toString() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   760
            return super.toString() + "/parser=" + String.valueOf(parser);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   761
        }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   762
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   763
}