author | chegar |
Thu, 09 Aug 2018 11:23:12 +0100 | |
changeset 51364 | 31d9e82b2e64 |
parent 50681 | 4254bed3c09d |
child 52196 | 420445d16008 |
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; |
50681 | 29 |
import java.net.ConnectException; |
51364
31d9e82b2e64
8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
50681
diff
changeset
|
30 |
import java.net.http.HttpConnectTimeoutException; |
49765 | 31 |
import java.util.Iterator; |
32 |
import java.util.LinkedList; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
33 |
import java.security.AccessControlContext; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
34 |
import java.util.concurrent.CompletableFuture; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
35 |
import java.util.concurrent.CompletionException; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
36 |
import java.util.concurrent.ExecutionException; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
37 |
import java.util.concurrent.Executor; |
48083 | 38 |
import java.util.concurrent.atomic.AtomicInteger; |
39 |
import java.util.function.Function; |
|
49765 | 40 |
|
41 |
import java.net.http.HttpClient; |
|
42 |
import java.net.http.HttpRequest; |
|
43 |
import java.net.http.HttpResponse; |
|
44 |
import java.net.http.HttpResponse.PushPromiseHandler; |
|
45 |
import java.net.http.HttpTimeoutException; |
|
46 |
import jdk.internal.net.http.common.Log; |
|
47 |
import jdk.internal.net.http.common.Logger; |
|
48 |
import jdk.internal.net.http.common.MinimalFuture; |
|
49 |
import jdk.internal.net.http.common.ConnectionExpiredException; |
|
50 |
import jdk.internal.net.http.common.Utils; |
|
51 |
import static jdk.internal.net.http.common.MinimalFuture.completedFuture; |
|
52 |
import static jdk.internal.net.http.common.MinimalFuture.failedFuture; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
53 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
54 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
55 |
* Encapsulates multiple Exchanges belonging to one HttpRequestImpl. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
56 |
* - manages filters |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
57 |
* - retries due to filters. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
58 |
* - I/O errors and most other exceptions get returned directly to user |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
59 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
60 |
* Creates a new Exchange for each request/response interaction |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
61 |
*/ |
49765 | 62 |
class MultiExchange<T> { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
63 |
|
49765 | 64 |
static final Logger debug = |
65 |
Utils.getDebugLogger("MultiExchange"::toString, Utils.DEBUG); |
|
48083 | 66 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
67 |
private final HttpRequest userRequest; // the user request |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
68 |
private final HttpRequestImpl request; // a copy of the user request |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
69 |
final AccessControlContext acc; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
70 |
final HttpClientImpl client; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
71 |
final HttpResponse.BodyHandler<T> responseHandler; |
50681 | 72 |
final HttpClientImpl.DelegatingExecutor executor; |
48083 | 73 |
final AtomicInteger attempts = new AtomicInteger(); |
49765 | 74 |
HttpRequestImpl currentreq; // used for retries & redirect |
75 |
HttpRequestImpl previousreq; // used for retries & redirect |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
76 |
Exchange<T> exchange; // the current exchange |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
77 |
Exchange<T> previous; |
48083 | 78 |
volatile Throwable retryCause; |
79 |
volatile boolean expiredOnce; |
|
80 |
volatile HttpResponse<T> response = null; |
|
81 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
82 |
// Maximum number of times a request will be retried/redirected |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
83 |
// for any reason |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
84 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
85 |
static final int DEFAULT_MAX_ATTEMPTS = 5; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
86 |
static final int max_attempts = Utils.getIntegerNetProperty( |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
87 |
"jdk.httpclient.redirects.retrylimit", DEFAULT_MAX_ATTEMPTS |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
88 |
); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
89 |
|
49765 | 90 |
private final LinkedList<HeaderFilter> filters; |
51364
31d9e82b2e64
8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
50681
diff
changeset
|
91 |
ResponseTimerEvent responseTimerEvent; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
92 |
volatile boolean cancelled; |
49765 | 93 |
final PushGroup<T> pushGroup; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
94 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
95 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
96 |
* Filter fields. These are attached as required by filters |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
97 |
* and only used by the filter implementations. This could be |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
98 |
* generalised into Objects that are passed explicitly to the filters |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
99 |
* (one per MultiExchange object, and one per Exchange object possibly) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
100 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
101 |
volatile AuthenticationFilter.AuthInfo serverauth, proxyauth; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
102 |
// RedirectHandler |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
103 |
volatile int numberOfRedirects = 0; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
104 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
105 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
106 |
* MultiExchange with one final response. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
107 |
*/ |
48083 | 108 |
MultiExchange(HttpRequest userRequest, |
109 |
HttpRequestImpl requestImpl, |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
110 |
HttpClientImpl client, |
48083 | 111 |
HttpResponse.BodyHandler<T> responseHandler, |
49765 | 112 |
PushPromiseHandler<T> pushPromiseHandler, |
48083 | 113 |
AccessControlContext acc) { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
114 |
this.previous = null; |
48083 | 115 |
this.userRequest = userRequest; |
116 |
this.request = requestImpl; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
117 |
this.currentreq = request; |
49765 | 118 |
this.previousreq = null; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
119 |
this.client = client; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
120 |
this.filters = client.filterChain(); |
48083 | 121 |
this.acc = acc; |
122 |
this.executor = client.theExecutor(); |
|
123 |
this.responseHandler = responseHandler; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
124 |
|
49765 | 125 |
if (pushPromiseHandler != null) { |
126 |
Executor executor = acc == null |
|
50681 | 127 |
? this.executor.delegate() |
128 |
: new PrivilegedExecutor(this.executor.delegate(), acc); |
|
49765 | 129 |
this.pushGroup = new PushGroup<>(pushPromiseHandler, request, executor); |
130 |
} else { |
|
131 |
pushGroup = null; |
|
132 |
} |
|
133 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
134 |
this.exchange = new Exchange<>(request, this); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
135 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
136 |
|
51364
31d9e82b2e64
8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
50681
diff
changeset
|
137 |
synchronized Exchange<T> getExchange() { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
138 |
return exchange; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
139 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
140 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
141 |
HttpClientImpl client() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
142 |
return client; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
143 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
144 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
145 |
HttpClient.Version version() { |
49765 | 146 |
HttpClient.Version vers = request.version().orElse(client.version()); |
147 |
if (vers == HttpClient.Version.HTTP_2 && !request.secure() && request.proxy() != null) |
|
148 |
vers = HttpClient.Version.HTTP_1_1; |
|
149 |
return vers; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
150 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
151 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
152 |
private synchronized void setExchange(Exchange<T> exchange) { |
48083 | 153 |
if (this.exchange != null && exchange != this.exchange) { |
154 |
this.exchange.released(); |
|
155 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
156 |
this.exchange = exchange; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
157 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
158 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
159 |
private void cancelTimer() { |
51364
31d9e82b2e64
8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
50681
diff
changeset
|
160 |
if (responseTimerEvent != null) { |
31d9e82b2e64
8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
50681
diff
changeset
|
161 |
client.cancelTimer(responseTimerEvent); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
162 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
163 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
164 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
165 |
private void requestFilters(HttpRequestImpl r) throws IOException { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
166 |
Log.logTrace("Applying request filters"); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
167 |
for (HeaderFilter filter : filters) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
168 |
Log.logTrace("Applying {0}", filter); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
169 |
filter.request(r, this); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
170 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
171 |
Log.logTrace("All filters applied"); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
172 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
173 |
|
43984 | 174 |
private HttpRequestImpl responseFilters(Response response) throws IOException |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
175 |
{ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
176 |
Log.logTrace("Applying response filters"); |
49765 | 177 |
Iterator<HeaderFilter> reverseItr = filters.descendingIterator(); |
178 |
while (reverseItr.hasNext()) { |
|
179 |
HeaderFilter filter = reverseItr.next(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
180 |
Log.logTrace("Applying {0}", filter); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
181 |
HttpRequestImpl newreq = filter.response(response); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
182 |
if (newreq != null) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
183 |
Log.logTrace("New request: stopping filters"); |
43984 | 184 |
return newreq; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
185 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
186 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
187 |
Log.logTrace("All filters applied"); |
43984 | 188 |
return null; |
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(IOException cause) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
192 |
cancelled = true; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
193 |
getExchange().cancel(cause); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
194 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
195 |
|
50681 | 196 |
public CompletableFuture<HttpResponse<T>> responseAsync(Executor executor) { |
43984 | 197 |
CompletableFuture<Void> start = new MinimalFuture<>(); |
48083 | 198 |
CompletableFuture<HttpResponse<T>> cf = responseAsync0(start); |
43984 | 199 |
start.completeAsync( () -> null, executor); // trigger execution |
200 |
return cf; |
|
201 |
} |
|
202 |
||
48083 | 203 |
private CompletableFuture<HttpResponse<T>> |
204 |
responseAsync0(CompletableFuture<Void> start) { |
|
43984 | 205 |
return start.thenCompose( v -> responseAsyncImpl()) |
48083 | 206 |
.thenCompose((Response r) -> { |
207 |
Exchange<T> exch = getExchange(); |
|
208 |
return exch.readBodyAsync(responseHandler) |
|
209 |
.thenApply((T body) -> { |
|
210 |
this.response = |
|
49765 | 211 |
new HttpResponseImpl<>(r.request(), r, this.response, body, exch); |
48083 | 212 |
return this.response; |
213 |
}); |
|
214 |
}); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
215 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
216 |
|
43984 | 217 |
private CompletableFuture<Response> responseAsyncImpl() { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
218 |
CompletableFuture<Response> cf; |
48083 | 219 |
if (attempts.incrementAndGet() > max_attempts) { |
220 |
cf = failedFuture(new IOException("Too many retries", retryCause)); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
221 |
} else { |
48083 | 222 |
if (currentreq.timeout().isPresent()) { |
51364
31d9e82b2e64
8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
50681
diff
changeset
|
223 |
responseTimerEvent = ResponseTimerEvent.of(this); |
31d9e82b2e64
8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
50681
diff
changeset
|
224 |
client.registerTimer(responseTimerEvent); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
225 |
} |
43984 | 226 |
try { |
48083 | 227 |
// 1. apply request filters |
49765 | 228 |
// if currentreq == previousreq the filters have already |
229 |
// been applied once. Applying them a second time might |
|
230 |
// cause some headers values to be added twice: for |
|
231 |
// instance, the same cookie might be added again. |
|
232 |
if (currentreq != previousreq) { |
|
233 |
requestFilters(currentreq); |
|
234 |
} |
|
43984 | 235 |
} catch (IOException e) { |
48083 | 236 |
return failedFuture(e); |
43984 | 237 |
} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
238 |
Exchange<T> exch = getExchange(); |
43984 | 239 |
// 2. get response |
240 |
cf = exch.responseAsync() |
|
48083 | 241 |
.thenCompose((Response response) -> { |
242 |
HttpRequestImpl newrequest; |
|
243 |
try { |
|
244 |
// 3. apply response filters |
|
245 |
newrequest = responseFilters(response); |
|
246 |
} catch (IOException e) { |
|
247 |
return failedFuture(e); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
248 |
} |
48083 | 249 |
// 4. check filter result and repeat or continue |
250 |
if (newrequest == null) { |
|
251 |
if (attempts.get() > 1) { |
|
252 |
Log.logError("Succeeded on attempt: " + attempts); |
|
253 |
} |
|
254 |
return completedFuture(response); |
|
255 |
} else { |
|
256 |
this.response = |
|
257 |
new HttpResponseImpl<>(currentreq, response, this.response, null, exch); |
|
258 |
Exchange<T> oldExch = exch; |
|
259 |
return exch.ignoreBody().handle((r,t) -> { |
|
49765 | 260 |
previousreq = currentreq; |
48083 | 261 |
currentreq = newrequest; |
262 |
expiredOnce = false; |
|
263 |
setExchange(new Exchange<>(currentreq, this, acc)); |
|
264 |
return responseAsyncImpl(); |
|
265 |
}).thenCompose(Function.identity()); |
|
266 |
} }) |
|
267 |
.handle((response, ex) -> { |
|
268 |
// 5. handle errors and cancel any timer set |
|
269 |
cancelTimer(); |
|
270 |
if (ex == null) { |
|
271 |
assert response != null; |
|
272 |
return completedFuture(response); |
|
273 |
} |
|
274 |
// all exceptions thrown are handled here |
|
275 |
CompletableFuture<Response> errorCF = getExceptionalCF(ex); |
|
276 |
if (errorCF == null) { |
|
277 |
return responseAsyncImpl(); |
|
278 |
} else { |
|
279 |
return errorCF; |
|
280 |
} }) |
|
281 |
.thenCompose(Function.identity()); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
282 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
283 |
return cf; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
284 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
285 |
|
49765 | 286 |
private static boolean retryPostValue() { |
287 |
String s = Utils.getNetProperty("jdk.httpclient.enableAllMethodRetry"); |
|
50681 | 288 |
if (s == null) |
289 |
return false; |
|
290 |
return s.isEmpty() ? true : Boolean.parseBoolean(s); |
|
291 |
} |
|
292 |
||
293 |
private static boolean retryConnect() { |
|
294 |
String s = Utils.getNetProperty("jdk.httpclient.disableRetryConnect"); |
|
295 |
if (s == null) |
|
296 |
return false; |
|
297 |
return s.isEmpty() ? true : Boolean.parseBoolean(s); |
|
49765 | 298 |
} |
299 |
||
300 |
/** True if ALL ( even non-idempotent ) requests can be automatic retried. */ |
|
301 |
private static final boolean RETRY_ALWAYS = retryPostValue(); |
|
50681 | 302 |
/** True if ConnectException should cause a retry. Enabled by default */ |
303 |
private static final boolean RETRY_CONNECT = retryConnect(); |
|
49765 | 304 |
|
305 |
/** Returns true is given request has an idempotent method. */ |
|
306 |
private static boolean isIdempotentRequest(HttpRequest request) { |
|
307 |
String method = request.method(); |
|
308 |
switch (method) { |
|
309 |
case "GET" : |
|
310 |
case "HEAD" : |
|
311 |
return true; |
|
312 |
default : |
|
313 |
return false; |
|
314 |
} |
|
315 |
} |
|
316 |
||
317 |
/** Returns true if the given request can be automatically retried. */ |
|
318 |
private static boolean canRetryRequest(HttpRequest request) { |
|
50681 | 319 |
if (RETRY_ALWAYS) |
320 |
return true; |
|
49765 | 321 |
if (isIdempotentRequest(request)) |
322 |
return true; |
|
323 |
return false; |
|
324 |
} |
|
325 |
||
50681 | 326 |
private boolean retryOnFailure(Throwable t) { |
327 |
return t instanceof ConnectionExpiredException |
|
328 |
|| (RETRY_CONNECT && (t instanceof ConnectException)); |
|
329 |
} |
|
330 |
||
331 |
private Throwable retryCause(Throwable t) { |
|
332 |
Throwable cause = t instanceof ConnectionExpiredException ? t.getCause() : t; |
|
333 |
return cause == null ? t : cause; |
|
334 |
} |
|
335 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
336 |
/** |
48083 | 337 |
* Takes a Throwable and returns a suitable CompletableFuture that is |
338 |
* completed exceptionally, or null. |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
339 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
340 |
private CompletableFuture<Response> getExceptionalCF(Throwable t) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
341 |
if ((t instanceof CompletionException) || (t instanceof ExecutionException)) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
342 |
if (t.getCause() != null) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
343 |
t = t.getCause(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
344 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
345 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
346 |
if (cancelled && t instanceof IOException) { |
51364
31d9e82b2e64
8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
50681
diff
changeset
|
347 |
if (!(t instanceof HttpTimeoutException)) { |
31d9e82b2e64
8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
50681
diff
changeset
|
348 |
t = toTimeoutException((IOException)t); |
31d9e82b2e64
8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
50681
diff
changeset
|
349 |
} |
50681 | 350 |
} else if (retryOnFailure(t)) { |
351 |
Throwable cause = retryCause(t); |
|
49765 | 352 |
|
50681 | 353 |
if (!(t instanceof ConnectException)) { |
354 |
if (!canRetryRequest(currentreq)) { |
|
355 |
return failedFuture(cause); // fails with original cause |
|
356 |
} |
|
49765 | 357 |
} |
358 |
||
48083 | 359 |
// allow the retry mechanism to do its work |
49765 | 360 |
retryCause = cause; |
48083 | 361 |
if (!expiredOnce) { |
49765 | 362 |
if (debug.on()) |
50681 | 363 |
debug.log(t.getClass().getSimpleName() + " (async): retrying...", t); |
48083 | 364 |
expiredOnce = true; |
49765 | 365 |
// The connection was abruptly closed. |
366 |
// We return null to retry the same request a second time. |
|
367 |
// The request filters have already been applied to the |
|
368 |
// currentreq, so we set previousreq = currentreq to |
|
369 |
// prevent them from being applied again. |
|
370 |
previousreq = currentreq; |
|
48083 | 371 |
return null; |
372 |
} else { |
|
50681 | 373 |
if (debug.on()) { |
374 |
debug.log(t.getClass().getSimpleName() |
|
375 |
+ " (async): already retried once.", t); |
|
376 |
} |
|
377 |
t = cause; |
|
48083 | 378 |
} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
379 |
} |
48083 | 380 |
return failedFuture(t); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
381 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
382 |
|
51364
31d9e82b2e64
8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
50681
diff
changeset
|
383 |
private HttpTimeoutException toTimeoutException(IOException ioe) { |
31d9e82b2e64
8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
50681
diff
changeset
|
384 |
HttpTimeoutException t = null; |
31d9e82b2e64
8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
50681
diff
changeset
|
385 |
|
31d9e82b2e64
8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
50681
diff
changeset
|
386 |
// more specific, "request timed out", when connected |
31d9e82b2e64
8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
50681
diff
changeset
|
387 |
Exchange<?> exchange = getExchange(); |
31d9e82b2e64
8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
50681
diff
changeset
|
388 |
if (exchange != null) { |
31d9e82b2e64
8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
50681
diff
changeset
|
389 |
ExchangeImpl<?> exchangeImpl = exchange.exchImpl; |
31d9e82b2e64
8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
50681
diff
changeset
|
390 |
if (exchangeImpl != null) { |
31d9e82b2e64
8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
50681
diff
changeset
|
391 |
if (exchangeImpl.connection().connected()) { |
31d9e82b2e64
8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
50681
diff
changeset
|
392 |
t = new HttpTimeoutException("request timed out"); |
31d9e82b2e64
8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
50681
diff
changeset
|
393 |
t.initCause(ioe); |
31d9e82b2e64
8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
50681
diff
changeset
|
394 |
} |
31d9e82b2e64
8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
50681
diff
changeset
|
395 |
} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
396 |
} |
51364
31d9e82b2e64
8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
50681
diff
changeset
|
397 |
if (t == null) { |
31d9e82b2e64
8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
50681
diff
changeset
|
398 |
t = new HttpConnectTimeoutException("HTTP connect timed out"); |
31d9e82b2e64
8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
50681
diff
changeset
|
399 |
t.initCause(new ConnectException("HTTP connect timed out")); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
400 |
} |
51364
31d9e82b2e64
8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
50681
diff
changeset
|
401 |
return t; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
402 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
403 |
} |