author | chegar |
Wed, 20 Jun 2018 09:05:57 -0700 | |
changeset 50681 | 4254bed3c09d |
parent 49944 | 4690a2871b44 |
child 50985 | cd41f34e548c |
child 56795 | 03ece2518428 |
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; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
29 |
import java.net.InetSocketAddress; |
49765 | 30 |
import java.net.http.HttpResponse.BodyHandler; |
31 |
import java.net.http.HttpResponse.BodySubscriber; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
32 |
import java.nio.ByteBuffer; |
48083 | 33 |
import java.util.Objects; |
42460
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.LinkedList; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
36 |
import java.util.List; |
48083 | 37 |
import java.util.concurrent.ConcurrentLinkedDeque; |
42460
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.Flow; |
49765 | 40 |
import jdk.internal.net.http.common.Demand; |
41 |
import jdk.internal.net.http.common.Log; |
|
42 |
import jdk.internal.net.http.common.FlowTube; |
|
43 |
import jdk.internal.net.http.common.Logger; |
|
44 |
import jdk.internal.net.http.common.SequentialScheduler; |
|
45 |
import jdk.internal.net.http.common.MinimalFuture; |
|
46 |
import jdk.internal.net.http.common.Utils; |
|
47 |
import static java.net.http.HttpClient.Version.HTTP_1_1; |
|
50681 | 48 |
import static jdk.internal.net.http.common.Utils.wrapWithExtraDetail; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
49 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
50 |
/** |
48083 | 51 |
* Encapsulates one HTTP/1.1 request/response exchange. |
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 |
class Http1Exchange<T> extends ExchangeImpl<T> { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
54 |
|
49765 | 55 |
final Logger debug = Utils.getDebugLogger(this::dbgString, Utils.DEBUG); |
48083 | 56 |
final HttpRequestImpl request; // main request |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
57 |
final Http1Request requestAction; |
44639
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43999
diff
changeset
|
58 |
private volatile Http1Response<T> response; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
59 |
final HttpConnection connection; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
60 |
final HttpClientImpl client; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
61 |
final Executor executor; |
48083 | 62 |
private final Http1AsyncReceiver asyncReceiver; |
63 |
||
64 |
/** Records a possible cancellation raised before any operation |
|
65 |
* has been initiated, or an error received while sending the request. */ |
|
66 |
private Throwable failed; |
|
67 |
private final List<CompletableFuture<?>> operations; // used for cancel |
|
68 |
||
69 |
/** Must be held when operating on any internal state or data. */ |
|
70 |
private final Object lock = new Object(); |
|
71 |
||
72 |
/** Holds the outgoing data, either the headers or a request body part. Or |
|
73 |
* an error from the request body publisher. At most there can be ~2 pieces |
|
74 |
* of outgoing data ( onComplete|onError can be invoked without demand ).*/ |
|
75 |
final ConcurrentLinkedDeque<DataPair> outgoing = new ConcurrentLinkedDeque<>(); |
|
76 |
||
77 |
/** The write publisher, responsible for writing the complete request ( both |
|
78 |
* headers and body ( if any ). */ |
|
79 |
private final Http1Publisher writePublisher = new Http1Publisher(); |
|
80 |
||
81 |
/** Completed when the header have been published, or there is an error */ |
|
49765 | 82 |
private final CompletableFuture<ExchangeImpl<T>> headersSentCF = new MinimalFuture<>(); |
48083 | 83 |
/** Completed when the body has been published, or there is an error */ |
49765 | 84 |
private final CompletableFuture<ExchangeImpl<T>> bodySentCF = new MinimalFuture<>(); |
48083 | 85 |
|
86 |
/** The subscriber to the request's body published. Maybe null. */ |
|
87 |
private volatile Http1BodySubscriber bodySubscriber; |
|
88 |
||
89 |
enum State { INITIAL, |
|
90 |
HEADERS, |
|
91 |
BODY, |
|
92 |
ERROR, // terminal state |
|
93 |
COMPLETING, |
|
94 |
COMPLETED } // terminal state |
|
95 |
||
96 |
private State state = State.INITIAL; |
|
97 |
||
98 |
/** A carrier for either data or an error. Used to carry data, and communicate |
|
99 |
* errors from the request ( both headers and body ) to the exchange. */ |
|
100 |
static class DataPair { |
|
101 |
Throwable throwable; |
|
102 |
List<ByteBuffer> data; |
|
103 |
DataPair(List<ByteBuffer> data, Throwable throwable){ |
|
104 |
this.data = data; |
|
105 |
this.throwable = throwable; |
|
106 |
} |
|
107 |
@Override |
|
108 |
public String toString() { |
|
109 |
return "DataPair [data=" + data + ", throwable=" + throwable + "]"; |
|
110 |
} |
|
111 |
} |
|
112 |
||
113 |
/** An abstract supertype for HTTP/1.1 body subscribers. There are two |
|
114 |
* concrete implementations: {@link Http1Request.StreamSubscriber}, and |
|
115 |
* {@link Http1Request.FixedContentSubscriber}, for receiving chunked and |
|
116 |
* fixed length bodies, respectively. */ |
|
117 |
static abstract class Http1BodySubscriber implements Flow.Subscriber<ByteBuffer> { |
|
49765 | 118 |
final MinimalFuture<Flow.Subscription> whenSubscribed = new MinimalFuture<>(); |
119 |
private volatile Flow.Subscription subscription; |
|
120 |
volatile boolean complete; |
|
121 |
private final Logger debug; |
|
122 |
Http1BodySubscriber(Logger debug) { |
|
123 |
assert debug != null; |
|
124 |
this.debug = debug; |
|
125 |
} |
|
48083 | 126 |
|
127 |
/** Final sentinel in the stream of request body. */ |
|
128 |
static final List<ByteBuffer> COMPLETED = List.of(ByteBuffer.allocate(0)); |
|
129 |
||
49765 | 130 |
final void request(long n) { |
131 |
if (debug.on()) |
|
132 |
debug.log("Http1BodySubscriber requesting %d, from %s", |
|
133 |
n, subscription); |
|
48083 | 134 |
subscription.request(n); |
135 |
} |
|
136 |
||
50681 | 137 |
/** A current-state message suitable for inclusion in an exception detail message. */ |
138 |
abstract String currentStateMessage(); |
|
139 |
||
49765 | 140 |
final boolean isSubscribed() { |
141 |
return subscription != null; |
|
142 |
} |
|
143 |
||
144 |
final void setSubscription(Flow.Subscription subscription) { |
|
145 |
this.subscription = subscription; |
|
146 |
whenSubscribed.complete(subscription); |
|
147 |
} |
|
148 |
||
149 |
final void cancelSubscription() { |
|
150 |
try { |
|
151 |
subscription.cancel(); |
|
152 |
} catch(Throwable t) { |
|
153 |
String msg = "Ignoring exception raised when canceling BodyPublisher subscription"; |
|
154 |
if (debug.on()) debug.log("%s: %s", msg, t); |
|
155 |
Log.logError("{0}: {1}", msg, (Object)t); |
|
156 |
} |
|
157 |
} |
|
158 |
||
159 |
static Http1BodySubscriber completeSubscriber(Logger debug) { |
|
160 |
return new Http1BodySubscriber(debug) { |
|
48083 | 161 |
@Override public void onSubscribe(Flow.Subscription subscription) { error(); } |
162 |
@Override public void onNext(ByteBuffer item) { error(); } |
|
163 |
@Override public void onError(Throwable throwable) { error(); } |
|
164 |
@Override public void onComplete() { error(); } |
|
50681 | 165 |
@Override String currentStateMessage() { return null; } |
48083 | 166 |
private void error() { |
167 |
throw new InternalError("should not reach here"); |
|
168 |
} |
|
169 |
}; |
|
170 |
} |
|
171 |
} |
|
42460
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 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
174 |
public String toString() { |
48083 | 175 |
return "HTTP/1.1 " + request.toString(); |
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 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
178 |
HttpRequestImpl request() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
179 |
return request; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
180 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
181 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
182 |
Http1Exchange(Exchange<T> exchange, HttpConnection connection) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
183 |
throws IOException |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
184 |
{ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
185 |
super(exchange); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
186 |
this.request = exchange.request(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
187 |
this.client = exchange.client(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
188 |
this.executor = exchange.executor(); |
44639
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43999
diff
changeset
|
189 |
this.operations = new LinkedList<>(); |
48083 | 190 |
operations.add(headersSentCF); |
191 |
operations.add(bodySentCF); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
192 |
if (connection != null) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
193 |
this.connection = connection; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
194 |
} else { |
48083 | 195 |
InetSocketAddress addr = request.getAddress(); |
196 |
this.connection = HttpConnection.getConnection(addr, client, request, HTTP_1_1); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
197 |
} |
48083 | 198 |
this.requestAction = new Http1Request(request, this); |
199 |
this.asyncReceiver = new Http1AsyncReceiver(executor, this); |
|
200 |
} |
|
201 |
||
202 |
@Override |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
203 |
HttpConnection connection() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
204 |
return connection; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
205 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
206 |
|
48083 | 207 |
private void connectFlows(HttpConnection connection) { |
208 |
FlowTube tube = connection.getConnectionFlow(); |
|
49765 | 209 |
if (debug.on()) debug.log("%s connecting flows", tube); |
48083 | 210 |
|
211 |
// Connect the flow to our Http1TubeSubscriber: |
|
212 |
// asyncReceiver.subscriber(). |
|
213 |
tube.connectFlows(writePublisher, |
|
214 |
asyncReceiver.subscriber()); |
|
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 |
@Override |
48083 | 218 |
CompletableFuture<ExchangeImpl<T>> sendHeadersAsync() { |
219 |
// create the response before sending the request headers, so that |
|
220 |
// the response can set the appropriate receivers. |
|
49765 | 221 |
if (debug.on()) debug.log("Sending headers only"); |
50681 | 222 |
// If the first attempt to read something triggers EOF, or |
223 |
// IOException("channel reset by peer"), we're going to retry. |
|
224 |
// Instruct the asyncReceiver to throw ConnectionExpiredException |
|
225 |
// to force a retry. |
|
226 |
asyncReceiver.setRetryOnError(true); |
|
48083 | 227 |
if (response == null) { |
228 |
response = new Http1Response<>(connection, this, asyncReceiver); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
229 |
} |
48083 | 230 |
|
49765 | 231 |
if (debug.on()) debug.log("response created in advance"); |
48083 | 232 |
|
233 |
CompletableFuture<Void> connectCF; |
|
234 |
if (!connection.connected()) { |
|
49765 | 235 |
if (debug.on()) debug.log("initiating connect async"); |
48083 | 236 |
connectCF = connection.connectAsync(); |
49944 | 237 |
Throwable cancelled; |
48083 | 238 |
synchronized (lock) { |
49944 | 239 |
if ((cancelled = failed) == null) { |
240 |
operations.add(connectCF); |
|
241 |
} |
|
242 |
} |
|
243 |
if (cancelled != null) { |
|
244 |
if (client.isSelectorThread()) { |
|
245 |
executor.execute(() -> |
|
246 |
connectCF.completeExceptionally(cancelled)); |
|
247 |
} else { |
|
248 |
connectCF.completeExceptionally(cancelled); |
|
249 |
} |
|
48083 | 250 |
} |
251 |
} else { |
|
252 |
connectCF = new MinimalFuture<>(); |
|
253 |
connectCF.complete(null); |
|
254 |
} |
|
255 |
||
256 |
return connectCF |
|
257 |
.thenCompose(unused -> { |
|
258 |
CompletableFuture<Void> cf = new MinimalFuture<>(); |
|
259 |
try { |
|
260 |
connectFlows(connection); |
|
261 |
||
49765 | 262 |
if (debug.on()) debug.log("requestAction.headers"); |
48083 | 263 |
List<ByteBuffer> data = requestAction.headers(); |
264 |
synchronized (lock) { |
|
265 |
state = State.HEADERS; |
|
266 |
} |
|
49765 | 267 |
if (debug.on()) debug.log("setting outgoing with headers"); |
48083 | 268 |
assert outgoing.isEmpty() : "Unexpected outgoing:" + outgoing; |
269 |
appendToOutgoing(data); |
|
270 |
cf.complete(null); |
|
271 |
return cf; |
|
272 |
} catch (Throwable t) { |
|
49765 | 273 |
if (debug.on()) debug.log("Failed to send headers: %s", t); |
50681 | 274 |
headersSentCF.completeExceptionally(t); |
275 |
bodySentCF.completeExceptionally(t); |
|
48083 | 276 |
connection.close(); |
277 |
cf.completeExceptionally(t); |
|
278 |
return cf; |
|
279 |
} }) |
|
280 |
.thenCompose(unused -> headersSentCF); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
281 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
282 |
|
50681 | 283 |
private void cancelIfFailed(Flow.Subscription s) { |
284 |
asyncReceiver.whenFinished.whenCompleteAsync((r,t) -> { |
|
285 |
if (debug.on()) debug.log("asyncReceiver finished (failed=%s)", t); |
|
286 |
if (t != null) { |
|
287 |
s.cancel(); |
|
288 |
// Don't complete exceptionally here as 't' |
|
289 |
// might not be the right exception: it will |
|
290 |
// not have been decorated yet. |
|
291 |
// t is an exception raised by the read side, |
|
292 |
// an EOFException or Broken Pipe... |
|
293 |
// We are cancelling the BodyPublisher subscription |
|
294 |
// and completing bodySentCF to allow the next step |
|
295 |
// to flow and call readHeaderAsync, which will |
|
296 |
// get the right exception from the asyncReceiver. |
|
297 |
bodySentCF.complete(this); |
|
298 |
} |
|
299 |
}, executor); |
|
300 |
} |
|
301 |
||
48083 | 302 |
@Override |
303 |
CompletableFuture<ExchangeImpl<T>> sendBodyAsync() { |
|
304 |
assert headersSentCF.isDone(); |
|
50681 | 305 |
if (debug.on()) debug.log("sendBodyAsync"); |
48083 | 306 |
try { |
307 |
bodySubscriber = requestAction.continueRequest(); |
|
50681 | 308 |
if (debug.on()) debug.log("bodySubscriber is %s", |
309 |
bodySubscriber == null ? null : bodySubscriber.getClass()); |
|
48083 | 310 |
if (bodySubscriber == null) { |
49765 | 311 |
bodySubscriber = Http1BodySubscriber.completeSubscriber(debug); |
48083 | 312 |
appendToOutgoing(Http1BodySubscriber.COMPLETED); |
313 |
} else { |
|
49765 | 314 |
// start |
315 |
bodySubscriber.whenSubscribed |
|
50681 | 316 |
.thenAccept((s) -> cancelIfFailed(s)) |
49765 | 317 |
.thenAccept((s) -> requestMoreBody()); |
48083 | 318 |
} |
319 |
} catch (Throwable t) { |
|
49765 | 320 |
cancelImpl(t); |
48083 | 321 |
bodySentCF.completeExceptionally(t); |
322 |
} |
|
50681 | 323 |
return Utils.wrapForDebug(debug, "sendBodyAsync", bodySentCF); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
324 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
325 |
|
48083 | 326 |
@Override |
327 |
CompletableFuture<Response> getResponseAsync(Executor executor) { |
|
50681 | 328 |
if (debug.on()) debug.log("reading headers"); |
48083 | 329 |
CompletableFuture<Response> cf = response.readHeadersAsync(executor); |
330 |
Throwable cause; |
|
331 |
synchronized (lock) { |
|
332 |
operations.add(cf); |
|
333 |
cause = failed; |
|
334 |
failed = null; |
|
335 |
} |
|
336 |
||
337 |
if (cause != null) { |
|
338 |
Log.logTrace("Http1Exchange: request [{0}/timeout={1}ms]" |
|
339 |
+ "\n\tCompleting exceptionally with {2}\n", |
|
340 |
request.uri(), |
|
341 |
request.timeout().isPresent() ? |
|
342 |
// calling duration.toMillis() can throw an exception. |
|
343 |
// this is just debugging, we don't care if it overflows. |
|
344 |
(request.timeout().get().getSeconds() * 1000 |
|
345 |
+ request.timeout().get().getNano() / 1000000) : -1, |
|
346 |
cause); |
|
347 |
boolean acknowledged = cf.completeExceptionally(cause); |
|
49765 | 348 |
if (debug.on()) |
349 |
debug.log(acknowledged ? ("completed response with " + cause) |
|
350 |
: ("response already completed, ignoring " + cause)); |
|
48083 | 351 |
} |
50681 | 352 |
return Utils.wrapForDebug(debug, "getResponseAsync", cf); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
353 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
354 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
355 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
356 |
CompletableFuture<T> readBodyAsync(BodyHandler<T> handler, |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
43984
diff
changeset
|
357 |
boolean returnConnectionToPool, |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
358 |
Executor executor) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
359 |
{ |
49765 | 360 |
BodySubscriber<T> bs = handler.apply(new ResponseInfoImpl(response.responseCode(), |
361 |
response.responseHeaders(), |
|
362 |
HTTP_1_1)); |
|
48083 | 363 |
CompletableFuture<T> bodyCF = response.readBody(bs, |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
43984
diff
changeset
|
364 |
returnConnectionToPool, |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
365 |
executor); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
366 |
return bodyCF; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
367 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
368 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
369 |
@Override |
48083 | 370 |
CompletableFuture<Void> ignoreBody() { |
371 |
return response.ignoreBody(executor); |
|
372 |
} |
|
373 |
||
374 |
ByteBuffer drainLeftOverBytes() { |
|
375 |
synchronized (lock) { |
|
376 |
asyncReceiver.stop(); |
|
377 |
return asyncReceiver.drain(Utils.EMPTY_BYTEBUFFER); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
378 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
379 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
380 |
|
48083 | 381 |
void released() { |
382 |
Http1Response<T> resp = this.response; |
|
383 |
if (resp != null) resp.completed(); |
|
384 |
asyncReceiver.clear(); |
|
42460
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 |
|
48083 | 387 |
void completed() { |
388 |
Http1Response<T> resp = this.response; |
|
389 |
if (resp != null) resp.completed(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
390 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
391 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
392 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
393 |
* Cancel checks to see if request and responseAsync finished already. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
394 |
* If not it closes the connection and completes all pending operations |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
395 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
396 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
397 |
void cancel() { |
48083 | 398 |
cancelImpl(new IOException("Request cancelled")); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
399 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
400 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
401 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
402 |
* Cancel checks to see if request and responseAsync finished already. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
403 |
* If not it closes the connection and completes all pending operations |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
404 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
405 |
@Override |
48083 | 406 |
void cancel(IOException cause) { |
407 |
cancelImpl(cause); |
|
408 |
} |
|
409 |
||
410 |
private void cancelImpl(Throwable cause) { |
|
411 |
LinkedList<CompletableFuture<?>> toComplete = null; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
412 |
int count = 0; |
49765 | 413 |
Throwable error; |
48083 | 414 |
synchronized (lock) { |
49765 | 415 |
if ((error = failed) == null) { |
416 |
failed = error = cause; |
|
417 |
} |
|
49944 | 418 |
if (debug.on()) { |
419 |
debug.log(request.uri() + ": " + error); |
|
420 |
} |
|
48083 | 421 |
if (requestAction != null && requestAction.finished() |
422 |
&& response != null && response.finished()) { |
|
423 |
return; |
|
424 |
} |
|
425 |
writePublisher.writeScheduler.stop(); |
|
426 |
if (operations.isEmpty()) { |
|
427 |
Log.logTrace("Http1Exchange: request [{0}/timeout={1}ms] no pending operation." |
|
428 |
+ "\n\tCan''t cancel yet with {2}", |
|
429 |
request.uri(), |
|
430 |
request.timeout().isPresent() ? |
|
431 |
// calling duration.toMillis() can throw an exception. |
|
432 |
// this is just debugging, we don't care if it overflows. |
|
433 |
(request.timeout().get().getSeconds() * 1000 |
|
434 |
+ request.timeout().get().getNano() / 1000000) : -1, |
|
435 |
cause); |
|
436 |
} else { |
|
437 |
for (CompletableFuture<?> cf : operations) { |
|
438 |
if (!cf.isDone()) { |
|
439 |
if (toComplete == null) toComplete = new LinkedList<>(); |
|
440 |
toComplete.add(cf); |
|
441 |
count++; |
|
442 |
} |
|
443 |
} |
|
444 |
operations.clear(); |
|
44639
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43999
diff
changeset
|
445 |
} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
446 |
} |
50681 | 447 |
try { |
448 |
Log.logError("Http1Exchange.cancel: count=" + count); |
|
449 |
if (toComplete != null) { |
|
450 |
// We might be in the selector thread in case of timeout, when |
|
451 |
// the SelectorManager calls purgeTimeoutsAndReturnNextDeadline() |
|
452 |
// There may or may not be other places that reach here |
|
453 |
// from the SelectorManager thread, so just make sure we |
|
454 |
// don't complete any CF from within the selector manager |
|
455 |
// thread. |
|
456 |
Executor exec = client.isSelectorThread() |
|
457 |
? executor |
|
458 |
: this::runInline; |
|
459 |
Throwable x = error; |
|
460 |
while (!toComplete.isEmpty()) { |
|
461 |
CompletableFuture<?> cf = toComplete.poll(); |
|
462 |
exec.execute(() -> { |
|
463 |
if (cf.completeExceptionally(x)) { |
|
464 |
if (debug.on()) |
|
465 |
debug.log("%s: completed cf with %s", request.uri(), x); |
|
466 |
} |
|
467 |
}); |
|
468 |
} |
|
48083 | 469 |
} |
50681 | 470 |
} finally { |
471 |
connection.close(); |
|
48083 | 472 |
} |
473 |
} |
|
474 |
||
475 |
private void runInline(Runnable run) { |
|
476 |
assert !client.isSelectorThread(); |
|
477 |
run.run(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
478 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
479 |
|
48083 | 480 |
/** Returns true if this exchange was canceled. */ |
481 |
boolean isCanceled() { |
|
482 |
synchronized (lock) { |
|
483 |
return failed != null; |
|
484 |
} |
|
485 |
} |
|
486 |
||
487 |
/** Returns the cause for which this exchange was canceled, if available. */ |
|
488 |
Throwable getCancelCause() { |
|
489 |
synchronized (lock) { |
|
490 |
return failed; |
|
491 |
} |
|
492 |
} |
|
493 |
||
494 |
/** Convenience for {@link #appendToOutgoing(DataPair)}, with just a Throwable. */ |
|
495 |
void appendToOutgoing(Throwable throwable) { |
|
496 |
appendToOutgoing(new DataPair(null, throwable)); |
|
497 |
} |
|
498 |
||
499 |
/** Convenience for {@link #appendToOutgoing(DataPair)}, with just data. */ |
|
500 |
void appendToOutgoing(List<ByteBuffer> item) { |
|
501 |
appendToOutgoing(new DataPair(item, null)); |
|
502 |
} |
|
503 |
||
504 |
private void appendToOutgoing(DataPair dp) { |
|
49765 | 505 |
if (debug.on()) debug.log("appending to outgoing " + dp); |
48083 | 506 |
outgoing.add(dp); |
507 |
writePublisher.writeScheduler.runOrSchedule(); |
|
508 |
} |
|
509 |
||
510 |
/** Tells whether, or not, there is any outgoing data that can be published, |
|
511 |
* or if there is an error. */ |
|
512 |
private boolean hasOutgoing() { |
|
513 |
return !outgoing.isEmpty(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
514 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
515 |
|
49765 | 516 |
private void requestMoreBody() { |
517 |
try { |
|
50681 | 518 |
if (debug.on()) debug.log("requesting more request body from the subscriber"); |
49765 | 519 |
bodySubscriber.request(1); |
520 |
} catch (Throwable t) { |
|
521 |
if (debug.on()) debug.log("Subscription::request failed", t); |
|
522 |
cancelImpl(t); |
|
523 |
bodySentCF.completeExceptionally(t); |
|
524 |
} |
|
525 |
} |
|
526 |
||
48083 | 527 |
// Invoked only by the publisher |
528 |
// ALL tasks should execute off the Selector-Manager thread |
|
529 |
/** Returns the next portion of the HTTP request, or the error. */ |
|
530 |
private DataPair getOutgoing() { |
|
531 |
final Executor exec = client.theExecutor(); |
|
532 |
final DataPair dp = outgoing.pollFirst(); |
|
533 |
||
50681 | 534 |
if (writePublisher.cancelled) { |
535 |
if (debug.on()) debug.log("cancelling upstream publisher"); |
|
536 |
if (bodySubscriber != null) { |
|
537 |
exec.execute(bodySubscriber::cancelSubscription); |
|
538 |
} else if (debug.on()) { |
|
539 |
debug.log("bodySubscriber is null"); |
|
540 |
} |
|
541 |
headersSentCF.completeAsync(() -> this, exec); |
|
542 |
bodySentCF.completeAsync(() -> this, exec); |
|
543 |
return null; |
|
544 |
} |
|
545 |
||
48083 | 546 |
if (dp == null) // publisher has not published anything yet |
547 |
return null; |
|
548 |
||
50681 | 549 |
if (dp.throwable != null) { |
550 |
synchronized (lock) { |
|
48083 | 551 |
state = State.ERROR; |
552 |
} |
|
50681 | 553 |
exec.execute(() -> { |
554 |
headersSentCF.completeExceptionally(dp.throwable); |
|
555 |
bodySentCF.completeExceptionally(dp.throwable); |
|
556 |
connection.close(); |
|
557 |
}); |
|
48083 | 558 |
return dp; |
559 |
} |
|
50681 | 560 |
|
561 |
switch (state) { |
|
562 |
case HEADERS: |
|
563 |
synchronized (lock) { |
|
564 |
state = State.BODY; |
|
565 |
} |
|
566 |
// completeAsync, since dependent tasks should run in another thread |
|
567 |
if (debug.on()) debug.log("initiating completion of headersSentCF"); |
|
568 |
headersSentCF.completeAsync(() -> this, exec); |
|
569 |
break; |
|
570 |
case BODY: |
|
571 |
if (dp.data == Http1BodySubscriber.COMPLETED) { |
|
572 |
synchronized (lock) { |
|
573 |
state = State.COMPLETING; |
|
574 |
} |
|
575 |
if (debug.on()) debug.log("initiating completion of bodySentCF"); |
|
576 |
bodySentCF.completeAsync(() -> this, exec); |
|
577 |
} else { |
|
578 |
exec.execute(this::requestMoreBody); |
|
579 |
} |
|
580 |
break; |
|
581 |
case INITIAL: |
|
582 |
case ERROR: |
|
583 |
case COMPLETING: |
|
584 |
case COMPLETED: |
|
585 |
default: |
|
586 |
assert false : "Unexpected state:" + state; |
|
587 |
} |
|
588 |
||
589 |
return dp; |
|
48083 | 590 |
} |
591 |
||
592 |
/** A Publisher of HTTP/1.1 headers and request body. */ |
|
593 |
final class Http1Publisher implements FlowTube.TubePublisher { |
|
594 |
||
49765 | 595 |
final Logger debug = Utils.getDebugLogger(this::dbgString); |
48083 | 596 |
volatile Flow.Subscriber<? super List<ByteBuffer>> subscriber; |
597 |
volatile boolean cancelled; |
|
598 |
final Http1WriteSubscription subscription = new Http1WriteSubscription(); |
|
599 |
final Demand demand = new Demand(); |
|
600 |
final SequentialScheduler writeScheduler = |
|
601 |
SequentialScheduler.synchronizedScheduler(new WriteTask()); |
|
602 |
||
603 |
@Override |
|
604 |
public void subscribe(Flow.Subscriber<? super List<ByteBuffer>> s) { |
|
605 |
assert state == State.INITIAL; |
|
606 |
Objects.requireNonNull(s); |
|
607 |
assert subscriber == null; |
|
608 |
||
609 |
subscriber = s; |
|
49765 | 610 |
if (debug.on()) debug.log("got subscriber: %s", s); |
48083 | 611 |
s.onSubscribe(subscription); |
44639
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43999
diff
changeset
|
612 |
} |
48083 | 613 |
|
614 |
volatile String dbgTag; |
|
615 |
String dbgString() { |
|
616 |
String tag = dbgTag; |
|
617 |
Object flow = connection.getConnectionFlow(); |
|
618 |
if (tag == null && flow != null) { |
|
619 |
dbgTag = tag = "Http1Publisher(" + flow + ")"; |
|
620 |
} else if (tag == null) { |
|
621 |
tag = "Http1Publisher(?)"; |
|
622 |
} |
|
623 |
return tag; |
|
44639
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43999
diff
changeset
|
624 |
} |
48083 | 625 |
|
626 |
final class WriteTask implements Runnable { |
|
627 |
@Override |
|
628 |
public void run() { |
|
629 |
assert state != State.COMPLETED : "Unexpected state:" + state; |
|
49765 | 630 |
if (debug.on()) debug.log("WriteTask"); |
50681 | 631 |
|
632 |
if (cancelled) { |
|
633 |
if (debug.on()) debug.log("handling cancellation"); |
|
634 |
writeScheduler.stop(); |
|
635 |
getOutgoing(); |
|
636 |
return; |
|
637 |
} |
|
638 |
||
48083 | 639 |
if (subscriber == null) { |
49765 | 640 |
if (debug.on()) debug.log("no subscriber yet"); |
48083 | 641 |
return; |
642 |
} |
|
49765 | 643 |
if (debug.on()) debug.log(() -> "hasOutgoing = " + hasOutgoing()); |
48083 | 644 |
while (hasOutgoing() && demand.tryDecrement()) { |
645 |
DataPair dp = getOutgoing(); |
|
50681 | 646 |
if (dp == null) |
647 |
break; |
|
48083 | 648 |
|
649 |
if (dp.throwable != null) { |
|
49765 | 650 |
if (debug.on()) debug.log("onError"); |
48083 | 651 |
// Do not call the subscriber's onError, it is not required. |
652 |
writeScheduler.stop(); |
|
653 |
} else { |
|
654 |
List<ByteBuffer> data = dp.data; |
|
655 |
if (data == Http1BodySubscriber.COMPLETED) { |
|
656 |
synchronized (lock) { |
|
657 |
assert state == State.COMPLETING : "Unexpected state:" + state; |
|
658 |
state = State.COMPLETED; |
|
659 |
} |
|
49765 | 660 |
if (debug.on()) |
661 |
debug.log("completed, stopping %s", writeScheduler); |
|
48083 | 662 |
writeScheduler.stop(); |
663 |
// Do nothing more. Just do not publish anything further. |
|
664 |
// The next Subscriber will eventually take over. |
|
665 |
||
666 |
} else { |
|
49765 | 667 |
if (debug.on()) |
668 |
debug.log("onNext with " + Utils.remaining(data) + " bytes"); |
|
48083 | 669 |
subscriber.onNext(data); |
670 |
} |
|
671 |
} |
|
672 |
} |
|
673 |
} |
|
674 |
} |
|
675 |
||
676 |
final class Http1WriteSubscription implements Flow.Subscription { |
|
677 |
||
678 |
@Override |
|
679 |
public void request(long n) { |
|
680 |
if (cancelled) |
|
681 |
return; //no-op |
|
682 |
demand.increase(n); |
|
49765 | 683 |
if (debug.on()) |
684 |
debug.log("subscription request(%d), demand=%s", n, demand); |
|
685 |
writeScheduler.runOrSchedule(client.theExecutor()); |
|
48083 | 686 |
} |
687 |
||
688 |
@Override |
|
689 |
public void cancel() { |
|
49765 | 690 |
if (debug.on()) debug.log("subscription cancelled"); |
48083 | 691 |
if (cancelled) |
692 |
return; //no-op |
|
693 |
cancelled = true; |
|
50681 | 694 |
writeScheduler.runOrSchedule(client.theExecutor()); |
48083 | 695 |
} |
696 |
} |
|
697 |
} |
|
698 |
||
699 |
String dbgString() { |
|
700 |
return "Http1Exchange"; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
701 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
702 |
} |