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