55763
|
1 |
/*
|
|
2 |
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
|
|
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 |
|
|
26 |
package jdk.incubator.http.internal.common;
|
|
27 |
|
|
28 |
import java.lang.System.Logger.Level;
|
|
29 |
import java.nio.ByteBuffer;
|
|
30 |
import java.util.List;
|
|
31 |
import java.util.Objects;
|
|
32 |
import java.util.concurrent.CompletableFuture;
|
|
33 |
import java.util.concurrent.Executor;
|
|
34 |
import java.util.concurrent.Flow;
|
|
35 |
import java.util.concurrent.atomic.AtomicReference;
|
|
36 |
import java.util.function.Consumer;
|
|
37 |
import javax.net.ssl.SSLEngine;
|
|
38 |
import javax.net.ssl.SSLHandshakeException;
|
|
39 |
import javax.net.ssl.SSLEngineResult.HandshakeStatus;
|
|
40 |
import static javax.net.ssl.SSLEngineResult.HandshakeStatus.NOT_HANDSHAKING;
|
|
41 |
import static javax.net.ssl.SSLEngineResult.HandshakeStatus.FINISHED;
|
|
42 |
|
|
43 |
public class SSLTube implements FlowTube {
|
|
44 |
|
|
45 |
static final boolean DEBUG = Utils.DEBUG; // revisit: temporary developer's flag.
|
|
46 |
final System.Logger debug =
|
|
47 |
Utils.getDebugLogger(this::dbgString, DEBUG);
|
|
48 |
|
|
49 |
private final FlowTube tube;
|
|
50 |
private final SSLSubscriberWrapper readSubscriber;
|
|
51 |
private final SSLSubscriptionWrapper writeSubscription;
|
|
52 |
private final SSLFlowDelegate sslDelegate;
|
|
53 |
private final SSLEngine engine;
|
|
54 |
private volatile boolean finished;
|
|
55 |
|
|
56 |
public SSLTube(SSLEngine engine, Executor executor, FlowTube tube) {
|
|
57 |
Objects.requireNonNull(engine);
|
|
58 |
Objects.requireNonNull(executor);
|
|
59 |
this.tube = Objects.requireNonNull(tube);
|
|
60 |
writeSubscription = new SSLSubscriptionWrapper();
|
|
61 |
readSubscriber = new SSLSubscriberWrapper();
|
|
62 |
this.engine = engine;
|
|
63 |
sslDelegate = new SSLFlowDelegate(engine,
|
|
64 |
executor,
|
|
65 |
readSubscriber,
|
|
66 |
tube); // FIXME
|
|
67 |
tube.subscribe(sslDelegate.upstreamReader());
|
|
68 |
sslDelegate.upstreamWriter().onSubscribe(writeSubscription);
|
|
69 |
}
|
|
70 |
|
|
71 |
public CompletableFuture<String> getALPN() {
|
|
72 |
return sslDelegate.alpn();
|
|
73 |
}
|
|
74 |
|
|
75 |
@Override
|
|
76 |
public void subscribe(Flow.Subscriber<? super List<ByteBuffer>> s) {
|
|
77 |
readSubscriber.dropSubscription();
|
|
78 |
readSubscriber.setDelegate(s);
|
|
79 |
s.onSubscribe(readSubscription);
|
|
80 |
}
|
|
81 |
|
|
82 |
/**
|
|
83 |
* Tells whether, or not, this FlowTube has finished receiving data.
|
|
84 |
*
|
|
85 |
* @return true when one of this FlowTube Subscriber's OnError or onComplete
|
|
86 |
* methods have been invoked
|
|
87 |
*/
|
|
88 |
@Override
|
|
89 |
public boolean isFinished() {
|
|
90 |
return finished;
|
|
91 |
}
|
|
92 |
|
|
93 |
private volatile Flow.Subscription readSubscription;
|
|
94 |
|
|
95 |
// The DelegateWrapper wraps a subscribed {@code Flow.Subscriber} and
|
|
96 |
// tracks the subscriber's state. In particular it makes sure that
|
|
97 |
// onComplete/onError are not called before onSubscribed.
|
|
98 |
final static class DelegateWrapper implements FlowTube.TubeSubscriber {
|
|
99 |
private final FlowTube.TubeSubscriber delegate;
|
|
100 |
volatile boolean subscribedCalled;
|
|
101 |
volatile boolean subscribedDone;
|
|
102 |
volatile boolean completed;
|
|
103 |
volatile Throwable error;
|
|
104 |
DelegateWrapper(Flow.Subscriber<? super List<ByteBuffer>> delegate) {
|
|
105 |
this.delegate = FlowTube.asTubeSubscriber(delegate);
|
|
106 |
}
|
|
107 |
|
|
108 |
@Override
|
|
109 |
public void dropSubscription() {
|
|
110 |
if (subscribedCalled) {
|
|
111 |
delegate.dropSubscription();
|
|
112 |
}
|
|
113 |
}
|
|
114 |
|
|
115 |
@Override
|
|
116 |
public void onNext(List<ByteBuffer> item) {
|
|
117 |
assert subscribedCalled;
|
|
118 |
delegate.onNext(item);
|
|
119 |
}
|
|
120 |
|
|
121 |
@Override
|
|
122 |
public void onSubscribe(Flow.Subscription subscription) {
|
|
123 |
onSubscribe(delegate::onSubscribe, subscription);
|
|
124 |
}
|
|
125 |
|
|
126 |
@Override
|
|
127 |
public void onConnection(Flow.Subscription subscription) {
|
|
128 |
onSubscribe(delegate::onConnection, subscription);
|
|
129 |
}
|
|
130 |
|
|
131 |
private void onSubscribe(Consumer<Flow.Subscription> method,
|
|
132 |
Flow.Subscription subscription) {
|
|
133 |
subscribedCalled = true;
|
|
134 |
method.accept(subscription);
|
|
135 |
Throwable x;
|
|
136 |
boolean finished;
|
|
137 |
synchronized (this) {
|
|
138 |
subscribedDone = true;
|
|
139 |
x = error;
|
|
140 |
finished = completed;
|
|
141 |
}
|
|
142 |
if (x != null) {
|
|
143 |
delegate.onError(x);
|
|
144 |
} else if (finished) {
|
|
145 |
delegate.onComplete();
|
|
146 |
}
|
|
147 |
}
|
|
148 |
|
|
149 |
@Override
|
|
150 |
public void onError(Throwable t) {
|
|
151 |
if (completed) return;
|
|
152 |
boolean subscribed;
|
|
153 |
synchronized (this) {
|
|
154 |
if (completed) return;
|
|
155 |
error = t;
|
|
156 |
completed = true;
|
|
157 |
subscribed = subscribedDone;
|
|
158 |
}
|
|
159 |
if (subscribed) {
|
|
160 |
delegate.onError(t);
|
|
161 |
}
|
|
162 |
}
|
|
163 |
|
|
164 |
@Override
|
|
165 |
public void onComplete() {
|
|
166 |
if (completed) return;
|
|
167 |
boolean subscribed;
|
|
168 |
synchronized (this) {
|
|
169 |
if (completed) return;
|
|
170 |
completed = true;
|
|
171 |
subscribed = subscribedDone;
|
|
172 |
}
|
|
173 |
if (subscribed) {
|
|
174 |
delegate.onComplete();
|
|
175 |
}
|
|
176 |
}
|
|
177 |
|
|
178 |
@Override
|
|
179 |
public String toString() {
|
|
180 |
return "DelegateWrapper:" + delegate.toString();
|
|
181 |
}
|
|
182 |
|
|
183 |
}
|
|
184 |
|
|
185 |
// Used to read data from the SSLTube.
|
|
186 |
final class SSLSubscriberWrapper implements FlowTube.TubeSubscriber {
|
|
187 |
private volatile DelegateWrapper delegate;
|
|
188 |
private volatile DelegateWrapper subscribed;
|
|
189 |
private volatile boolean onCompleteReceived;
|
|
190 |
private final AtomicReference<Throwable> errorRef
|
|
191 |
= new AtomicReference<>();
|
|
192 |
|
|
193 |
void setDelegate(Flow.Subscriber<? super List<ByteBuffer>> delegate) {
|
|
194 |
debug.log(Level.DEBUG, "SSLSubscriberWrapper (reader) got delegate: %s",
|
|
195 |
delegate);
|
|
196 |
assert delegate != null;
|
|
197 |
DelegateWrapper delegateWrapper = new DelegateWrapper(delegate);
|
|
198 |
Flow.Subscription subscription;
|
|
199 |
synchronized (this) {
|
|
200 |
this.delegate = delegateWrapper;
|
|
201 |
subscription = readSubscription;
|
|
202 |
}
|
|
203 |
if (subscription == null) {
|
|
204 |
debug.log(Level.DEBUG, "SSLSubscriberWrapper (reader) no subscription yet");
|
|
205 |
return;
|
|
206 |
}
|
|
207 |
|
|
208 |
onNewSubscription(delegateWrapper,
|
|
209 |
delegateWrapper::onSubscribe,
|
|
210 |
subscription);
|
|
211 |
}
|
|
212 |
|
|
213 |
@Override
|
|
214 |
public void dropSubscription() {
|
|
215 |
DelegateWrapper subscriberImpl = delegate;
|
|
216 |
if (subscriberImpl != null) {
|
|
217 |
subscriberImpl.dropSubscription();
|
|
218 |
}
|
|
219 |
}
|
|
220 |
|
|
221 |
@Override
|
|
222 |
public void onConnection(Flow.Subscription subscription) {
|
|
223 |
debug.log(Level.DEBUG,
|
|
224 |
"SSLSubscriberWrapper (reader) onConnection(%s)",
|
|
225 |
subscription);
|
|
226 |
assert subscription != null;
|
|
227 |
DelegateWrapper subscriberImpl;
|
|
228 |
synchronized (this) {
|
|
229 |
subscriberImpl = delegate;
|
|
230 |
readSubscription = subscription;
|
|
231 |
}
|
|
232 |
if (subscriberImpl == null) {
|
|
233 |
debug.log(Level.DEBUG,
|
|
234 |
"SSLSubscriberWrapper (reader) onConnection: no delegate yet");
|
|
235 |
return;
|
|
236 |
}
|
|
237 |
onNewSubscription(subscriberImpl,
|
|
238 |
subscriberImpl::onConnection,
|
|
239 |
subscription);
|
|
240 |
}
|
|
241 |
|
|
242 |
@Override
|
|
243 |
public void onSubscribe(Flow.Subscription subscription) {
|
|
244 |
debug.log(Level.DEBUG,
|
|
245 |
"SSLSubscriberWrapper (reader) onSubscribe(%s)",
|
|
246 |
subscription);
|
|
247 |
readSubscription = subscription;
|
|
248 |
assert subscription != null;
|
|
249 |
DelegateWrapper subscriberImpl;
|
|
250 |
synchronized (this) {
|
|
251 |
subscriberImpl = delegate;
|
|
252 |
readSubscription = subscription;
|
|
253 |
}
|
|
254 |
if (subscriberImpl == null) {
|
|
255 |
debug.log(Level.DEBUG,
|
|
256 |
"SSLSubscriberWrapper (reader) onSubscribe: no delegate yet");
|
|
257 |
return;
|
|
258 |
}
|
|
259 |
onNewSubscription(subscriberImpl,
|
|
260 |
subscriberImpl::onSubscribe,
|
|
261 |
subscription);
|
|
262 |
}
|
|
263 |
|
|
264 |
private void onNewSubscription(DelegateWrapper subscriberImpl,
|
|
265 |
Consumer<Flow.Subscription> method,
|
|
266 |
Flow.Subscription subscription) {
|
|
267 |
assert subscriberImpl != null;
|
|
268 |
assert method != null;
|
|
269 |
assert subscription != null;
|
|
270 |
|
|
271 |
Throwable failed;
|
|
272 |
boolean completed;
|
|
273 |
// reset any demand that may have been made by the previous
|
|
274 |
// subscriber
|
|
275 |
sslDelegate.resetReaderDemand();
|
|
276 |
// send the subscription to the subscriber.
|
|
277 |
method.accept(subscription);
|
|
278 |
// reschedule after calling onSubscribe (this should not be
|
|
279 |
// strictly needed as the first call to subscription.request()
|
|
280 |
// coming after resetting the demand should trigger it).
|
|
281 |
// However, it should not do any harm.
|
|
282 |
sslDelegate.resumeReader();
|
|
283 |
|
|
284 |
// The following twisted logic is just here that we don't invoke
|
|
285 |
// onError before onSubscribe. It also prevents race conditions
|
|
286 |
// if onError is invoked concurrently with setDelegate.
|
|
287 |
synchronized (this) {
|
|
288 |
failed = this.errorRef.get();
|
|
289 |
completed = finished;
|
|
290 |
if (delegate == subscriberImpl) {
|
|
291 |
subscribed = subscriberImpl;
|
|
292 |
}
|
|
293 |
}
|
|
294 |
if (failed != null) {
|
|
295 |
subscriberImpl.onError(failed);
|
|
296 |
} else if (completed) {
|
|
297 |
subscriberImpl.onComplete();
|
|
298 |
}
|
|
299 |
}
|
|
300 |
|
|
301 |
@Override
|
|
302 |
public void onNext(List<ByteBuffer> item) {
|
|
303 |
delegate.onNext(item);
|
|
304 |
}
|
|
305 |
|
|
306 |
public void onErrorImpl(Throwable throwable) {
|
|
307 |
// The following twisted logic is just here that we don't invoke
|
|
308 |
// onError before onSubscribe. It also prevents race conditions
|
|
309 |
// if onError is invoked concurrently with setDelegate.
|
|
310 |
// See setDelegate.
|
|
311 |
|
|
312 |
errorRef.compareAndSet(null, throwable);
|
|
313 |
Throwable failed = errorRef.get();
|
|
314 |
finished = true;
|
|
315 |
debug.log(Level.DEBUG, "%s: onErrorImpl: %s", this, throwable);
|
|
316 |
DelegateWrapper subscriberImpl;
|
|
317 |
synchronized (this) {
|
|
318 |
subscriberImpl = subscribed;
|
|
319 |
}
|
|
320 |
if (subscriberImpl != null) {
|
|
321 |
subscriberImpl.onError(failed);
|
|
322 |
} else {
|
|
323 |
debug.log(Level.DEBUG, "%s: delegate null, stored %s", this, failed);
|
|
324 |
}
|
|
325 |
}
|
|
326 |
|
|
327 |
@Override
|
|
328 |
public void onError(Throwable throwable) {
|
|
329 |
assert !finished && !onCompleteReceived;
|
|
330 |
onErrorImpl(throwable);
|
|
331 |
}
|
|
332 |
|
|
333 |
private boolean handshaking() {
|
|
334 |
HandshakeStatus hs = engine.getHandshakeStatus();
|
|
335 |
return !(hs == NOT_HANDSHAKING || hs == FINISHED);
|
|
336 |
}
|
|
337 |
|
|
338 |
@Override
|
|
339 |
public void onComplete() {
|
|
340 |
assert !finished && !onCompleteReceived;
|
|
341 |
onCompleteReceived = true;
|
|
342 |
DelegateWrapper subscriberImpl;
|
|
343 |
synchronized(this) {
|
|
344 |
subscriberImpl = subscribed;
|
|
345 |
}
|
|
346 |
|
|
347 |
if (handshaking()) {
|
|
348 |
onErrorImpl(new SSLHandshakeException(
|
|
349 |
"Remote host terminated the handshake"));
|
|
350 |
} else if (subscriberImpl != null) {
|
|
351 |
finished = true;
|
|
352 |
subscriberImpl.onComplete();
|
|
353 |
}
|
|
354 |
}
|
|
355 |
}
|
|
356 |
|
|
357 |
@Override
|
|
358 |
public void connectFlows(TubePublisher writePub,
|
|
359 |
TubeSubscriber readSub) {
|
|
360 |
debug.log(Level.DEBUG, "connecting flows");
|
|
361 |
readSubscriber.setDelegate(readSub);
|
|
362 |
writePub.subscribe(this);
|
|
363 |
}
|
|
364 |
|
|
365 |
/** Outstanding write demand from the SSL Flow Delegate. */
|
|
366 |
private final Demand writeDemand = new Demand();
|
|
367 |
|
|
368 |
final class SSLSubscriptionWrapper implements Flow.Subscription {
|
|
369 |
|
|
370 |
volatile Flow.Subscription delegate;
|
|
371 |
|
|
372 |
void setSubscription(Flow.Subscription sub) {
|
|
373 |
long demand = writeDemand.get(); // FIXME: isn't it a racy way of passing the demand?
|
|
374 |
delegate = sub;
|
|
375 |
debug.log(Level.DEBUG, "setSubscription: demand=%d", demand);
|
|
376 |
if (demand > 0)
|
|
377 |
sub.request(demand);
|
|
378 |
}
|
|
379 |
|
|
380 |
@Override
|
|
381 |
public void request(long n) {
|
|
382 |
writeDemand.increase(n);
|
|
383 |
debug.log(Level.DEBUG, "request: n=%d", n);
|
|
384 |
Flow.Subscription sub = delegate;
|
|
385 |
if (sub != null && n > 0) {
|
|
386 |
sub.request(n);
|
|
387 |
}
|
|
388 |
}
|
|
389 |
|
|
390 |
@Override
|
|
391 |
public void cancel() {
|
|
392 |
// TODO: no-op or error?
|
|
393 |
}
|
|
394 |
}
|
|
395 |
|
|
396 |
/* Subscriber - writing side */
|
|
397 |
@Override
|
|
398 |
public void onSubscribe(Flow.Subscription subscription) {
|
|
399 |
Objects.requireNonNull(subscription);
|
|
400 |
Flow.Subscription x = writeSubscription.delegate;
|
|
401 |
if (x != null)
|
|
402 |
x.cancel();
|
|
403 |
|
|
404 |
writeSubscription.setSubscription(subscription);
|
|
405 |
}
|
|
406 |
|
|
407 |
@Override
|
|
408 |
public void onNext(List<ByteBuffer> item) {
|
|
409 |
Objects.requireNonNull(item);
|
|
410 |
boolean decremented = writeDemand.tryDecrement();
|
|
411 |
assert decremented : "Unexpected writeDemand: ";
|
|
412 |
debug.log(Level.DEBUG,
|
|
413 |
"sending %d buffers to SSL flow delegate", item.size());
|
|
414 |
sslDelegate.upstreamWriter().onNext(item);
|
|
415 |
}
|
|
416 |
|
|
417 |
@Override
|
|
418 |
public void onError(Throwable throwable) {
|
|
419 |
Objects.requireNonNull(throwable);
|
|
420 |
sslDelegate.upstreamWriter().onError(throwable);
|
|
421 |
}
|
|
422 |
|
|
423 |
@Override
|
|
424 |
public void onComplete() {
|
|
425 |
sslDelegate.upstreamWriter().onComplete();
|
|
426 |
}
|
|
427 |
|
|
428 |
@Override
|
|
429 |
public String toString() {
|
|
430 |
return dbgString();
|
|
431 |
}
|
|
432 |
|
|
433 |
final String dbgString() {
|
|
434 |
return "SSLTube(" + tube + ")";
|
|
435 |
}
|
|
436 |
|
|
437 |
}
|