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