src/java.net.http/share/classes/jdk/internal/net/http/Exchange.java
author mbaesken
Thu, 28 Nov 2019 13:02:39 +0100
changeset 59323 ae2eb76c486d
parent 53387 c9622e15ba29
permissions -rw-r--r--
8234821: remove unused functions from libjli Reviewed-by: clanger, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     1
/*
53300
54aa3ea04fe8 8216974: HttpConnection not returned to the pool after 204 response
dfuchs
parents: 52554
diff changeset
     2
 * Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     4
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    10
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    15
 * accompanied this code).
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    16
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    20
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    23
 * questions.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    24
 */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    25
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
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;
48083
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.net.InetSocketAddress;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    31
import java.net.ProxySelector;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    32
import java.net.URI;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    33
import java.net.URISyntaxException;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    34
import java.net.URLPermission;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    35
import java.security.AccessControlContext;
53387
c9622e15ba29 8216561: HttpClient: The logic of retry on connect exception is inverted
dfuchs
parents: 53300
diff changeset
    36
import java.time.Duration;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    37
import java.util.List;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    38
import java.util.Map;
53387
c9622e15ba29 8216561: HttpClient: The logic of retry on connect exception is inverted
dfuchs
parents: 53300
diff changeset
    39
import java.util.Optional;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    40
import java.util.concurrent.CompletableFuture;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    41
import java.util.concurrent.Executor;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
    42
import java.util.function.Function;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
    43
import java.net.http.HttpClient;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
    44
import java.net.http.HttpHeaders;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
    45
import java.net.http.HttpResponse;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
    46
import java.net.http.HttpTimeoutException;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    47
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
    48
import jdk.internal.net.http.common.Logger;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
    49
import jdk.internal.net.http.common.MinimalFuture;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
    50
import jdk.internal.net.http.common.Utils;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
    51
import jdk.internal.net.http.common.Log;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
    52
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
    53
