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