author | dfuchs |
Mon, 23 Apr 2018 15:45:40 +0100 | |
branch | http-client-branch |
changeset 56474 | fe2bf7b369b8 |
parent 56451 | 9585061fdb04 |
child 56507 | 2294c51eae30 |
permissions | -rw-r--r-- |
48083 | 1 |
/* |
49765 | 2 |
* Copyright (c) 2017, 2018, 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) { |
|
56474
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56451
diff
changeset
|
80 |
this(engine, executor, null, tube); |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56451
diff
changeset
|
81 |
} |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56451
diff
changeset
|
82 |
|
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56451
diff
changeset
|
83 |
public SSLTube(SSLEngine engine, |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56451
diff
changeset
|
84 |
Executor executor, |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56451
diff
changeset
|
85 |
Consumer<ByteBuffer> recycler, |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56451
diff
changeset
|
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, |
|
56474
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56451
diff
changeset
|
95 |
recycler, |
48083 | 96 |
readSubscriber, |
97 |
tube); |
|
98 |
} |
|
99 |
||
100 |
final class SSLTubeFlowDelegate extends SSLFlowDelegate { |
|
101 |
SSLTubeFlowDelegate(SSLEngine engine, Executor executor, |
|
56474
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56451
diff
changeset
|
102 |
Consumer<ByteBuffer> recycler, |
48083 | 103 |
SSLSubscriberWrapper readSubscriber, |
104 |
FlowTube tube) { |
|
56474
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56451
diff
changeset
|
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() { |
|
277 |
return "DelegateWrapper:" + delegate.toString(); |
|
278 |
} |
|
279 |
||
280 |
} |
|
281 |
||
282 |
// Used to read data from the SSLTube. |
|
283 |
final class SSLSubscriberWrapper implements FlowTube.TubeSubscriber { |
|
284 |
private AtomicReference<DelegateWrapper> pendingDelegate = |
|
285 |
new AtomicReference<>(); |
|
286 |
private volatile DelegateWrapper subscribed; |
|
287 |
private volatile boolean onCompleteReceived; |
|
288 |
private final AtomicReference<Throwable> errorRef |
|
289 |
= new AtomicReference<>(); |
|
290 |
||
291 |
// setDelegate can be called asynchronously when the SSLTube flow |
|
292 |
// is connected. At this time the permanent subscriber (this class) |
|
293 |
// may already be subscribed (readSubscription != null) or not. |
|
294 |
// 1. If it's already subscribed (readSubscription != null), we |
|
295 |
// are going to signal the SSLFlowDelegate reader, and make sure |
|
296 |
// onSubscribed is called within the reader flow |
|
297 |
// 2. If it's not yet subscribed (readSubscription == null), then |
|
298 |
// we're going to wait for onSubscribe to be called. |
|
299 |
// |
|
300 |
void setDelegate(Flow.Subscriber<? super List<ByteBuffer>> delegate) { |
|
49765 | 301 |
if (debug.on()) |
302 |
debug.log("SSLSubscriberWrapper (reader) got delegate: %s", |
|
303 |
delegate); |
|
48083 | 304 |
assert delegate != null; |
305 |
DelegateWrapper delegateWrapper = new DelegateWrapper(delegate, debug); |
|
306 |
DelegateWrapper previous; |
|
307 |
Flow.Subscription subscription; |
|
308 |
boolean handleNow; |
|
309 |
synchronized (this) { |
|
310 |
previous = pendingDelegate.getAndSet(delegateWrapper); |
|
311 |
subscription = readSubscription; |
|
312 |
handleNow = this.errorRef.get() != null || finished; |
|
313 |
} |
|
314 |
if (previous != null) { |
|
315 |
previous.dropSubscription(); |
|
316 |
} |
|
317 |
if (subscription == null) { |
|
49765 | 318 |
if (debug.on()) |
319 |
debug.log("SSLSubscriberWrapper (reader) no subscription yet"); |
|
48083 | 320 |
return; |
321 |
} |
|
322 |
if (handleNow || !sslDelegate.resumeReader()) { |
|
323 |
processPendingSubscriber(); |
|
324 |
} |
|
325 |
} |
|
326 |
||
327 |
// Can be called outside of the flow if an error has already been |
|
328 |
// raise. Otherwise, must be called within the SSLFlowDelegate |
|
329 |
// downstream reader flow. |
|
330 |
// If there is a subscription, and if there is a pending delegate, |
|
331 |
// calls dropSubscription() on the previous delegate (if any), |
|
332 |
// then subscribe the pending delegate. |
|
333 |
void processPendingSubscriber() { |
|
334 |
Flow.Subscription subscription; |
|
335 |
DelegateWrapper delegateWrapper, previous; |
|
336 |
synchronized (this) { |
|
337 |
delegateWrapper = pendingDelegate.get(); |
|
338 |
if (delegateWrapper == null) return; |
|
339 |
subscription = readSubscription; |
|
340 |
previous = subscribed; |
|
341 |
} |
|
342 |
if (subscription == null) { |
|
49765 | 343 |
if (debug.on()) |
344 |
debug.log("SSLSubscriberWrapper (reader) " + |
|
345 |
"processPendingSubscriber: no subscription yet"); |
|
48083 | 346 |
return; |
347 |
} |
|
348 |
delegateWrapper = pendingDelegate.getAndSet(null); |
|
349 |
if (delegateWrapper == null) return; |
|
350 |
if (previous != null) { |
|
351 |
previous.dropSubscription(); |
|
352 |
} |
|
353 |
onNewSubscription(delegateWrapper, subscription); |
|
354 |
} |
|
355 |
||
356 |
@Override |
|
357 |
public void dropSubscription() { |
|
358 |
DelegateWrapper subscriberImpl = subscribed; |
|
359 |
if (subscriberImpl != null) { |
|
360 |
subscriberImpl.dropSubscription(); |
|
361 |
} |
|
362 |
} |
|
363 |
||
364 |
@Override |
|
365 |
public void onSubscribe(Flow.Subscription subscription) { |
|
49765 | 366 |
if (debug.on()) |
367 |
debug.log("SSLSubscriberWrapper (reader) onSubscribe(%s)", |
|
368 |
subscription); |
|
48083 | 369 |
onSubscribeImpl(subscription); |
370 |
} |
|
371 |
||
372 |
// called in the reader flow, from onSubscribe. |
|
373 |
private void onSubscribeImpl(Flow.Subscription subscription) { |
|
374 |
assert subscription != null; |
|
375 |
DelegateWrapper subscriberImpl, pending; |
|
376 |
synchronized (this) { |
|
377 |
readSubscription = subscription; |
|
378 |
subscriberImpl = subscribed; |
|
379 |
pending = pendingDelegate.get(); |
|
380 |
} |
|
381 |
||
382 |
if (subscriberImpl == null && pending == null) { |
|
49765 | 383 |
if (debug.on()) |
384 |
debug.log("SSLSubscriberWrapper (reader) onSubscribeImpl: " |
|
385 |
+ "no delegate yet"); |
|
48083 | 386 |
return; |
387 |
} |
|
388 |
||
389 |
if (pending == null) { |
|
390 |
// There is no pending delegate, but we have a previously |
|
391 |
// subscribed delegate. This is obviously a re-subscribe. |
|
392 |
// We are in the downstream reader flow, so we should call |
|
393 |
// onSubscribe directly. |
|
49765 | 394 |
if (debug.on()) |
395 |
debug.log("SSLSubscriberWrapper (reader) onSubscribeImpl: " |
|
396 |
+ "resubscribing"); |
|
48083 | 397 |
onNewSubscription(subscriberImpl, subscription); |
398 |
} else { |
|
399 |
// We have some pending subscriber: subscribe it now that we have |
|
400 |
// a subscription. If we already had a previous delegate then |
|
401 |
// it will get a dropSubscription(). |
|
49765 | 402 |
if (debug.on()) |
403 |
debug.log("SSLSubscriberWrapper (reader) onSubscribeImpl: " |
|
404 |
+ "subscribing pending"); |
|
48083 | 405 |
processPendingSubscriber(); |
406 |
} |
|
407 |
} |
|
408 |
||
409 |
private void onNewSubscription(DelegateWrapper subscriberImpl, |
|
410 |
Flow.Subscription subscription) { |
|
411 |
assert subscriberImpl != null; |
|
412 |
assert subscription != null; |
|
413 |
||
414 |
Throwable failed; |
|
415 |
boolean completed; |
|
416 |
// reset any demand that may have been made by the previous |
|
417 |
// subscriber |
|
418 |
sslDelegate.resetReaderDemand(); |
|
419 |
// send the subscription to the subscriber. |
|
420 |
subscriberImpl.onSubscribe(subscription); |
|
421 |
||
422 |
// The following twisted logic is just here that we don't invoke |
|
423 |
// onError before onSubscribe. It also prevents race conditions |
|
424 |
// if onError is invoked concurrently with setDelegate. |
|
425 |
synchronized (this) { |
|
426 |
failed = this.errorRef.get(); |
|
427 |
completed = finished; |
|
428 |
subscribed = subscriberImpl; |
|
429 |
} |
|
430 |
if (failed != null) { |
|
431 |
subscriberImpl.onError(failed); |
|
432 |
} else if (completed) { |
|
433 |
subscriberImpl.onComplete(); |
|
434 |
} |
|
435 |
} |
|
436 |
||
437 |
@Override |
|
438 |
public void onNext(List<ByteBuffer> item) { |
|
439 |
subscribed.onNext(item); |
|
440 |
} |
|
441 |
||
442 |
public void onErrorImpl(Throwable throwable) { |
|
443 |
// The following twisted logic is just here that we don't invoke |
|
444 |
// onError before onSubscribe. It also prevents race conditions |
|
445 |
// if onError is invoked concurrently with setDelegate. |
|
446 |
// See setDelegate. |
|
447 |
||
448 |
errorRef.compareAndSet(null, throwable); |
|
449 |
Throwable failed = errorRef.get(); |
|
450 |
finished = true; |
|
49765 | 451 |
if (debug.on()) |
452 |
debug.log("%s: onErrorImpl: %s", this, throwable); |
|
48083 | 453 |
DelegateWrapper subscriberImpl; |
454 |
synchronized (this) { |
|
455 |
subscriberImpl = subscribed; |
|
456 |
} |
|
457 |
if (subscriberImpl != null) { |
|
458 |
subscriberImpl.onError(failed); |
|
459 |
} else { |
|
49765 | 460 |
if (debug.on()) |
461 |
debug.log("%s: delegate null, stored %s", this, failed); |
|
48083 | 462 |
} |
463 |
// now if we have any pending subscriber, we should forward |
|
464 |
// the error to them immediately as the read scheduler will |
|
465 |
// already be stopped. |
|
466 |
processPendingSubscriber(); |
|
467 |
} |
|
468 |
||
469 |
@Override |
|
470 |
public void onError(Throwable throwable) { |
|
471 |
assert !finished && !onCompleteReceived; |
|
472 |
onErrorImpl(throwable); |
|
473 |
} |
|
474 |
||
475 |
private boolean handshaking() { |
|
476 |
HandshakeStatus hs = engine.getHandshakeStatus(); |
|
477 |
return !(hs == NOT_HANDSHAKING || hs == FINISHED); |
|
478 |
} |
|
479 |
||
480 |
private boolean handshakeFailed() { |
|
481 |
// sslDelegate can be null if we reach here |
|
482 |
// during the initial handshake, as that happens |
|
483 |
// within the SSLFlowDelegate constructor. |
|
484 |
// In that case we will want to raise an exception. |
|
485 |
return handshaking() |
|
486 |
&& (sslDelegate == null |
|
487 |
|| !sslDelegate.closeNotifyReceived()); |
|
488 |
} |
|
489 |
||
490 |
@Override |
|
491 |
public void onComplete() { |
|
492 |
assert !finished && !onCompleteReceived; |
|
493 |
onCompleteReceived = true; |
|
494 |
DelegateWrapper subscriberImpl; |
|
495 |
synchronized(this) { |
|
496 |
subscriberImpl = subscribed; |
|
497 |
} |
|
498 |
||
499 |
if (handshakeFailed()) { |
|
49765 | 500 |
if (debug.on()) |
501 |
debug.log("handshake: %s, inbound done: %s outbound done: %s", |
|
502 |
engine.getHandshakeStatus(), |
|
503 |
engine.isInboundDone(), |
|
504 |
engine.isOutboundDone()); |
|
48083 | 505 |
onErrorImpl(new SSLHandshakeException( |
506 |
"Remote host terminated the handshake")); |
|
507 |
} else if (subscriberImpl != null) { |
|
508 |
finished = true; |
|
509 |
subscriberImpl.onComplete(); |
|
510 |
} |
|
511 |
// now if we have any pending subscriber, we should complete |
|
512 |
// them immediately as the read scheduler will already be stopped. |
|
513 |
processPendingSubscriber(); |
|
514 |
} |
|
515 |
} |
|
516 |
||
517 |
@Override |
|
518 |
public void connectFlows(TubePublisher writePub, |
|
519 |
TubeSubscriber readSub) { |
|
49765 | 520 |
if (debug.on()) debug.log("connecting flows"); |
48083 | 521 |
readSubscriber.setDelegate(readSub); |
522 |
writePub.subscribe(this); |
|
523 |
} |
|
524 |
||
525 |
/** Outstanding write demand from the SSL Flow Delegate. */ |
|
526 |
private final Demand writeDemand = new Demand(); |
|
527 |
||
528 |
final class SSLSubscriptionWrapper implements Flow.Subscription { |
|
529 |
||
530 |
volatile Flow.Subscription delegate; |
|
531 |
||
532 |
void setSubscription(Flow.Subscription sub) { |
|
533 |
long demand = writeDemand.get(); // FIXME: isn't it a racy way of passing the demand? |
|
534 |
delegate = sub; |
|
49765 | 535 |
if (debug.on()) debug.log("setSubscription: demand=%d", demand); |
48083 | 536 |
if (demand > 0) |
537 |
sub.request(demand); |
|
538 |
} |
|
539 |
||
540 |
@Override |
|
541 |
public void request(long n) { |
|
542 |
writeDemand.increase(n); |
|
49765 | 543 |
if (debug.on()) debug.log("request: n=%d", n); |
48083 | 544 |
Flow.Subscription sub = delegate; |
545 |
if (sub != null && n > 0) { |
|
546 |
sub.request(n); |
|
547 |
} |
|
548 |
} |
|
549 |
||
550 |
@Override |
|
551 |
public void cancel() { |
|
552 |
// TODO: no-op or error? |
|
553 |
} |
|
554 |
} |
|
555 |
||
556 |
/* Subscriber - writing side */ |
|
557 |
@Override |
|
558 |
public void onSubscribe(Flow.Subscription subscription) { |
|
559 |
Objects.requireNonNull(subscription); |
|
560 |
Flow.Subscription x = writeSubscription.delegate; |
|
561 |
if (x != null) |
|
562 |
x.cancel(); |
|
563 |
||
564 |
writeSubscription.setSubscription(subscription); |
|
565 |
} |
|
566 |
||
567 |
@Override |
|
568 |
public void onNext(List<ByteBuffer> item) { |
|
569 |
Objects.requireNonNull(item); |
|
570 |
boolean decremented = writeDemand.tryDecrement(); |
|
571 |
assert decremented : "Unexpected writeDemand: "; |
|
49765 | 572 |
if (debug.on()) |
573 |
debug.log("sending %d buffers to SSL flow delegate", item.size()); |
|
48083 | 574 |
sslDelegate.upstreamWriter().onNext(item); |
575 |
} |
|
576 |
||
577 |
@Override |
|
578 |
public void onError(Throwable throwable) { |
|
579 |
Objects.requireNonNull(throwable); |
|
580 |
sslDelegate.upstreamWriter().onError(throwable); |
|
581 |
} |
|
582 |
||
583 |
@Override |
|
584 |
public void onComplete() { |
|
585 |
sslDelegate.upstreamWriter().onComplete(); |
|
586 |
} |
|
587 |
||
588 |
@Override |
|
589 |
public String toString() { |
|
590 |
return dbgString(); |
|
591 |
} |
|
592 |
||
593 |
final String dbgString() { |
|
594 |
return "SSLTube(" + tube + ")"; |
|
595 |
} |
|
596 |
||
597 |
} |