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