src/java.net.http/share/classes/jdk/internal/net/http/MultiExchange.java
author mbaesken
Thu, 28 Nov 2019 13:02:39 +0100
changeset 59323 ae2eb76c486d
parent 53701 e57bcfd7bf79
permissions -rw-r--r--
8234821: remove unused functions from libjli Reviewed-by: clanger, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     1
/*
53300
54aa3ea04fe8 8216974: HttpConnection not returned to the pool after 204 response
dfuchs
parents: 52196
diff changeset
     2
 * Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     4
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    10
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    15
 * accompanied this code).
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    16
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    20
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    23
 * questions.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    24
 */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    25
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    26
package jdk.internal.net.http;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    27
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    28
import java.io.IOException;
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
    29
import java.net.ConnectException;
51364
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 50681
diff changeset
    30
import java.net.http.HttpConnectTimeoutException;
53387
c9622e15ba29 8216561: HttpClient: The logic of retry on connect exception is inverted
dfuchs
parents: 53300
diff changeset
    31
import java.time.Duration;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    32
import java.util.Iterator;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    33
import java.util.LinkedList;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    34
import java.security.AccessControlContext;
53387
c9622e15ba29 8216561: HttpClient: The logic of retry on connect exception is inverted
dfuchs
parents: 53300
diff changeset
    35
import java.util.Objects;
c9622e15ba29 8216561: HttpClient: The logic of retry on connect exception is inverted
dfuchs
parents: 53300
diff changeset
    36
import java.util.Optional;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    37
import java.util.concurrent.CompletableFuture;
52196
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents: 51364
diff changeset
    38
import java.util.concurrent.CompletionStage;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    39
import java.util.concurrent.CompletionException;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    40
import java.util.concurrent.ExecutionException;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    41
import java.util.concurrent.Executor;
52196
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents: 51364
diff changeset
    42
import java.util.concurrent.Flow;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    43
import java.util.concurrent.atomic.AtomicInteger;
53387
c9622e15ba29 8216561: HttpClient: The logic of retry on connect exception is inverted
dfuchs
parents: 53300
diff changeset
    44
import java.util.concurrent.atomic.AtomicLong;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    45
import java.util.function.Function;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    46
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    47
import java.net.http.HttpClient;
52196
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents: 51364
diff changeset
    48
import java.net.http.HttpHeaders;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    49
import java.net.http.HttpRequest;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    50
import java.net.http.HttpResponse;
52196
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents: 51364
diff changeset
    51