import static jdk.internal.net.http.common.Utils.permissionForProxy;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    54
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    55
/**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    56
 * One request/response exchange (handles 100/101 intermediate response also).
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    57
 * depth field used to track number of times a new request is being sent
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    58
 * for a given API request. If limit exceeded exception is thrown.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    59
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    60
 * Security check is performed here:
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    61
 * - uses AccessControlContext captured at API level
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    62
 * - checks for appropriate URLPermission for request
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    63
 * - if permission allowed, grants equivalent SocketPermission to call
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    64
 * - in case of direct HTTP proxy, checks additionally for access to proxy
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    65
 *    (CONNECT proxying uses its own Exchange, so check done there)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    66
 *
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
final class Exchange<T> {
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: 48376
diff changeset
    70
    final Logger debug = Utils.getDebugLogger(this::dbgString, Utils.DEBUG);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    71
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    72
    final HttpRequestImpl request;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    73
    final HttpClientImpl client;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    74
    volatile ExchangeImpl<T> exchImpl;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    75
    volatile CompletableFuture<? extends ExchangeImpl<T>> exchangeCF;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
    76
    volatile CompletableFuture<Void> bodyIgnored;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
    77
44639
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
    78
    // used to record possible cancellation raised before the exchImpl
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
    79
    // has been established.
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
    80
    private volatile IOException failed;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    81
    final AccessControlContext acc;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
    82
    final MultiExchange<T> multi;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    83
    final Executor parentExecutor;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    84
    boolean upgrading; // to HTTP/2
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
    85
    final PushGroup<T> pushGroup;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    86
    final String dbgTag;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    87
51364
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 50985
diff changeset
    88
    // Keeps track of the underlying connection when establishing an HTTP/2
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 50985
diff changeset
    89
    // exchange so that it can be aborted/timed out mid setup.
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 50985
diff changeset
    90
    final ConnectionAborter connectionAborter = new ConnectionAborter();
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 50985
diff changeset
    91
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
    92
    Exchange(HttpRequestImpl request, MultiExchange<T> multi) {
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    93
        this.request = request;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    94
        this.upgrading = false;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    95
        this.client = multi.client();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    96
        this.multi = multi;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    97
        this.acc = multi.acc;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    98
        this.parentExecutor = multi.executor;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    99
        this.pushGroup = multi.pushGroup;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   100
        this.dbgTag = "Exchange";
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   101
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   102
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   103
    /* If different AccessControlContext to be used  */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   104
    Exchange(HttpRequestImpl request,
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   105
             MultiExchange<T> multi,
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   106
             AccessControlContext acc)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   107
    {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   108
        this.request = request;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   109
        this.acc = acc;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   110
        this.upgrading = false;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   111
        this.client = multi.client();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   112
        this.multi = multi;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   113
        this.parentExecutor = multi.executor;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   114
        this.pushGroup = multi.pushGroup;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   115
        this.dbgTag = "Exchange";
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   116
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   117
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   118
    PushGroup<T> getPushGroup() {
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   119
        return pushGroup;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   120
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   121
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   122
    Executor executor() {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   123
        return parentExecutor;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   124
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   125
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   126
    public HttpRequestImpl request() {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   127
        return request;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   128
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   129
53387
c9622e15ba29 8216561: HttpClient: The logic of retry on connect exception is inverted
dfuchs
parents: 53300
diff changeset
   130
    public Optional<Duration> remainingConnectTimeout() {
c9622e15ba29 8216561: HttpClient: The logic of retry on connect exception is inverted
dfuchs
parents: 53300
diff changeset
   131
        return multi.remainingConnectTimeout();
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
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   134
    HttpClientImpl client() {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   135
        return client;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   136
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   137
51364
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 50985
diff changeset
   138
    // Keeps track of the underlying connection when establishing an HTTP/2
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 50985
diff changeset
   139
    // exchange so that it can be aborted/timed out mid setup.
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 50985
diff changeset
   140
    static final class ConnectionAborter {
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 50985
diff changeset
   141
        private volatile HttpConnection connection;
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 50985
diff changeset
   142
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 50985
diff changeset
   143
        void connection(HttpConnection connection) {
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 50985
diff changeset
   144
            this.connection = connection;
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 50985
diff changeset
   145
        }
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 50985
diff changeset
   146
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 50985
diff changeset
   147
        void closeConnection() {
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 50985
diff changeset
   148
            HttpConnection connection = this.connection;
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 50985
diff changeset
   149
            this.connection = null;
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 50985
diff changeset
   150
            if (connection != null) {
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 50985
diff changeset
   151
                try {
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 50985
diff changeset
   152
                    connection.close();
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 50985
diff changeset
   153
                } catch (Throwable t) {
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 50985
diff changeset
   154
                    // ignore
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 50985
diff changeset
   155
                }
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 50985
diff changeset
   156
            }
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 50985
diff changeset
   157
        }
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 50985
diff changeset
   158
    }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   159
53300
54aa3ea04fe8 8216974: HttpConnection not returned to the pool after 204 response
dfuchs
parents: 52554
diff changeset
   160
    // Called for 204 response - when no body is permitted
54aa3ea04fe8 8216974: HttpConnection not returned to the pool after 204 response
dfuchs
parents: 52554
diff changeset
   161
    // This is actually only needed for HTTP/1.1 in order
54aa3ea04fe8 8216974: HttpConnection not returned to the pool after 204 response
dfuchs
parents: 52554
diff changeset
   162
    // to return the connection to the pool (or close it)
54aa3ea04fe8 8216974: HttpConnection not returned to the pool after 204 response
dfuchs
parents: 52554
diff changeset
   163
    void nullBody(HttpResponse<T> resp, Throwable t) {
54aa3ea04fe8 8216974: HttpConnection not returned to the pool after 204 response
dfuchs
parents: 52554
diff changeset
   164
        exchImpl.nullBody(resp, t);
54aa3ea04fe8 8216974: HttpConnection not returned to the pool after 204 response
dfuchs
parents: 52554
diff changeset
   165
    }
54aa3ea04fe8 8216974: HttpConnection not returned to the pool after 204 response
dfuchs
parents: 52554
diff changeset
   166
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   167
    public CompletableFuture<T> readBodyAsync(HttpResponse.BodyHandler<T> handler) {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   168
        // If we received a 407 while establishing the exchange
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   169
        // there will be no body to read: bodyIgnored will be true,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   170
        // and exchImpl will be null (if we were trying to establish
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   171
        // an HTTP/2 tunnel through an HTTP/1.1 proxy)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   172
        if (bodyIgnored != null) return MinimalFuture.completedFuture(null);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   173
43999
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 43984
diff changeset
   174
        // The connection will not be returned to the pool in the case of WebSocket
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   175
        return exchImpl.readBodyAsync(handler, !request.isWebSocket(), parentExecutor)
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   176
                .whenComplete((r,t) -> exchImpl.completed());
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   177
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   178
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   179
    /**
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   180
     * Called after a redirect or similar kind of retry where a body might
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   181
     * be sent but we don't want it. Should send a RESET in h2. For http/1.1
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   182
     * we can consume small quantity of data, or close the connection in
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   183
     * other cases.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   184
     */
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   185
    public CompletableFuture<Void> ignoreBody() {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   186
        if (bodyIgnored != null) return bodyIgnored;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   187
        return exchImpl.ignoreBody();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   188
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   189
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   190
    /**
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   191
     * Called when a new exchange is created to replace this exchange.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   192
     * At this point it is guaranteed that readBody/readBodyAsync will
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   193
     * not be called.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   194
     */
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   195
    public void released() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   196
        ExchangeImpl<?> impl = exchImpl;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   197
        if (impl != null) impl.released();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   198
        // Don't set exchImpl to null here. We need to keep
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   199
        // it alive until it's replaced by a Stream in wrapForUpgrade.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   200
        // Setting it to null here might get it GC'ed too early, because
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   201
        // the Http1Response is now only weakly referenced by the Selector.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   202
    }
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
    public void cancel() {
44639
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   205
        // cancel can be called concurrently before or at the same time
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   206
        // that the exchange impl is being established.
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   207
        // In that case we won't be able to propagate the cancellation
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   208
        // right away
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   209
        if (exchImpl != null) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   210
            exchImpl.cancel();
44639
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   211
        } else {
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   212
            // no impl - can't cancel impl yet.
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   213
            // call cancel(IOException) instead which takes care
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   214
            // of race conditions between impl/cancel.
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   215
            cancel(new IOException("Request cancelled"));
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   216
        }
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
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   219
    public void cancel(IOException cause) {
51364
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 50985
diff changeset
   220
        if (debug.on()) debug.log("cancel exchImpl: %s, with \"%s\"", exchImpl, cause);
44639
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   221
        // If the impl is non null, propagate the exception right away.
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   222
        // Otherwise record it so that it can be propagated once the
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   223
        // exchange impl has been established.
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   224
        ExchangeImpl<?> impl = exchImpl;
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   225
        if (impl != null) {
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   226
            // propagate the exception to the impl
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   227
            if (debug.on()) debug.log("Cancelling exchImpl: %s", exchImpl);
44639
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   228
            impl.cancel(cause);
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   229
        } else {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   230
            // no impl yet. record the exception
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   231
            failed = cause;
51364
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 50985
diff changeset
   232
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 50985
diff changeset
   233
            // abort/close the connection if setting up the exchange. This can
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 50985
diff changeset
   234
            // be important when setting up HTTP/2
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 50985
diff changeset
   235
            connectionAborter.closeConnection();
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents: 50985
diff changeset
   236
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   237
            // now call checkCancelled to recheck the impl.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   238
            // if the failed state is set and the impl is not null, reset
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   239
            // the failed state and propagate the exception to the impl.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   240
            checkCancelled();
44639
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   241
        }
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   242
    }
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   243
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   244
    // This method will raise an exception if one was reported and if
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   245
    // it is possible to do so. If the exception can be raised, then
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   246
    // the failed state will be reset. Otherwise, the failed state
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   247
    // will persist until the exception can be raised and the failed state
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   248
    // can be cleared.
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   249
    // Takes care of possible race conditions.
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   250
    private void checkCancelled() {
44639
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   251
        ExchangeImpl<?> impl = null;
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   252
        IOException cause = null;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   253
        CompletableFuture<? extends ExchangeImpl<T>> cf = null;
44639
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   254
        if (failed != null) {
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   255
            synchronized(this) {
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   256
                cause = failed;
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   257
                impl = exchImpl;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   258
                cf = exchangeCF;
44639
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   259
            }
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   260
        }
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   261
        if (cause == null) return;
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   262
        if (impl != null) {
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   263
            // The exception is raised by propagating it to the impl.
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   264
            if (debug.on()) debug.log("Cancelling exchImpl: %s", impl);
44639
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   265
            impl.cancel(cause);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   266
            failed = null;
44639
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   267
        } else {
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   268
            Log.logTrace("Exchange: request [{0}/timeout={1}ms] no impl is set."
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   269
                         + "\n\tCan''t cancel yet with {2}",
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   270
                         request.uri(),
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   271
                         request.timeout().isPresent() ?
44639
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   272
                         // calling duration.toMillis() can throw an exception.
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   273
                         // this is just debugging, we don't care if it overflows.
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   274
                         (request.timeout().get().getSeconds() * 1000
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   275
                          + request.timeout().get().getNano() / 1000000) : -1,
44639
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   276
                         cause);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   277
            if (cf != null) cf.completeExceptionally(cause);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   278
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   279
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   280
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   281
    public void h2Upgrade() {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   282
        upgrading = true;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   283
        request.setH2Upgrade(client.client2());
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   284
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   285
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   286
    synchronized IOException getCancelCause() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   287
        return failed;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   288
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   289
44639
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   290
    // get/set the exchange impl, solving race condition issues with
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   291
    // potential concurrent calls to cancel() or cancel(IOException)
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   292
    private CompletableFuture<? extends ExchangeImpl<T>>
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   293
    establishExchange(HttpConnection connection) {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   294
        if (debug.on()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   295
            debug.log("establishing exchange for %s,%n\t proxy=%s",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   296
                      request, request.proxy());
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   297
        }
44639
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   298
        // check if we have been cancelled first.
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   299
        Throwable t = getCancelCause();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   300
        checkCancelled();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   301
        if (t != null) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   302
            return MinimalFuture.failedFuture(t);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   303
        }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   304
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   305
        CompletableFuture<? extends ExchangeImpl<T>> cf, res;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   306
        cf = ExchangeImpl.get(this, connection);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   307
        // We should probably use a VarHandle to get/set exchangeCF
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   308
        // instead - as we need CAS semantics.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   309
        synchronized (this) { exchangeCF = cf; };
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   310
        res = cf.whenComplete((r,x) -> {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   311
            synchronized(Exchange.this) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   312
                if (exchangeCF == cf) exchangeCF = null;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   313
            }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   314
        });
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   315
        checkCancelled();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   316
        return res.thenCompose((eimpl) -> {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   317
                    // recheck for cancelled, in case of race conditions
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   318
                    exchImpl = eimpl;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   319
                    IOException tt = getCancelCause();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   320
                    checkCancelled();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   321
                    if (tt != null) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   322
                        return MinimalFuture.failedFuture(tt);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   323
                    } else {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   324
                        // Now we're good to go. Because exchImpl is no longer
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   325
                        // null cancel() will be able to propagate directly to
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   326
                        // the impl after this point ( if needed ).
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   327
                        return MinimalFuture.completedFuture(eimpl);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   328
                    } });
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   329
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   330
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   331
    // Completed HttpResponse will be null if response succeeded
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   332
    // will be a non null responseAsync if expect continue returns an error
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   333
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   334
    public CompletableFuture<Response> responseAsync() {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   335
        return responseAsyncImpl(null);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   336
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   337
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   338
    CompletableFuture<Response> responseAsyncImpl(HttpConnection connection) {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   339
        SecurityException e = checkPermissions();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   340
        if (e != null) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   341
            return MinimalFuture.failedFuture(e);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   342
        } else {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   343
            return responseAsyncImpl0(connection);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   344
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   345
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   346
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   347
    // check whether the headersSentCF was completed exceptionally with
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   348
    // ProxyAuthorizationRequired. If so the Response embedded in the
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   349
    // exception is returned. Otherwise we proceed.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   350
    private CompletableFuture<Response> checkFor407(ExchangeImpl<T> ex, Throwable t,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   351
                                                    Function<ExchangeImpl<T>,CompletableFuture<Response>> andThen) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   352
        t = Utils.getCompletionCause(t);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   353
        if (t instanceof ProxyAuthenticationRequired) {
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   354
            if (debug.on()) debug.log("checkFor407: ProxyAuthenticationRequired: building synthetic response");
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   355
            bodyIgnored = MinimalFuture.completedFuture(null);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   356
            Response proxyResponse = ((ProxyAuthenticationRequired)t).proxyResponse;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   357
            HttpConnection c = ex == null ? null : ex.connection();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   358
            Response syntheticResponse = new Response(request, this,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   359
                    proxyResponse.headers, c, proxyResponse.statusCode,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   360
                    proxyResponse.version, true);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   361
            return MinimalFuture.completedFuture(syntheticResponse);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   362
        } else if (t != null) {
50985
cd41f34e548c 8206001: Enable TLS1.3 by default in Http Client
michaelm
parents: 50681
diff changeset
   363
            if (debug.on()) debug.log("checkFor407: no response - %s", (Object)t);
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   364
            return MinimalFuture.failedFuture(t);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   365
        } else {
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   366
            if (debug.on()) debug.log("checkFor407: all clear");
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   367
            return andThen.apply(ex);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   368
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   369
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   370
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   371
    // After sending the request headers, if no ProxyAuthorizationRequired
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   372
    // was raised and the expectContinue flag is on, we need to wait
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   373
    // for the 100-Continue response
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   374
    private CompletableFuture<Response> expectContinue(ExchangeImpl<T> ex) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   375
        assert request.expectContinue();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   376
        return ex.getResponseAsync(parentExecutor)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   377
                .thenCompose((Response r1) -> {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   378
            Log.logResponse(r1::toString);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   379
            int rcode = r1.statusCode();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   380
            if (rcode == 100) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   381
                Log.logTrace("Received 100-Continue: sending body");
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   382
                if (debug.on()) debug.log("Received 100-Continue for %s", r1);
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   383
                CompletableFuture<Response> cf =
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   384
                        exchImpl.sendBodyAsync()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   385
                                .thenCompose(exIm -> exIm.getResponseAsync(parentExecutor));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   386
                cf = wrapForUpgrade(cf);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   387
                cf = wrapForLog(cf);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   388
                return cf;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   389
            } else {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   390
                Log.logTrace("Expectation failed: Received {0}",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   391
                        rcode);
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   392
                if (debug.on()) debug.log("Expect-Continue failed (%d) for: %s", rcode, r1);
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   393
                if (upgrading && rcode == 101) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   394
                    IOException failed = new IOException(
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   395
                            "Unable to handle 101 while waiting for 100");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   396
                    return MinimalFuture.failedFuture(failed);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   397
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   398
                return exchImpl.readBodyAsync(this::ignoreBody, false, parentExecutor)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   399
                        .thenApply(v ->  r1);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   400
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   401
        });
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   402
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   403
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   404
    // After sending the request headers, if no ProxyAuthorizationRequired
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   405
    // was raised and the expectContinue flag is off, we can immediately
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   406
    // send the request body and proceed.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   407
    private CompletableFuture<Response> sendRequestBody(ExchangeImpl<T> ex) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   408
        assert !request.expectContinue();
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   409
        if (debug.on()) debug.log("sendRequestBody");
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   410
        CompletableFuture<Response> cf = ex.sendBodyAsync()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   411
                .thenCompose(exIm -> exIm.getResponseAsync(parentExecutor));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   412
        cf = wrapForUpgrade(cf);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   413
        cf = wrapForLog(cf);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   414
        return cf;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   415
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   416
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   417
    CompletableFuture<Response> responseAsyncImpl0(HttpConnection connection) {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   418
        Function<ExchangeImpl<T>, CompletableFuture<Response>> after407Check;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   419
        bodyIgnored = null;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   420
        if (request.expectContinue()) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   421
            request.addSystemHeader("Expect", "100-Continue");
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   422
            Log.logTrace("Sending Expect: 100-Continue");
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   423
            // wait for 100-Continue before sending body
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   424
            after407Check = this::expectContinue;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   425
        } else {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   426
            // send request body and proceed.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   427
            after407Check = this::sendRequestBody;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   428
        }
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   429
        // The ProxyAuthorizationRequired can be triggered either by
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   430
        // establishExchange (case of HTTP/2 SSL tunneling through HTTP/1.1 proxy
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   431
        // or by sendHeaderAsync (case of HTTP/1.1 SSL tunneling through HTTP/1.1 proxy
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   432
        // Therefore we handle it with a call to this checkFor407(...) after these
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   433
        // two places.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   434
        Function<ExchangeImpl<T>, CompletableFuture<Response>> afterExch407Check =
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   435
                (ex) -> ex.sendHeadersAsync()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   436
                        .handle((r,t) -> this.checkFor407(r, t, after407Check))
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   437
                        .thenCompose(Function.identity());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   438
        return establishExchange(connection)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   439
                .handle((r,t) -> this.checkFor407(r,t, afterExch407Check))
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   440
                .thenCompose(Function.identity());
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   441
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   442
43984
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents: 42460
diff changeset
   443
    private CompletableFuture<Response> wrapForUpgrade(CompletableFuture<Response> cf) {
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents: 42460
diff changeset
   444
        if (upgrading) {
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents: 42460
diff changeset
   445
            return cf.thenCompose(r -> checkForUpgradeAsync(r, exchImpl));
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents: 42460
diff changeset
   446
        }
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents: 42460
diff changeset
   447
        return cf;
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents: 42460
diff changeset
   448
    }
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents: 42460
diff changeset
   449
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents: 42460
diff changeset
   450
    private CompletableFuture<Response> wrapForLog(CompletableFuture<Response> cf) {
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents: 42460
diff changeset
   451
        if (Log.requests()) {
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents: 42460
diff changeset
   452
            return cf.thenApply(response -> {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   453
                Log.logResponse(response::toString);
43984
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents: 42460
diff changeset
   454
                return response;
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents: 42460
diff changeset
   455
            });
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents: 42460
diff changeset
   456
        }
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents: 42460
diff changeset
   457
        return cf;
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents: 42460
diff changeset
   458
    }
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents: 42460
diff changeset
   459
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   460
    HttpResponse.BodySubscriber<T> ignoreBody(HttpResponse.ResponseInfo hdrs) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   461
        return HttpResponse.BodySubscribers.replacing(null);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   462
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   463
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   464
    // if this response was received in reply to an upgrade
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   465
    // then create the Http2Connection from the HttpConnection
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   466
    // initialize it and wait for the real response on a newly created Stream
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   467
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   468
    private CompletableFuture<Response>
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   469
    checkForUpgradeAsync(Response resp,
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   470
                         ExchangeImpl<T> ex) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   471
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   472
        int rcode = resp.statusCode();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   473
        if (upgrading && (rcode == 101)) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   474
            Http1Exchange<T> e = (Http1Exchange<T>)ex;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   475
            // check for 101 switching protocols
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   476
            // 101 responses are not supposed to contain a body.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   477
            //    => should we fail if there is one?
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   478
            if (debug.on()) debug.log("Upgrading async %s", e.connection());
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   479
            return e.readBodyAsync(this::ignoreBody, false, parentExecutor)
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   480
                .thenCompose((T v) -> {// v is null
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   481
                    debug.log("Ignored body");
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   482
                    // we pass e::getBuffer to allow the ByteBuffers to accumulate
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   483
                    // while we build the Http2Connection
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   484
                    return Http2Connection.createAsync(e.connection(),
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   485
                                                 client.client2(),
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   486
                                                 this, e::drainLeftOverBytes)
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   487
                        .thenCompose((Http2Connection c) -> {
48376
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48083
diff changeset
   488
                            boolean cached = c.offerConnection();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   489
                            Stream<T> s = c.getStream(1);
48376
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48083
diff changeset
   490
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   491
                            if (s == null) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   492
                                // s can be null if an exception occurred
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   493
                                // asynchronously while sending the preface.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   494
                                Throwable t = c.getRecordedCause();
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   495
                                IOException ioe;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   496
                                if (t != null) {
48376
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48083
diff changeset
   497
                                    if (!cached)
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48083
diff changeset
   498
                                        c.close();
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   499
                                    ioe = new IOException("Can't get stream 1: " + t, t);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   500
                                } else {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   501
                                    ioe = new IOException("Can't get stream 1");
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   502
                                }
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   503
                                return MinimalFuture.failedFuture(ioe);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   504
                            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   505
                            exchImpl.released();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   506
                            Throwable t;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   507
                            // There's a race condition window where an external
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   508
                            // thread (SelectorManager) might complete the
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   509
                            // exchange in timeout at the same time where we're
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   510
                            // trying to switch the exchange impl.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   511
                            // 'failed' will be reset to null after
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   512
                            // exchImpl.cancel() has completed, so either we
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   513
                            // will observe failed != null here, or we will
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   514
                            // observe e.getCancelCause() != null, or the
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   515
                            // timeout exception will be routed to 's'.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   516
                            // Either way, we need to relay it to s.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   517
                            synchronized (this) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   518
                                exchImpl = s;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   519
                                t = failed;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   520
                            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   521
                            // Check whether the HTTP/1.1 was cancelled.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   522
                            if (t == null) t = e.getCancelCause();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   523
                            // if HTTP/1.1 exchange was timed out, don't
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   524
                            // try to go further.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   525
                            if (t instanceof HttpTimeoutException) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   526
                                 s.cancelImpl(t);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   527
                                 return MinimalFuture.failedFuture(t);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   528
                            }
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   529
                            if (debug.on())
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48376
diff changeset
   530
                                debug.log("Getting response async %s", s);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   531
                            return s.getResponseAsync(null);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   532
                        });}
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   533
                );
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   534
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   535
        return MinimalFuture.completedFuture(resp);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   536
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   537
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   538
    private URI getURIForSecurityCheck() {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   539
        URI u;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   540
        String method = request.method();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   541
        InetSocketAddress authority = request.authority();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   542
        URI uri = request.uri();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   543
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   544
        // CONNECT should be restricted at API level
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   545
        if (method.equalsIgnoreCase("CONNECT")) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   546
            try {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   547
                u = new URI("socket",
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   548
                             null,
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   549
                             authority.getHostString(),
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   550
                             authority.getPort(),
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   551
                             null,
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   552
                             null,
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   553
                             null);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   554
            } catch (URISyntaxException e) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   555
                throw new InternalError(e); // shouldn't happen
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   556
            }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   557
        } else {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   558
            u = uri;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   559
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   560
        return u;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   561
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   562
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   563
    /**
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   564
     * Returns the security permission required for the given details.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   565
     * If method is CONNECT, then uri must be of form "scheme://host:port"
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   566
     */
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   567
    private static URLPermission permissionForServer(URI uri,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   568
                                                     String method,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   569
                                                     Map<String, List<String>> headers) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   570
        if (method.equals("CONNECT")) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   571
            return new URLPermission(uri.toString(), "CONNECT");
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   572
        } else {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   573
            return Utils.permissionForServer(uri, method, headers.keySet().stream());
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   574
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   575
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   576
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   577
    /**
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   578
     * Performs the necessary security permission checks required to retrieve
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   579
     * the response. Returns a security exception representing the denied
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   580
     * permission, or null if all checks pass or there is no security manager.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   581
     */
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   582
    private SecurityException checkPermissions() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   583
        String method = request.method();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   584
        SecurityManager sm = System.getSecurityManager();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   585
        if (sm == null || method.equals("CONNECT")) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   586
            // tunneling will have a null acc, which is fine. The proxy
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   587
            // permission check will have already been preformed.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   588
            return null;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   589
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   590
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   591
        HttpHeaders userHeaders = request.getUserHeaders();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   592
        URI u = getURIForSecurityCheck();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   593
        URLPermission p = permissionForServer(u, method, userHeaders.map());
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   594
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   595
        try {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   596
            assert acc != null;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   597
            sm.checkPermission(p, acc);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   598
        } catch (SecurityException e) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   599
            return e;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   600
        }
52554
5f1ca46703f9 8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents: 51364
diff changeset
   601
        String hostHeader = userHeaders.firstValue("Host").orElse(null);
5f1ca46703f9 8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents: 51364
diff changeset
   602
        if (hostHeader != null && !hostHeader.equalsIgnoreCase(u.getHost())) {
5f1ca46703f9 8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents: 51364
diff changeset
   603
            // user has set a Host header different to request URI
5f1ca46703f9 8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents: 51364
diff changeset
   604
            // must check that for URLPermission also
5f1ca46703f9 8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents: 51364
diff changeset
   605
            URI u1 = replaceHostInURI(u, hostHeader);
5f1ca46703f9 8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents: 51364
diff changeset
   606
            URLPermission p1 = permissionForServer(u1, method, userHeaders.map());
5f1ca46703f9 8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents: 51364
diff changeset
   607
            try {
5f1ca46703f9 8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents: 51364
diff changeset
   608
                assert acc != null;
5f1ca46703f9 8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents: 51364
diff changeset
   609
                sm.checkPermission(p1, acc);
5f1ca46703f9 8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents: 51364
diff changeset
   610
            } catch (SecurityException e) {
5f1ca46703f9 8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents: 51364
diff changeset
   611
                return e;
5f1ca46703f9 8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents: 51364
diff changeset
   612
            }
5f1ca46703f9 8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents: 51364
diff changeset
   613
        }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   614
        ProxySelector ps = client.proxySelector();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   615
        if (ps != null) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   616
            if (!method.equals("CONNECT")) {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   617
                // a non-tunneling HTTP proxy. Need to check access
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   618
                URLPermission proxyPerm = permissionForProxy(request.proxy());
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   619
                if (proxyPerm != null) {
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
                        sm.checkPermission(proxyPerm, acc);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   622
                    } catch (SecurityException e) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   623
                        return e;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   624
                    }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   625
                }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   626
            }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   627
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   628
        return null;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   629
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   630
52554
5f1ca46703f9 8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents: 51364
diff changeset
   631
    private static URI replaceHostInURI(URI u, String hostPort) {
5f1ca46703f9 8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents: 51364
diff changeset
   632
        StringBuilder sb = new StringBuilder();
5f1ca46703f9 8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents: 51364
diff changeset
   633
        sb.append(u.getScheme())
5f1ca46703f9 8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents: 51364
diff changeset
   634
                .append("://")
5f1ca46703f9 8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents: 51364
diff changeset
   635
                .append(hostPort)
5f1ca46703f9 8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents: 51364
diff changeset
   636
                .append(u.getRawPath());
5f1ca46703f9 8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents: 51364
diff changeset
   637
        return URI.create(sb.toString());
5f1ca46703f9 8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents: 51364
diff changeset
   638
    }
5f1ca46703f9 8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents: 51364
diff changeset
   639
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   640
    HttpClient.Version version() {
44854
5a486e0acd29 8175814: Update default HttpClient protocol version and optional request version
michaelm
parents: 44639
diff changeset
   641
        return multi.version();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   642
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   643
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   644
    String dbgString() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   645
        return dbgTag;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   646
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   647
}