author | dfuchs |
Wed, 16 Jan 2019 19:09:16 +0000 | |
changeset 53350 | a47b8125b7cc |
parent 50985 | cd41f34e548c |
permissions | -rw-r--r-- |
48083 | 1 |
/* |
53350
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
50985
diff
changeset
|
2 |
* Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved. |
48083 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
49765 | 26 |
package jdk.internal.net.http.common; |
48083 | 27 |
|
28 |
import java.nio.ByteBuffer; |
|
29 |
import java.util.List; |
|
30 |
import java.util.Objects; |
|
31 |
import java.util.concurrent.CompletableFuture; |
|
32 |
import java.util.concurrent.Executor; |
|
33 |
import java.util.concurrent.Flow; |
|
34 |
import java.util.concurrent.atomic.AtomicReference; |
|
35 |
import java.util.function.Consumer; |
|
36 |
import javax.net.ssl.SSLEngine; |
|
37 |
import javax.net.ssl.SSLHandshakeException; |
|
38 |
import javax.net.ssl.SSLEngineResult.HandshakeStatus; |
|
49765 | 39 |
import jdk.internal.net.http.common.SubscriberWrapper.SchedulingAction; |
48083 | 40 |
import static javax.net.ssl.SSLEngineResult.HandshakeStatus.NOT_HANDSHAKING; |
41 |
import static javax.net.ssl.SSLEngineResult.HandshakeStatus.FINISHED; |
|
42 |
||
43 |
/** |
|
44 |
* An implementation of FlowTube that wraps another FlowTube in an |
|
45 |
* SSL flow. |
|
46 |
* <p> |
|
47 |
* The following diagram shows a typical usage of the SSLTube, where |
|
48 |
* the SSLTube wraps a SocketTube on the right hand side, and is connected |
|
49 |
* to an HttpConnection on the left hand side. |
|
50 |
* |
|
51 |
* <preformatted>{@code |
|
52 |
* +---------- SSLTube -------------------------+ |
|
53 |
* | | |
|
54 |
* | +---SSLFlowDelegate---+ | |
|
55 |
* HttpConnection | | | | SocketTube |
|
56 |
* read sink <- SSLSubscriberW. <- Reader <- upstreamR.() <---- read source |
|
57 |
* (a subscriber) | | \ / | | (a publisher) |
|
58 |
* | | SSLEngine | | |
|
59 |
* HttpConnection | | / \ | | SocketTube |
|
60 |
* write source -> SSLSubscriptionW. -> upstreamW.() -> Writer ----> write sink |
|
61 |
* (a publisher) | | | | (a subscriber) |
|
62 |
* | +---------------------+ | |
|
63 |
* | | |
|
64 |
* +---------------------------------------------+ |
|
65 |
* }</preformatted> |
|
66 |
*/ |
|
67 |
public class SSLTube implements FlowTube { |
|
68 |
||
49765 | 69 |
final Logger debug = |
70 |
Utils.getDebugLogger(this::dbgString, Utils.DEBUG); |
|
48083 | 71 |
|
72 |
private final FlowTube tube; |
|
73 |
private final SSLSubscriberWrapper readSubscriber; |
|
74 |
private final SSLSubscriptionWrapper writeSubscription; |
|
75 |
private final SSLFlowDelegate sslDelegate; |
|
76 |
private final SSLEngine engine; |
|
77 |
private volatile boolean finished; |
|
78 |
||
79 |
public SSLTube(SSLEngine engine, Executor executor, FlowTube tube) { |
|
49944 | 80 |
this(engine, executor, null, tube); |
81 |
} |
|
82 |
||
83 |
public SSLTube(SSLEngine engine, |
|
84 |
Executor executor, |
|
85 |
Consumer<ByteBuffer> recycler, |
|
86 |
FlowTube tube) { |
|
48083 | 87 |
Objects.requireNonNull(engine); |
88 |
Objects.requireNonNull(executor); |
|
89 |
this.tube = Objects.requireNonNull(tube); |
|
90 |
writeSubscription = new SSLSubscriptionWrapper(); |
|
91 |
readSubscriber = new SSLSubscriberWrapper(); |
|
92 |
this.engine = engine; |
|
93 |
sslDelegate = new SSLTubeFlowDelegate(engine, |
|
94 |
executor, |
|
49944 | 95 |
recycler, |
48083 | 96 |
readSubscriber, |
97 |
tube); |
|
98 |
} |
|
99 |
||
100 |
final class SSLTubeFlowDelegate extends SSLFlowDelegate { |
|
101 |
SSLTubeFlowDelegate(SSLEngine engine, Executor executor, |
|
49944 | 102 |
Consumer<ByteBuffer> recycler, |
48083 | 103 |
SSLSubscriberWrapper readSubscriber, |
104 |
FlowTube tube) { |
|
49944 | 105 |
super(engine, executor, recycler, readSubscriber, tube); |
48083 | 106 |
} |
107 |
protected SchedulingAction enterReadScheduling() { |
|
108 |
readSubscriber.processPendingSubscriber(); |
|
109 |
return SchedulingAction.CONTINUE; |
|
110 |
} |
|
111 |
void connect(Flow.Subscriber<? super List<ByteBuffer>> downReader, |
|
112 |
Flow.Subscriber<? super List<ByteBuffer>> downWriter) { |
|
113 |
assert downWriter == tube; |
|
114 |
assert downReader == readSubscriber; |
|
115 |
||
116 |
// Connect the read sink first. That's the left-hand side |
|
117 |
// downstream subscriber from the HttpConnection (or more |
|
118 |
// accurately, the SSLSubscriberWrapper that will wrap it |
|
119 |
// when SSLTube::connectFlows is called. |
|
120 |
reader.subscribe(downReader); |
|
121 |
||
122 |
// Connect the right hand side tube (the socket tube). |
|
123 |
// |
|
124 |
// The SSLFlowDelegate.writer publishes ByteBuffer to |
|
125 |
// the SocketTube for writing on the socket, and the |
|
126 |
// SSLFlowDelegate::upstreamReader subscribes to the |
|
127 |
// SocketTube to receive ByteBuffers read from the socket. |
|
128 |
// |
|
129 |
// Basically this method is equivalent to: |
|
130 |
// // connect the read source: |
|
131 |
// // subscribe the SSLFlowDelegate upstream reader |
|
132 |
// // to the socket tube publisher. |
|
133 |
// tube.subscribe(upstreamReader()); |
|
134 |
// // connect the write sink: |
|
135 |
// // subscribe the socket tube write subscriber |
|
136 |
// // with the SSLFlowDelegate downstream writer. |
|
137 |
// writer.subscribe(tube); |
|
138 |
tube.connectFlows(FlowTube.asTubePublisher(writer), |
|
139 |
FlowTube.asTubeSubscriber(upstreamReader())); |
|
140 |
||
141 |
// Finally connect the write source. That's the left |
|
142 |
// hand side publisher which will push ByteBuffer for |
|
143 |
// writing and encryption to the SSLFlowDelegate. |
|
144 |
// The writeSubscription is in fact the SSLSubscriptionWrapper |
|
145 |
// that will wrap the subscription provided by the |
|
146 |
// HttpConnection publisher when SSLTube::connectFlows |
|
147 |
// is called. |
|
148 |
upstreamWriter().onSubscribe(writeSubscription); |
|
149 |
} |
|
150 |
} |
|
151 |
||
152 |
public CompletableFuture<String> getALPN() { |
|
153 |
return sslDelegate.alpn(); |
|
154 |
} |
|
155 |
||
156 |
@Override |
|
157 |
public void subscribe(Flow.Subscriber<? super List<ByteBuffer>> s) { |
|
158 |
readSubscriber.dropSubscription(); |
|
159 |
readSubscriber.setDelegate(s); |
|
160 |
s.onSubscribe(readSubscription); |
|
161 |
} |
|
162 |
||
163 |
/** |
|
164 |
* Tells whether, or not, this FlowTube has finished receiving data. |
|
165 |
* |
|
166 |
* @return true when one of this FlowTube Subscriber's OnError or onComplete |
|
167 |
* methods have been invoked |
|
168 |
*/ |
|
169 |
@Override |
|
170 |
public boolean isFinished() { |
|
171 |
return finished; |
|
172 |
} |
|
173 |
||
174 |
private volatile Flow.Subscription readSubscription; |
|
175 |
||
176 |
// The DelegateWrapper wraps a subscribed {@code Flow.Subscriber} and |
|
177 |
// tracks the subscriber's state. In particular it makes sure that |
|
178 |
// onComplete/onError are not called before onSubscribed. |
|
179 |
final static class DelegateWrapper implements FlowTube.TubeSubscriber { |
|
180 |
private final FlowTube.TubeSubscriber delegate; |
|
49765 | 181 |
private final Logger debug; |
48083 | 182 |
volatile boolean subscribedCalled; |
183 |
volatile boolean subscribedDone; |
|
184 |
volatile boolean completed; |
|
185 |
volatile Throwable error; |
|
186 |
DelegateWrapper(Flow.Subscriber<? super List<ByteBuffer>> delegate, |
|
49765 | 187 |
Logger debug) { |
48083 | 188 |
this.delegate = FlowTube.asTubeSubscriber(delegate); |
189 |
this.debug = debug; |
|
190 |
} |
|
191 |
||
192 |
@Override |
|
193 |
public void dropSubscription() { |
|
194 |
if (subscribedCalled && !completed) { |
|
195 |
delegate.dropSubscription(); |
|
196 |
} |
|
197 |
} |
|
198 |
||
199 |
@Override |
|
200 |
public void onNext(List<ByteBuffer> item) { |
|
201 |
assert subscribedCalled; |
|
202 |
delegate.onNext(item); |
|
203 |
} |
|
204 |
||
205 |
@Override |
|
206 |
public void onSubscribe(Flow.Subscription subscription) { |
|
207 |
onSubscribe(delegate::onSubscribe, subscription); |
|
208 |
} |
|
209 |
||
210 |
private void onSubscribe(Consumer<Flow.Subscription> method, |
|
211 |
Flow.Subscription subscription) { |
|
212 |
subscribedCalled = true; |
|
213 |
method.accept(subscription); |
|
214 |
Throwable x; |
|
215 |
boolean finished; |
|
216 |
synchronized (this) { |
|
217 |
subscribedDone = true; |
|
218 |
x = error; |
|
219 |
finished = completed; |
|
220 |
} |
|
221 |
if (x != null) { |
|
49765 | 222 |
if (debug.on()) |
223 |
debug.log("Subscriber completed before subscribe: forwarding %s", |
|
224 |
(Object)x); |
|
48083 | 225 |
delegate.onError(x); |
226 |
} else if (finished) { |
|
49765 | 227 |
if (debug.on()) |
228 |
debug.log("Subscriber completed before subscribe: calling onComplete()"); |
|
48083 | 229 |
delegate.onComplete(); |
230 |
} |
|
231 |
} |
|
232 |
||
233 |
@Override |
|
234 |
public void onError(Throwable t) { |
|
235 |
if (completed) { |
|
49765 | 236 |
if (debug.on()) |
237 |
debug.log("Subscriber already completed: ignoring %s", |
|
238 |
(Object)t); |
|
48083 | 239 |
return; |
240 |
} |
|
241 |
boolean subscribed; |
|
242 |
synchronized (this) { |
|
243 |
if (completed) return; |
|
244 |
error = t; |
|
245 |
completed = true; |
|
246 |
subscribed = subscribedDone; |
|
247 |
} |
|
248 |
if (subscribed) { |
|
249 |
delegate.onError(t); |
|
250 |
} else { |
|
49765 | 251 |
if (debug.on()) |
252 |
debug.log("Subscriber not yet subscribed: stored %s", |
|
253 |
(Object)t); |
|
48083 | 254 |
} |
255 |
} |
|
256 |
||
257 |
@Override |
|
258 |
public void onComplete() { |
|
259 |
if (completed) return; |
|
260 |
boolean subscribed; |
|
261 |
synchronized (this) { |
|
262 |
if (completed) return; |
|
263 |
completed = true; |
|
264 |
subscribed = subscribedDone; |
|
265 |
} |
|
266 |
if (subscribed) { |
|
49765 | 267 |
if (debug.on()) debug.log("DelegateWrapper: completing subscriber"); |
48083 | 268 |
delegate.onComplete(); |
269 |
} else { |
|
49765 | 270 |
if (debug.on()) |
271 |
debug.log("Subscriber not yet subscribed: stored completed=true"); |
|
48083 | 272 |
} |
273 |
} |
|
274 |
||
275 |
@Override |
|
276 |
public String toString() { |
|
53350
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
50985
diff
changeset
|
277 |
return "DelegateWrapper[subscribedCalled: " + subscribedCalled |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
50985
diff
changeset
|
278 |
+", subscribedDone: " + subscribedDone |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
50985
diff
changeset
|
279 |
+", completed: " + completed |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
50985
diff
changeset
|
280 |
+", error: " + error |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
50985
diff
changeset
|
281 |
+"]: " + delegate; |
48083 | 282 |
} |
283 |
||
284 |
} |
|
285 |
||
286 |
// Used to read data from the SSLTube. |
|
287 |
final class SSLSubscriberWrapper implements FlowTube.TubeSubscriber { |
|
288 |
private AtomicReference<DelegateWrapper> pendingDelegate = |
|
289 |
new AtomicReference<>(); |
|
290 |
private volatile DelegateWrapper subscribed; |
|
291 |
private volatile boolean onCompleteReceived; |
|
292 |
private final AtomicReference<Throwable> errorRef |
|
293 |
= new AtomicReference<>(); |
|
294 |
||
53350
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
50985
diff
changeset
|
295 |
@Override |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
50985
diff
changeset
|
296 |
public String toString() { |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
50985
diff
changeset
|
297 |
DelegateWrapper sub = subscribed; |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
50985
diff
changeset
|
298 |
DelegateWrapper pend = pendingDelegate.get(); |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
50985
diff
changeset
|
299 |
// Though final sslFD may be null if called from within |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
50985
diff
changeset
|
300 |
// SSLFD::connect() as SSLTube is not fully constructed yet. |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
50985
diff
changeset
|
301 |
SSLFlowDelegate sslFD = sslDelegate; |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
50985
diff
changeset
|
302 |
return "SSLSubscriberWrapper[" + SSLTube.this |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
50985
diff
changeset
|
303 |
+ ", delegate: " + (sub == null ? pend :sub) |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
50985
diff
changeset
|
304 |
+ ", getALPN: " + (sslFD == null ? null : sslFD.alpn()) |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
50985
diff
changeset
|
305 |
+ ", onCompleteReceived: " + onCompleteReceived |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
50985
diff
changeset
|
306 |
+ ", onError: " + errorRef.get() + "]"; |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
50985
diff
changeset
|
307 |
} |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
50985
diff
changeset
|
308 |
|
48083 | 309 |
// setDelegate can be called asynchronously when the SSLTube flow |
310 |
// is connected. At this time the permanent subscriber (this class) |
|
311 |
// may already be subscribed (readSubscription != null) or not. |
|
312 |
// 1. If it's already subscribed (readSubscription != null), we |
|
313 |
// are going to signal the SSLFlowDelegate reader, and make sure |
|
314 |
// onSubscribed is called within the reader flow |
|
315 |
// 2. If it's not yet subscribed (readSubscription == null), then |
|
316 |
// we're going to wait for onSubscribe to be called. |
|
317 |
// |
|
318 |
void setDelegate(Flow.Subscriber<? super List<ByteBuffer>> delegate) { |
|
49765 | 319 |
if (debug.on()) |
320 |
debug.log("SSLSubscriberWrapper (reader) got delegate: %s", |
|
321 |
delegate); |
|
48083 | 322 |
assert delegate != null; |
323 |
DelegateWrapper delegateWrapper = new DelegateWrapper(delegate, debug); |
|
324 |
DelegateWrapper previous; |
|
325 |
Flow.Subscription subscription; |
|
326 |
boolean handleNow; |
|
327 |
synchronized (this) { |
|
328 |
previous = pendingDelegate.getAndSet(delegateWrapper); |
|
329 |
subscription = readSubscription; |
|
50681 | 330 |
handleNow = this.errorRef.get() != null || onCompleteReceived; |
48083 | 331 |
} |
332 |
if (previous != null) { |
|
333 |
previous.dropSubscription(); |
|
334 |
} |
|
335 |
if (subscription == null) { |
|
49765 | 336 |
if (debug.on()) |
337 |
debug.log("SSLSubscriberWrapper (reader) no subscription yet"); |
|
48083 | 338 |
return; |
339 |
} |
|
53350
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
50985
diff
changeset
|
340 |
// sslDelegate field should have been initialized by the |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
50985
diff
changeset
|
341 |
// the time we reach here, as there can be no subscriber |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
50985
diff
changeset
|
342 |
// until SSLTube is fully constructed. |
48083 | 343 |
if (handleNow || !sslDelegate.resumeReader()) { |
344 |
processPendingSubscriber(); |
|
345 |
} |
|
346 |
} |
|
347 |
||
348 |
// Can be called outside of the flow if an error has already been |
|
349 |
// raise. Otherwise, must be called within the SSLFlowDelegate |
|
350 |
// downstream reader flow. |
|
351 |
// If there is a subscription, and if there is a pending delegate, |
|
352 |
// calls dropSubscription() on the previous delegate (if any), |
|
353 |
// then subscribe the pending delegate. |
|
354 |
void processPendingSubscriber() { |
|
355 |
Flow.Subscription subscription; |
|
356 |
DelegateWrapper delegateWrapper, previous; |
|
357 |
synchronized (this) { |
|
358 |
delegateWrapper = pendingDelegate.get(); |
|
359 |
if (delegateWrapper == null) return; |
|
360 |
subscription = readSubscription; |
|
361 |
previous = subscribed; |
|
362 |
} |
|
363 |
if (subscription == null) { |
|
49765 | 364 |
if (debug.on()) |
365 |
debug.log("SSLSubscriberWrapper (reader) " + |
|
366 |
"processPendingSubscriber: no subscription yet"); |
|
48083 | 367 |
return; |
368 |
} |
|
369 |
delegateWrapper = pendingDelegate.getAndSet(null); |
|
370 |
if (delegateWrapper == null) return; |
|
371 |
if (previous != null) { |
|
372 |
previous.dropSubscription(); |
|
373 |
} |
|
374 |
onNewSubscription(delegateWrapper, subscription); |
|
375 |
} |
|
376 |
||
377 |
@Override |
|
378 |
public void dropSubscription() { |
|
379 |
DelegateWrapper subscriberImpl = subscribed; |
|
380 |
if (subscriberImpl != null) { |
|
381 |
subscriberImpl.dropSubscription(); |
|
382 |
} |
|
383 |
} |
|
384 |
||
385 |
@Override |
|
386 |
public void onSubscribe(Flow.Subscription subscription) { |
|
49765 | 387 |
if (debug.on()) |
388 |
debug.log("SSLSubscriberWrapper (reader) onSubscribe(%s)", |
|
389 |
subscription); |
|
48083 | 390 |
onSubscribeImpl(subscription); |
391 |
} |
|
392 |
||
393 |
// called in the reader flow, from onSubscribe. |
|
394 |
private void onSubscribeImpl(Flow.Subscription subscription) { |
|
395 |
assert subscription != null; |
|
396 |
DelegateWrapper subscriberImpl, pending; |
|
397 |
synchronized (this) { |
|
398 |
readSubscription = subscription; |
|
399 |
subscriberImpl = subscribed; |
|
400 |
pending = pendingDelegate.get(); |
|
401 |
} |
|
402 |
||
403 |
if (subscriberImpl == null && pending == null) { |
|
49765 | 404 |
if (debug.on()) |
405 |
debug.log("SSLSubscriberWrapper (reader) onSubscribeImpl: " |
|
406 |
+ "no delegate yet"); |
|
48083 | 407 |
return; |
408 |
} |
|
409 |
||
410 |
if (pending == null) { |
|
411 |
// There is no pending delegate, but we have a previously |
|
412 |
// subscribed delegate. This is obviously a re-subscribe. |
|
413 |
// We are in the downstream reader flow, so we should call |
|
414 |
// onSubscribe directly. |
|
49765 | 415 |
if (debug.on()) |
416 |
debug.log("SSLSubscriberWrapper (reader) onSubscribeImpl: " |
|
417 |
+ "resubscribing"); |
|
48083 | 418 |
onNewSubscription(subscriberImpl, subscription); |
419 |
} else { |
|
420 |
// We have some pending subscriber: subscribe it now that we have |
|
421 |
// a subscription. If we already had a previous delegate then |
|
422 |
// it will get a dropSubscription(). |
|
49765 | 423 |
if (debug.on()) |
424 |
debug.log("SSLSubscriberWrapper (reader) onSubscribeImpl: " |
|
425 |
+ "subscribing pending"); |
|
48083 | 426 |
processPendingSubscriber(); |
427 |
} |
|
428 |
} |
|
429 |
||
50985
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
50681
diff
changeset
|
430 |
private void complete(DelegateWrapper subscriberImpl, Throwable t) { |
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
50681
diff
changeset
|
431 |
try { |
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
50681
diff
changeset
|
432 |
if (t == null) subscriberImpl.onComplete(); |
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
50681
diff
changeset
|
433 |
else subscriberImpl.onError(t); |
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
50681
diff
changeset
|
434 |
if (debug.on()) { |
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
50681
diff
changeset
|
435 |
debug.log("subscriber completed %s" |
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
50681
diff
changeset
|
436 |
+ ((t == null) ? "normally" : ("with error: " + t))); |
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
50681
diff
changeset
|
437 |
} |
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
50681
diff
changeset
|
438 |
} finally { |
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
50681
diff
changeset
|
439 |
// Error or EOF while reading: |
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
50681
diff
changeset
|
440 |
// cancel write side after completing read side |
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
50681
diff
changeset
|
441 |
writeSubscription.cancel(); |
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
50681
diff
changeset
|
442 |
} |
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
50681
diff
changeset
|
443 |
} |
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
50681
diff
changeset
|
444 |
|
48083 | 445 |
private void onNewSubscription(DelegateWrapper subscriberImpl, |
446 |
Flow.Subscription subscription) { |
|
447 |
assert subscriberImpl != null; |
|
448 |
assert subscription != null; |
|
449 |
||
450 |
Throwable failed; |
|
451 |
boolean completed; |
|
452 |
// reset any demand that may have been made by the previous |
|
53350
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
50985
diff
changeset
|
453 |
// subscriber. sslDelegate field should have been initialized, |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
50985
diff
changeset
|
454 |
// since we only reach here when there is a subscriber. |
48083 | 455 |
sslDelegate.resetReaderDemand(); |
456 |
// send the subscription to the subscriber. |
|
457 |
subscriberImpl.onSubscribe(subscription); |
|
458 |
||
459 |
// The following twisted logic is just here that we don't invoke |
|
460 |
// onError before onSubscribe. It also prevents race conditions |
|
461 |
// if onError is invoked concurrently with setDelegate. |
|
462 |
synchronized (this) { |
|
463 |
failed = this.errorRef.get(); |
|
50681 | 464 |
completed = onCompleteReceived; |
48083 | 465 |
subscribed = subscriberImpl; |
466 |
} |
|
50681 | 467 |
|
48083 | 468 |
if (failed != null) { |
50681 | 469 |
if (debug.on()) |
470 |
debug.log("onNewSubscription: subscriberImpl:%s, invoking onError:%s", |
|
471 |
subscriberImpl, failed); |
|
50985
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
50681
diff
changeset
|
472 |
complete(subscriberImpl, failed); |
48083 | 473 |
} else if (completed) { |
50681 | 474 |
if (debug.on()) |
475 |
debug.log("onNewSubscription: subscriberImpl:%s, invoking onCompleted", |
|
476 |
subscriberImpl); |
|
477 |
finished = true; |
|
50985
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
50681
diff
changeset
|
478 |
complete(subscriberImpl, null); |
48083 | 479 |
} |
480 |
} |
|
481 |
||
482 |
@Override |
|
483 |
public void onNext(List<ByteBuffer> item) { |
|
484 |
subscribed.onNext(item); |
|
485 |
} |
|
486 |
||
487 |
public void onErrorImpl(Throwable throwable) { |
|
488 |
// The following twisted logic is just here that we don't invoke |
|
489 |
// onError before onSubscribe. It also prevents race conditions |
|
490 |
// if onError is invoked concurrently with setDelegate. |
|
491 |
// See setDelegate. |
|
492 |
||
493 |
errorRef.compareAndSet(null, throwable); |
|
494 |
Throwable failed = errorRef.get(); |
|
495 |
finished = true; |
|
49765 | 496 |
if (debug.on()) |
497 |
debug.log("%s: onErrorImpl: %s", this, throwable); |
|
48083 | 498 |
DelegateWrapper subscriberImpl; |
499 |
synchronized (this) { |
|
500 |
subscriberImpl = subscribed; |
|
501 |
} |
|
502 |
if (subscriberImpl != null) { |
|
50985
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
50681
diff
changeset
|
503 |
complete(subscriberImpl, failed); |
48083 | 504 |
} else { |
49765 | 505 |
if (debug.on()) |
506 |
debug.log("%s: delegate null, stored %s", this, failed); |
|
48083 | 507 |
} |
508 |
// now if we have any pending subscriber, we should forward |
|
509 |
// the error to them immediately as the read scheduler will |
|
510 |
// already be stopped. |
|
511 |
processPendingSubscriber(); |
|
512 |
} |
|
513 |
||
514 |
@Override |
|
515 |
public void onError(Throwable throwable) { |
|
516 |
assert !finished && !onCompleteReceived; |
|
517 |
onErrorImpl(throwable); |
|
518 |
} |
|
519 |
||
520 |
private boolean handshaking() { |
|
521 |
HandshakeStatus hs = engine.getHandshakeStatus(); |
|
522 |
return !(hs == NOT_HANDSHAKING || hs == FINISHED); |
|
523 |
} |
|
524 |
||
50985
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
50681
diff
changeset
|
525 |
private String handshakeFailed() { |
48083 | 526 |
// sslDelegate can be null if we reach here |
527 |
// during the initial handshake, as that happens |
|
528 |
// within the SSLFlowDelegate constructor. |
|
529 |
// In that case we will want to raise an exception. |
|
50985
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
50681
diff
changeset
|
530 |
if (handshaking() |
48083 | 531 |
&& (sslDelegate == null |
50985
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
50681
diff
changeset
|
532 |
|| !sslDelegate.closeNotifyReceived())) { |
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
50681
diff
changeset
|
533 |
return "Remote host terminated the handshake"; |
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
50681
diff
changeset
|
534 |
} |
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
50681
diff
changeset
|
535 |
// The initial handshake may not have been started yet. |
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
50681
diff
changeset
|
536 |
// In which case - if we are completed before the initial handshake |
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
50681
diff
changeset
|
537 |
// is started, we consider this a handshake failure as well. |
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
50681
diff
changeset
|
538 |
if ("SSL_NULL_WITH_NULL_NULL".equals(engine.getSession().getCipherSuite())) |
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
50681
diff
changeset
|
539 |
return "Remote host closed the channel"; |
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
50681
diff
changeset
|
540 |
return null; |
48083 | 541 |
} |
542 |
||
543 |
@Override |
|
544 |
public void onComplete() { |
|
545 |
assert !finished && !onCompleteReceived; |
|
546 |
DelegateWrapper subscriberImpl; |
|
547 |
synchronized(this) { |
|
548 |
subscriberImpl = subscribed; |
|
549 |
} |
|
550 |
||
50985
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
50681
diff
changeset
|
551 |
String handshakeFailed = handshakeFailed(); |
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
50681
diff
changeset
|
552 |
if (handshakeFailed != null) { |
49765 | 553 |
if (debug.on()) |
50985
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
50681
diff
changeset
|
554 |
debug.log("handshake: %s, inbound done: %s, outbound done: %s: %s", |
49765 | 555 |
engine.getHandshakeStatus(), |
556 |
engine.isInboundDone(), |
|
50985
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
50681
diff
changeset
|
557 |
engine.isOutboundDone(), |
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
50681
diff
changeset
|
558 |
handshakeFailed); |
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
50681
diff
changeset
|
559 |
onErrorImpl(new SSLHandshakeException(handshakeFailed)); |
48083 | 560 |
} else if (subscriberImpl != null) { |
50681 | 561 |
onCompleteReceived = finished = true; |
50985
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
50681
diff
changeset
|
562 |
complete(subscriberImpl, null); |
50681 | 563 |
} else { |
564 |
onCompleteReceived = true; |
|
48083 | 565 |
} |
566 |
// now if we have any pending subscriber, we should complete |
|
567 |
// them immediately as the read scheduler will already be stopped. |
|
568 |
processPendingSubscriber(); |
|
569 |
} |
|
570 |
} |
|
571 |
||
572 |
@Override |
|
573 |
public void connectFlows(TubePublisher writePub, |
|
574 |
TubeSubscriber readSub) { |
|
49765 | 575 |
if (debug.on()) debug.log("connecting flows"); |
48083 | 576 |
readSubscriber.setDelegate(readSub); |
577 |
writePub.subscribe(this); |
|
578 |
} |
|
579 |
||
580 |
/** Outstanding write demand from the SSL Flow Delegate. */ |
|
581 |
private final Demand writeDemand = new Demand(); |
|
582 |
||
583 |
final class SSLSubscriptionWrapper implements Flow.Subscription { |
|
584 |
||
585 |
volatile Flow.Subscription delegate; |
|
50681 | 586 |
private volatile boolean cancelled; |
48083 | 587 |
|
588 |
void setSubscription(Flow.Subscription sub) { |
|
589 |
long demand = writeDemand.get(); // FIXME: isn't it a racy way of passing the demand? |
|
590 |
delegate = sub; |
|
50681 | 591 |
if (debug.on()) |
592 |
debug.log("setSubscription: demand=%d, cancelled:%s", demand, cancelled); |
|
593 |
||
594 |
if (cancelled) |
|
595 |
delegate.cancel(); |
|
596 |
else if (demand > 0) |
|
48083 | 597 |
sub.request(demand); |
598 |
} |
|
599 |
||
600 |
@Override |
|
601 |
public void request(long n) { |
|
602 |
writeDemand.increase(n); |
|
49765 | 603 |
if (debug.on()) debug.log("request: n=%d", n); |
48083 | 604 |
Flow.Subscription sub = delegate; |
605 |
if (sub != null && n > 0) { |
|
606 |
sub.request(n); |
|
607 |
} |
|
608 |
} |
|
609 |
||
610 |
@Override |
|
611 |
public void cancel() { |
|
50681 | 612 |
cancelled = true; |
613 |
if (delegate != null) |
|
614 |
delegate.cancel(); |
|
48083 | 615 |
} |
616 |
} |
|
617 |
||
618 |
/* Subscriber - writing side */ |
|
619 |
@Override |
|
620 |
public void onSubscribe(Flow.Subscription subscription) { |
|
621 |
Objects.requireNonNull(subscription); |
|
622 |
Flow.Subscription x = writeSubscription.delegate; |
|
623 |
if (x != null) |
|
624 |
x.cancel(); |
|
625 |
||
626 |
writeSubscription.setSubscription(subscription); |
|
627 |
} |
|
628 |
||
629 |
@Override |
|
630 |
public void onNext(List<ByteBuffer> item) { |
|
631 |
Objects.requireNonNull(item); |
|
632 |
boolean decremented = writeDemand.tryDecrement(); |
|
633 |
assert decremented : "Unexpected writeDemand: "; |
|
49765 | 634 |
if (debug.on()) |
635 |
debug.log("sending %d buffers to SSL flow delegate", item.size()); |
|
48083 | 636 |
sslDelegate.upstreamWriter().onNext(item); |
637 |
} |
|
638 |
||
639 |
@Override |
|
640 |
public void onError(Throwable throwable) { |
|
641 |
Objects.requireNonNull(throwable); |
|
642 |
sslDelegate.upstreamWriter().onError(throwable); |
|
643 |
} |
|
644 |
||
645 |
@Override |
|
646 |
public void onComplete() { |
|
647 |
sslDelegate.upstreamWriter().onComplete(); |
|
648 |
} |
|
649 |
||
650 |
@Override |
|
651 |
public String toString() { |
|
652 |
return dbgString(); |
|
653 |
} |
|
654 |
||
655 |
final String dbgString() { |
|
656 |
return "SSLTube(" + tube + ")"; |
|
657 |
} |
|
658 |
||
659 |
} |