author | chegar |
Mon, 16 Jul 2018 12:11:02 +0100 | |
branch | http-client-branch |
changeset 56833 | be0819373531 |
parent 56813 | 28f791bb1c2d |
parent 50985 | cd41f34e548c |
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) { |
|
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() { |
|
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; |
|
50681 | 312 |
handleNow = this.errorRef.get() != null || onCompleteReceived; |
48083 | 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 |
||
50985
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
50681
diff
changeset
|
409 |
private void complete(DelegateWrapper subscriberImpl, Throwable t) { |
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
50681
diff
changeset
|
410 |
try { |
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
50681
diff
changeset
|
411 |
if (t == null) subscriberImpl.onComplete(); |
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
50681
diff
changeset
|
412 |
else subscriberImpl.onError(t); |
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
50681
diff
changeset
|
413 |
if (debug.on()) { |
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
50681
diff
changeset
|
414 |
debug.log("subscriber completed %s" |
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
50681
diff
changeset
|
415 |
+ ((t == null) ? "normally" : ("with error: " + t))); |
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
50681
diff
changeset
|
416 |
} |
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
50681
diff
changeset
|
417 |
} finally { |
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
50681
diff
changeset
|
418 |
// Error or EOF while reading: |
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
50681
diff
changeset
|
419 |
// cancel write side after completing read side |
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
50681
diff
changeset
|
420 |
writeSubscription.cancel(); |
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
50681
diff
changeset
|
421 |
} |
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
50681
diff
changeset
|
422 |
} |
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
50681
diff
changeset
|
423 |
|
48083 | 424 |
private void onNewSubscription(DelegateWrapper subscriberImpl, |
425 |
Flow.Subscription subscription) { |
|
426 |
assert subscriberImpl != null; |
|
427 |
assert subscription != null; |
|
428 |
||
429 |
Throwable failed; |
|
430 |
boolean completed; |
|
431 |
// reset any demand that may have been made by the previous |
|
432 |
// subscriber |
|
433 |
sslDelegate.resetReaderDemand(); |
|
434 |
// send the subscription to the subscriber. |
|
435 |
subscriberImpl.onSubscribe(subscription); |
|
436 |
||
437 |
// The following twisted logic is just here that we don't invoke |
|
438 |
// onError before onSubscribe. It also prevents race conditions |
|
439 |
// if onError is invoked concurrently with setDelegate. |
|
440 |
synchronized (this) { |
|
441 |
failed = this.errorRef.get(); |
|
50681 | 442 |
completed = onCompleteReceived; |
48083 | 443 |
subscribed = subscriberImpl; |
444 |
} |
|
50681 | 445 |
|
48083 | 446 |
if (failed != null) { |
50681 | 447 |
if (debug.on()) |
448 |
debug.log("onNewSubscription: subscriberImpl:%s, invoking onError:%s", |
|
449 |
subscriberImpl, failed); |
|
50985
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
50681
diff
changeset
|
450 |
complete(subscriberImpl, failed); |
48083 | 451 |
} else if (completed) { |
50681 | 452 |
if (debug.on()) |
453 |
debug.log("onNewSubscription: subscriberImpl:%s, invoking onCompleted", |
|
454 |
subscriberImpl); |
|
455 |
finished = true; |
|
50985
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
50681
diff
changeset
|
456 |
complete(subscriberImpl, null); |
48083 | 457 |
} |
458 |
} |
|
459 |
||
460 |
@Override |
|
461 |
public void onNext(List<ByteBuffer> item) { |
|
462 |
subscribed.onNext(item); |
|
463 |
} |
|
464 |
||
465 |
public void onErrorImpl(Throwable throwable) { |
|
466 |
// The following twisted logic is just here that we don't invoke |
|
467 |
// onError before onSubscribe. It also prevents race conditions |
|
468 |
// if onError is invoked concurrently with setDelegate. |
|
469 |
// See setDelegate. |
|
470 |
||
471 |
errorRef.compareAndSet(null, throwable); |
|
472 |
Throwable failed = errorRef.get(); |
|
473 |
finished = true; |
|
49765 | 474 |
if (debug.on()) |
475 |
debug.log("%s: onErrorImpl: %s", this, throwable); |
|
48083 | 476 |
DelegateWrapper subscriberImpl; |
477 |
synchronized (this) { |
|
478 |
subscriberImpl = subscribed; |
|
479 |
} |
|
480 |
if (subscriberImpl != null) { |
|
50985
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
50681
diff
changeset
|
481 |
complete(subscriberImpl, failed); |
48083 | 482 |
} else { |
49765 | 483 |
if (debug.on()) |
484 |
debug.log("%s: delegate null, stored %s", this, failed); |
|
48083 | 485 |
} |
486 |
// now if we have any pending subscriber, we should forward |
|
487 |
// the error to them immediately as the read scheduler will |
|
488 |
// already be stopped. |
|
489 |
processPendingSubscriber(); |
|
490 |
} |
|
491 |
||
492 |
@Override |
|
493 |
public void onError(Throwable throwable) { |
|
494 |
assert !finished && !onCompleteReceived; |
|
495 |
onErrorImpl(throwable); |
|
496 |
} |
|
497 |
||
498 |
private boolean handshaking() { |
|
499 |
HandshakeStatus hs = engine.getHandshakeStatus(); |
|
500 |
return !(hs == NOT_HANDSHAKING || hs == FINISHED); |
|
501 |
} |
|
502 |
||
50985
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
50681
diff
changeset
|
503 |
private String handshakeFailed() { |
48083 | 504 |
// sslDelegate can be null if we reach here |
505 |
// during the initial handshake, as that happens |
|
506 |
// within the SSLFlowDelegate constructor. |
|
507 |
// 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
|
508 |
if (handshaking() |
48083 | 509 |
&& (sslDelegate == null |
50985
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
50681
diff
changeset
|
510 |
|| !sslDelegate.closeNotifyReceived())) { |
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
50681
diff
changeset
|
511 |
return "Remote host terminated the handshake"; |
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
50681
diff
changeset
|
512 |
} |
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
50681
diff
changeset
|
513 |
// The initial handshake may not have been started yet. |
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
50681
diff
changeset
|
514 |
// 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
|
515 |
// 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
|
516 |
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
|
517 |
return "Remote host closed the channel"; |
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
50681
diff
changeset
|
518 |
return null; |
48083 | 519 |
} |
520 |
||
521 |
@Override |
|
522 |
public void onComplete() { |
|
523 |
assert !finished && !onCompleteReceived; |
|
524 |
DelegateWrapper subscriberImpl; |
|
525 |
synchronized(this) { |
|
526 |
subscriberImpl = subscribed; |
|
527 |
} |
|
528 |
||
50985
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
50681
diff
changeset
|
529 |
String handshakeFailed = handshakeFailed(); |
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
50681
diff
changeset
|
530 |
if (handshakeFailed != null) { |
49765 | 531 |
if (debug.on()) |
50985
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
50681
diff
changeset
|
532 |
debug.log("handshake: %s, inbound done: %s, outbound done: %s: %s", |
49765 | 533 |
engine.getHandshakeStatus(), |
534 |
engine.isInboundDone(), |
|
50985
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
50681
diff
changeset
|
535 |
engine.isOutboundDone(), |
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
50681
diff
changeset
|
536 |
handshakeFailed); |
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
50681
diff
changeset
|
537 |
onErrorImpl(new SSLHandshakeException(handshakeFailed)); |
48083 | 538 |
} else if (subscriberImpl != null) { |
50681 | 539 |
onCompleteReceived = finished = true; |
50985
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
50681
diff
changeset
|
540 |
complete(subscriberImpl, null); |
50681 | 541 |
} else { |
542 |
onCompleteReceived = true; |
|
48083 | 543 |
} |
544 |
// now if we have any pending subscriber, we should complete |
|
545 |
// them immediately as the read scheduler will already be stopped. |
|
546 |
processPendingSubscriber(); |
|
547 |
} |
|
548 |
} |
|
549 |
||
550 |
@Override |
|
551 |
public void connectFlows(TubePublisher writePub, |
|
552 |
TubeSubscriber readSub) { |
|
49765 | 553 |
if (debug.on()) debug.log("connecting flows"); |
48083 | 554 |
readSubscriber.setDelegate(readSub); |
555 |
writePub.subscribe(this); |
|
556 |
} |
|
557 |
||
558 |
/** Outstanding write demand from the SSL Flow Delegate. */ |
|
559 |
private final Demand writeDemand = new Demand(); |
|
560 |
||
561 |
final class SSLSubscriptionWrapper implements Flow.Subscription { |
|
562 |
||
563 |
volatile Flow.Subscription delegate; |
|
50681 | 564 |
private volatile boolean cancelled; |
48083 | 565 |
|
566 |
void setSubscription(Flow.Subscription sub) { |
|
567 |
long demand = writeDemand.get(); // FIXME: isn't it a racy way of passing the demand? |
|
568 |
delegate = sub; |
|
50681 | 569 |
if (debug.on()) |
570 |
debug.log("setSubscription: demand=%d, cancelled:%s", demand, cancelled); |
|
571 |
||
572 |
if (cancelled) |
|
573 |
delegate.cancel(); |
|
574 |
else if (demand > 0) |
|
48083 | 575 |
sub.request(demand); |
576 |
} |
|
577 |
||
578 |
@Override |
|
579 |
public void request(long n) { |
|
580 |
writeDemand.increase(n); |
|
49765 | 581 |
if (debug.on()) debug.log("request: n=%d", n); |
48083 | 582 |
Flow.Subscription sub = delegate; |
583 |
if (sub != null && n > 0) { |
|
584 |
sub.request(n); |
|
585 |
} |
|
586 |
} |
|
587 |
||
588 |
@Override |
|
589 |
public void cancel() { |
|
50681 | 590 |
cancelled = true; |
591 |
if (delegate != null) |
|
592 |
delegate.cancel(); |
|
48083 | 593 |
} |
594 |
} |
|
595 |
||
596 |
/* Subscriber - writing side */ |
|
597 |
@Override |
|
598 |
public void onSubscribe(Flow.Subscription subscription) { |
|
599 |
Objects.requireNonNull(subscription); |
|
600 |
Flow.Subscription x = writeSubscription.delegate; |
|
601 |
if (x != null) |
|
602 |
x.cancel(); |
|
603 |
||
604 |
writeSubscription.setSubscription(subscription); |
|
605 |
} |
|
606 |
||
607 |
@Override |
|
608 |
public void onNext(List<ByteBuffer> item) { |
|
609 |
Objects.requireNonNull(item); |
|
610 |
boolean decremented = writeDemand.tryDecrement(); |
|
611 |
assert decremented : "Unexpected writeDemand: "; |
|
49765 | 612 |
if (debug.on()) |
613 |
debug.log("sending %d buffers to SSL flow delegate", item.size()); |
|
48083 | 614 |
sslDelegate.upstreamWriter().onNext(item); |
615 |
} |
|
616 |
||
617 |
@Override |
|
618 |
public void onError(Throwable throwable) { |
|
619 |
Objects.requireNonNull(throwable); |
|
620 |
sslDelegate.upstreamWriter().onError(throwable); |
|
621 |
} |
|
622 |
||
623 |
@Override |
|
624 |
public void onComplete() { |
|
625 |
sslDelegate.upstreamWriter().onComplete(); |
|
626 |
} |
|
627 |
||
628 |
@Override |
|
629 |
public String toString() { |
|
630 |
return dbgString(); |
|
631 |
} |
|
632 |
||
633 |
final String dbgString() { |
|
634 |
return "SSLTube(" + tube + ")"; |
|
635 |
} |
|
636 |
||
637 |
} |