author | chegar |
Mon, 11 Jun 2018 13:07:18 +0100 | |
branch | http-client-branch |
changeset 56730 | b08918259eed |
parent 56682 | 9822bbe48b9b |
child 56736 | 705824c1e596 |
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; |
|
56682
9822bbe48b9b
http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
56621
diff
changeset
|
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 |
||
56682
9822bbe48b9b
http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
56621
diff
changeset
|
137 |
/** A current-state message suitable for inclusion in an exception detail message. */ |
9822bbe48b9b
http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
56621
diff
changeset
|
138 |
abstract String currentStateMessage(); |
9822bbe48b9b
http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
56621
diff
changeset
|
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(); } |
|
56682
9822bbe48b9b
http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
56621
diff
changeset
|
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"); |
48083 | 222 |
if (response == null) { |
223 |
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
|
224 |
} |
48083 | 225 |
|
49765 | 226 |
if (debug.on()) debug.log("response created in advance"); |
48083 | 227 |
// If the first attempt to read something triggers EOF, or |
228 |
// IOException("channel reset by peer"), we're going to retry. |
|
229 |
// Instruct the asyncReceiver to throw ConnectionExpiredException |
|
230 |
// to force a retry. |
|
231 |
asyncReceiver.setRetryOnError(true); |
|
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); |
48083 | 274 |
connection.close(); |
275 |
cf.completeExceptionally(t); |
|
276 |
return cf; |
|
277 |
} }) |
|
278 |
.thenCompose(unused -> headersSentCF); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
279 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
280 |
|
48083 | 281 |
@Override |
282 |
CompletableFuture<ExchangeImpl<T>> sendBodyAsync() { |
|
283 |
assert headersSentCF.isDone(); |
|
284 |
try { |
|
285 |
bodySubscriber = requestAction.continueRequest(); |
|
286 |
if (bodySubscriber == null) { |
|
49765 | 287 |
bodySubscriber = Http1BodySubscriber.completeSubscriber(debug); |
48083 | 288 |
appendToOutgoing(Http1BodySubscriber.COMPLETED); |
289 |
} else { |
|
49765 | 290 |
// start |
291 |
bodySubscriber.whenSubscribed |
|
292 |
.thenAccept((s) -> requestMoreBody()); |
|
48083 | 293 |
} |
294 |
} catch (Throwable t) { |
|
49765 | 295 |
cancelImpl(t); |
48083 | 296 |
bodySentCF.completeExceptionally(t); |
297 |
} |
|
298 |
return bodySentCF; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
299 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
300 |
|
48083 | 301 |
@Override |
302 |
CompletableFuture<Response> getResponseAsync(Executor executor) { |
|
303 |
CompletableFuture<Response> cf = response.readHeadersAsync(executor); |
|
304 |
Throwable cause; |
|
305 |
synchronized (lock) { |
|
306 |
operations.add(cf); |
|
307 |
cause = failed; |
|
308 |
failed = null; |
|
309 |
} |
|
310 |
||
311 |
if (cause != null) { |
|
312 |
Log.logTrace("Http1Exchange: request [{0}/timeout={1}ms]" |
|
313 |
+ "\n\tCompleting exceptionally with {2}\n", |
|
314 |
request.uri(), |
|
315 |
request.timeout().isPresent() ? |
|
316 |
// calling duration.toMillis() can throw an exception. |
|
317 |
// this is just debugging, we don't care if it overflows. |
|
318 |
(request.timeout().get().getSeconds() * 1000 |
|
319 |
+ request.timeout().get().getNano() / 1000000) : -1, |
|
320 |
cause); |
|
321 |
boolean acknowledged = cf.completeExceptionally(cause); |
|
49765 | 322 |
if (debug.on()) |
323 |
debug.log(acknowledged ? ("completed response with " + cause) |
|
324 |
: ("response already completed, ignoring " + cause)); |
|
48083 | 325 |
} |
326 |
return cf; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
327 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
328 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
329 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
330 |
CompletableFuture<T> readBodyAsync(BodyHandler<T> handler, |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
43984
diff
changeset
|
331 |
boolean returnConnectionToPool, |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
332 |
Executor executor) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
333 |
{ |
49765 | 334 |
BodySubscriber<T> bs = handler.apply(new ResponseInfoImpl(response.responseCode(), |
335 |
response.responseHeaders(), |
|
336 |
HTTP_1_1)); |
|
48083 | 337 |
CompletableFuture<T> bodyCF = response.readBody(bs, |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
43984
diff
changeset
|
338 |
returnConnectionToPool, |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
339 |
executor); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
340 |
return bodyCF; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
341 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
342 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
343 |
@Override |
48083 | 344 |
CompletableFuture<Void> ignoreBody() { |
345 |
return response.ignoreBody(executor); |
|
346 |
} |
|
347 |
||
348 |
ByteBuffer drainLeftOverBytes() { |
|
349 |
synchronized (lock) { |
|
350 |
asyncReceiver.stop(); |
|
351 |
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
|
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 |
|
48083 | 355 |
void released() { |
356 |
Http1Response<T> resp = this.response; |
|
357 |
if (resp != null) resp.completed(); |
|
358 |
asyncReceiver.clear(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
359 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
360 |
|
48083 | 361 |
void completed() { |
362 |
Http1Response<T> resp = this.response; |
|
363 |
if (resp != null) resp.completed(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
364 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
365 |
|
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 |
* 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
|
368 |
* 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
|
369 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
370 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
371 |
void cancel() { |
48083 | 372 |
cancelImpl(new IOException("Request cancelled")); |
42460
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 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
376 |
* 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
|
377 |
* 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
|
378 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
379 |
@Override |
48083 | 380 |
void cancel(IOException cause) { |
381 |
cancelImpl(cause); |
|
382 |
} |
|
383 |
||
384 |
private void cancelImpl(Throwable cause) { |
|
385 |
LinkedList<CompletableFuture<?>> toComplete = null; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
386 |
int count = 0; |
49765 | 387 |
Throwable error; |
48083 | 388 |
synchronized (lock) { |
49765 | 389 |
if ((error = failed) == null) { |
390 |
failed = error = cause; |
|
391 |
} |
|
49944 | 392 |
if (debug.on()) { |
393 |
debug.log(request.uri() + ": " + error); |
|
394 |
} |
|
48083 | 395 |
if (requestAction != null && requestAction.finished() |
396 |
&& response != null && response.finished()) { |
|
397 |
return; |
|
398 |
} |
|
399 |
connection.close(); // TODO: ensure non-blocking if holding the lock |
|
400 |
writePublisher.writeScheduler.stop(); |
|
401 |
if (operations.isEmpty()) { |
|
402 |
Log.logTrace("Http1Exchange: request [{0}/timeout={1}ms] no pending operation." |
|
403 |
+ "\n\tCan''t cancel yet with {2}", |
|
404 |
request.uri(), |
|
405 |
request.timeout().isPresent() ? |
|
406 |
// calling duration.toMillis() can throw an exception. |
|
407 |
// this is just debugging, we don't care if it overflows. |
|
408 |
(request.timeout().get().getSeconds() * 1000 |
|
409 |
+ request.timeout().get().getNano() / 1000000) : -1, |
|
410 |
cause); |
|
411 |
} else { |
|
412 |
for (CompletableFuture<?> cf : operations) { |
|
413 |
if (!cf.isDone()) { |
|
414 |
if (toComplete == null) toComplete = new LinkedList<>(); |
|
415 |
toComplete.add(cf); |
|
416 |
count++; |
|
417 |
} |
|
418 |
} |
|
419 |
operations.clear(); |
|
44639
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43999
diff
changeset
|
420 |
} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
421 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
422 |
Log.logError("Http1Exchange.cancel: count=" + count); |
48083 | 423 |
if (toComplete != null) { |
424 |
// We might be in the selector thread in case of timeout, when |
|
425 |
// the SelectorManager calls purgeTimeoutsAndReturnNextDeadline() |
|
426 |
// There may or may not be other places that reach here |
|
427 |
// from the SelectorManager thread, so just make sure we |
|
428 |
// don't complete any CF from within the selector manager |
|
429 |
// thread. |
|
430 |
Executor exec = client.isSelectorThread() |
|
431 |
? executor |
|
432 |
: this::runInline; |
|
49765 | 433 |
Throwable x = error; |
48083 | 434 |
while (!toComplete.isEmpty()) { |
435 |
CompletableFuture<?> cf = toComplete.poll(); |
|
436 |
exec.execute(() -> { |
|
49765 | 437 |
if (cf.completeExceptionally(x)) { |
438 |
if (debug.on()) |
|
49944 | 439 |
debug.log("%s: completed cf with %s", request.uri(), x); |
48083 | 440 |
} |
441 |
}); |
|
442 |
} |
|
443 |
} |
|
444 |
} |
|
445 |
||
446 |
private void runInline(Runnable run) { |
|
447 |
assert !client.isSelectorThread(); |
|
448 |
run.run(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
449 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
450 |
|
48083 | 451 |
/** Returns true if this exchange was canceled. */ |
452 |
boolean isCanceled() { |
|
453 |
synchronized (lock) { |
|
454 |
return failed != null; |
|
455 |
} |
|
456 |
} |
|
457 |
||
458 |
/** Returns the cause for which this exchange was canceled, if available. */ |
|
459 |
Throwable getCancelCause() { |
|
460 |
synchronized (lock) { |
|
461 |
return failed; |
|
462 |
} |
|
463 |
} |
|
464 |
||
465 |
/** Convenience for {@link #appendToOutgoing(DataPair)}, with just a Throwable. */ |
|
466 |
void appendToOutgoing(Throwable throwable) { |
|
467 |
appendToOutgoing(new DataPair(null, throwable)); |
|
468 |
} |
|
469 |
||
470 |
/** Convenience for {@link #appendToOutgoing(DataPair)}, with just data. */ |
|
471 |
void appendToOutgoing(List<ByteBuffer> item) { |
|
472 |
appendToOutgoing(new DataPair(item, null)); |
|
473 |
} |
|
474 |
||
475 |
private void appendToOutgoing(DataPair dp) { |
|
49765 | 476 |
if (debug.on()) debug.log("appending to outgoing " + dp); |
48083 | 477 |
outgoing.add(dp); |
478 |
writePublisher.writeScheduler.runOrSchedule(); |
|
479 |
} |
|
480 |
||
481 |
/** Tells whether, or not, there is any outgoing data that can be published, |
|
482 |
* or if there is an error. */ |
|
483 |
private boolean hasOutgoing() { |
|
484 |
return !outgoing.isEmpty(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
485 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
486 |
|
49765 | 487 |
private void requestMoreBody() { |
488 |
try { |
|
56730
b08918259eed
http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents:
56682
diff
changeset
|
489 |
if (debug.on()) debug.log("requesting more request body from the subscriber"); |
49765 | 490 |
bodySubscriber.request(1); |
491 |
} catch (Throwable t) { |
|
492 |
if (debug.on()) debug.log("Subscription::request failed", t); |
|
493 |
cancelImpl(t); |
|
494 |
bodySentCF.completeExceptionally(t); |
|
495 |
} |
|
496 |
} |
|
497 |
||
48083 | 498 |
// Invoked only by the publisher |
499 |
// ALL tasks should execute off the Selector-Manager thread |
|
500 |
/** Returns the next portion of the HTTP request, or the error. */ |
|
501 |
private DataPair getOutgoing() { |
|
502 |
final Executor exec = client.theExecutor(); |
|
503 |
final DataPair dp = outgoing.pollFirst(); |
|
504 |
||
56730
b08918259eed
http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents:
56682
diff
changeset
|
505 |
if (writePublisher.cancelled) { |
b08918259eed
http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents:
56682
diff
changeset
|
506 |
if (debug.on()) debug.log("cancelling upstream publisher"); |
b08918259eed
http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents:
56682
diff
changeset
|
507 |
if (bodySubscriber != null) { |
b08918259eed
http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents:
56682
diff
changeset
|
508 |
exec.execute(bodySubscriber::cancelSubscription); |
b08918259eed
http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents:
56682
diff
changeset
|
509 |
} else if (debug.on()) { |
b08918259eed
http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents:
56682
diff
changeset
|
510 |
debug.log("bodySubscriber is null"); |
b08918259eed
http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents:
56682
diff
changeset
|
511 |
} |
b08918259eed
http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents:
56682
diff
changeset
|
512 |
headersSentCF.completeAsync(() -> this, exec); |
b08918259eed
http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents:
56682
diff
changeset
|
513 |
bodySentCF.completeAsync(() -> this, exec); |
b08918259eed
http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents:
56682
diff
changeset
|
514 |
return null; |
b08918259eed
http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents:
56682
diff
changeset
|
515 |
} |
b08918259eed
http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents:
56682
diff
changeset
|
516 |
|
48083 | 517 |
if (dp == null) // publisher has not published anything yet |
518 |
return null; |
|
519 |
||
56621
a85c163fc41c
http-client-branch: performance - reduce context switching
dfuchs
parents:
56507
diff
changeset
|
520 |
if (dp.throwable != null) { |
a85c163fc41c
http-client-branch: performance - reduce context switching
dfuchs
parents:
56507
diff
changeset
|
521 |
synchronized (lock) { |
48083 | 522 |
state = State.ERROR; |
523 |
} |
|
56621
a85c163fc41c
http-client-branch: performance - reduce context switching
dfuchs
parents:
56507
diff
changeset
|
524 |
exec.execute(() -> { |
a85c163fc41c
http-client-branch: performance - reduce context switching
dfuchs
parents:
56507
diff
changeset
|
525 |
headersSentCF.completeExceptionally(dp.throwable); |
a85c163fc41c
http-client-branch: performance - reduce context switching
dfuchs
parents:
56507
diff
changeset
|
526 |
bodySentCF.completeExceptionally(dp.throwable); |
a85c163fc41c
http-client-branch: performance - reduce context switching
dfuchs
parents:
56507
diff
changeset
|
527 |
connection.close(); |
a85c163fc41c
http-client-branch: performance - reduce context switching
dfuchs
parents:
56507
diff
changeset
|
528 |
}); |
48083 | 529 |
return dp; |
530 |
} |
|
56621
a85c163fc41c
http-client-branch: performance - reduce context switching
dfuchs
parents:
56507
diff
changeset
|
531 |
|
a85c163fc41c
http-client-branch: performance - reduce context switching
dfuchs
parents:
56507
diff
changeset
|
532 |
switch (state) { |
a85c163fc41c
http-client-branch: performance - reduce context switching
dfuchs
parents:
56507
diff
changeset
|
533 |
case HEADERS: |
a85c163fc41c
http-client-branch: performance - reduce context switching
dfuchs
parents:
56507
diff
changeset
|
534 |
synchronized (lock) { |
a85c163fc41c
http-client-branch: performance - reduce context switching
dfuchs
parents:
56507
diff
changeset
|
535 |
state = State.BODY; |
a85c163fc41c
http-client-branch: performance - reduce context switching
dfuchs
parents:
56507
diff
changeset
|
536 |
} |
a85c163fc41c
http-client-branch: performance - reduce context switching
dfuchs
parents:
56507
diff
changeset
|
537 |
// completeAsync, since dependent tasks should run in another thread |
a85c163fc41c
http-client-branch: performance - reduce context switching
dfuchs
parents:
56507
diff
changeset
|
538 |
if (debug.on()) debug.log("initiating completion of headersSentCF"); |
a85c163fc41c
http-client-branch: performance - reduce context switching
dfuchs
parents:
56507
diff
changeset
|
539 |
headersSentCF.completeAsync(() -> this, exec); |
a85c163fc41c
http-client-branch: performance - reduce context switching
dfuchs
parents:
56507
diff
changeset
|
540 |
break; |
a85c163fc41c
http-client-branch: performance - reduce context switching
dfuchs
parents:
56507
diff
changeset
|
541 |
case BODY: |
a85c163fc41c
http-client-branch: performance - reduce context switching
dfuchs
parents:
56507
diff
changeset
|
542 |
if (dp.data == Http1BodySubscriber.COMPLETED) { |
a85c163fc41c
http-client-branch: performance - reduce context switching
dfuchs
parents:
56507
diff
changeset
|
543 |
synchronized (lock) { |
a85c163fc41c
http-client-branch: performance - reduce context switching
dfuchs
parents:
56507
diff
changeset
|
544 |
state = State.COMPLETING; |
a85c163fc41c
http-client-branch: performance - reduce context switching
dfuchs
parents:
56507
diff
changeset
|
545 |
} |
a85c163fc41c
http-client-branch: performance - reduce context switching
dfuchs
parents:
56507
diff
changeset
|
546 |
if (debug.on()) debug.log("initiating completion of bodySentCF"); |
a85c163fc41c
http-client-branch: performance - reduce context switching
dfuchs
parents:
56507
diff
changeset
|
547 |
bodySentCF.completeAsync(() -> this, exec); |
a85c163fc41c
http-client-branch: performance - reduce context switching
dfuchs
parents:
56507
diff
changeset
|
548 |
} else { |
a85c163fc41c
http-client-branch: performance - reduce context switching
dfuchs
parents:
56507
diff
changeset
|
549 |
exec.execute(this::requestMoreBody); |
a85c163fc41c
http-client-branch: performance - reduce context switching
dfuchs
parents:
56507
diff
changeset
|
550 |
} |
a85c163fc41c
http-client-branch: performance - reduce context switching
dfuchs
parents:
56507
diff
changeset
|
551 |
break; |
a85c163fc41c
http-client-branch: performance - reduce context switching
dfuchs
parents:
56507
diff
changeset
|
552 |
case INITIAL: |
a85c163fc41c
http-client-branch: performance - reduce context switching
dfuchs
parents:
56507
diff
changeset
|
553 |
case ERROR: |
a85c163fc41c
http-client-branch: performance - reduce context switching
dfuchs
parents:
56507
diff
changeset
|
554 |
case COMPLETING: |
a85c163fc41c
http-client-branch: performance - reduce context switching
dfuchs
parents:
56507
diff
changeset
|
555 |
case COMPLETED: |
a85c163fc41c
http-client-branch: performance - reduce context switching
dfuchs
parents:
56507
diff
changeset
|
556 |
default: |
a85c163fc41c
http-client-branch: performance - reduce context switching
dfuchs
parents:
56507
diff
changeset
|
557 |
assert false : "Unexpected state:" + state; |
a85c163fc41c
http-client-branch: performance - reduce context switching
dfuchs
parents:
56507
diff
changeset
|
558 |
} |
a85c163fc41c
http-client-branch: performance - reduce context switching
dfuchs
parents:
56507
diff
changeset
|
559 |
|
a85c163fc41c
http-client-branch: performance - reduce context switching
dfuchs
parents:
56507
diff
changeset
|
560 |
return dp; |
48083 | 561 |
} |
562 |
||
563 |
/** A Publisher of HTTP/1.1 headers and request body. */ |
|
564 |
final class Http1Publisher implements FlowTube.TubePublisher { |
|
565 |
||
49765 | 566 |
final Logger debug = Utils.getDebugLogger(this::dbgString); |
48083 | 567 |
volatile Flow.Subscriber<? super List<ByteBuffer>> subscriber; |
568 |
volatile boolean cancelled; |
|
569 |
final Http1WriteSubscription subscription = new Http1WriteSubscription(); |
|
570 |
final Demand demand = new Demand(); |
|
571 |
final SequentialScheduler writeScheduler = |
|
572 |
SequentialScheduler.synchronizedScheduler(new WriteTask()); |
|
573 |
||
574 |
@Override |
|
575 |
public void subscribe(Flow.Subscriber<? super List<ByteBuffer>> s) { |
|
576 |
assert state == State.INITIAL; |
|
577 |
Objects.requireNonNull(s); |
|
578 |
assert subscriber == null; |
|
579 |
||
580 |
subscriber = s; |
|
49765 | 581 |
if (debug.on()) debug.log("got subscriber: %s", s); |
48083 | 582 |
s.onSubscribe(subscription); |
44639
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43999
diff
changeset
|
583 |
} |
48083 | 584 |
|
585 |
volatile String dbgTag; |
|
586 |
String dbgString() { |
|
587 |
String tag = dbgTag; |
|
588 |
Object flow = connection.getConnectionFlow(); |
|
589 |
if (tag == null && flow != null) { |
|
590 |
dbgTag = tag = "Http1Publisher(" + flow + ")"; |
|
591 |
} else if (tag == null) { |
|
592 |
tag = "Http1Publisher(?)"; |
|
593 |
} |
|
594 |
return tag; |
|
44639
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43999
diff
changeset
|
595 |
} |
48083 | 596 |
|
597 |
final class WriteTask implements Runnable { |
|
598 |
@Override |
|
599 |
public void run() { |
|
600 |
assert state != State.COMPLETED : "Unexpected state:" + state; |
|
49765 | 601 |
if (debug.on()) debug.log("WriteTask"); |
56730
b08918259eed
http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents:
56682
diff
changeset
|
602 |
|
b08918259eed
http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents:
56682
diff
changeset
|
603 |
if (cancelled) { |
b08918259eed
http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents:
56682
diff
changeset
|
604 |
if (debug.on()) debug.log("handling cancellation"); |
b08918259eed
http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents:
56682
diff
changeset
|
605 |
writeScheduler.stop(); |
b08918259eed
http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents:
56682
diff
changeset
|
606 |
getOutgoing(); |
b08918259eed
http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents:
56682
diff
changeset
|
607 |
return; |
b08918259eed
http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents:
56682
diff
changeset
|
608 |
} |
b08918259eed
http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents:
56682
diff
changeset
|
609 |
|
48083 | 610 |
if (subscriber == null) { |
49765 | 611 |
if (debug.on()) debug.log("no subscriber yet"); |
48083 | 612 |
return; |
613 |
} |
|
49765 | 614 |
if (debug.on()) debug.log(() -> "hasOutgoing = " + hasOutgoing()); |
48083 | 615 |
while (hasOutgoing() && demand.tryDecrement()) { |
616 |
DataPair dp = getOutgoing(); |
|
56730
b08918259eed
http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents:
56682
diff
changeset
|
617 |
if (dp == null) |
b08918259eed
http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents:
56682
diff
changeset
|
618 |
break; |
48083 | 619 |
|
620 |
if (dp.throwable != null) { |
|
49765 | 621 |
if (debug.on()) debug.log("onError"); |
48083 | 622 |
// Do not call the subscriber's onError, it is not required. |
623 |
writeScheduler.stop(); |
|
624 |
} else { |
|
625 |
List<ByteBuffer> data = dp.data; |
|
626 |
if (data == Http1BodySubscriber.COMPLETED) { |
|
627 |
synchronized (lock) { |
|
628 |
assert state == State.COMPLETING : "Unexpected state:" + state; |
|
629 |
state = State.COMPLETED; |
|
630 |
} |
|
49765 | 631 |
if (debug.on()) |
632 |
debug.log("completed, stopping %s", writeScheduler); |
|
48083 | 633 |
writeScheduler.stop(); |
634 |
// Do nothing more. Just do not publish anything further. |
|
635 |
// The next Subscriber will eventually take over. |
|
636 |
||
637 |
} else { |
|
49765 | 638 |
if (debug.on()) |
639 |
debug.log("onNext with " + Utils.remaining(data) + " bytes"); |
|
48083 | 640 |
subscriber.onNext(data); |
641 |
} |
|
642 |
} |
|
643 |
} |
|
644 |
} |
|
645 |
} |
|
646 |
||
647 |
final class Http1WriteSubscription implements Flow.Subscription { |
|
648 |
||
649 |
@Override |
|
650 |
public void request(long n) { |
|
651 |
if (cancelled) |
|
652 |
return; //no-op |
|
653 |
demand.increase(n); |
|
49765 | 654 |
if (debug.on()) |
655 |
debug.log("subscription request(%d), demand=%s", n, demand); |
|
656 |
writeScheduler.runOrSchedule(client.theExecutor()); |
|
48083 | 657 |
} |
658 |
||
659 |
@Override |
|
660 |
public void cancel() { |
|
49765 | 661 |
if (debug.on()) debug.log("subscription cancelled"); |
48083 | 662 |
if (cancelled) |
663 |
return; //no-op |
|
664 |
cancelled = true; |
|
56730
b08918259eed
http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents:
56682
diff
changeset
|
665 |
writeScheduler.runOrSchedule(client.theExecutor()); |
48083 | 666 |
} |
667 |
} |
|
668 |
} |
|
669 |
||
670 |
String dbgString() { |
|
671 |
return "Http1Exchange"; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
672 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
673 |
} |