48083
|
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.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
|
22 |
*/
|
|
23 |
|
|
24 |
package jdk.incubator.http;
|
|
25 |
|
|
26 |
import jdk.incubator.http.internal.common.Demand;
|
|
27 |
import jdk.incubator.http.internal.common.FlowTube;
|
|
28 |
import jdk.incubator.http.internal.common.SSLTube;
|
|
29 |
import jdk.incubator.http.internal.common.SequentialScheduler;
|
|
30 |
import jdk.incubator.http.internal.common.Utils;
|
|
31 |
import org.testng.annotations.Test;
|
|
32 |
|
|
33 |
import java.io.IOException;
|
|
34 |
import java.nio.ByteBuffer;
|
|
35 |
import java.util.List;
|
|
36 |
import java.util.Queue;
|
|
37 |
import java.util.concurrent.CompletableFuture;
|
|
38 |
import java.util.concurrent.ConcurrentLinkedQueue;
|
|
39 |
import java.util.concurrent.Executor;
|
|
40 |
import java.util.concurrent.ExecutorService;
|
|
41 |
import java.util.concurrent.Executors;
|
|
42 |
import java.util.concurrent.Flow;
|
|
43 |
import java.util.concurrent.atomic.AtomicBoolean;
|
|
44 |
import java.util.concurrent.atomic.AtomicReference;
|
|
45 |
import java.util.function.Consumer;
|
|
46 |
|
|
47 |
@Test
|
|
48 |
public class SSLEchoTubeTest extends AbstractSSLTubeTest {
|
|
49 |
|
|
50 |
@Test
|
|
51 |
public void runWithEchoServer() throws IOException {
|
|
52 |
ExecutorService sslExecutor = Executors.newCachedThreadPool();
|
|
53 |
|
|
54 |
/* Start of wiring */
|
|
55 |
/* Emulates an echo server */
|
|
56 |
FlowTube server = crossOverEchoServer(sslExecutor);
|
|
57 |
|
|
58 |
run(server, sslExecutor, allBytesReceived);
|
|
59 |
}
|
|
60 |
|
|
61 |
/**
|
|
62 |
* Creates a cross-over FlowTube than can be plugged into a client-side
|
|
63 |
* SSLTube (in place of the SSLLoopbackSubscriber).
|
|
64 |
* Note that the only method that can be called on the return tube
|
|
65 |
* is connectFlows(). Calling any other method will trigger an
|
|
66 |
* InternalError.
|
|
67 |
* @param sslExecutor an executor
|
|
68 |
* @return a cross-over FlowTube connected to an EchoTube.
|
|
69 |
* @throws IOException
|
|
70 |
*/
|
|
71 |
private FlowTube crossOverEchoServer(Executor sslExecutor) throws IOException {
|
|
72 |
LateBindingTube crossOver = new LateBindingTube();
|
|
73 |
FlowTube server = new SSLTube(createSSLEngine(false),
|
|
74 |
sslExecutor,
|
|
75 |
crossOver);
|
|
76 |
EchoTube echo = new EchoTube(6);
|
|
77 |
server.connectFlows(FlowTube.asTubePublisher(echo), FlowTube.asTubeSubscriber(echo));
|
|
78 |
|
|
79 |
return new CrossOverTube(crossOver);
|
|
80 |
}
|
|
81 |
|
|
82 |
/**
|
|
83 |
* A cross-over FlowTube that makes it possible to reverse the direction
|
|
84 |
* of flows. The typical usage is to connect an two opposite SSLTube,
|
|
85 |
* one encrypting, one decrypting, to e.g. an EchoTube, with the help
|
|
86 |
* of a LateBindingTube:
|
|
87 |
* {@code
|
|
88 |
* client app => SSLTube => CrossOverTube <= LateBindingTube <= SSLTube <= EchoTube
|
|
89 |
* }
|
|
90 |
* <p>
|
|
91 |
* Note that the only method that can be called on the CrossOverTube is
|
|
92 |
* connectFlows(). Calling any other method will cause an InternalError to
|
|
93 |
* be thrown.
|
|
94 |
* Also connectFlows() can be called only once.
|
|
95 |
*/
|
|
96 |
private static final class CrossOverTube implements FlowTube {
|
|
97 |
final LateBindingTube tube;
|
|
98 |
CrossOverTube(LateBindingTube tube) {
|
|
99 |
this.tube = tube;
|
|
100 |
}
|
|
101 |
|
|
102 |
@Override
|
|
103 |
public void subscribe(Flow.Subscriber<? super List<ByteBuffer>> subscriber) {
|
|
104 |
throw newInternalError();
|
|
105 |
}
|
|
106 |
|
|
107 |
@Override
|
|
108 |
public void connectFlows(TubePublisher writePublisher, TubeSubscriber readSubscriber) {
|
|
109 |
tube.start(writePublisher, readSubscriber);
|
|
110 |
}
|
|
111 |
|
|
112 |
@Override
|
|
113 |
public boolean isFinished() {
|
|
114 |
return tube.isFinished();
|
|
115 |
}
|
|
116 |
|
|
117 |
Error newInternalError() {
|
|
118 |
InternalError error = new InternalError();
|
|
119 |
error.printStackTrace(System.out);
|
|
120 |
return error;
|
|
121 |
}
|
|
122 |
|
|
123 |
@Override
|
|
124 |
public void onSubscribe(Flow.Subscription subscription) {
|
|
125 |
throw newInternalError();
|
|
126 |
}
|
|
127 |
|
|
128 |
@Override
|
|
129 |
public void onError(Throwable throwable) {
|
|
130 |
throw newInternalError();
|
|
131 |
}
|
|
132 |
|
|
133 |
@Override
|
|
134 |
public void onComplete() {
|
|
135 |
throw newInternalError();
|
|
136 |
}
|
|
137 |
|
|
138 |
@Override
|
|
139 |
public void onNext(List<ByteBuffer> item) {
|
|
140 |
throw newInternalError();
|
|
141 |
}
|
|
142 |
}
|
|
143 |
|
|
144 |
/**
|
|
145 |
* A late binding tube that makes it possible to create an
|
|
146 |
* SSLTube before the right-hand-side tube has been created.
|
|
147 |
* The typical usage is to make it possible to connect two
|
|
148 |
* opposite SSLTube (one encrypting, one decrypting) through a
|
|
149 |
* CrossOverTube:
|
|
150 |
* {@code
|
|
151 |
* client app => SSLTube => CrossOverTube <= LateBindingTube <= SSLTube <= EchoTube
|
|
152 |
* }
|
|
153 |
* <p>
|
|
154 |
* Note that this class only supports a single call to start(): it cannot be
|
|
155 |
* subscribed more than once from its left-hand-side (the cross over tube side).
|
|
156 |
*/
|
|
157 |
private static class LateBindingTube implements FlowTube {
|
|
158 |
|
|
159 |
final CompletableFuture<Flow.Publisher<List<ByteBuffer>>> futurePublisher
|
|
160 |
= new CompletableFuture<>();
|
|
161 |
final ConcurrentLinkedQueue<Consumer<Flow.Subscriber<? super List<ByteBuffer>>>> queue
|
|
162 |
= new ConcurrentLinkedQueue<>();
|
|
163 |
AtomicReference<Flow.Subscriber<? super List<ByteBuffer>>> subscriberRef = new AtomicReference<>();
|
|
164 |
SequentialScheduler scheduler = SequentialScheduler.synchronizedScheduler(this::loop);
|
|
165 |
AtomicReference<Throwable> errorRef = new AtomicReference<>();
|
|
166 |
private volatile boolean finished;
|
|
167 |
private volatile boolean completed;
|
|
168 |
|
|
169 |
|
|
170 |
public void start(Flow.Publisher<List<ByteBuffer>> publisher,
|
|
171 |
Flow.Subscriber<? super List<ByteBuffer>> subscriber) {
|
|
172 |
subscriberRef.set(subscriber);
|
|
173 |
futurePublisher.complete(publisher);
|
|
174 |
scheduler.runOrSchedule();
|
|
175 |
}
|
|
176 |
|
|
177 |
@Override
|
|
178 |
public boolean isFinished() {
|
|
179 |
return finished;
|
|
180 |
}
|
|
181 |
|
|
182 |
@Override
|
|
183 |
public void subscribe(Flow.Subscriber<? super List<ByteBuffer>> subscriber) {
|
|
184 |
futurePublisher.thenAccept((p) -> p.subscribe(subscriber));
|
|
185 |
scheduler.runOrSchedule();
|
|
186 |
}
|
|
187 |
|
|
188 |
@Override
|
|
189 |
public void onSubscribe(Flow.Subscription subscription) {
|
|
190 |
queue.add((s) -> s.onSubscribe(subscription));
|
|
191 |
scheduler.runOrSchedule();
|
|
192 |
}
|
|
193 |
|
|
194 |
@Override
|
|
195 |
public void onNext(List<ByteBuffer> item) {
|
|
196 |
queue.add((s) -> s.onNext(item));
|
|
197 |
scheduler.runOrSchedule();
|
|
198 |
}
|
|
199 |
|
|
200 |
@Override
|
|
201 |
public void onError(Throwable throwable) {
|
|
202 |
System.out.println("LateBindingTube onError");
|
|
203 |
throwable.printStackTrace(System.out);
|
|
204 |
queue.add((s) -> {
|
|
205 |
errorRef.compareAndSet(null, throwable);
|
|
206 |
try {
|
|
207 |
System.out.println("LateBindingTube subscriber onError: " + throwable);
|
|
208 |
s.onError(errorRef.get());
|
|
209 |
} finally {
|
|
210 |
finished = true;
|
|
211 |
System.out.println("LateBindingTube finished");
|
|
212 |
}
|
|
213 |
});
|
|
214 |
scheduler.runOrSchedule();
|
|
215 |
}
|
|
216 |
|
|
217 |
@Override
|
|
218 |
public void onComplete() {
|
|
219 |
System.out.println("LateBindingTube completing");
|
|
220 |
queue.add((s) -> {
|
|
221 |
completed = true;
|
|
222 |
try {
|
|
223 |
System.out.println("LateBindingTube complete subscriber");
|
|
224 |
s.onComplete();
|
|
225 |
} finally {
|
|
226 |
finished = true;
|
|
227 |
System.out.println("LateBindingTube finished");
|
|
228 |
}
|
|
229 |
});
|
|
230 |
scheduler.runOrSchedule();
|
|
231 |
}
|
|
232 |
|
|
233 |
private void loop() {
|
|
234 |
if (finished) {
|
|
235 |
scheduler.stop();
|
|
236 |
return;
|
|
237 |
}
|
|
238 |
Flow.Subscriber<? super List<ByteBuffer>> subscriber = subscriberRef.get();
|
|
239 |
if (subscriber == null) return;
|
|
240 |
try {
|
|
241 |
Consumer<Flow.Subscriber<? super List<ByteBuffer>>> s;
|
|
242 |
while ((s = queue.poll()) != null) {
|
|
243 |
s.accept(subscriber);
|
|
244 |
}
|
|
245 |
} catch (Throwable t) {
|
|
246 |
if (errorRef.compareAndSet(null, t)) {
|
|
247 |
onError(t);
|
|
248 |
}
|
|
249 |
}
|
|
250 |
}
|
|
251 |
}
|
|
252 |
|
|
253 |
/**
|
|
254 |
* An echo tube that just echoes back whatever bytes it receives.
|
|
255 |
* This cannot be plugged to the right-hand-side of an SSLTube
|
|
256 |
* since handshake data cannot be simply echoed back, and
|
|
257 |
* application data most likely also need to be decrypted and
|
|
258 |
* re-encrypted.
|
|
259 |
*/
|
|
260 |
private static final class EchoTube implements FlowTube {
|
|
261 |
|
|
262 |
private final static Object EOF = new Object();
|
|
263 |
private final Executor executor = Executors.newSingleThreadExecutor();
|
|
264 |
|
|
265 |
private final Queue<Object> queue = new ConcurrentLinkedQueue<>();
|
|
266 |
private final int maxQueueSize;
|
|
267 |
private final SequentialScheduler processingScheduler =
|
|
268 |
new SequentialScheduler(createProcessingTask());
|
|
269 |
|
|
270 |
/* Writing into this tube */
|
|
271 |
private volatile long requested;
|
|
272 |
private Flow.Subscription subscription;
|
|
273 |
|
|
274 |
/* Reading from this tube */
|
|
275 |
private final Demand demand = new Demand();
|
|
276 |
private final AtomicBoolean cancelled = new AtomicBoolean();
|
|
277 |
private Flow.Subscriber<? super List<ByteBuffer>> subscriber;
|
|
278 |
|
|
279 |
private EchoTube(int maxBufferSize) {
|
|
280 |
if (maxBufferSize < 1)
|
|
281 |
throw new IllegalArgumentException();
|
|
282 |
this.maxQueueSize = maxBufferSize;
|
|
283 |
}
|
|
284 |
|
|
285 |
@Override
|
|
286 |
public void subscribe(Flow.Subscriber<? super List<ByteBuffer>> subscriber) {
|
|
287 |
this.subscriber = subscriber;
|
|
288 |
System.out.println("EchoTube got subscriber: " + subscriber);
|
|
289 |
this.subscriber.onSubscribe(new InternalSubscription());
|
|
290 |
}
|
|
291 |
|
|
292 |
@Override
|
|
293 |
public void onSubscribe(Flow.Subscription subscription) {
|
|
294 |
System.out.println("EchoTube request: " + maxQueueSize);
|
|
295 |
(this.subscription = subscription).request(requested = maxQueueSize);
|
|
296 |
}
|
|
297 |
|
|
298 |
private void requestMore() {
|
|
299 |
Flow.Subscription s = subscription;
|
|
300 |
if (s == null || cancelled.get()) return;
|
|
301 |
long unfulfilled = queue.size() + --requested;
|
|
302 |
if (unfulfilled <= maxQueueSize/2) {
|
|
303 |
long req = maxQueueSize - unfulfilled;
|
|
304 |
requested += req;
|
|
305 |
s.request(req);
|
|
306 |
System.out.printf("EchoTube request: %s [requested:%s, queue:%s, unfulfilled:%s]%n",
|
|
307 |
req, requested-req, queue.size(), unfulfilled );
|
|
308 |
}
|
|
309 |
}
|
|
310 |
|
|
311 |
@Override
|
|
312 |
public void onNext(List<ByteBuffer> item) {
|
|
313 |
System.out.printf("EchoTube add %s [requested:%s, queue:%s]%n",
|
|
314 |
Utils.remaining(item), requested, queue.size());
|
|
315 |
queue.add(item);
|
|
316 |
processingScheduler.deferOrSchedule(executor);
|
|
317 |
}
|
|
318 |
|
|
319 |
@Override
|
|
320 |
public void onError(Throwable throwable) {
|
|
321 |
System.out.println("EchoTube add " + throwable);
|
|
322 |
queue.add(throwable);
|
|
323 |
processingScheduler.deferOrSchedule(executor);
|
|
324 |
}
|
|
325 |
|
|
326 |
@Override
|
|
327 |
public void onComplete() {
|
|
328 |
System.out.println("EchoTube add EOF");
|
|
329 |
queue.add(EOF);
|
|
330 |
processingScheduler.deferOrSchedule(executor);
|
|
331 |
}
|
|
332 |
|
|
333 |
@Override
|
|
334 |
public boolean isFinished() {
|
|
335 |
return cancelled.get();
|
|
336 |
}
|
|
337 |
|
|
338 |
private class InternalSubscription implements Flow.Subscription {
|
|
339 |
|
|
340 |
@Override
|
|
341 |
public void request(long n) {
|
|
342 |
System.out.println("EchoTube got request: " + n);
|
|
343 |
if (n <= 0) {
|
|
344 |
throw new InternalError();
|
|
345 |
}
|
|
346 |
if (demand.increase(n)) {
|
|
347 |
processingScheduler.deferOrSchedule(executor);
|
|
348 |
}
|
|
349 |
}
|
|
350 |
|
|
351 |
@Override
|
|
352 |
public void cancel() {
|
|
353 |
cancelled.set(true);
|
|
354 |
}
|
|
355 |
}
|
|
356 |
|
|
357 |
@Override
|
|
358 |
public String toString() {
|
|
359 |
return "EchoTube";
|
|
360 |
}
|
|
361 |
|
|
362 |
int transmitted = 0;
|
|
363 |
private SequentialScheduler.RestartableTask createProcessingTask() {
|
|
364 |
return new SequentialScheduler.CompleteRestartableTask() {
|
|
365 |
|
|
366 |
@Override
|
|
367 |
protected void run() {
|
|
368 |
try {
|
|
369 |
while (!cancelled.get()) {
|
|
370 |
Object item = queue.peek();
|
|
371 |
if (item == null) {
|
|
372 |
System.out.printf("EchoTube: queue empty, requested=%s, demand=%s, transmitted=%s%n",
|
|
373 |
requested, demand.get(), transmitted);
|
|
374 |
requestMore();
|
|
375 |
return;
|
|
376 |
}
|
|
377 |
try {
|
|
378 |
System.out.printf("EchoTube processing item, requested=%s, demand=%s, transmitted=%s%n",
|
|
379 |
requested, demand.get(), transmitted);
|
|
380 |
if (item instanceof List) {
|
|
381 |
if (!demand.tryDecrement()) {
|
|
382 |
System.out.println("EchoTube no demand");
|
|
383 |
return;
|
|
384 |
}
|
|
385 |
@SuppressWarnings("unchecked")
|
|
386 |
List<ByteBuffer> bytes = (List<ByteBuffer>) item;
|
|
387 |
Object removed = queue.remove();
|
|
388 |
assert removed == item;
|
|
389 |
System.out.println("EchoTube processing "
|
|
390 |
+ Utils.remaining(bytes));
|
|
391 |
transmitted++;
|
|
392 |
subscriber.onNext(bytes);
|
|
393 |
requestMore();
|
|
394 |
} else if (item instanceof Throwable) {
|
|
395 |
cancelled.set(true);
|
|
396 |
Object removed = queue.remove();
|
|
397 |
assert removed == item;
|
|
398 |
System.out.println("EchoTube processing " + item);
|
|
399 |
subscriber.onError((Throwable) item);
|
|
400 |
} else if (item == EOF) {
|
|
401 |
cancelled.set(true);
|
|
402 |
Object removed = queue.remove();
|
|
403 |
assert removed == item;
|
|
404 |
System.out.println("EchoTube processing EOF");
|
|
405 |
subscriber.onComplete();
|
|
406 |
} else {
|
|
407 |
throw new InternalError(String.valueOf(item));
|
|
408 |
}
|
|
409 |
} finally {
|
|
410 |
}
|
|
411 |
}
|
|
412 |
} catch(Throwable t) {
|
|
413 |
t.printStackTrace();
|
|
414 |
throw t;
|
|
415 |
}
|
|
416 |
}
|
|
417 |
};
|
|
418 |
}
|
|
419 |
}
|
|
420 |
}
|