import java.net.http.HttpResponse.BodySubscriber;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    52
import java.net.http.HttpResponse.PushPromiseHandler;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    53
import java.net.http.HttpTimeoutException;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    54
import jdk.internal.net.http.common.Log;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    55
import jdk.internal.net.http.common.Logger;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    56
import jdk.internal.net.http.common.MinimalFuture;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    57
import jdk.internal.net.http.common.ConnectionExpiredException;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    58
import jdk.internal.net.http.common.Utils;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    59
import static jdk.internal.net.http.common.MinimalFuture.completedFuture;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    60
import static jdk.internal.net.http.common.MinimalFuture.failedFuture;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    61
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    62
/**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    63
 * Encapsulates multiple Exchanges belonging to one HttpRequestImpl.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    64
 * - manages filters
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    65
 * - retries due to filters.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    66
 * - I/O errors and most other exceptions get returned directly to user
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    67
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    68
 * Creates a new Exchange for each request/response interaction
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    69
 */
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    70
class MultiExchange<T> {
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    71
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    72
    static final Logger debug =
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    73
            Utils.getDebugLogger("MultiExchange"::toString, Utils.DEBUG);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    74
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    75
    private final HttpRequest userRequest; // the user request
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    76
    private final HttpRequestImpl request; // a copy of the user request
53387
c9622e15ba29 8216561: HttpClient: The logic of retry on connect exception is inverted
dfuchs
parents: 53300
diff changeset
    77
    private final ConnectTimeoutTracker connectTimeout; // null if no timeout
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    78
    final AccessControlContext acc;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    79
    final HttpClientImpl client;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    80
    final HttpResponse.BodyHandler<T> responseHandler;
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
    81
    final HttpClientImpl.DelegatingExecutor executor;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    82
    final AtomicInteger attempts = new AtomicInteger();
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    83
    HttpRequestImpl currentreq; // used for retries & redirect
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    84
    HttpRequestImpl previousreq; // used for retries & redirect
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    85
    Exchange<T> exchange; // the current exchange
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    86
    Exchange<T> previous;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    87
    volatile Throwable retryCause;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    88
    volatile boolean expiredOnce;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    89
    volatile HttpResponse<T> response = null;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    90
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    91
    // Maximum number of times a request will be retried/redirected
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    92
    // for any reason
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    93
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    94
    static final int DEFAULT_MAX_ATTEMPTS = 5;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    95
    static final int max_attempts = Utils.getIntegerNetProperty(
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    96
            "jdk.httpclient.redirects.retrylimit", DEFAULT_MAX_ATTEMPTS
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    97
    );
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    98
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    99
    private final LinkedList<HeaderFilter> filters;
51364
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 50681
diff changeset
   100
    ResponseTimerEvent responseTimerEvent;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   101
    volatile boolean cancelled;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   102
    final PushGroup<T> pushGroup;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   103
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   104
    /**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   105
     * Filter fields. These are attached as required by filters
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   106
     * and only used by the filter implementations. This could be
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   107
     * generalised into Objects that are passed explicitly to the filters
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   108
     * (one per MultiExchange object, and one per Exchange object possibly)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   109
     */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   110
    volatile AuthenticationFilter.AuthInfo serverauth, proxyauth;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   111
    // RedirectHandler
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   112
    volatile int numberOfRedirects = 0;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   113
53387
c9622e15ba29 8216561: HttpClient: The logic of retry on connect exception is inverted
dfuchs
parents: 53300
diff changeset
   114
    // This class is used to keep track of the connection timeout
c9622e15ba29 8216561: HttpClient: The logic of retry on connect exception is inverted
dfuchs
parents: 53300
diff changeset
   115
    // across retries, when a ConnectException causes a retry.
c9622e15ba29 8216561: HttpClient: The logic of retry on connect exception is inverted
dfuchs
parents: 53300
diff changeset
   116
    // In that case - we will retry the connect, but we don't
c9622e15ba29 8216561: HttpClient: The logic of retry on connect exception is inverted
dfuchs
parents: 53300
diff changeset
   117
    // want to double the timeout by starting a new timer with
c9622e15ba29 8216561: HttpClient: The logic of retry on connect exception is inverted
dfuchs
parents: 53300
diff changeset
   118
    // the full connectTimeout again.
c9622e15ba29 8216561: HttpClient: The logic of retry on connect exception is inverted
dfuchs
parents: 53300
diff changeset
   119
    // Instead we use the ConnectTimeoutTracker to return a new
c9622e15ba29 8216561: HttpClient: The logic of retry on connect exception is inverted
dfuchs
parents: 53300
diff changeset
   120
    // duration that takes into account the time spent in the
c9622e15ba29 8216561: HttpClient: The logic of retry on connect exception is inverted
dfuchs
parents: 53300
diff changeset
   121
    // first connect attempt.
c9622e15ba29 8216561: HttpClient: The logic of retry on connect exception is inverted
dfuchs
parents: 53300
diff changeset
   122
    // If however, the connection gets connected, but we later
c9622e15ba29 8216561: HttpClient: The logic of retry on connect exception is inverted
dfuchs
parents: 53300
diff changeset
   123
    // retry the whole operation, then we reset the timer before
c9622e15ba29 8216561: HttpClient: The logic of retry on connect exception is inverted
dfuchs
parents: 53300
diff changeset
   124
    // retrying (since the connection used for the second request
c9622e15ba29 8216561: HttpClient: The logic of retry on connect exception is inverted
dfuchs
parents: 53300
diff changeset
   125
    // will not necessarily be the same: it could be a new
c9622e15ba29 8216561: HttpClient: The logic of retry on connect exception is inverted
dfuchs
parents: 53300
diff changeset
   126
    // unconnected connection) - see getExceptionalCF().
c9622e15ba29 8216561: HttpClient: The logic of retry on connect exception is inverted
dfuchs
parents: 53300
diff changeset
   127
    private static final class ConnectTimeoutTracker {
c9622e15ba29 8216561: HttpClient: The logic of retry on connect exception is inverted
dfuchs
parents: 53300
diff changeset
   128
        final Duration max;
c9622e15ba29 8216561: HttpClient: The logic of retry on connect exception is inverted
dfuchs
parents: 53300
diff changeset
   129
        final AtomicLong startTime = new AtomicLong();
c9622e15ba29 8216561: HttpClient: The logic of retry on connect exception is inverted
dfuchs
parents: 53300
diff changeset
   130
        ConnectTimeoutTracker(Duration connectTimeout) {
c9622e15ba29 8216561: HttpClient: The logic of retry on connect exception is inverted
dfuchs
parents: 53300
diff changeset
   131
            this.max = Objects.requireNonNull(connectTimeout);
c9622e15ba29 8216561: HttpClient: The logic of retry on connect exception is inverted
dfuchs
parents: 53300
diff changeset
   132
        }
c9622e15ba29 8216561: HttpClient: The logic of retry on connect exception is inverted
dfuchs
parents: 53300
diff changeset
   133
c9622e15ba29 8216561: HttpClient: The logic of retry on connect exception is inverted
dfuchs
parents: 53300
diff changeset
   134
        Duration getRemaining() {
c9622e15ba29 8216561: HttpClient: The logic of retry on connect exception is inverted
dfuchs
parents: 53300
diff changeset
   135
            long now = System.nanoTime();
c9622e15ba29 8216561: HttpClient: The logic of retry on connect exception is inverted
dfuchs
parents: 53300
diff changeset
   136
            long previous = startTime.compareAndExchange(0, now);
c9622e15ba29 8216561: HttpClient: The logic of retry on connect exception is inverted
dfuchs
parents: 53300
diff changeset
   137
            if (previous == 0 || max.isZero()) return max;
c9622e15ba29 8216561: HttpClient: The logic of retry on connect exception is inverted
dfuchs
parents: 53300
diff changeset
   138
            Duration remaining = max.minus(Duration.ofNanos(now - previous));
c9622e15ba29 8216561: HttpClient: The logic of retry on connect exception is inverted
dfuchs
parents: 53300
diff changeset
   139
            assert remaining.compareTo(max) <= 0;
c9622e15ba29 8216561: HttpClient: The logic of retry on connect exception is inverted
dfuchs
parents: 53300
diff changeset
   140
            return remaining.isNegative() ? Duration.ZERO : remaining;
c9622e15ba29 8216561: HttpClient: The logic of retry on connect exception is inverted
dfuchs
parents: 53300
diff changeset
   141
        }
c9622e15ba29 8216561: HttpClient: The logic of retry on connect exception is inverted
dfuchs
parents: 53300
diff changeset
   142
c9622e15ba29 8216561: HttpClient: The logic of retry on connect exception is inverted
dfuchs
parents: 53300
diff changeset
   143
        void reset() { startTime.set(0); }
c9622e15ba29 8216561: HttpClient: The logic of retry on connect exception is inverted
dfuchs
parents: 53300
diff changeset
   144
    }
c9622e15ba29 8216561: HttpClient: The logic of retry on connect exception is inverted
dfuchs
parents: 53300
diff changeset
   145
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   146
    /**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   147
     * MultiExchange with one final response.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   148
     */
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   149
    MultiExchange(HttpRequest userRequest,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   150
                  HttpRequestImpl requestImpl,
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   151
                  HttpClientImpl client,
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   152
                  HttpResponse.BodyHandler<T> responseHandler,
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   153
                  PushPromiseHandler<T> pushPromiseHandler,
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   154
                  AccessControlContext acc) {
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   155
        this.previous = null;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   156
        this.userRequest = userRequest;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   157
        this.request = requestImpl;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   158
        this.currentreq = request;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   159
        this.previousreq = null;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   160
        this.client = client;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   161
        this.filters = client.filterChain();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   162
        this.acc = acc;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   163
        this.executor = client.theExecutor();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   164
        this.responseHandler = responseHandler;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   165
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   166
        if (pushPromiseHandler != null) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   167
            Executor executor = acc == null
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   168
                    ? this.executor.delegate()
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   169
                    : new PrivilegedExecutor(this.executor.delegate(), acc);
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   170
            this.pushGroup = new PushGroup<>(pushPromiseHandler, request, executor);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   171
        } else {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   172
            pushGroup = null;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   173
        }
53387
c9622e15ba29 8216561: HttpClient: The logic of retry on connect exception is inverted
dfuchs
parents: 53300
diff changeset
   174
        this.connectTimeout = client.connectTimeout()
c9622e15ba29 8216561: HttpClient: The logic of retry on connect exception is inverted
dfuchs
parents: 53300
diff changeset
   175
                .map(ConnectTimeoutTracker::new).orElse(null);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   176
        this.exchange = new Exchange<>(request, this);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   177
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   178
51364
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 50681
diff changeset
   179
    synchronized Exchange<T> getExchange() {
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   180
        return exchange;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   181
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   182
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   183
    HttpClientImpl client() {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   184
        return client;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   185
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   186
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   187
    HttpClient.Version version() {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   188
        HttpClient.Version vers = request.version().orElse(client.version());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   189
        if (vers == HttpClient.Version.HTTP_2 && !request.secure() && request.proxy() != null)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   190
            vers = HttpClient.Version.HTTP_1_1;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   191
        return vers;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   192
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   193
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   194
    private synchronized void setExchange(Exchange<T> exchange) {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   195
        if (this.exchange != null && exchange != this.exchange) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   196
            this.exchange.released();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   197
        }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   198
        this.exchange = exchange;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   199
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   200
53387
c9622e15ba29 8216561: HttpClient: The logic of retry on connect exception is inverted
dfuchs
parents: 53300
diff changeset
   201
    public Optional<Duration> remainingConnectTimeout() {
c9622e15ba29 8216561: HttpClient: The logic of retry on connect exception is inverted
dfuchs
parents: 53300
diff changeset
   202
        return Optional.ofNullable(connectTimeout)
c9622e15ba29 8216561: HttpClient: The logic of retry on connect exception is inverted
dfuchs
parents: 53300
diff changeset
   203
                .map(ConnectTimeoutTracker::getRemaining);
c9622e15ba29 8216561: HttpClient: The logic of retry on connect exception is inverted
dfuchs
parents: 53300
diff changeset
   204
    }
c9622e15ba29 8216561: HttpClient: The logic of retry on connect exception is inverted
dfuchs
parents: 53300
diff changeset
   205
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   206
    private void cancelTimer() {
51364
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 50681
diff changeset
   207
        if (responseTimerEvent != null) {
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 50681
diff changeset
   208
            client.cancelTimer(responseTimerEvent);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   209
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   210
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   211
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   212
    private void requestFilters(HttpRequestImpl r) throws IOException {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   213
        Log.logTrace("Applying request filters");
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   214
        for (HeaderFilter filter : filters) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   215
            Log.logTrace("Applying {0}", filter);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   216
            filter.request(r, this);
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
        Log.logTrace("All filters applied");
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
43984
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents: 42460
diff changeset
   221
    private HttpRequestImpl responseFilters(Response response) throws IOException
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   222
    {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   223
        Log.logTrace("Applying response filters");
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   224
        Iterator<HeaderFilter> reverseItr = filters.descendingIterator();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   225
        while (reverseItr.hasNext()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   226
            HeaderFilter filter = reverseItr.next();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   227
            Log.logTrace("Applying {0}", filter);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   228
            HttpRequestImpl newreq = filter.response(response);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   229
            if (newreq != null) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   230
                Log.logTrace("New request: stopping filters");
43984
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents: 42460
diff changeset
   231
                return newreq;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   232
            }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   233
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   234
        Log.logTrace("All filters applied");
43984
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents: 42460
diff changeset
   235
        return null;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   236
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   237
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   238
    public void cancel(IOException cause) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   239
        cancelled = true;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   240
        getExchange().cancel(cause);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   241
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   242
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   243
    public CompletableFuture<HttpResponse<T>> responseAsync(Executor executor) {
43984
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents: 42460
diff changeset
   244
        CompletableFuture<Void> start = new MinimalFuture<>();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   245
        CompletableFuture<HttpResponse<T>> cf = responseAsync0(start);
43984
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents: 42460
diff changeset
   246
        start.completeAsync( () -> null, executor); // trigger execution
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents: 42460
diff changeset
   247
        return cf;
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents: 42460
diff changeset
   248
    }
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents: 42460
diff changeset
   249
52196
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents: 51364
diff changeset
   250
    // return true if the response is a type where a response body is never possible
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents: 51364
diff changeset
   251
    // and therefore doesn't have to include header information which indicates no
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents: 51364
diff changeset
   252
    // body is present. This is distinct from responses that also do not contain
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents: 51364
diff changeset
   253
    // response bodies (possibly ever) but which are required to have content length
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents: 51364
diff changeset
   254
    // info in the header (eg 205). Those cases do not have to be handled specially
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents: 51364
diff changeset
   255
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents: 51364
diff changeset
   256
    private static boolean bodyNotPermitted(Response r) {
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents: 51364
diff changeset
   257
        return r.statusCode == 204;
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents: 51364
diff changeset
   258
    }
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents: 51364
diff changeset
   259
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents: 51364
diff changeset
   260
    private boolean bodyIsPresent(Response r) {
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents: 51364
diff changeset
   261
        HttpHeaders headers = r.headers();
53700
4ce47bc1fb92 8218662: Allow 204 responses with Content-Length:0
chegar
parents: 52196
diff changeset
   262
        if (headers.firstValueAsLong("Content-length").orElse(0L) != 0L)
52196
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents: 51364
diff changeset
   263
            return true;
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents: 51364
diff changeset
   264
        if (headers.firstValue("Transfer-encoding").isPresent())
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents: 51364
diff changeset
   265
            return true;
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents: 51364
diff changeset
   266
        return false;
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents: 51364
diff changeset
   267
    }
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents: 51364
diff changeset
   268
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents: 51364
diff changeset
   269
    // Call the user's body handler to get an empty body object
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents: 51364
diff changeset
   270
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents: 51364
diff changeset
   271
    private CompletableFuture<HttpResponse<T>> handleNoBody(Response r, Exchange<T> exch) {
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents: 51364
diff changeset
   272
        BodySubscriber<T> bs = responseHandler.apply(new ResponseInfoImpl(r.statusCode(),
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents: 51364
diff changeset
   273
                r.headers(), r.version()));
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents: 51364
diff changeset
   274
        bs.onSubscribe(new NullSubscription());
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents: 51364
diff changeset
   275
        bs.onComplete();
53467
97cf88608d76 8217264: HttpClient: Blocking operations in mapper function do not work as documented
dfuchs
parents: 53387
diff changeset
   276
        CompletionStage<T> cs = ResponseSubscribers.getBodyAsync(executor, bs);
52196
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents: 51364
diff changeset
   277
        MinimalFuture<HttpResponse<T>> result = new MinimalFuture<>();
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents: 51364
diff changeset
   278
        cs.whenComplete((nullBody, exception) -> {
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents: 51364
diff changeset
   279
            if (exception != null)
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents: 51364
diff changeset
   280
                result.completeExceptionally(exception);
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents: 51364
diff changeset
   281
            else {
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents: 51364
diff changeset
   282
                this.response =
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents: 51364
diff changeset
   283
                        new HttpResponseImpl<>(r.request(), r, this.response, nullBody, exch);
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents: 51364
diff changeset
   284
                result.complete(this.response);
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents: 51364
diff changeset
   285
            }
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents: 51364
diff changeset
   286
        });
53300
54aa3ea04fe8 8216974: HttpConnection not returned to the pool after 204 response
dfuchs
parents: 52196
diff changeset
   287
        // ensure that the connection is closed or returned to the pool.
54aa3ea04fe8 8216974: HttpConnection not returned to the pool after 204 response
dfuchs
parents: 52196
diff changeset
   288
        return result.whenComplete(exch::nullBody);
52196
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents: 51364
diff changeset
   289
    }
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents: 51364
diff changeset
   290
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   291
    private CompletableFuture<HttpResponse<T>>
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   292
    responseAsync0(CompletableFuture<Void> start) {
43984
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents: 42460
diff changeset
   293
        return start.thenCompose( v -> responseAsyncImpl())
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   294
                    .thenCompose((Response r) -> {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   295
                        Exchange<T> exch = getExchange();
52196
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents: 51364
diff changeset
   296
                        if (bodyNotPermitted(r)) {
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents: 51364
diff changeset
   297
                            if (bodyIsPresent(r)) {
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents: 51364
diff changeset
   298
                                IOException ioe = new IOException(
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents: 51364
diff changeset
   299
                                    "unexpected content length header with 204 response");
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents: 51364
diff changeset
   300
                                exch.cancel(ioe);
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents: 51364
diff changeset
   301
                                return MinimalFuture.failedFuture(ioe);
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents: 51364
diff changeset
   302
                            } else
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents: 51364
diff changeset
   303
                                return handleNoBody(r, exch);
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents: 51364
diff changeset
   304
                        }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   305
                        return exch.readBodyAsync(responseHandler)
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   306
                            .thenApply((T body) -> {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   307
                                this.response =
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   308
                                    new HttpResponseImpl<>(r.request(), r, this.response, body, exch);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   309
                                return this.response;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   310
                            });
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   311
                    });
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   312
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   313
52196
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents: 51364
diff changeset
   314
    static class NullSubscription implements Flow.Subscription {
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents: 51364
diff changeset
   315
        @Override
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents: 51364
diff changeset
   316
        public void request(long n) {
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents: 51364
diff changeset
   317
        }
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents: 51364
diff changeset
   318
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents: 51364
diff changeset
   319
        @Override
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents: 51364
diff changeset
   320
        public void cancel() {
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents: 51364
diff changeset
   321
        }
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents: 51364
diff changeset
   322
    }
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents: 51364
diff changeset
   323
43984
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents: 42460
diff changeset
   324
    private CompletableFuture<Response> responseAsyncImpl() {
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   325
        CompletableFuture<Response> cf;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   326
        if (attempts.incrementAndGet() > max_attempts) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   327
            cf = failedFuture(new IOException("Too many retries", retryCause));
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   328
        } else {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   329
            if (currentreq.timeout().isPresent()) {
51364
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 50681
diff changeset
   330
                responseTimerEvent = ResponseTimerEvent.of(this);
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 50681
diff changeset
   331
                client.registerTimer(responseTimerEvent);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   332
            }
43984
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents: 42460
diff changeset
   333
            try {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   334
                // 1. apply request filters
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   335
                // if currentreq == previousreq the filters have already
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   336
                // been applied once. Applying them a second time might
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   337
                // cause some headers values to be added twice: for
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   338
                // instance, the same cookie might be added again.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   339
                if (currentreq != previousreq) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   340
                    requestFilters(currentreq);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   341
                }
43984
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents: 42460
diff changeset
   342
            } catch (IOException e) {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   343
                return failedFuture(e);
43984
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents: 42460
diff changeset
   344
            }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   345
            Exchange<T> exch = getExchange();
43984
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents: 42460
diff changeset
   346
            // 2. get response
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents: 42460
diff changeset
   347
            cf = exch.responseAsync()
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   348
                     .thenCompose((Response response) -> {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   349
                        HttpRequestImpl newrequest;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   350
                        try {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   351
                            // 3. apply response filters
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   352
                            newrequest = responseFilters(response);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   353
                        } catch (IOException e) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   354
                            return failedFuture(e);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   355
                        }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   356
                        // 4. check filter result and repeat or continue
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   357
                        if (newrequest == null) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   358
                            if (attempts.get() > 1) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   359
                                Log.logError("Succeeded on attempt: " + attempts);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   360
                            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   361
                            return completedFuture(response);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   362
                        } else {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   363
                            this.response =
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   364
                                new HttpResponseImpl<>(currentreq, response, this.response, null, exch);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   365
                            Exchange<T> oldExch = exch;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   366
                            return exch.ignoreBody().handle((r,t) -> {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   367
                                previousreq = currentreq;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   368
                                currentreq = newrequest;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   369
                                expiredOnce = false;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   370
                                setExchange(new Exchange<>(currentreq, this, acc));
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   371
                                return responseAsyncImpl();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   372
                            }).thenCompose(Function.identity());
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   373
                        } })
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   374
                     .handle((response, ex) -> {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   375
                        // 5. handle errors and cancel any timer set
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   376
                        cancelTimer();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   377
                        if (ex == null) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   378
                            assert response != null;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   379
                            return completedFuture(response);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   380
                        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   381
                        // all exceptions thrown are handled here
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   382
                        CompletableFuture<Response> errorCF = getExceptionalCF(ex);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   383
                        if (errorCF == null) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   384
                            return responseAsyncImpl();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   385
                        } else {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   386
                            return errorCF;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   387
                        } })
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   388
                     .thenCompose(Function.identity());
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   389
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   390
        return cf;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   391
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   392
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   393
    private static boolean retryPostValue() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   394
        String s = Utils.getNetProperty("jdk.httpclient.enableAllMethodRetry");
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   395
        if (s == null)
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   396
            return false;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   397
        return s.isEmpty() ? true : Boolean.parseBoolean(s);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   398
    }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   399
53387
c9622e15ba29 8216561: HttpClient: The logic of retry on connect exception is inverted
dfuchs
parents: 53300
diff changeset
   400
    private static boolean disableRetryConnect() {
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   401
        String s = Utils.getNetProperty("jdk.httpclient.disableRetryConnect");
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   402
        if (s == null)
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   403
            return false;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   404
        return s.isEmpty() ? true : Boolean.parseBoolean(s);
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   405
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   406
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   407
    /** True if ALL ( even non-idempotent ) requests can be automatic retried. */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   408
    private static final boolean RETRY_ALWAYS = retryPostValue();
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   409
    /** True if ConnectException should cause a retry. Enabled by default */
53387
c9622e15ba29 8216561: HttpClient: The logic of retry on connect exception is inverted
dfuchs
parents: 53300
diff changeset
   410
    private static final boolean RETRY_CONNECT = !disableRetryConnect();
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   411
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   412
    /** Returns true is given request has an idempotent method. */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   413
    private static boolean isIdempotentRequest(HttpRequest request) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   414
        String method = request.method();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   415
        switch (method) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   416
            case "GET" :
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   417
            case "HEAD" :
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   418
                return true;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   419
            default :
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   420
                return false;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   421
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   422
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   423
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   424
    /** Returns true if the given request can be automatically retried. */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   425
    private static boolean canRetryRequest(HttpRequest request) {
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   426
        if (RETRY_ALWAYS)
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   427
            return true;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   428
        if (isIdempotentRequest(request))
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   429
            return true;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   430
        return false;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   431
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   432
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   433
    private boolean retryOnFailure(Throwable t) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   434
        return t instanceof ConnectionExpiredException
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   435
                || (RETRY_CONNECT && (t instanceof ConnectException));
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   436
    }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   437
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   438
    private Throwable retryCause(Throwable t) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   439
        Throwable cause = t instanceof ConnectionExpiredException ? t.getCause() : t;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   440
        return cause == null ? t : cause;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   441
    }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   442
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   443
    /**
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   444
     * Takes a Throwable and returns a suitable CompletableFuture that is
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   445
     * completed exceptionally, or null.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   446
     */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   447
    private CompletableFuture<Response> getExceptionalCF(Throwable t) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   448
        if ((t instanceof CompletionException) || (t instanceof ExecutionException)) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   449
            if (t.getCause() != null) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   450
                t = t.getCause();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   451
            }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   452
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   453
        if (cancelled && t instanceof IOException) {
51364
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 50681
diff changeset
   454
            if (!(t instanceof HttpTimeoutException)) {
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 50681
diff changeset
   455
                t = toTimeoutException((IOException)t);
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 50681
diff changeset
   456
            }
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   457
        } else if (retryOnFailure(t)) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   458
            Throwable cause = retryCause(t);
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   459
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   460
            if (!(t instanceof ConnectException)) {
53387
c9622e15ba29 8216561: HttpClient: The logic of retry on connect exception is inverted
dfuchs
parents: 53300
diff changeset
   461
                // we may need to start a new connection, and if so
c9622e15ba29 8216561: HttpClient: The logic of retry on connect exception is inverted
dfuchs
parents: 53300
diff changeset
   462
                // we want to start with a fresh connect timeout again.
c9622e15ba29 8216561: HttpClient: The logic of retry on connect exception is inverted
dfuchs
parents: 53300
diff changeset
   463
                if (connectTimeout != null) connectTimeout.reset();
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   464
                if (!canRetryRequest(currentreq)) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   465
                    return failedFuture(cause); // fails with original cause
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   466
                }
53387
c9622e15ba29 8216561: HttpClient: The logic of retry on connect exception is inverted
dfuchs
parents: 53300
diff changeset
   467
            } // ConnectException: retry, but don't reset the connectTimeout.
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   468
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   469
            // allow the retry mechanism to do its work
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   470
            retryCause = cause;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   471
            if (!expiredOnce) {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   472
                if (debug.on())
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   473
                    debug.log(t.getClass().getSimpleName() + " (async): retrying...", t);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   474
                expiredOnce = true;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   475
                // The connection was abruptly closed.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   476
                // We return null to retry the same request a second time.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   477
                // The request filters have already been applied to the
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   478
                // currentreq, so we set previousreq = currentreq to
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   479
                // prevent them from being applied again.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   480
                previousreq = currentreq;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   481
                return null;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   482
            } else {
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   483
                if (debug.on()) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   484
                    debug.log(t.getClass().getSimpleName()
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   485
                            + " (async): already retried once.", t);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   486
                }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   487
                t = cause;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   488
            }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   489
        }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   490
        return failedFuture(t);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   491
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   492
51364
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 50681
diff changeset
   493
    private HttpTimeoutException toTimeoutException(IOException ioe) {
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 50681
diff changeset
   494
        HttpTimeoutException t = null;
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 50681
diff changeset
   495
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 50681
diff changeset
   496
        // more specific, "request timed out", when connected
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 50681
diff changeset
   497
        Exchange<?> exchange = getExchange();
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 50681
diff changeset
   498
        if (exchange != null) {
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 50681
diff changeset
   499
            ExchangeImpl<?> exchangeImpl = exchange.exchImpl;
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 50681
diff changeset
   500
            if (exchangeImpl != null) {
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 50681
diff changeset
   501
                if (exchangeImpl.connection().connected()) {
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 50681
diff changeset
   502
                    t = new HttpTimeoutException("request timed out");
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 50681
diff changeset
   503
                    t.initCause(ioe);
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 50681
diff changeset
   504
                }
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 50681
diff changeset
   505
            }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   506
        }
51364
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 50681
diff changeset
   507
        if (t == null) {
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 50681
diff changeset
   508
            t = new HttpConnectTimeoutException("HTTP connect timed out");
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 50681
diff changeset
   509
            t.initCause(new ConnectException("HTTP connect timed out"));
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   510
        }
51364
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 50681
diff changeset
   511
        return t;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   512
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   513
}