author | chegar |
Wed, 11 Apr 2018 15:00:35 +0100 | |
branch | http-client-branch |
changeset 56417 | 312811f70c43 |
parent 56345 | eb72d194235c |
child 56437 | f8b3f053cfbb |
permissions | -rw-r--r-- |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1 |
/* |
56079
d23b02f37fce
http-client-branch: more remaining impl types to internal
chegar
parents:
56043
diff
changeset
|
2 |
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
4 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. Oracle designates this |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
10 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
15 |
* accompanied this code). |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
16 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License version |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
20 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
22 |
* or visit www.oracle.com if you need additional information or have any |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
23 |
* questions. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
24 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
25 |
|
56092
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
26 |
package jdk.internal.net.http; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
27 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
28 |
import java.io.IOException; |
48083 | 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.time.Duration; |
56264 | 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; |
|
56079
d23b02f37fce
http-client-branch: more remaining impl types to internal
chegar
parents:
56043
diff
changeset
|
40 |
|
56089
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56079
diff
changeset
|
41 |
import java.net.http.HttpClient; |
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56079
diff
changeset
|
42 |
import java.net.http.HttpRequest; |
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56079
diff
changeset
|
43 |
import java.net.http.HttpResponse; |
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56079
diff
changeset
|
44 |
import java.net.http.HttpResponse.PushPromiseHandler; |
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56079
diff
changeset
|
45 |
import java.net.http.HttpTimeoutException; |
56092
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
46 |
import jdk.internal.net.http.common.Log; |
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
47 |
import jdk.internal.net.http.common.MinimalFuture; |
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
48 |
import jdk.internal.net.http.common.ConnectionExpiredException; |
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
49 |
import jdk.internal.net.http.common.Utils; |
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
50 |
import static jdk.internal.net.http.common.MinimalFuture.completedFuture; |
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
51 |
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
|
52 |
|
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 |
* 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
|
55 |
* - manages filters |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
56 |
* - retries due to filters. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
57 |
* - 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
|
58 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
59 |
* 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
|
60 |
*/ |
56010 | 61 |
class MultiExchange<T> { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
62 |
|
48083 | 63 |
static final boolean DEBUG = Utils.DEBUG; // Revisit: temporary dev flag. |
64 |
static final System.Logger DEBUG_LOGGER = |
|
65 |
Utils.getDebugLogger("MultiExchange"::toString, DEBUG); |
|
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; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
72 |
final Executor executor; |
48083 | 73 |
final AtomicInteger attempts = new AtomicInteger(); |
56345
eb72d194235c
http-client-branch: fix HTTP/1.1 retry to avoid duplication of header values
dfuchs
parents:
56282
diff
changeset
|
74 |
HttpRequestImpl currentreq; // used for retries & redirect |
eb72d194235c
http-client-branch: fix HTTP/1.1 retry to avoid duplication of header values
dfuchs
parents:
56282
diff
changeset
|
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 |
|
56264 | 90 |
private final LinkedList<HeaderFilter> filters; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
91 |
TimedEvent timedEvent; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
92 |
volatile boolean cancelled; |
56010 | 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, |
56010 | 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; |
56345
eb72d194235c
http-client-branch: fix HTTP/1.1 retry to avoid duplication of header values
dfuchs
parents:
56282
diff
changeset
|
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 |
|
56010 | 125 |
if (pushPromiseHandler != null) { |
56282
10cebcd18d47
http-client-branch: verify that dependent actions registered by the user in the CF we return are always executed either in the user thread that joins or asynchronously in the executor.
dfuchs
parents:
56264
diff
changeset
|
126 |
Executor executor = acc == null |
10cebcd18d47
http-client-branch: verify that dependent actions registered by the user in the CF we return are always executed either in the user thread that joins or asynchronously in the executor.
dfuchs
parents:
56264
diff
changeset
|
127 |
? this.executor |
10cebcd18d47
http-client-branch: verify that dependent actions registered by the user in the CF we return are always executed either in the user thread that joins or asynchronously in the executor.
dfuchs
parents:
56264
diff
changeset
|
128 |
: new PrivilegedExecutor(this.executor, acc); |
10cebcd18d47
http-client-branch: verify that dependent actions registered by the user in the CF we return are always executed either in the user thread that joins or asynchronously in the executor.
dfuchs
parents:
56264
diff
changeset
|
129 |
this.pushGroup = new PushGroup<>(pushPromiseHandler, request, executor); |
56010 | 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 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
137 |
private synchronized Exchange<T> getExchange() { |
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() { |
56043
08e8e41841cf
http-client-branch: changed default protocol version when plain http over proxy requested.
michaelm
parents:
56010
diff
changeset
|
146 |
HttpClient.Version vers = request.version().orElse(client.version()); |
08e8e41841cf
http-client-branch: changed default protocol version when plain http over proxy requested.
michaelm
parents:
56010
diff
changeset
|
147 |
if (vers == HttpClient.Version.HTTP_2 && !request.secure() && request.proxy() != null) |
08e8e41841cf
http-client-branch: changed default protocol version when plain http over proxy requested.
michaelm
parents:
56010
diff
changeset
|
148 |
vers = HttpClient.Version.HTTP_1_1; |
08e8e41841cf
http-client-branch: changed default protocol version when plain http over proxy requested.
michaelm
parents:
56010
diff
changeset
|
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() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
160 |
if (timedEvent != null) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
161 |
client.cancelTimer(timedEvent); |
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"); |
56264 | 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 |
|
48083 | 196 |
public CompletableFuture<HttpResponse<T>> responseAsync() { |
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 = |
|
56253
875dbf6234f2
http-client-branch: set correct request returned by response when redirecting, and add additional test coverage
chegar
parents:
56210
diff
changeset
|
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()) { |
223 |
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
|
224 |
client.registerTimer(timedEvent); |
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 |
56345
eb72d194235c
http-client-branch: fix HTTP/1.1 retry to avoid duplication of header values
dfuchs
parents:
56282
diff
changeset
|
228 |
// if currentreq == previousreq the filters have already |
eb72d194235c
http-client-branch: fix HTTP/1.1 retry to avoid duplication of header values
dfuchs
parents:
56282
diff
changeset
|
229 |
// been applied once. Applying them a second time might |
eb72d194235c
http-client-branch: fix HTTP/1.1 retry to avoid duplication of header values
dfuchs
parents:
56282
diff
changeset
|
230 |
// cause some headers values to be added twice: for |
eb72d194235c
http-client-branch: fix HTTP/1.1 retry to avoid duplication of header values
dfuchs
parents:
56282
diff
changeset
|
231 |
// instance, the same cookie might be added again. |
eb72d194235c
http-client-branch: fix HTTP/1.1 retry to avoid duplication of header values
dfuchs
parents:
56282
diff
changeset
|
232 |
if (currentreq != previousreq) { |
eb72d194235c
http-client-branch: fix HTTP/1.1 retry to avoid duplication of header values
dfuchs
parents:
56282
diff
changeset
|
233 |
requestFilters(currentreq); |
eb72d194235c
http-client-branch: fix HTTP/1.1 retry to avoid duplication of header values
dfuchs
parents:
56282
diff
changeset
|
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) -> { |
|
56345
eb72d194235c
http-client-branch: fix HTTP/1.1 retry to avoid duplication of header values
dfuchs
parents:
56282
diff
changeset
|
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 |
|
56417
312811f70c43
http-client-branch: review comment - do not retry non-idempotent requests
chegar
parents:
56345
diff
changeset
|
286 |
private static boolean retryPostValue() { |
312811f70c43
http-client-branch: review comment - do not retry non-idempotent requests
chegar
parents:
56345
diff
changeset
|
287 |
String s = Utils.getNetProperty("jdk.httpclient.enableAllMethodRetry"); |
312811f70c43
http-client-branch: review comment - do not retry non-idempotent requests
chegar
parents:
56345
diff
changeset
|
288 |
if (s == "" || "true".equals(s)) |
312811f70c43
http-client-branch: review comment - do not retry non-idempotent requests
chegar
parents:
56345
diff
changeset
|
289 |
return true; |
312811f70c43
http-client-branch: review comment - do not retry non-idempotent requests
chegar
parents:
56345
diff
changeset
|
290 |
return false; |
312811f70c43
http-client-branch: review comment - do not retry non-idempotent requests
chegar
parents:
56345
diff
changeset
|
291 |
} |
312811f70c43
http-client-branch: review comment - do not retry non-idempotent requests
chegar
parents:
56345
diff
changeset
|
292 |
|
312811f70c43
http-client-branch: review comment - do not retry non-idempotent requests
chegar
parents:
56345
diff
changeset
|
293 |
/** True if ALL ( even non-idempotent ) requests can be automatic retried. */ |
312811f70c43
http-client-branch: review comment - do not retry non-idempotent requests
chegar
parents:
56345
diff
changeset
|
294 |
private static final boolean RETRY_ALWAYS = retryPostValue(); |
312811f70c43
http-client-branch: review comment - do not retry non-idempotent requests
chegar
parents:
56345
diff
changeset
|
295 |
|
312811f70c43
http-client-branch: review comment - do not retry non-idempotent requests
chegar
parents:
56345
diff
changeset
|
296 |
/** Returns true is given request has an idempotent method. */ |
312811f70c43
http-client-branch: review comment - do not retry non-idempotent requests
chegar
parents:
56345
diff
changeset
|
297 |
private static boolean isIdempotentRequest(HttpRequest request) { |
312811f70c43
http-client-branch: review comment - do not retry non-idempotent requests
chegar
parents:
56345
diff
changeset
|
298 |
String method = request.method(); |
312811f70c43
http-client-branch: review comment - do not retry non-idempotent requests
chegar
parents:
56345
diff
changeset
|
299 |
switch (method) { |
312811f70c43
http-client-branch: review comment - do not retry non-idempotent requests
chegar
parents:
56345
diff
changeset
|
300 |
case "GET" : |
312811f70c43
http-client-branch: review comment - do not retry non-idempotent requests
chegar
parents:
56345
diff
changeset
|
301 |
case "HEAD" : |
312811f70c43
http-client-branch: review comment - do not retry non-idempotent requests
chegar
parents:
56345
diff
changeset
|
302 |
return true; |
312811f70c43
http-client-branch: review comment - do not retry non-idempotent requests
chegar
parents:
56345
diff
changeset
|
303 |
default : |
312811f70c43
http-client-branch: review comment - do not retry non-idempotent requests
chegar
parents:
56345
diff
changeset
|
304 |
return false; |
312811f70c43
http-client-branch: review comment - do not retry non-idempotent requests
chegar
parents:
56345
diff
changeset
|
305 |
} |
312811f70c43
http-client-branch: review comment - do not retry non-idempotent requests
chegar
parents:
56345
diff
changeset
|
306 |
} |
312811f70c43
http-client-branch: review comment - do not retry non-idempotent requests
chegar
parents:
56345
diff
changeset
|
307 |
|
312811f70c43
http-client-branch: review comment - do not retry non-idempotent requests
chegar
parents:
56345
diff
changeset
|
308 |
/** Returns true if the given request can be automatically retried. */ |
312811f70c43
http-client-branch: review comment - do not retry non-idempotent requests
chegar
parents:
56345
diff
changeset
|
309 |
private static boolean canRetryRequest(HttpRequest request) { |
312811f70c43
http-client-branch: review comment - do not retry non-idempotent requests
chegar
parents:
56345
diff
changeset
|
310 |
if (isIdempotentRequest(request)) |
312811f70c43
http-client-branch: review comment - do not retry non-idempotent requests
chegar
parents:
56345
diff
changeset
|
311 |
return true; |
312811f70c43
http-client-branch: review comment - do not retry non-idempotent requests
chegar
parents:
56345
diff
changeset
|
312 |
if (RETRY_ALWAYS) |
312811f70c43
http-client-branch: review comment - do not retry non-idempotent requests
chegar
parents:
56345
diff
changeset
|
313 |
return true; |
312811f70c43
http-client-branch: review comment - do not retry non-idempotent requests
chegar
parents:
56345
diff
changeset
|
314 |
return false; |
312811f70c43
http-client-branch: review comment - do not retry non-idempotent requests
chegar
parents:
56345
diff
changeset
|
315 |
} |
312811f70c43
http-client-branch: review comment - do not retry non-idempotent requests
chegar
parents:
56345
diff
changeset
|
316 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
317 |
/** |
48083 | 318 |
* Takes a Throwable and returns a suitable CompletableFuture that is |
319 |
* completed exceptionally, or null. |
|
42460
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 |
private CompletableFuture<Response> getExceptionalCF(Throwable t) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
322 |
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
|
323 |
if (t.getCause() != null) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
324 |
t = t.getCause(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
325 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
326 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
327 |
if (cancelled && t instanceof IOException) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
328 |
t = new HttpTimeoutException("request timed out"); |
48083 | 329 |
} else if (t instanceof ConnectionExpiredException) { |
56417
312811f70c43
http-client-branch: review comment - do not retry non-idempotent requests
chegar
parents:
56345
diff
changeset
|
330 |
Throwable cause = t; |
312811f70c43
http-client-branch: review comment - do not retry non-idempotent requests
chegar
parents:
56345
diff
changeset
|
331 |
if (t.getCause() != null) { |
312811f70c43
http-client-branch: review comment - do not retry non-idempotent requests
chegar
parents:
56345
diff
changeset
|
332 |
cause = t.getCause(); // unwrap the ConnectionExpiredException |
312811f70c43
http-client-branch: review comment - do not retry non-idempotent requests
chegar
parents:
56345
diff
changeset
|
333 |
} |
312811f70c43
http-client-branch: review comment - do not retry non-idempotent requests
chegar
parents:
56345
diff
changeset
|
334 |
|
312811f70c43
http-client-branch: review comment - do not retry non-idempotent requests
chegar
parents:
56345
diff
changeset
|
335 |
if (!canRetryRequest(currentreq)) { |
312811f70c43
http-client-branch: review comment - do not retry non-idempotent requests
chegar
parents:
56345
diff
changeset
|
336 |
return failedFuture(cause); // fails with original cause |
312811f70c43
http-client-branch: review comment - do not retry non-idempotent requests
chegar
parents:
56345
diff
changeset
|
337 |
} |
312811f70c43
http-client-branch: review comment - do not retry non-idempotent requests
chegar
parents:
56345
diff
changeset
|
338 |
|
48083 | 339 |
// allow the retry mechanism to do its work |
56417
312811f70c43
http-client-branch: review comment - do not retry non-idempotent requests
chegar
parents:
56345
diff
changeset
|
340 |
retryCause = cause; |
48083 | 341 |
if (!expiredOnce) { |
342 |
DEBUG_LOGGER.log(Level.DEBUG, |
|
56210 | 343 |
"ConnectionExpiredException (async): retrying...", |
48083 | 344 |
t); |
345 |
expiredOnce = true; |
|
56345
eb72d194235c
http-client-branch: fix HTTP/1.1 retry to avoid duplication of header values
dfuchs
parents:
56282
diff
changeset
|
346 |
// The connection was abruptly closed. |
eb72d194235c
http-client-branch: fix HTTP/1.1 retry to avoid duplication of header values
dfuchs
parents:
56282
diff
changeset
|
347 |
// We return null to retry the same request a second time. |
eb72d194235c
http-client-branch: fix HTTP/1.1 retry to avoid duplication of header values
dfuchs
parents:
56282
diff
changeset
|
348 |
// The request filters have already been applied to the |
eb72d194235c
http-client-branch: fix HTTP/1.1 retry to avoid duplication of header values
dfuchs
parents:
56282
diff
changeset
|
349 |
// currentreq, so we set previousreq = currentreq to |
eb72d194235c
http-client-branch: fix HTTP/1.1 retry to avoid duplication of header values
dfuchs
parents:
56282
diff
changeset
|
350 |
// prevent them from being applied again. |
eb72d194235c
http-client-branch: fix HTTP/1.1 retry to avoid duplication of header values
dfuchs
parents:
56282
diff
changeset
|
351 |
previousreq = currentreq; |
48083 | 352 |
return null; |
353 |
} else { |
|
354 |
DEBUG_LOGGER.log(Level.DEBUG, |
|
56210 | 355 |
"ConnectionExpiredException (async): already retried once.", |
48083 | 356 |
t); |
357 |
if (t.getCause() != null) t = t.getCause(); |
|
358 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
359 |
} |
48083 | 360 |
return failedFuture(t); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
361 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
362 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
363 |
class TimedEvent extends TimeoutEvent { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
364 |
TimedEvent(Duration duration) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
365 |
super(duration); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
366 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
367 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
368 |
public void handle() { |
48083 | 369 |
DEBUG_LOGGER.log(Level.DEBUG, |
370 |
"Cancelling MultiExchange due to timeout for request %s", |
|
371 |
request); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
372 |
cancel(new HttpTimeoutException("request timed out")); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
373 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
374 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
375 |
} |