author | chegar |
Wed, 02 May 2018 02:36:17 -0700 | |
changeset 49944 | 4690a2871b44 |
parent 49765 | ee6f7a61f3a5 |
child 50681 | 4254bed3c09d |
child 56507 | 2294c51eae30 |
permissions | -rw-r--r-- |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1 |
/* |
49765 | 2 |
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
4 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. Oracle designates this |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
10 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
15 |
* accompanied this code). |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
16 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License version |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
20 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
22 |
* or visit www.oracle.com if you need additional information or have any |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
23 |
* questions. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
24 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
25 |
|
49765 | 26 |
package jdk.internal.net.http; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
27 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
28 |
import java.io.IOException; |
48083 | 29 |
import java.lang.System.Logger.Level; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
30 |
import java.net.InetSocketAddress; |
49765 | 31 |
import java.net.http.HttpResponse.BodyHandler; |
32 |
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
|
33 |
import java.nio.ByteBuffer; |
48083 | 34 |
import java.util.Objects; |
42460
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.LinkedList; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
37 |
import java.util.List; |
48083 | 38 |
import java.util.concurrent.ConcurrentLinkedDeque; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
39 |
import java.util.concurrent.Executor; |
48083 | 40 |
import java.util.concurrent.Flow; |
49765 | 41 |
import jdk.internal.net.http.common.Demand; |
42 |
import jdk.internal.net.http.common.Log; |
|
43 |
import jdk.internal.net.http.common.FlowTube; |
|
44 |
import jdk.internal.net.http.common.Logger; |
|
45 |
import jdk.internal.net.http.common.SequentialScheduler; |
|
46 |
import jdk.internal.net.http.common.MinimalFuture; |
|
47 |
import jdk.internal.net.http.common.Utils; |
|
48 |
import static java.net.http.HttpClient.Version.HTTP_1_1; |
|
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 |
||
49765 | 137 |
final boolean isSubscribed() { |
138 |
return subscription != null; |
|
139 |
} |
|
140 |
||
141 |
final void setSubscription(Flow.Subscription subscription) { |
|
142 |
this.subscription = subscription; |
|
143 |
whenSubscribed.complete(subscription); |
|
144 |
} |
|
145 |
||
146 |
final void cancelSubscription() { |
|
147 |
try { |
|
148 |
subscription.cancel(); |
|
149 |
} catch(Throwable t) { |
|
150 |
String msg = "Ignoring exception raised when canceling BodyPublisher subscription"; |
|
151 |
if (debug.on()) debug.log("%s: %s", msg, t); |
|
152 |
Log.logError("{0}: {1}", msg, (Object)t); |
|
153 |
} |
|
154 |
} |
|
155 |
||
156 |
static Http1BodySubscriber completeSubscriber(Logger debug) { |
|
157 |
return new Http1BodySubscriber(debug) { |
|
48083 | 158 |
@Override public void onSubscribe(Flow.Subscription subscription) { error(); } |
159 |
@Override public void onNext(ByteBuffer item) { error(); } |
|
160 |
@Override public void onError(Throwable throwable) { error(); } |
|
161 |
@Override public void onComplete() { error(); } |
|
162 |
private void error() { |
|
163 |
throw new InternalError("should not reach here"); |
|
164 |
} |
|
165 |
}; |
|
166 |
} |
|
167 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
168 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
169 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
170 |
public String toString() { |
48083 | 171 |
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
|
172 |
} |
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 |
HttpRequestImpl request() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
175 |
return request; |
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 |
Http1Exchange(Exchange<T> exchange, HttpConnection connection) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
179 |
throws IOException |
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 |
super(exchange); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
182 |
this.request = exchange.request(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
183 |
this.client = exchange.client(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
184 |
this.executor = exchange.executor(); |
44639
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43999
diff
changeset
|
185 |
this.operations = new LinkedList<>(); |
48083 | 186 |
operations.add(headersSentCF); |
187 |
operations.add(bodySentCF); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
188 |
if (connection != null) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
189 |
this.connection = connection; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
190 |
} else { |
48083 | 191 |
InetSocketAddress addr = request.getAddress(); |
192 |
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
|
193 |
} |
48083 | 194 |
this.requestAction = new Http1Request(request, this); |
195 |
this.asyncReceiver = new Http1AsyncReceiver(executor, this); |
|
196 |
asyncReceiver.subscribe(new InitialErrorReceiver()); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
197 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
198 |
|
48083 | 199 |
/** An initial receiver that handles no data, but cancels the request if |
200 |
* it receives an error. Will be replaced when reading response body. */ |
|
201 |
final class InitialErrorReceiver implements Http1AsyncReceiver.Http1AsyncDelegate { |
|
202 |
volatile AbstractSubscription s; |
|
203 |
@Override |
|
204 |
public boolean tryAsyncReceive(ByteBuffer ref) { |
|
205 |
return false; // no data has been processed, leave it in the queue |
|
206 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
207 |
|
48083 | 208 |
@Override |
209 |
public void onReadError(Throwable ex) { |
|
210 |
cancelImpl(ex); |
|
211 |
} |
|
212 |
||
213 |
@Override |
|
214 |
public void onSubscribe(AbstractSubscription s) { |
|
215 |
this.s = s; |
|
216 |
} |
|
217 |
||
49765 | 218 |
@Override |
48083 | 219 |
public AbstractSubscription subscription() { |
220 |
return s; |
|
221 |
} |
|
49765 | 222 |
|
223 |
@Override |
|
224 |
public void close(Throwable error) {} |
|
48083 | 225 |
} |
226 |
||
227 |
@Override |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
228 |
HttpConnection connection() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
229 |
return connection; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
230 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
231 |
|
48083 | 232 |
private void connectFlows(HttpConnection connection) { |
233 |
FlowTube tube = connection.getConnectionFlow(); |
|
49765 | 234 |
if (debug.on()) debug.log("%s connecting flows", tube); |
48083 | 235 |
|
236 |
// Connect the flow to our Http1TubeSubscriber: |
|
237 |
// asyncReceiver.subscriber(). |
|
238 |
tube.connectFlows(writePublisher, |
|
239 |
asyncReceiver.subscriber()); |
|
240 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
241 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
242 |
@Override |
48083 | 243 |
CompletableFuture<ExchangeImpl<T>> sendHeadersAsync() { |
244 |
// create the response before sending the request headers, so that |
|
245 |
// the response can set the appropriate receivers. |
|
49765 | 246 |
if (debug.on()) debug.log("Sending headers only"); |
48083 | 247 |
if (response == null) { |
248 |
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
|
249 |
} |
48083 | 250 |
|
49765 | 251 |
if (debug.on()) debug.log("response created in advance"); |
48083 | 252 |
// If the first attempt to read something triggers EOF, or |
253 |
// IOException("channel reset by peer"), we're going to retry. |
|
254 |
// Instruct the asyncReceiver to throw ConnectionExpiredException |
|
255 |
// to force a retry. |
|
256 |
asyncReceiver.setRetryOnError(true); |
|
257 |
||
258 |
CompletableFuture<Void> connectCF; |
|
259 |
if (!connection.connected()) { |
|
49765 | 260 |
if (debug.on()) debug.log("initiating connect async"); |
48083 | 261 |
connectCF = connection.connectAsync(); |
49944 | 262 |
Throwable cancelled; |
48083 | 263 |
synchronized (lock) { |
49944 | 264 |
if ((cancelled = failed) == null) { |
265 |
operations.add(connectCF); |
|
266 |
} |
|
267 |
} |
|
268 |
if (cancelled != null) { |
|
269 |
if (client.isSelectorThread()) { |
|
270 |
executor.execute(() -> |
|
271 |
connectCF.completeExceptionally(cancelled)); |
|
272 |
} else { |
|
273 |
connectCF.completeExceptionally(cancelled); |
|
274 |
} |
|
48083 | 275 |
} |
276 |
} else { |
|
277 |
connectCF = new MinimalFuture<>(); |
|
278 |
connectCF.complete(null); |
|
279 |
} |
|
280 |
||
281 |
return connectCF |
|
282 |
.thenCompose(unused -> { |
|
283 |
CompletableFuture<Void> cf = new MinimalFuture<>(); |
|
284 |
try { |
|
285 |
connectFlows(connection); |
|
286 |
||
49765 | 287 |
if (debug.on()) debug.log("requestAction.headers"); |
48083 | 288 |
List<ByteBuffer> data = requestAction.headers(); |
289 |
synchronized (lock) { |
|
290 |
state = State.HEADERS; |
|
291 |
} |
|
49765 | 292 |
if (debug.on()) debug.log("setting outgoing with headers"); |
48083 | 293 |
assert outgoing.isEmpty() : "Unexpected outgoing:" + outgoing; |
294 |
appendToOutgoing(data); |
|
295 |
cf.complete(null); |
|
296 |
return cf; |
|
297 |
} catch (Throwable t) { |
|
49765 | 298 |
if (debug.on()) debug.log("Failed to send headers: %s", t); |
48083 | 299 |
connection.close(); |
300 |
cf.completeExceptionally(t); |
|
301 |
return cf; |
|
302 |
} }) |
|
303 |
.thenCompose(unused -> headersSentCF); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
304 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
305 |
|
48083 | 306 |
@Override |
307 |
CompletableFuture<ExchangeImpl<T>> sendBodyAsync() { |
|
308 |
assert headersSentCF.isDone(); |
|
309 |
try { |
|
310 |
bodySubscriber = requestAction.continueRequest(); |
|
311 |
if (bodySubscriber == null) { |
|
49765 | 312 |
bodySubscriber = Http1BodySubscriber.completeSubscriber(debug); |
48083 | 313 |
appendToOutgoing(Http1BodySubscriber.COMPLETED); |
314 |
} else { |
|
49765 | 315 |
// start |
316 |
bodySubscriber.whenSubscribed |
|
317 |
.thenAccept((s) -> requestMoreBody()); |
|
48083 | 318 |
} |
319 |
} catch (Throwable t) { |
|
49765 | 320 |
cancelImpl(t); |
48083 | 321 |
bodySentCF.completeExceptionally(t); |
322 |
} |
|
323 |
return 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) { |
|
328 |
CompletableFuture<Response> cf = response.readHeadersAsync(executor); |
|
329 |
Throwable cause; |
|
330 |
synchronized (lock) { |
|
331 |
operations.add(cf); |
|
332 |
cause = failed; |
|
333 |
failed = null; |
|
334 |
} |
|
335 |
||
336 |
if (cause != null) { |
|
337 |
Log.logTrace("Http1Exchange: request [{0}/timeout={1}ms]" |
|
338 |
+ "\n\tCompleting exceptionally with {2}\n", |
|
339 |
request.uri(), |
|
340 |
request.timeout().isPresent() ? |
|
341 |
// calling duration.toMillis() can throw an exception. |
|
342 |
// this is just debugging, we don't care if it overflows. |
|
343 |
(request.timeout().get().getSeconds() * 1000 |
|
344 |
+ request.timeout().get().getNano() / 1000000) : -1, |
|
345 |
cause); |
|
346 |
boolean acknowledged = cf.completeExceptionally(cause); |
|
49765 | 347 |
if (debug.on()) |
348 |
debug.log(acknowledged ? ("completed response with " + cause) |
|
349 |
: ("response already completed, ignoring " + cause)); |
|
48083 | 350 |
} |
351 |
return cf; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
352 |
} |
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 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
355 |
CompletableFuture<T> readBodyAsync(BodyHandler<T> handler, |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
43984
diff
changeset
|
356 |
boolean returnConnectionToPool, |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
357 |
Executor executor) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
358 |
{ |
49765 | 359 |
BodySubscriber<T> bs = handler.apply(new ResponseInfoImpl(response.responseCode(), |
360 |
response.responseHeaders(), |
|
361 |
HTTP_1_1)); |
|
48083 | 362 |
CompletableFuture<T> bodyCF = response.readBody(bs, |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
43984
diff
changeset
|
363 |
returnConnectionToPool, |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
364 |
executor); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
365 |
return bodyCF; |
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 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
368 |
@Override |
48083 | 369 |
CompletableFuture<Void> ignoreBody() { |
370 |
return response.ignoreBody(executor); |
|
371 |
} |
|
372 |
||
373 |
ByteBuffer drainLeftOverBytes() { |
|
374 |
synchronized (lock) { |
|
375 |
asyncReceiver.stop(); |
|
376 |
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
|
377 |
} |
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 |
|
48083 | 380 |
void released() { |
381 |
Http1Response<T> resp = this.response; |
|
382 |
if (resp != null) resp.completed(); |
|
383 |
asyncReceiver.clear(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
384 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
385 |
|
48083 | 386 |
void completed() { |
387 |
Http1Response<T> resp = this.response; |
|
388 |
if (resp != null) resp.completed(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
389 |
} |
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 |
* 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
|
393 |
* 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
|
394 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
395 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
396 |
void cancel() { |
48083 | 397 |
cancelImpl(new IOException("Request cancelled")); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
398 |
} |
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 |
* 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
|
402 |
* 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
|
403 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
404 |
@Override |
48083 | 405 |
void cancel(IOException cause) { |
406 |
cancelImpl(cause); |
|
407 |
} |
|
408 |
||
409 |
private void cancelImpl(Throwable cause) { |
|
410 |
LinkedList<CompletableFuture<?>> toComplete = null; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
411 |
int count = 0; |
49765 | 412 |
Throwable error; |
48083 | 413 |
synchronized (lock) { |
49765 | 414 |
if ((error = failed) == null) { |
415 |
failed = error = cause; |
|
416 |
} |
|
49944 | 417 |
if (debug.on()) { |
418 |
debug.log(request.uri() + ": " + error); |
|
419 |
} |
|
48083 | 420 |
if (requestAction != null && requestAction.finished() |
421 |
&& response != null && response.finished()) { |
|
422 |
return; |
|
423 |
} |
|
424 |
connection.close(); // TODO: ensure non-blocking if holding the lock |
|
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 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
447 |
Log.logError("Http1Exchange.cancel: count=" + count); |
48083 | 448 |
if (toComplete != null) { |
449 |
// We might be in the selector thread in case of timeout, when |
|
450 |
// the SelectorManager calls purgeTimeoutsAndReturnNextDeadline() |
|
451 |
// There may or may not be other places that reach here |
|
452 |
// from the SelectorManager thread, so just make sure we |
|
453 |
// don't complete any CF from within the selector manager |
|
454 |
// thread. |
|
455 |
Executor exec = client.isSelectorThread() |
|
456 |
? executor |
|
457 |
: this::runInline; |
|
49765 | 458 |
Throwable x = error; |
48083 | 459 |
while (!toComplete.isEmpty()) { |
460 |
CompletableFuture<?> cf = toComplete.poll(); |
|
461 |
exec.execute(() -> { |
|
49765 | 462 |
if (cf.completeExceptionally(x)) { |
463 |
if (debug.on()) |
|
49944 | 464 |
debug.log("%s: completed cf with %s", request.uri(), x); |
48083 | 465 |
} |
466 |
}); |
|
467 |
} |
|
468 |
} |
|
469 |
} |
|
470 |
||
471 |
private void runInline(Runnable run) { |
|
472 |
assert !client.isSelectorThread(); |
|
473 |
run.run(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
474 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
475 |
|
48083 | 476 |
/** Returns true if this exchange was canceled. */ |
477 |
boolean isCanceled() { |
|
478 |
synchronized (lock) { |
|
479 |
return failed != null; |
|
480 |
} |
|
481 |
} |
|
482 |
||
483 |
/** Returns the cause for which this exchange was canceled, if available. */ |
|
484 |
Throwable getCancelCause() { |
|
485 |
synchronized (lock) { |
|
486 |
return failed; |
|
487 |
} |
|
488 |
} |
|
489 |
||
490 |
/** Convenience for {@link #appendToOutgoing(DataPair)}, with just a Throwable. */ |
|
491 |
void appendToOutgoing(Throwable throwable) { |
|
492 |
appendToOutgoing(new DataPair(null, throwable)); |
|
493 |
} |
|
494 |
||
495 |
/** Convenience for {@link #appendToOutgoing(DataPair)}, with just data. */ |
|
496 |
void appendToOutgoing(List<ByteBuffer> item) { |
|
497 |
appendToOutgoing(new DataPair(item, null)); |
|
498 |
} |
|
499 |
||
500 |
private void appendToOutgoing(DataPair dp) { |
|
49765 | 501 |
if (debug.on()) debug.log("appending to outgoing " + dp); |
48083 | 502 |
outgoing.add(dp); |
503 |
writePublisher.writeScheduler.runOrSchedule(); |
|
504 |
} |
|
505 |
||
506 |
/** Tells whether, or not, there is any outgoing data that can be published, |
|
507 |
* or if there is an error. */ |
|
508 |
private boolean hasOutgoing() { |
|
509 |
return !outgoing.isEmpty(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
510 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
511 |
|
49765 | 512 |
private void requestMoreBody() { |
513 |
try { |
|
514 |
if (debug.on()) debug.log("requesting more body from the subscriber"); |
|
515 |
bodySubscriber.request(1); |
|
516 |
} catch (Throwable t) { |
|
517 |
if (debug.on()) debug.log("Subscription::request failed", t); |
|
518 |
cancelImpl(t); |
|
519 |
bodySentCF.completeExceptionally(t); |
|
520 |
} |
|
521 |
} |
|
522 |
||
48083 | 523 |
// Invoked only by the publisher |
524 |
// ALL tasks should execute off the Selector-Manager thread |
|
525 |
/** Returns the next portion of the HTTP request, or the error. */ |
|
526 |
private DataPair getOutgoing() { |
|
527 |
final Executor exec = client.theExecutor(); |
|
528 |
final DataPair dp = outgoing.pollFirst(); |
|
529 |
||
530 |
if (dp == null) // publisher has not published anything yet |
|
531 |
return null; |
|
532 |
||
533 |
synchronized (lock) { |
|
534 |
if (dp.throwable != null) { |
|
535 |
state = State.ERROR; |
|
536 |
exec.execute(() -> { |
|
537 |
headersSentCF.completeExceptionally(dp.throwable); |
|
538 |
bodySentCF.completeExceptionally(dp.throwable); |
|
49765 | 539 |
connection.close(); |
48083 | 540 |
}); |
541 |
return dp; |
|
542 |
} |
|
543 |
||
544 |
switch (state) { |
|
545 |
case HEADERS: |
|
546 |
state = State.BODY; |
|
547 |
// completeAsync, since dependent tasks should run in another thread |
|
49765 | 548 |
if (debug.on()) debug.log("initiating completion of headersSentCF"); |
48083 | 549 |
headersSentCF.completeAsync(() -> this, exec); |
550 |
break; |
|
551 |
case BODY: |
|
552 |
if (dp.data == Http1BodySubscriber.COMPLETED) { |
|
553 |
state = State.COMPLETING; |
|
49765 | 554 |
if (debug.on()) debug.log("initiating completion of bodySentCF"); |
48083 | 555 |
bodySentCF.completeAsync(() -> this, exec); |
556 |
} else { |
|
49765 | 557 |
exec.execute(this::requestMoreBody); |
48083 | 558 |
} |
559 |
break; |
|
560 |
case INITIAL: |
|
561 |
case ERROR: |
|
562 |
case COMPLETING: |
|
563 |
case COMPLETED: |
|
564 |
default: |
|
565 |
assert false : "Unexpected state:" + state; |
|
566 |
} |
|
567 |
||
568 |
return dp; |
|
569 |
} |
|
570 |
} |
|
571 |
||
572 |
/** A Publisher of HTTP/1.1 headers and request body. */ |
|
573 |
final class Http1Publisher implements FlowTube.TubePublisher { |
|
574 |
||
49765 | 575 |
final Logger debug = Utils.getDebugLogger(this::dbgString); |
48083 | 576 |
volatile Flow.Subscriber<? super List<ByteBuffer>> subscriber; |
577 |
volatile boolean cancelled; |
|
578 |
final Http1WriteSubscription subscription = new Http1WriteSubscription(); |
|
579 |
final Demand demand = new Demand(); |
|
580 |
final SequentialScheduler writeScheduler = |
|
581 |
SequentialScheduler.synchronizedScheduler(new WriteTask()); |
|
582 |
||
583 |
@Override |
|
584 |
public void subscribe(Flow.Subscriber<? super List<ByteBuffer>> s) { |
|
585 |
assert state == State.INITIAL; |
|
586 |
Objects.requireNonNull(s); |
|
587 |
assert subscriber == null; |
|
588 |
||
589 |
subscriber = s; |
|
49765 | 590 |
if (debug.on()) debug.log("got subscriber: %s", s); |
48083 | 591 |
s.onSubscribe(subscription); |
44639
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43999
diff
changeset
|
592 |
} |
48083 | 593 |
|
594 |
volatile String dbgTag; |
|
595 |
String dbgString() { |
|
596 |
String tag = dbgTag; |
|
597 |
Object flow = connection.getConnectionFlow(); |
|
598 |
if (tag == null && flow != null) { |
|
599 |
dbgTag = tag = "Http1Publisher(" + flow + ")"; |
|
600 |
} else if (tag == null) { |
|
601 |
tag = "Http1Publisher(?)"; |
|
602 |
} |
|
603 |
return tag; |
|
44639
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43999
diff
changeset
|
604 |
} |
48083 | 605 |
|
606 |
final class WriteTask implements Runnable { |
|
607 |
@Override |
|
608 |
public void run() { |
|
609 |
assert state != State.COMPLETED : "Unexpected state:" + state; |
|
49765 | 610 |
if (debug.on()) debug.log("WriteTask"); |
48083 | 611 |
if (subscriber == null) { |
49765 | 612 |
if (debug.on()) debug.log("no subscriber yet"); |
48083 | 613 |
return; |
614 |
} |
|
49765 | 615 |
if (debug.on()) debug.log(() -> "hasOutgoing = " + hasOutgoing()); |
48083 | 616 |
while (hasOutgoing() && demand.tryDecrement()) { |
617 |
DataPair dp = getOutgoing(); |
|
618 |
||
619 |
if (dp.throwable != null) { |
|
49765 | 620 |
if (debug.on()) debug.log("onError"); |
48083 | 621 |
// Do not call the subscriber's onError, it is not required. |
622 |
writeScheduler.stop(); |
|
623 |
} else { |
|
624 |
List<ByteBuffer> data = dp.data; |
|
625 |
if (data == Http1BodySubscriber.COMPLETED) { |
|
626 |
synchronized (lock) { |
|
627 |
assert state == State.COMPLETING : "Unexpected state:" + state; |
|
628 |
state = State.COMPLETED; |
|
629 |
} |
|
49765 | 630 |
if (debug.on()) |
631 |
debug.log("completed, stopping %s", writeScheduler); |
|
48083 | 632 |
writeScheduler.stop(); |
633 |
// Do nothing more. Just do not publish anything further. |
|
634 |
// The next Subscriber will eventually take over. |
|
635 |
||
636 |
} else { |
|
49765 | 637 |
if (debug.on()) |
638 |
debug.log("onNext with " + Utils.remaining(data) + " bytes"); |
|
48083 | 639 |
subscriber.onNext(data); |
640 |
} |
|
641 |
} |
|
642 |
} |
|
643 |
} |
|
644 |
} |
|
645 |
||
646 |
final class Http1WriteSubscription implements Flow.Subscription { |
|
647 |
||
648 |
@Override |
|
649 |
public void request(long n) { |
|
650 |
if (cancelled) |
|
651 |
return; //no-op |
|
652 |
demand.increase(n); |
|
49765 | 653 |
if (debug.on()) |
654 |
debug.log("subscription request(%d), demand=%s", n, demand); |
|
655 |
writeScheduler.runOrSchedule(client.theExecutor()); |
|
48083 | 656 |
} |
657 |
||
658 |
@Override |
|
659 |
public void cancel() { |
|
49765 | 660 |
if (debug.on()) debug.log("subscription cancelled"); |
48083 | 661 |
if (cancelled) |
662 |
return; //no-op |
|
663 |
cancelled = true; |
|
664 |
writeScheduler.stop(); |
|
665 |
} |
|
666 |
} |
|
667 |
} |
|
668 |
||
669 |
String dbgString() { |
|
670 |
return "Http1Exchange"; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
671 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
672 |
} |