src/java.net.http/share/classes/jdk/internal/net/http/Exchange.java
author chegar
Wed, 07 Feb 2018 21:45:37 +0000
branchhttp-client-branch
changeset 56092 fd85b2bf2b0d
parent 56089 src/java.net.http/share/classes/java/net/http/internal/Exchange.java@42208b2f224e
child 56104 3420c1bdd254
permissions -rw-r--r--
http-client-branch: move implementation to jdk.internal.net.http
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
/*
56041
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
     2
 * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     4
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    10
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    15
 * accompanied this code).
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    16
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    20
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    23
 * questions.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    24
 */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    25
56092
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    26
package jdk.internal.net.http;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    27
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;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    36
import java.util.List;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    37
import java.util.Map;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    38
import java.util.concurrent.CompletableFuture;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    39
import java.util.concurrent.Executor;
56041
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
    40
import java.util.function.Function;
56089
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56082
diff changeset
    41
import java.net.http.HttpClient;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56082
diff changeset
    42
import java.net.http.HttpHeaders;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56082
diff changeset
    43
import java.net.http.HttpResponse;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56082
diff changeset
    44
import java.net.http.HttpTimeoutException;
56092
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    45
import jdk.internal.net.http.common.MinimalFuture;
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    46
import jdk.internal.net.http.common.Utils;
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    47
import jdk.internal.net.http.common.Log;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    48
56092
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    49
import static jdk.internal.net.http.common.Utils.permissionForProxy;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    50
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    51
/**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    52
 * 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
    53
 * 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
    54
 * 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
    55
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    56
 * Security check is performed here:
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    57
 * - uses AccessControlContext captured at API level
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    58
 * - checks for appropriate URLPermission for request
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    59
 * - 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
    60
 * - 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
    61
 *    (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
    62
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    63
 */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    64
final class Exchange<T> {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    65
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    66
    static final boolean DEBUG = Utils.DEBUG; // Revisit: temporary dev flag.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    67
    final System.Logger  debug = Utils.getDebugLogger(this::dbgString, DEBUG);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    68
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    69
    final HttpRequestImpl request;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    70
    final HttpClientImpl client;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    71
    volatile ExchangeImpl<T> exchImpl;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    72
    volatile CompletableFuture<? extends ExchangeImpl<T>> exchangeCF;
56041
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
    73
    volatile CompletableFuture<Void> bodyIgnored;
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
    74
44639
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
    75
    // 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
    76
    // has been established.
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
    77
    private volatile IOException failed;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    78
    final AccessControlContext acc;
56010
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 56008
diff changeset
    79
    final MultiExchange<T> multi;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    80
    final Executor parentExecutor;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    81
    boolean upgrading; // to HTTP/2
56010
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 56008
diff changeset
    82
    final PushGroup<T> pushGroup;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    83
    final String dbgTag;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    84
56010
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 56008
diff changeset
    85
    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
    86
        this.request = request;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    87
        this.upgrading = false;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    88
        this.client = multi.client();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    89
        this.multi = multi;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    90
        this.acc = multi.acc;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    91
        this.parentExecutor = multi.executor;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    92
        this.pushGroup = multi.pushGroup;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    93
        this.dbgTag = "Exchange";
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    94
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    95
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    96
    /* If different AccessControlContext to be used  */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    97
    Exchange(HttpRequestImpl request,
56010
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 56008
diff changeset
    98
             MultiExchange<T> multi,
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    99
             AccessControlContext acc)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   100
    {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   101
        this.request = request;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   102
        this.acc = acc;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   103
        this.upgrading = false;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   104
        this.client = multi.client();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   105
        this.multi = multi;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   106
        this.parentExecutor = multi.executor;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   107
        this.pushGroup = multi.pushGroup;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   108
        this.dbgTag = "Exchange";
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   109
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   110
56010
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 56008
diff changeset
   111
    PushGroup<T> getPushGroup() {
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   112
        return pushGroup;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   113
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   114
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   115
    Executor executor() {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   116
        return parentExecutor;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   117
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   118
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   119
    public HttpRequestImpl request() {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   120
        return request;
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
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   123
    HttpClientImpl client() {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   124
        return client;
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
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   127
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   128
    public CompletableFuture<T> readBodyAsync(HttpResponse.BodyHandler<T> handler) {
56041
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   129
        // If we received a 407 while establishing the exchange
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   130
        // there will be no body to read: bodyIgnored will be true,
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   131
        // and exchImpl will be null (if we were trying to establish
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   132
        // an HTTP/2 tunnel through an HTTP/1.1 proxy)
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   133
        if (bodyIgnored != null) return MinimalFuture.completedFuture(null);
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   134
43999
4cc44dd9f14f 8164625: Pooled HttpConnection should be removed during close
prappo
parents: 43984
diff changeset
   135
        // 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
   136
        return exchImpl.readBodyAsync(handler, !request.isWebSocket(), parentExecutor)
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   137
                .whenComplete((r,t) -> exchImpl.completed());
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   138
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   139
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   140
    /**
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   141
     * Called after a redirect or similar kind of retry where a body might
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   142
     * 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
   143
     * we can consume small quantity of data, or close the connection in
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   144
     * other cases.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   145
     */
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   146
    public CompletableFuture<Void> ignoreBody() {
56041
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   147
        if (bodyIgnored != null) return bodyIgnored;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   148
        return exchImpl.ignoreBody();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   149
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   150
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   151
    /**
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   152
     * Called when a new exchange is created to replace this exchange.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   153
     * At this point it is guaranteed that readBody/readBodyAsync will
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   154
     * not be called.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   155
     */
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   156
    public void released() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   157
        ExchangeImpl<?> impl = exchImpl;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   158
        if (impl != null) impl.released();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   159
        // Don't set exchImpl to null here. We need to keep
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   160
        // it alive until it's replaced by a Stream in wrapForUpgrade.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   161
        // Setting it to null here might get it GC'ed too early, because
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   162
        // 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
   163
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   164
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   165
    public void cancel() {
44639
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   166
        // 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
   167
        // 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
   168
        // 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
   169
        // right away
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   170
        if (exchImpl != null) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   171
            exchImpl.cancel();
44639
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   172
        } else {
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   173
            // 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
   174
            // 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
   175
            // of race conditions between impl/cancel.
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   176
            cancel(new IOException("Request cancelled"));
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   177
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   178
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   179
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   180
    public void cancel(IOException cause) {
44639
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   181
        // 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
   182
        // 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
   183
        // exchange impl has been established.
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   184
        ExchangeImpl<?> impl = exchImpl;
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   185
        if (impl != null) {
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   186
            // propagate the exception to the impl
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   187
            debug.log(Level.DEBUG, "Cancelling exchImpl: %s", exchImpl);
44639
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   188
            impl.cancel(cause);
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   189
        } else {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   190
            // no impl yet. record the exception
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   191
            failed = cause;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   192
            // now call checkCancelled to recheck the impl.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   193
            // if the failed state is set and the impl is not null, reset
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   194
            // the failed state and propagate the exception to the impl.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   195
            checkCancelled();
44639
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   196
        }
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   197
    }
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   198
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   199
    // 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
   200
    // 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
   201
    // 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
   202
    // 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
   203
    // can be cleared.
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   204
    // Takes care of possible race conditions.
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   205
    private void checkCancelled() {
44639
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   206
        ExchangeImpl<?> impl = null;
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   207
        IOException cause = null;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   208
        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
   209
        if (failed != null) {
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   210
            synchronized(this) {
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   211
                cause = failed;
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   212
                impl = exchImpl;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   213
                cf = exchangeCF;
44639
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   214
            }
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   215
        }
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   216
        if (cause == null) return;
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   217
        if (impl != null) {
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   218
            // The exception is raised by propagating it to the impl.
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   219
            debug.log(Level.DEBUG, "Cancelling exchImpl: %s", impl);
44639
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   220
            impl.cancel(cause);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   221
            failed = null;
44639
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   222
        } else {
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   223
            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
   224
                         + "\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
   225
                         request.uri(),
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   226
                         request.timeout().isPresent() ?
44639
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   227
                         // 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
   228
                         // this is just debugging, we don't care if it overflows.
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   229
                         (request.timeout().get().getSeconds() * 1000
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   230
                          + 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
   231
                         cause);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   232
            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
   233
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   234
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   235
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   236
    public void h2Upgrade() {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   237
        upgrading = true;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   238
        request.setH2Upgrade(client.client2());
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   239
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   240
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   241
    synchronized IOException getCancelCause() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   242
        return failed;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   243
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   244
44639
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   245
    // 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
   246
    // potential concurrent calls to cancel() or cancel(IOException)
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   247
    private CompletableFuture<? extends ExchangeImpl<T>>
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   248
    establishExchange(HttpConnection connection) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   249
        if (debug.isLoggable(Level.DEBUG)) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   250
            debug.log(Level.DEBUG,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   251
                    "establishing exchange for %s,%n\t proxy=%s",
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   252
                    request,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   253
                    request.proxy());
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   254
        }
44639
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents: 43999
diff changeset
   255
        // check if we have been cancelled first.
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   256
        Throwable t = getCancelCause();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   257
        checkCancelled();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   258
        if (t != null) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   259
            return MinimalFuture.failedFuture(t);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   260
        }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   261
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   262
        CompletableFuture<? extends ExchangeImpl<T>> cf, res;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   263
        cf = ExchangeImpl.get(this, connection);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   264
        // We should probably use a VarHandle to get/set exchangeCF
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   265
        // instead - as we need CAS semantics.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   266
        synchronized (this) { exchangeCF = cf; };
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   267
        res = cf.whenComplete((r,x) -> {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   268
            synchronized(Exchange.this) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   269
                if (exchangeCF == cf) exchangeCF = null;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   270
            }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   271
        });
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   272
        checkCancelled();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   273
        return res.thenCompose((eimpl) -> {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   274
                    // recheck for cancelled, in case of race conditions
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   275
                    exchImpl = eimpl;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   276
                    IOException tt = getCancelCause();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   277
                    checkCancelled();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   278
                    if (tt != null) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   279
                        return MinimalFuture.failedFuture(tt);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   280
                    } else {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   281
                        // Now we're good to go. Because exchImpl is no longer
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   282
                        // null cancel() will be able to propagate directly to
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   283
                        // the impl after this point ( if needed ).
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   284
                        return MinimalFuture.completedFuture(eimpl);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   285
                    } });
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   286
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   287
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   288
    // 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
   289
    // 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
   290
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   291
    public CompletableFuture<Response> responseAsync() {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   292
        return responseAsyncImpl(null);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   293
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   294
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   295
    CompletableFuture<Response> responseAsyncImpl(HttpConnection connection) {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   296
        SecurityException e = checkPermissions();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   297
        if (e != null) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   298
            return MinimalFuture.failedFuture(e);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   299
        } else {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   300
            return responseAsyncImpl0(connection);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   301
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   302
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   303
56041
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   304
    // check whether the headersSentCF was completed exceptionally with
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   305
    // ProxyAuthorizationRequired. If so the Response embedded in the
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   306
    // exception is returned. Otherwise we proceed.
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   307
    private CompletableFuture<Response> checkFor407(ExchangeImpl<T> ex, Throwable t,
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   308
                                                    Function<ExchangeImpl<T>,CompletableFuture<Response>> andThen) {
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   309
        t = Utils.getCompletionCause(t);
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   310
        if (t instanceof ProxyAuthenticationRequired) {
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   311
            bodyIgnored = MinimalFuture.completedFuture(null);
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   312
            Response proxyResponse = ((ProxyAuthenticationRequired)t).proxyResponse;
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   313
            Response syntheticResponse = new Response(request, this,
56054
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   314
                    proxyResponse.headers, proxyResponse.statusCode,
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   315
                    proxyResponse.version, true);
56041
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   316
            return MinimalFuture.completedFuture(syntheticResponse);
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   317
        } else if (t != null) {
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   318
            return MinimalFuture.failedFuture(t);
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   319
        } else {
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   320
            return andThen.apply(ex);
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   321
        }
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   322
    }
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   323
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   324
    // After sending the request headers, if no ProxyAuthorizationRequired
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   325
    // was raised and the expectContinue flag is on, we need to wait
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   326
    // for the 100-Continue response
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   327
    private CompletableFuture<Response> expectContinue(ExchangeImpl<T> ex) {
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   328
        assert request.expectContinue();
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   329
        return ex.getResponseAsync(parentExecutor)
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   330
                .thenCompose((Response r1) -> {
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   331
            Log.logResponse(r1::toString);
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   332
            int rcode = r1.statusCode();
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   333
            if (rcode == 100) {
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   334
                Log.logTrace("Received 100-Continue: sending body");
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   335
                CompletableFuture<Response> cf =
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   336
                        exchImpl.sendBodyAsync()
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   337
                                .thenCompose(exIm -> exIm.getResponseAsync(parentExecutor));
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   338
                cf = wrapForUpgrade(cf);
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   339
                cf = wrapForLog(cf);
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   340
                return cf;
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   341
            } else {
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   342
                Log.logTrace("Expectation failed: Received {0}",
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   343
                        rcode);
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   344
                if (upgrading && rcode == 101) {
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   345
                    IOException failed = new IOException(
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   346
                            "Unable to handle 101 while waiting for 100");
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   347
                    return MinimalFuture.failedFuture(failed);
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   348
                }
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   349
                return exchImpl.readBodyAsync(this::ignoreBody, false, parentExecutor)
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   350
                        .thenApply(v ->  r1);
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   351
            }
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   352
        });
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   353
    }
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   354
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   355
    // After sending the request headers, if no ProxyAuthorizationRequired
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   356
    // was raised and the expectContinue flag is off, we can immediately
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   357
    // send the request body and proceed.
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   358
    private CompletableFuture<Response> sendRequestBody(ExchangeImpl<T> ex) {
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   359
        assert !request.expectContinue();
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   360
        CompletableFuture<Response> cf = ex.sendBodyAsync()
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   361
                .thenCompose(exIm -> exIm.getResponseAsync(parentExecutor));
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   362
        cf = wrapForUpgrade(cf);
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   363
        cf = wrapForLog(cf);
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   364
        return cf;
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   365
    }
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   366
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   367
    CompletableFuture<Response> responseAsyncImpl0(HttpConnection connection) {
56041
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   368
        Function<ExchangeImpl<T>, CompletableFuture<Response>> after407Check;
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   369
        bodyIgnored = null;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   370
        if (request.expectContinue()) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   371
            request.addSystemHeader("Expect", "100-Continue");
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   372
            Log.logTrace("Sending Expect: 100-Continue");
56041
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   373
            // wait for 100-Continue before sending body
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   374
            after407Check = this::expectContinue;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   375
        } else {
56041
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   376
            // send request body and proceed.
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   377
            after407Check = this::sendRequestBody;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   378
        }
56041
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   379
        // The ProxyAuthorizationRequired can be triggered either by
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   380
        // establishExchange (case of HTTP/2 SSL tunelling through HTTP/1.1 proxy
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   381
        // or by sendHeaderAsync (case of HTTP/1.1 SSL tunelling through HTTP/1.1 proxy
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   382
        // Therefore we handle it with a call to this checkFor407(...) after these
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   383
        // two places.
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   384
        Function<ExchangeImpl<T>, CompletableFuture<Response>> afterExch407Check =
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   385
                (ex) -> ex.sendHeadersAsync()
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   386
                        .handle((r,t) -> this.checkFor407(r, t, after407Check))
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   387
                        .thenCompose(Function.identity());
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   388
        return establishExchange(connection)
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   389
                .handle((r,t) -> this.checkFor407(r,t, afterExch407Check))
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56010
diff changeset
   390
                .thenCompose(Function.identity());
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   391
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   392
43984
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents: 42460
diff changeset
   393
    private CompletableFuture<Response> wrapForUpgrade(CompletableFuture<Response> cf) {
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents: 42460
diff changeset
   394
        if (upgrading) {
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents: 42460
diff changeset
   395
            return cf.thenCompose(r -> checkForUpgradeAsync(r, exchImpl));
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents: 42460
diff changeset
   396
        }
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents: 42460
diff changeset
   397
        return cf;
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents: 42460
diff changeset
   398
    }
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents: 42460
diff changeset
   399
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents: 42460
diff changeset
   400
    private CompletableFuture<Response> wrapForLog(CompletableFuture<Response> cf) {
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents: 42460
diff changeset
   401
        if (Log.requests()) {
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents: 42460
diff changeset
   402
            return cf.thenApply(response -> {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   403
                Log.logResponse(response::toString);
43984
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents: 42460
diff changeset
   404
                return response;
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents: 42460
diff changeset
   405
            });
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents: 42460
diff changeset
   406
        }
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents: 42460
diff changeset
   407
        return cf;
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents: 42460
diff changeset
   408
    }
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents: 42460
diff changeset
   409
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   410
    HttpResponse.BodySubscriber<T> ignoreBody(int status, HttpHeaders hdrs) {
56082
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents: 56079
diff changeset
   411
        return HttpResponse.BodySubscriber.replace(null);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   412
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   413
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   414
    // 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
   415
    // 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
   416
    // 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
   417
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   418
    private CompletableFuture<Response>
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   419
    checkForUpgradeAsync(Response resp,
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   420
                         ExchangeImpl<T> ex) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   421
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   422
        int rcode = resp.statusCode();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   423
        if (upgrading && (rcode == 101)) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   424
            Http1Exchange<T> e = (Http1Exchange<T>)ex;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   425
            // check for 101 switching protocols
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   426
            // 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
   427
            //    => should we fail if there is one?
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   428
            debug.log(Level.DEBUG, "Upgrading async %s", e.connection());
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   429
            return e.readBodyAsync(this::ignoreBody, false, parentExecutor)
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   430
                .thenCompose((T v) -> {// v is null
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   431
                    debug.log(Level.DEBUG, "Ignored body");
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   432
                    // we pass e::getBuffer to allow the ByteBuffers to accumulate
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   433
                    // while we build the Http2Connection
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   434
                    return Http2Connection.createAsync(e.connection(),
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   435
                                                 client.client2(),
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   436
                                                 this, e::drainLeftOverBytes)
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   437
                        .thenCompose((Http2Connection c) -> {
48376
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48083
diff changeset
   438
                            boolean cached = c.offerConnection();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   439
                            Stream<T> s = c.getStream(1);
48376
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48083
diff changeset
   440
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   441
                            if (s == null) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   442
                                // s can be null if an exception occurred
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   443
                                // asynchronously while sending the preface.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   444
                                Throwable t = c.getRecordedCause();
56068
34eabbaa1226 http-client-branch: possible null dereference in Exchange
chegar
parents: 56054
diff changeset
   445
                                IOException ioe;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   446
                                if (t != null) {
48376
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48083
diff changeset
   447
                                    if (!cached)
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48083
diff changeset
   448
                                        c.close();
56068
34eabbaa1226 http-client-branch: possible null dereference in Exchange
chegar
parents: 56054
diff changeset
   449
                                    ioe = new IOException("Can't get stream 1: " + t, t);
34eabbaa1226 http-client-branch: possible null dereference in Exchange
chegar
parents: 56054
diff changeset
   450
                                } else {
34eabbaa1226 http-client-branch: possible null dereference in Exchange
chegar
parents: 56054
diff changeset
   451
                                    ioe = new IOException("Can't get stream 1");
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   452
                                }
56068
34eabbaa1226 http-client-branch: possible null dereference in Exchange
chegar
parents: 56054
diff changeset
   453
                                return MinimalFuture.failedFuture(ioe);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   454
                            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   455
                            exchImpl.released();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   456
                            Throwable t;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   457
                            // There's a race condition window where an external
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   458
                            // thread (SelectorManager) might complete the
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   459
                            // exchange in timeout at the same time where we're
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   460
                            // trying to switch the exchange impl.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   461
                            // 'failed' will be reset to null after
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   462
                            // exchImpl.cancel() has completed, so either we
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   463
                            // will observe failed != null here, or we will
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   464
                            // observe e.getCancelCause() != null, or the
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   465
                            // timeout exception will be routed to 's'.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   466
                            // Either way, we need to relay it to s.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   467
                            synchronized (this) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   468
                                exchImpl = s;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   469
                                t = failed;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   470
                            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   471
                            // Check whether the HTTP/1.1 was cancelled.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   472
                            if (t == null) t = e.getCancelCause();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   473
                            // if HTTP/1.1 exchange was timed out, don't
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   474
                            // try to go further.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   475
                            if (t instanceof HttpTimeoutException) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   476
                                 s.cancelImpl(t);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   477
                                 return MinimalFuture.failedFuture(t);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   478
                            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   479
                            debug.log(Level.DEBUG, "Getting response async %s", s);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   480
                            return s.getResponseAsync(null);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   481
                        });}
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   482
                );
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   483
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   484
        return MinimalFuture.completedFuture(resp);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   485
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   486
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   487
    private URI getURIForSecurityCheck() {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   488
        URI u;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   489
        String method = request.method();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   490
        InetSocketAddress authority = request.authority();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   491
        URI uri = request.uri();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   492
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   493
        // 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
   494
        if (method.equalsIgnoreCase("CONNECT")) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   495
            try {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   496
                u = new URI("socket",
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   497
                             null,
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   498
                             authority.getHostString(),
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   499
                             authority.getPort(),
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   500
                             null,
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   501
                             null,
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   502
                             null);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   503
            } catch (URISyntaxException e) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   504
                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
   505
            }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   506
        } else {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   507
            u = uri;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   508
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   509
        return u;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   510
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   511
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   512
    /**
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   513
     * Returns the security permission required for the given details.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   514
     * 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
   515
     */
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   516
    private static URLPermission permissionForServer(URI uri,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   517
                                                     String method,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   518
                                                     Map<String, List<String>> headers) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   519
        if (method.equals("CONNECT")) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   520
            return new URLPermission(uri.toString(), "CONNECT");
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   521
        } else {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   522
            return Utils.permissionForServer(uri, method, headers.keySet().stream());
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   523
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   524
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   525
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   526
    /**
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   527
     * Performs the necessary security permission checks required to retrieve
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   528
     * the response. Returns a security exception representing the denied
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   529
     * permission, or null if all checks pass or there is no security manager.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   530
     */
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   531
    private SecurityException checkPermissions() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   532
        String method = request.method();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   533
        SecurityManager sm = System.getSecurityManager();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   534
        if (sm == null || method.equals("CONNECT")) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   535
            // tunneling will have a null acc, which is fine. The proxy
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   536
            // 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
   537
            return null;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   538
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   539
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   540
        HttpHeaders userHeaders = request.getUserHeaders();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   541
        URI u = getURIForSecurityCheck();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   542
        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
   543
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   544
        try {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   545
            assert acc != null;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   546
            sm.checkPermission(p, acc);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   547
        } catch (SecurityException e) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   548
            return e;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   549
        }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   550
        ProxySelector ps = client.proxySelector();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   551
        if (ps != null) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   552
            if (!method.equals("CONNECT")) {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   553
                // a non-tunneling HTTP proxy. Need to check access
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   554
                URLPermission proxyPerm = permissionForProxy(request.proxy());
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   555
                if (proxyPerm != null) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   556
                    try {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   557
                        sm.checkPermission(proxyPerm, acc);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   558
                    } catch (SecurityException e) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   559
                        return e;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   560
                    }
42460
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
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   564
        return null;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   565
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   566
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   567
    HttpClient.Version version() {
44854
5a486e0acd29 8175814: Update default HttpClient protocol version and optional request version
michaelm
parents: 44639
diff changeset
   568
        return multi.version();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   569
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   570
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   571
    String dbgString() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   572
        return dbgTag;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   573
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   574
}