author | chegar |
Fri, 02 Mar 2018 10:11:30 +0000 | |
branch | http-client-branch |
changeset 56223 | 377c5ebfc319 |
parent 56208 | d37c08ce784a |
child 56234 | fc391230cf7b |
permissions | -rw-r--r-- |
48083 | 1 |
/* |
56035 | 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 |
||
56092
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
26 |
package jdk.internal.net.http.common; |
48083 | 27 |
|
56092
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
28 |
import jdk.internal.net.http.common.SubscriberWrapper.SchedulingAction; |
56035 | 29 |
|
48083 | 30 |
import javax.net.ssl.SSLEngine; |
31 |
import javax.net.ssl.SSLEngineResult; |
|
32 |
import javax.net.ssl.SSLEngineResult.HandshakeStatus; |
|
33 |
import javax.net.ssl.SSLEngineResult.Status; |
|
34 |
import javax.net.ssl.SSLException; |
|
56035 | 35 |
import java.io.IOException; |
36 |
import java.lang.System.Logger.Level; |
|
37 |
import java.nio.ByteBuffer; |
|
38 |
import java.util.ArrayList; |
|
39 |
import java.util.Collections; |
|
40 |
import java.util.Iterator; |
|
41 |
import java.util.LinkedList; |
|
42 |
import java.util.List; |
|
43 |
import java.util.concurrent.CompletableFuture; |
|
44 |
import java.util.concurrent.ConcurrentLinkedQueue; |
|
45 |
import java.util.concurrent.Executor; |
|
46 |
import java.util.concurrent.Flow; |
|
47 |
import java.util.concurrent.Flow.Subscriber; |
|
48 |
import java.util.concurrent.atomic.AtomicInteger; |
|
48083 | 49 |
|
50 |
/** |
|
51 |
* Implements SSL using two SubscriberWrappers. |
|
52 |
* |
|
53 |
* <p> Constructor takes two Flow.Subscribers: one that receives the network |
|
54 |
* data (after it has been encrypted by SSLFlowDelegate) data, and one that |
|
55 |
* receives the application data (before it has been encrypted by SSLFlowDelegate). |
|
56 |
* |
|
57 |
* <p> Methods upstreamReader() and upstreamWriter() return the corresponding |
|
58 |
* Flow.Subscribers containing Flows for the encrypted/decrypted upstream data. |
|
59 |
* See diagram below. |
|
60 |
* |
|
61 |
* <p> How Flow.Subscribers are used in this class, and where they come from: |
|
62 |
* <pre> |
|
63 |
* {@code |
|
64 |
* |
|
65 |
* |
|
66 |
* |
|
67 |
* ---------> data flow direction |
|
68 |
* |
|
69 |
* |
|
70 |
* +------------------+ |
|
71 |
* upstreamWriter | | downWriter |
|
72 |
* ---------------> | | ------------> |
|
73 |
* obtained from this | | supplied to constructor |
|
74 |
* | SSLFlowDelegate | |
|
75 |
* downReader | | upstreamReader |
|
76 |
* <--------------- | | <-------------- |
|
77 |
* supplied to constructor | | obtained from this |
|
78 |
* +------------------+ |
|
56184
1c7b4d7140e2
http-client-branch: fix SSL hang issue where errors not reported correctly
michaelm
parents:
56101
diff
changeset
|
79 |
* |
1c7b4d7140e2
http-client-branch: fix SSL hang issue where errors not reported correctly
michaelm
parents:
56101
diff
changeset
|
80 |
* Errors are reported to the downReader Flow.Subscriber |
1c7b4d7140e2
http-client-branch: fix SSL hang issue where errors not reported correctly
michaelm
parents:
56101
diff
changeset
|
81 |
* |
48083 | 82 |
* } |
83 |
* </pre> |
|
84 |
*/ |
|
85 |
public class SSLFlowDelegate { |
|
86 |
||
87 |
static final boolean DEBUG = Utils.DEBUG; // Revisit: temporary dev flag. |
|
88 |
final System.Logger debug = |
|
89 |
Utils.getDebugLogger(this::dbgString, DEBUG); |
|
90 |
||
91 |
final Executor exec; |
|
92 |
final Reader reader; |
|
93 |
final Writer writer; |
|
94 |
final SSLEngine engine; |
|
95 |
final String tubeName; // hack |
|
96 |
final CompletableFuture<String> alpnCF; // completes on initial handshake |
|
97 |
final static ByteBuffer SENTINEL = Utils.EMPTY_BYTEBUFFER; |
|
98 |
volatile boolean close_notify_received; |
|
56207
03c89ed2070f
http-client-branch: Fixed SSLFlowDelegate to not call Subscriber.onError directly
michaelm
parents:
56187
diff
changeset
|
99 |
final CompletableFuture<Void> readerCF; |
03c89ed2070f
http-client-branch: Fixed SSLFlowDelegate to not call Subscriber.onError directly
michaelm
parents:
56187
diff
changeset
|
100 |
final CompletableFuture<Void> writerCF; |
56184
1c7b4d7140e2
http-client-branch: fix SSL hang issue where errors not reported correctly
michaelm
parents:
56101
diff
changeset
|
101 |
static AtomicInteger scount = new AtomicInteger(1); |
1c7b4d7140e2
http-client-branch: fix SSL hang issue where errors not reported correctly
michaelm
parents:
56101
diff
changeset
|
102 |
final int id; |
48083 | 103 |
|
104 |
/** |
|
105 |
* Creates an SSLFlowDelegate fed from two Flow.Subscribers. Each |
|
106 |
* Flow.Subscriber requires an associated {@link CompletableFuture} |
|
107 |
* for errors that need to be signaled from downstream to upstream. |
|
108 |
*/ |
|
109 |
public SSLFlowDelegate(SSLEngine engine, |
|
110 |
Executor exec, |
|
111 |
Subscriber<? super List<ByteBuffer>> downReader, |
|
112 |
Subscriber<? super List<ByteBuffer>> downWriter) |
|
113 |
{ |
|
56184
1c7b4d7140e2
http-client-branch: fix SSL hang issue where errors not reported correctly
michaelm
parents:
56101
diff
changeset
|
114 |
this.id = scount.getAndIncrement(); |
48083 | 115 |
this.tubeName = String.valueOf(downWriter); |
116 |
this.reader = new Reader(); |
|
117 |
this.writer = new Writer(); |
|
118 |
this.engine = engine; |
|
119 |
this.exec = exec; |
|
120 |
this.handshakeState = new AtomicInteger(NOT_HANDSHAKING); |
|
56207
03c89ed2070f
http-client-branch: Fixed SSLFlowDelegate to not call Subscriber.onError directly
michaelm
parents:
56187
diff
changeset
|
121 |
this.readerCF = reader.completion(); |
03c89ed2070f
http-client-branch: Fixed SSLFlowDelegate to not call Subscriber.onError directly
michaelm
parents:
56187
diff
changeset
|
122 |
this.writerCF = reader.completion(); |
03c89ed2070f
http-client-branch: Fixed SSLFlowDelegate to not call Subscriber.onError directly
michaelm
parents:
56187
diff
changeset
|
123 |
readerCF.exceptionally(this::stopOnError); |
03c89ed2070f
http-client-branch: Fixed SSLFlowDelegate to not call Subscriber.onError directly
michaelm
parents:
56187
diff
changeset
|
124 |
readerCF.exceptionally(this::stopOnError); |
56184
1c7b4d7140e2
http-client-branch: fix SSL hang issue where errors not reported correctly
michaelm
parents:
56101
diff
changeset
|
125 |
|
1c7b4d7140e2
http-client-branch: fix SSL hang issue where errors not reported correctly
michaelm
parents:
56101
diff
changeset
|
126 |
CompletableFuture.allOf(reader.completion(), writer.completion()) |
1c7b4d7140e2
http-client-branch: fix SSL hang issue where errors not reported correctly
michaelm
parents:
56101
diff
changeset
|
127 |
.thenRun(this::normalStop); |
48083 | 128 |
this.alpnCF = new MinimalFuture<>(); |
129 |
||
130 |
// connect the Reader to the downReader and the |
|
131 |
// Writer to the downWriter. |
|
132 |
connect(downReader, downWriter); |
|
133 |
||
56187
61ba287288d9
http-client-branch: disabled SSLFlowDelegate debug monitor
michaelm
parents:
56184
diff
changeset
|
134 |
//Monitor.add(this::monitor); |
48083 | 135 |
} |
136 |
||
137 |
/** |
|
138 |
* Returns true if the SSLFlowDelegate has detected a TLS |
|
139 |
* close_notify from the server. |
|
140 |
* @return true, if a close_notify was detected. |
|
141 |
*/ |
|
142 |
public boolean closeNotifyReceived() { |
|
143 |
return close_notify_received; |
|
144 |
} |
|
145 |
||
146 |
/** |
|
147 |
* Connects the read sink (downReader) to the SSLFlowDelegate Reader, |
|
148 |
* and the write sink (downWriter) to the SSLFlowDelegate Writer. |
|
149 |
* Called from within the constructor. Overwritten by SSLTube. |
|
150 |
* |
|
151 |
* @param downReader The left hand side read sink (typically, the |
|
152 |
* HttpConnection read subscriber). |
|
153 |
* @param downWriter The right hand side write sink (typically |
|
154 |
* the SocketTube write subscriber). |
|
155 |
*/ |
|
156 |
void connect(Subscriber<? super List<ByteBuffer>> downReader, |
|
157 |
Subscriber<? super List<ByteBuffer>> downWriter) { |
|
158 |
this.reader.subscribe(downReader); |
|
159 |
this.writer.subscribe(downWriter); |
|
160 |
} |
|
161 |
||
162 |
/** |
|
163 |
* Returns a CompletableFuture<String> which completes after |
|
164 |
* the initial handshake completes, and which contains the negotiated |
|
165 |
* alpn. |
|
166 |
*/ |
|
167 |
public CompletableFuture<String> alpn() { |
|
168 |
return alpnCF; |
|
169 |
} |
|
170 |
||
171 |
private void setALPN() { |
|
172 |
// Handshake is finished. So, can retrieve the ALPN now |
|
173 |
if (alpnCF.isDone()) |
|
174 |
return; |
|
175 |
String alpn = engine.getApplicationProtocol(); |
|
176 |
debug.log(Level.DEBUG, "setALPN = %s", alpn); |
|
177 |
alpnCF.complete(alpn); |
|
178 |
} |
|
179 |
||
180 |
public String monitor() { |
|
181 |
StringBuilder sb = new StringBuilder(); |
|
56184
1c7b4d7140e2
http-client-branch: fix SSL hang issue where errors not reported correctly
michaelm
parents:
56101
diff
changeset
|
182 |
sb.append("SSL: id ").append(id); |
1c7b4d7140e2
http-client-branch: fix SSL hang issue where errors not reported correctly
michaelm
parents:
56101
diff
changeset
|
183 |
sb.append(" HS state: " + states(handshakeState)); |
48083 | 184 |
sb.append(" Engine state: " + engine.getHandshakeStatus().toString()); |
185 |
sb.append(" LL : "); |
|
56101 | 186 |
for (String s: stateList) { |
187 |
sb.append(s).append(" "); |
|
48083 | 188 |
} |
189 |
sb.append("\r\n"); |
|
190 |
sb.append("Reader:: ").append(reader.toString()); |
|
191 |
sb.append("\r\n"); |
|
192 |
sb.append("Writer:: ").append(writer.toString()); |
|
193 |
sb.append("\r\n==================================="); |
|
194 |
return sb.toString(); |
|
195 |
} |
|
196 |
||
197 |
protected SchedulingAction enterReadScheduling() { |
|
198 |
return SchedulingAction.CONTINUE; |
|
199 |
} |
|
200 |
||
201 |
||
202 |
/** |
|
203 |
* Processing function for incoming data. Pass it thru SSLEngine.unwrap(). |
|
204 |
* Any decrypted buffers returned to be passed downstream. |
|
205 |
* Status codes: |
|
206 |
* NEED_UNWRAP: do nothing. Following incoming data will contain |
|
207 |
* any required handshake data |
|
208 |
* NEED_WRAP: call writer.addData() with empty buffer |
|
209 |
* NEED_TASK: delegate task to executor |
|
210 |
* BUFFER_OVERFLOW: allocate larger output buffer. Repeat unwrap |
|
211 |
* BUFFER_UNDERFLOW: keep buffer and wait for more data |
|
212 |
* OK: return generated buffers. |
|
213 |
* |
|
214 |
* Upstream subscription strategy is to try and keep no more than |
|
215 |
* TARGET_BUFSIZE bytes in readBuf |
|
216 |
*/ |
|
217 |
class Reader extends SubscriberWrapper { |
|
218 |
final SequentialScheduler scheduler; |
|
219 |
static final int TARGET_BUFSIZE = 16 * 1024; |
|
220 |
volatile ByteBuffer readBuf; |
|
221 |
volatile boolean completing = false; |
|
222 |
final Object readBufferLock = new Object(); |
|
223 |
final System.Logger debugr = |
|
224 |
Utils.getDebugLogger(this::dbgString, DEBUG); |
|
225 |
||
226 |
class ReaderDownstreamPusher implements Runnable { |
|
227 |
@Override public void run() { processData(); } |
|
228 |
} |
|
229 |
||
230 |
Reader() { |
|
231 |
super(); |
|
232 |
scheduler = SequentialScheduler.synchronizedScheduler( |
|
233 |
new ReaderDownstreamPusher()); |
|
234 |
this.readBuf = ByteBuffer.allocate(1024); |
|
235 |
readBuf.limit(0); // keep in read mode |
|
236 |
} |
|
237 |
||
238 |
protected SchedulingAction enterScheduling() { |
|
239 |
return enterReadScheduling(); |
|
240 |
} |
|
241 |
||
242 |
public final String dbgString() { |
|
243 |
return "SSL Reader(" + tubeName + ")"; |
|
244 |
} |
|
245 |
||
246 |
/** |
|
247 |
* entry point for buffers delivered from upstream Subscriber |
|
248 |
*/ |
|
249 |
@Override |
|
250 |
public void incoming(List<ByteBuffer> buffers, boolean complete) { |
|
251 |
debugr.log(Level.DEBUG, () -> "Adding " + Utils.remaining(buffers) |
|
252 |
+ " bytes to read buffer"); |
|
253 |
addToReadBuf(buffers, complete); |
|
254 |
scheduler.runOrSchedule(); |
|
255 |
} |
|
256 |
||
257 |
@Override |
|
258 |
public String toString() { |
|
259 |
return "READER: " + super.toString() + " readBuf: " + readBuf.toString() |
|
260 |
+ " count: " + count.toString(); |
|
261 |
} |
|
262 |
||
263 |
private void reallocReadBuf() { |
|
264 |
int sz = readBuf.capacity(); |
|
265 |
ByteBuffer newb = ByteBuffer.allocate(sz*2); |
|
266 |
readBuf.flip(); |
|
267 |
Utils.copy(readBuf, newb); |
|
268 |
readBuf = newb; |
|
269 |
} |
|
270 |
||
271 |
@Override |
|
272 |
protected long upstreamWindowUpdate(long currentWindow, long downstreamQsize) { |
|
273 |
if (readBuf.remaining() > TARGET_BUFSIZE) { |
|
274 |
return 0; |
|
275 |
} else { |
|
276 |
return super.upstreamWindowUpdate(currentWindow, downstreamQsize); |
|
277 |
} |
|
278 |
} |
|
279 |
||
280 |
// readBuf is kept ready for reading outside of this method |
|
281 |
private void addToReadBuf(List<ByteBuffer> buffers, boolean complete) { |
|
282 |
synchronized (readBufferLock) { |
|
283 |
for (ByteBuffer buf : buffers) { |
|
284 |
readBuf.compact(); |
|
285 |
while (readBuf.remaining() < buf.remaining()) |
|
286 |
reallocReadBuf(); |
|
287 |
readBuf.put(buf); |
|
288 |
readBuf.flip(); |
|
289 |
} |
|
290 |
if (complete) { |
|
291 |
this.completing = complete; |
|
292 |
} |
|
293 |
} |
|
294 |
} |
|
295 |
||
296 |
void schedule() { |
|
297 |
scheduler.runOrSchedule(); |
|
298 |
} |
|
299 |
||
300 |
void stop() { |
|
301 |
debugr.log(Level.DEBUG, "stop"); |
|
302 |
scheduler.stop(); |
|
303 |
} |
|
304 |
||
305 |
AtomicInteger count = new AtomicInteger(0); |
|
306 |
||
307 |
// work function where it all happens |
|
308 |
void processData() { |
|
309 |
try { |
|
310 |
debugr.log(Level.DEBUG, () -> "processData: " + readBuf.remaining() |
|
311 |
+ " bytes to unwrap " |
|
312 |
+ states(handshakeState) |
|
313 |
+ ", " + engine.getHandshakeStatus()); |
|
314 |
int len; |
|
315 |
boolean complete = false; |
|
316 |
while ((len = readBuf.remaining()) > 0) { |
|
317 |
boolean handshaking = false; |
|
318 |
try { |
|
319 |
EngineResult result; |
|
320 |
synchronized (readBufferLock) { |
|
321 |
complete = this.completing; |
|
322 |
result = unwrapBuffer(readBuf); |
|
323 |
debugr.log(Level.DEBUG, "Unwrapped: %s", result.result); |
|
324 |
} |
|
325 |
if (result.bytesProduced() > 0) { |
|
326 |
debugr.log(Level.DEBUG, "sending %d", result.bytesProduced()); |
|
327 |
count.addAndGet(result.bytesProduced()); |
|
328 |
outgoing(result.destBuffer, false); |
|
329 |
} |
|
330 |
if (result.status() == Status.BUFFER_UNDERFLOW) { |
|
331 |
debugr.log(Level.DEBUG, "BUFFER_UNDERFLOW"); |
|
332 |
// not enough data in the read buffer... |
|
333 |
requestMore(); |
|
334 |
synchronized (readBufferLock) { |
|
335 |
// check if we have received some data |
|
336 |
if (readBuf.remaining() > len) continue; |
|
337 |
return; |
|
338 |
} |
|
339 |
} |
|
340 |
if (complete && result.status() == Status.CLOSED) { |
|
341 |
debugr.log(Level.DEBUG, "Closed: completing"); |
|
342 |
outgoing(Utils.EMPTY_BB_LIST, true); |
|
343 |
return; |
|
344 |
} |
|
345 |
if (result.handshaking() && !complete) { |
|
346 |
debugr.log(Level.DEBUG, "handshaking"); |
|
347 |
doHandshake(result, READER); |
|
348 |
resumeActivity(); |
|
349 |
handshaking = true; |
|
350 |
} else { |
|
351 |
if ((handshakeState.getAndSet(NOT_HANDSHAKING) & ~DOING_TASKS) == HANDSHAKING) { |
|
352 |
setALPN(); |
|
353 |
handshaking = false; |
|
354 |
resumeActivity(); |
|
355 |
} |
|
356 |
} |
|
357 |
} catch (IOException ex) { |
|
56208
d37c08ce784a
http-client-branch: tweaked last revision wrong CF reference was being kept
michaelm
parents:
56207
diff
changeset
|
358 |
errorCommon(ex); |
48083 | 359 |
handleError(ex); |
360 |
} |
|
361 |
if (handshaking && !complete) |
|
362 |
return; |
|
363 |
} |
|
364 |
if (!complete) { |
|
365 |
synchronized (readBufferLock) { |
|
366 |
complete = this.completing && !readBuf.hasRemaining(); |
|
367 |
} |
|
368 |
} |
|
369 |
if (complete) { |
|
370 |
debugr.log(Level.DEBUG, "completing"); |
|
371 |
// Complete the alpnCF, if not already complete, regardless of |
|
372 |
// whether or not the ALPN is available, there will be no more |
|
373 |
// activity. |
|
374 |
setALPN(); |
|
375 |
outgoing(Utils.EMPTY_BB_LIST, true); |
|
376 |
} |
|
377 |
} catch (Throwable ex) { |
|
56208
d37c08ce784a
http-client-branch: tweaked last revision wrong CF reference was being kept
michaelm
parents:
56207
diff
changeset
|
378 |
errorCommon(ex); |
48083 | 379 |
handleError(ex); |
380 |
} |
|
381 |
} |
|
56223
377c5ebfc319
http-client-branch: move SSLFlowDelegate wrapBuffers/unwrapBuffers into their respective writer/reader classes - improve diagnostic
chegar
parents:
56208
diff
changeset
|
382 |
|
377c5ebfc319
http-client-branch: move SSLFlowDelegate wrapBuffers/unwrapBuffers into their respective writer/reader classes - improve diagnostic
chegar
parents:
56208
diff
changeset
|
383 |
EngineResult unwrapBuffer(ByteBuffer src) throws IOException { |
377c5ebfc319
http-client-branch: move SSLFlowDelegate wrapBuffers/unwrapBuffers into their respective writer/reader classes - improve diagnostic
chegar
parents:
56208
diff
changeset
|
384 |
ByteBuffer dst = getAppBuffer(); |
377c5ebfc319
http-client-branch: move SSLFlowDelegate wrapBuffers/unwrapBuffers into their respective writer/reader classes - improve diagnostic
chegar
parents:
56208
diff
changeset
|
385 |
while (true) { |
377c5ebfc319
http-client-branch: move SSLFlowDelegate wrapBuffers/unwrapBuffers into their respective writer/reader classes - improve diagnostic
chegar
parents:
56208
diff
changeset
|
386 |
SSLEngineResult sslResult = engine.unwrap(src, dst); |
377c5ebfc319
http-client-branch: move SSLFlowDelegate wrapBuffers/unwrapBuffers into their respective writer/reader classes - improve diagnostic
chegar
parents:
56208
diff
changeset
|
387 |
switch (sslResult.getStatus()) { |
377c5ebfc319
http-client-branch: move SSLFlowDelegate wrapBuffers/unwrapBuffers into their respective writer/reader classes - improve diagnostic
chegar
parents:
56208
diff
changeset
|
388 |
case BUFFER_OVERFLOW: |
377c5ebfc319
http-client-branch: move SSLFlowDelegate wrapBuffers/unwrapBuffers into their respective writer/reader classes - improve diagnostic
chegar
parents:
56208
diff
changeset
|
389 |
// may happen only if app size buffer was changed. |
377c5ebfc319
http-client-branch: move SSLFlowDelegate wrapBuffers/unwrapBuffers into their respective writer/reader classes - improve diagnostic
chegar
parents:
56208
diff
changeset
|
390 |
// get it again if app buffer size changed |
377c5ebfc319
http-client-branch: move SSLFlowDelegate wrapBuffers/unwrapBuffers into their respective writer/reader classes - improve diagnostic
chegar
parents:
56208
diff
changeset
|
391 |
int appSize = engine.getSession().getApplicationBufferSize(); |
377c5ebfc319
http-client-branch: move SSLFlowDelegate wrapBuffers/unwrapBuffers into their respective writer/reader classes - improve diagnostic
chegar
parents:
56208
diff
changeset
|
392 |
ByteBuffer b = ByteBuffer.allocate(appSize + dst.position()); |
377c5ebfc319
http-client-branch: move SSLFlowDelegate wrapBuffers/unwrapBuffers into their respective writer/reader classes - improve diagnostic
chegar
parents:
56208
diff
changeset
|
393 |
dst.flip(); |
377c5ebfc319
http-client-branch: move SSLFlowDelegate wrapBuffers/unwrapBuffers into their respective writer/reader classes - improve diagnostic
chegar
parents:
56208
diff
changeset
|
394 |
b.put(dst); |
377c5ebfc319
http-client-branch: move SSLFlowDelegate wrapBuffers/unwrapBuffers into their respective writer/reader classes - improve diagnostic
chegar
parents:
56208
diff
changeset
|
395 |
dst = b; |
377c5ebfc319
http-client-branch: move SSLFlowDelegate wrapBuffers/unwrapBuffers into their respective writer/reader classes - improve diagnostic
chegar
parents:
56208
diff
changeset
|
396 |
break; |
377c5ebfc319
http-client-branch: move SSLFlowDelegate wrapBuffers/unwrapBuffers into their respective writer/reader classes - improve diagnostic
chegar
parents:
56208
diff
changeset
|
397 |
case CLOSED: |
377c5ebfc319
http-client-branch: move SSLFlowDelegate wrapBuffers/unwrapBuffers into their respective writer/reader classes - improve diagnostic
chegar
parents:
56208
diff
changeset
|
398 |
return doClosure(new EngineResult(sslResult)); |
377c5ebfc319
http-client-branch: move SSLFlowDelegate wrapBuffers/unwrapBuffers into their respective writer/reader classes - improve diagnostic
chegar
parents:
56208
diff
changeset
|
399 |
case BUFFER_UNDERFLOW: |
377c5ebfc319
http-client-branch: move SSLFlowDelegate wrapBuffers/unwrapBuffers into their respective writer/reader classes - improve diagnostic
chegar
parents:
56208
diff
changeset
|
400 |
// handled implicitly by compaction/reallocation of readBuf |
377c5ebfc319
http-client-branch: move SSLFlowDelegate wrapBuffers/unwrapBuffers into their respective writer/reader classes - improve diagnostic
chegar
parents:
56208
diff
changeset
|
401 |
return new EngineResult(sslResult); |
377c5ebfc319
http-client-branch: move SSLFlowDelegate wrapBuffers/unwrapBuffers into their respective writer/reader classes - improve diagnostic
chegar
parents:
56208
diff
changeset
|
402 |
case OK: |
377c5ebfc319
http-client-branch: move SSLFlowDelegate wrapBuffers/unwrapBuffers into their respective writer/reader classes - improve diagnostic
chegar
parents:
56208
diff
changeset
|
403 |
dst.flip(); |
377c5ebfc319
http-client-branch: move SSLFlowDelegate wrapBuffers/unwrapBuffers into their respective writer/reader classes - improve diagnostic
chegar
parents:
56208
diff
changeset
|
404 |
return new EngineResult(sslResult, dst); |
377c5ebfc319
http-client-branch: move SSLFlowDelegate wrapBuffers/unwrapBuffers into their respective writer/reader classes - improve diagnostic
chegar
parents:
56208
diff
changeset
|
405 |
} |
377c5ebfc319
http-client-branch: move SSLFlowDelegate wrapBuffers/unwrapBuffers into their respective writer/reader classes - improve diagnostic
chegar
parents:
56208
diff
changeset
|
406 |
} |
377c5ebfc319
http-client-branch: move SSLFlowDelegate wrapBuffers/unwrapBuffers into their respective writer/reader classes - improve diagnostic
chegar
parents:
56208
diff
changeset
|
407 |
} |
48083 | 408 |
} |
409 |
||
410 |
public interface Monitorable { |
|
411 |
public String getInfo(); |
|
412 |
} |
|
413 |
||
414 |
public static class Monitor extends Thread { |
|
415 |
final List<Monitorable> list; |
|
416 |
static Monitor themon; |
|
417 |
||
418 |
static { |
|
419 |
themon = new Monitor(); |
|
420 |
themon.start(); // uncomment to enable Monitor |
|
421 |
} |
|
422 |
||
423 |
Monitor() { |
|
424 |
super("Monitor"); |
|
425 |
setDaemon(true); |
|
426 |
list = Collections.synchronizedList(new LinkedList<>()); |
|
427 |
} |
|
428 |
||
429 |
void addTarget(Monitorable o) { |
|
430 |
list.add(o); |
|
431 |
} |
|
432 |
||
433 |
public static void add(Monitorable o) { |
|
434 |
themon.addTarget(o); |
|
435 |
} |
|
436 |
||
437 |
@Override |
|
438 |
public void run() { |
|
439 |
System.out.println("Monitor starting"); |
|
56101 | 440 |
try { |
441 |
while (true) { |
|
442 |
Thread.sleep(20 * 1000); |
|
443 |
synchronized (list) { |
|
444 |
for (Monitorable o : list) { |
|
445 |
System.out.println(o.getInfo()); |
|
446 |
System.out.println("-------------------------"); |
|
447 |
} |
|
48083 | 448 |
} |
56101 | 449 |
System.out.println("--o-o-o-o-o-o-o-o-o-o-o-o-o-o-"); |
48083 | 450 |
} |
56101 | 451 |
} catch (InterruptedException e) { |
452 |
System.out.println("Monitor exiting with " + e); |
|
48083 | 453 |
} |
454 |
} |
|
455 |
} |
|
456 |
||
457 |
/** |
|
458 |
* Processing function for outgoing data. Pass it thru SSLEngine.wrap() |
|
459 |
* Any encrypted buffers generated are passed downstream to be written. |
|
460 |
* Status codes: |
|
461 |
* NEED_UNWRAP: call reader.addData() with empty buffer |
|
462 |
* NEED_WRAP: call addData() with empty buffer |
|
463 |
* NEED_TASK: delegate task to executor |
|
464 |
* BUFFER_OVERFLOW: allocate larger output buffer. Repeat wrap |
|
465 |
* BUFFER_UNDERFLOW: shouldn't happen on writing side |
|
466 |
* OK: return generated buffers |
|
467 |
*/ |
|
468 |
class Writer extends SubscriberWrapper { |
|
469 |
final SequentialScheduler scheduler; |
|
470 |
// queues of buffers received from upstream waiting |
|
471 |
// to be processed by the SSLEngine |
|
472 |
final List<ByteBuffer> writeList; |
|
473 |
final System.Logger debugw = |
|
474 |
Utils.getDebugLogger(this::dbgString, DEBUG); |
|
475 |
volatile boolean completing; |
|
476 |
boolean completed; // only accessed in processData |
|
477 |
||
478 |
class WriterDownstreamPusher extends SequentialScheduler.CompleteRestartableTask { |
|
479 |
@Override public void run() { processData(); } |
|
480 |
} |
|
481 |
||
482 |
Writer() { |
|
483 |
super(); |
|
484 |
writeList = Collections.synchronizedList(new LinkedList<>()); |
|
485 |
scheduler = new SequentialScheduler(new WriterDownstreamPusher()); |
|
486 |
} |
|
487 |
||
488 |
@Override |
|
489 |
protected void incoming(List<ByteBuffer> buffers, boolean complete) { |
|
490 |
assert complete ? buffers == Utils.EMPTY_BB_LIST : true; |
|
491 |
assert buffers != Utils.EMPTY_BB_LIST ? complete == false : true; |
|
492 |
if (complete) { |
|
493 |
debugw.log(Level.DEBUG, "adding SENTINEL"); |
|
494 |
completing = true; |
|
495 |
writeList.add(SENTINEL); |
|
496 |
} else { |
|
497 |
writeList.addAll(buffers); |
|
498 |
} |
|
499 |
debugw.log(Level.DEBUG, () -> "added " + buffers.size() |
|
500 |
+ " (" + Utils.remaining(buffers) |
|
501 |
+ " bytes) to the writeList"); |
|
502 |
scheduler.runOrSchedule(); |
|
503 |
} |
|
504 |
||
505 |
public final String dbgString() { |
|
506 |
return "SSL Writer(" + tubeName + ")"; |
|
507 |
} |
|
508 |
||
509 |
protected void onSubscribe() { |
|
510 |
doHandshake(EngineResult.INIT, INIT); |
|
511 |
resumeActivity(); |
|
512 |
} |
|
513 |
||
514 |
void schedule() { |
|
515 |
scheduler.runOrSchedule(); |
|
516 |
} |
|
517 |
||
518 |
void stop() { |
|
519 |
debugw.log(Level.DEBUG, "stop"); |
|
520 |
scheduler.stop(); |
|
521 |
} |
|
522 |
||
523 |
@Override |
|
524 |
public boolean closing() { |
|
525 |
return closeNotifyReceived(); |
|
526 |
} |
|
527 |
||
528 |
private boolean isCompleting() { |
|
529 |
return completing; |
|
530 |
} |
|
531 |
||
532 |
@Override |
|
533 |
protected long upstreamWindowUpdate(long currentWindow, long downstreamQsize) { |
|
534 |
if (writeList.size() > 10) |
|
535 |
return 0; |
|
536 |
else |
|
537 |
return super.upstreamWindowUpdate(currentWindow, downstreamQsize); |
|
538 |
} |
|
539 |
||
540 |
private boolean hsTriggered() { |
|
541 |
synchronized(writeList) { |
|
542 |
for (ByteBuffer b : writeList) |
|
543 |
if (b == HS_TRIGGER) |
|
544 |
return true; |
|
545 |
return false; |
|
546 |
} |
|
547 |
} |
|
548 |
||
549 |
private void processData() { |
|
550 |
boolean completing = isCompleting(); |
|
551 |
||
552 |
try { |
|
553 |
debugw.log(Level.DEBUG, () -> "processData(" + Utils.remaining(writeList) + ")"); |
|
554 |
while (Utils.remaining(writeList) > 0 || hsTriggered() |
|
555 |
|| needWrap()) { |
|
556 |
ByteBuffer[] outbufs = writeList.toArray(Utils.EMPTY_BB_ARRAY); |
|
557 |
EngineResult result = wrapBuffers(outbufs); |
|
558 |
debugw.log(Level.DEBUG, "wrapBuffer returned %s", result.result); |
|
559 |
||
560 |
if (result.status() == Status.CLOSED) { |
|
561 |
if (result.bytesProduced() <= 0) |
|
562 |
return; |
|
563 |
||
564 |
if (!completing && !completed) { |
|
565 |
completing = this.completing = true; |
|
566 |
// There could still be some outgoing data in outbufs. |
|
567 |
writeList.add(SENTINEL); |
|
568 |
} |
|
569 |
} |
|
570 |
||
571 |
boolean handshaking = false; |
|
572 |
if (result.handshaking()) { |
|
573 |
debugw.log(Level.DEBUG, "handshaking"); |
|
574 |
doHandshake(result, WRITER); |
|
575 |
handshaking = true; |
|
576 |
} else { |
|
577 |
if ((handshakeState.getAndSet(NOT_HANDSHAKING) & ~DOING_TASKS) == HANDSHAKING) { |
|
578 |
setALPN(); |
|
579 |
resumeActivity(); |
|
580 |
} |
|
581 |
} |
|
582 |
cleanList(writeList); // tidy up the source list |
|
583 |
sendResultBytes(result); |
|
584 |
if (handshaking && !completing) { |
|
585 |
if (writeList.isEmpty() && !result.needUnwrap()) { |
|
586 |
writer.addData(HS_TRIGGER); |
|
587 |
} |
|
588 |
if (needWrap()) continue; |
|
589 |
return; |
|
590 |
} |
|
591 |
} |
|
592 |
if (completing && Utils.remaining(writeList) == 0) { |
|
593 |
/* |
|
594 |
System.out.println("WRITER DOO 3"); |
|
595 |
engine.closeOutbound(); |
|
596 |
EngineResult result = wrapBuffers(Utils.EMPTY_BB_ARRAY); |
|
597 |
sendResultBytes(result); |
|
598 |
*/ |
|
599 |
if (!completed) { |
|
600 |
completed = true; |
|
601 |
writeList.clear(); |
|
602 |
outgoing(Utils.EMPTY_BB_LIST, true); |
|
603 |
} |
|
604 |
return; |
|
605 |
} |
|
606 |
if (writeList.isEmpty() && needWrap()) { |
|
607 |
writer.addData(HS_TRIGGER); |
|
608 |
} |
|
609 |
} catch (Throwable ex) { |
|
56208
d37c08ce784a
http-client-branch: tweaked last revision wrong CF reference was being kept
michaelm
parents:
56207
diff
changeset
|
610 |
errorCommon(ex); |
48083 | 611 |
handleError(ex); |
612 |
} |
|
613 |
} |
|
614 |
||
56223
377c5ebfc319
http-client-branch: move SSLFlowDelegate wrapBuffers/unwrapBuffers into their respective writer/reader classes - improve diagnostic
chegar
parents:
56208
diff
changeset
|
615 |
@SuppressWarnings("fallthrough") |
377c5ebfc319
http-client-branch: move SSLFlowDelegate wrapBuffers/unwrapBuffers into their respective writer/reader classes - improve diagnostic
chegar
parents:
56208
diff
changeset
|
616 |
EngineResult wrapBuffers(ByteBuffer[] src) throws SSLException { |
377c5ebfc319
http-client-branch: move SSLFlowDelegate wrapBuffers/unwrapBuffers into their respective writer/reader classes - improve diagnostic
chegar
parents:
56208
diff
changeset
|
617 |
debugw.log(Level.DEBUG, () -> "wrapping " + Utils.remaining(src) + " bytes"); |
377c5ebfc319
http-client-branch: move SSLFlowDelegate wrapBuffers/unwrapBuffers into their respective writer/reader classes - improve diagnostic
chegar
parents:
56208
diff
changeset
|
618 |
ByteBuffer dst = getNetBuffer(); |
377c5ebfc319
http-client-branch: move SSLFlowDelegate wrapBuffers/unwrapBuffers into their respective writer/reader classes - improve diagnostic
chegar
parents:
56208
diff
changeset
|
619 |
while (true) { |
377c5ebfc319
http-client-branch: move SSLFlowDelegate wrapBuffers/unwrapBuffers into their respective writer/reader classes - improve diagnostic
chegar
parents:
56208
diff
changeset
|
620 |
SSLEngineResult sslResult = engine.wrap(src, dst); |
377c5ebfc319
http-client-branch: move SSLFlowDelegate wrapBuffers/unwrapBuffers into their respective writer/reader classes - improve diagnostic
chegar
parents:
56208
diff
changeset
|
621 |
debugw.log(Level.DEBUG, () -> "SSLResult: " + sslResult); |
377c5ebfc319
http-client-branch: move SSLFlowDelegate wrapBuffers/unwrapBuffers into their respective writer/reader classes - improve diagnostic
chegar
parents:
56208
diff
changeset
|
622 |
switch (sslResult.getStatus()) { |
377c5ebfc319
http-client-branch: move SSLFlowDelegate wrapBuffers/unwrapBuffers into their respective writer/reader classes - improve diagnostic
chegar
parents:
56208
diff
changeset
|
623 |
case BUFFER_OVERFLOW: |
377c5ebfc319
http-client-branch: move SSLFlowDelegate wrapBuffers/unwrapBuffers into their respective writer/reader classes - improve diagnostic
chegar
parents:
56208
diff
changeset
|
624 |
// Shouldn't happen. We allocated buffer with packet size |
377c5ebfc319
http-client-branch: move SSLFlowDelegate wrapBuffers/unwrapBuffers into their respective writer/reader classes - improve diagnostic
chegar
parents:
56208
diff
changeset
|
625 |
// get it again if net buffer size was changed |
377c5ebfc319
http-client-branch: move SSLFlowDelegate wrapBuffers/unwrapBuffers into their respective writer/reader classes - improve diagnostic
chegar
parents:
56208
diff
changeset
|
626 |
debugw.log(Level.DEBUG, "BUFFER_OVERFLOW"); |
377c5ebfc319
http-client-branch: move SSLFlowDelegate wrapBuffers/unwrapBuffers into their respective writer/reader classes - improve diagnostic
chegar
parents:
56208
diff
changeset
|
627 |
int appSize = engine.getSession().getApplicationBufferSize(); |
377c5ebfc319
http-client-branch: move SSLFlowDelegate wrapBuffers/unwrapBuffers into their respective writer/reader classes - improve diagnostic
chegar
parents:
56208
diff
changeset
|
628 |
ByteBuffer b = ByteBuffer.allocate(appSize + dst.position()); |
377c5ebfc319
http-client-branch: move SSLFlowDelegate wrapBuffers/unwrapBuffers into their respective writer/reader classes - improve diagnostic
chegar
parents:
56208
diff
changeset
|
629 |
dst.flip(); |
377c5ebfc319
http-client-branch: move SSLFlowDelegate wrapBuffers/unwrapBuffers into their respective writer/reader classes - improve diagnostic
chegar
parents:
56208
diff
changeset
|
630 |
b.put(dst); |
377c5ebfc319
http-client-branch: move SSLFlowDelegate wrapBuffers/unwrapBuffers into their respective writer/reader classes - improve diagnostic
chegar
parents:
56208
diff
changeset
|
631 |
dst = b; |
377c5ebfc319
http-client-branch: move SSLFlowDelegate wrapBuffers/unwrapBuffers into their respective writer/reader classes - improve diagnostic
chegar
parents:
56208
diff
changeset
|
632 |
break; // try again |
377c5ebfc319
http-client-branch: move SSLFlowDelegate wrapBuffers/unwrapBuffers into their respective writer/reader classes - improve diagnostic
chegar
parents:
56208
diff
changeset
|
633 |
case CLOSED: |
377c5ebfc319
http-client-branch: move SSLFlowDelegate wrapBuffers/unwrapBuffers into their respective writer/reader classes - improve diagnostic
chegar
parents:
56208
diff
changeset
|
634 |
debugw.log(Level.DEBUG, "CLOSED"); |
377c5ebfc319
http-client-branch: move SSLFlowDelegate wrapBuffers/unwrapBuffers into their respective writer/reader classes - improve diagnostic
chegar
parents:
56208
diff
changeset
|
635 |
// fallthrough. There could be some remaining data in dst. |
377c5ebfc319
http-client-branch: move SSLFlowDelegate wrapBuffers/unwrapBuffers into their respective writer/reader classes - improve diagnostic
chegar
parents:
56208
diff
changeset
|
636 |
// CLOSED will be handled by the caller. |
377c5ebfc319
http-client-branch: move SSLFlowDelegate wrapBuffers/unwrapBuffers into their respective writer/reader classes - improve diagnostic
chegar
parents:
56208
diff
changeset
|
637 |
case OK: |
377c5ebfc319
http-client-branch: move SSLFlowDelegate wrapBuffers/unwrapBuffers into their respective writer/reader classes - improve diagnostic
chegar
parents:
56208
diff
changeset
|
638 |
dst.flip(); |
377c5ebfc319
http-client-branch: move SSLFlowDelegate wrapBuffers/unwrapBuffers into their respective writer/reader classes - improve diagnostic
chegar
parents:
56208
diff
changeset
|
639 |
final ByteBuffer dest = dst; |
377c5ebfc319
http-client-branch: move SSLFlowDelegate wrapBuffers/unwrapBuffers into their respective writer/reader classes - improve diagnostic
chegar
parents:
56208
diff
changeset
|
640 |
debugw.log(Level.DEBUG, () -> "OK => produced: " |
377c5ebfc319
http-client-branch: move SSLFlowDelegate wrapBuffers/unwrapBuffers into their respective writer/reader classes - improve diagnostic
chegar
parents:
56208
diff
changeset
|
641 |
+ dest.remaining() |
377c5ebfc319
http-client-branch: move SSLFlowDelegate wrapBuffers/unwrapBuffers into their respective writer/reader classes - improve diagnostic
chegar
parents:
56208
diff
changeset
|
642 |
+ " not wrapped: " |
377c5ebfc319
http-client-branch: move SSLFlowDelegate wrapBuffers/unwrapBuffers into their respective writer/reader classes - improve diagnostic
chegar
parents:
56208
diff
changeset
|
643 |
+ Utils.remaining(src)); |
377c5ebfc319
http-client-branch: move SSLFlowDelegate wrapBuffers/unwrapBuffers into their respective writer/reader classes - improve diagnostic
chegar
parents:
56208
diff
changeset
|
644 |
return new EngineResult(sslResult, dest); |
377c5ebfc319
http-client-branch: move SSLFlowDelegate wrapBuffers/unwrapBuffers into their respective writer/reader classes - improve diagnostic
chegar
parents:
56208
diff
changeset
|
645 |
case BUFFER_UNDERFLOW: |
377c5ebfc319
http-client-branch: move SSLFlowDelegate wrapBuffers/unwrapBuffers into their respective writer/reader classes - improve diagnostic
chegar
parents:
56208
diff
changeset
|
646 |
// Shouldn't happen. Doesn't returns when wrap() |
377c5ebfc319
http-client-branch: move SSLFlowDelegate wrapBuffers/unwrapBuffers into their respective writer/reader classes - improve diagnostic
chegar
parents:
56208
diff
changeset
|
647 |
// underflow handled externally |
377c5ebfc319
http-client-branch: move SSLFlowDelegate wrapBuffers/unwrapBuffers into their respective writer/reader classes - improve diagnostic
chegar
parents:
56208
diff
changeset
|
648 |
// assert false : "Buffer Underflow"; |
377c5ebfc319
http-client-branch: move SSLFlowDelegate wrapBuffers/unwrapBuffers into their respective writer/reader classes - improve diagnostic
chegar
parents:
56208
diff
changeset
|
649 |
debug.log(Level.DEBUG, "BUFFER_UNDERFLOW"); |
377c5ebfc319
http-client-branch: move SSLFlowDelegate wrapBuffers/unwrapBuffers into their respective writer/reader classes - improve diagnostic
chegar
parents:
56208
diff
changeset
|
650 |
return new EngineResult(sslResult); |
377c5ebfc319
http-client-branch: move SSLFlowDelegate wrapBuffers/unwrapBuffers into their respective writer/reader classes - improve diagnostic
chegar
parents:
56208
diff
changeset
|
651 |
default: |
377c5ebfc319
http-client-branch: move SSLFlowDelegate wrapBuffers/unwrapBuffers into their respective writer/reader classes - improve diagnostic
chegar
parents:
56208
diff
changeset
|
652 |
debugw.log(Level.DEBUG, "ASSERT"); |
377c5ebfc319
http-client-branch: move SSLFlowDelegate wrapBuffers/unwrapBuffers into their respective writer/reader classes - improve diagnostic
chegar
parents:
56208
diff
changeset
|
653 |
assert false; |
377c5ebfc319
http-client-branch: move SSLFlowDelegate wrapBuffers/unwrapBuffers into their respective writer/reader classes - improve diagnostic
chegar
parents:
56208
diff
changeset
|
654 |
} |
377c5ebfc319
http-client-branch: move SSLFlowDelegate wrapBuffers/unwrapBuffers into their respective writer/reader classes - improve diagnostic
chegar
parents:
56208
diff
changeset
|
655 |
} |
377c5ebfc319
http-client-branch: move SSLFlowDelegate wrapBuffers/unwrapBuffers into their respective writer/reader classes - improve diagnostic
chegar
parents:
56208
diff
changeset
|
656 |
} |
377c5ebfc319
http-client-branch: move SSLFlowDelegate wrapBuffers/unwrapBuffers into their respective writer/reader classes - improve diagnostic
chegar
parents:
56208
diff
changeset
|
657 |
|
48083 | 658 |
private boolean needWrap() { |
659 |
return engine.getHandshakeStatus() == HandshakeStatus.NEED_WRAP; |
|
660 |
} |
|
661 |
||
662 |
private void sendResultBytes(EngineResult result) { |
|
663 |
if (result.bytesProduced() > 0) { |
|
664 |
debugw.log(Level.DEBUG, "Sending %d bytes downstream", |
|
665 |
result.bytesProduced()); |
|
666 |
outgoing(result.destBuffer, false); |
|
667 |
} |
|
668 |
} |
|
669 |
||
670 |
@Override |
|
671 |
public String toString() { |
|
672 |
return "WRITER: " + super.toString() + |
|
673 |
" writeList size " + Integer.toString(writeList.size()); |
|
674 |
//" writeList: " + writeList.toString(); |
|
675 |
} |
|
676 |
} |
|
677 |
||
678 |
private void handleError(Throwable t) { |
|
679 |
debug.log(Level.DEBUG, "handleError", t); |
|
56207
03c89ed2070f
http-client-branch: Fixed SSLFlowDelegate to not call Subscriber.onError directly
michaelm
parents:
56187
diff
changeset
|
680 |
readerCF.completeExceptionally(t); |
03c89ed2070f
http-client-branch: Fixed SSLFlowDelegate to not call Subscriber.onError directly
michaelm
parents:
56187
diff
changeset
|
681 |
writerCF.completeExceptionally(t); |
48083 | 682 |
// no-op if already completed |
683 |
alpnCF.completeExceptionally(t); |
|
684 |
reader.stop(); |
|
685 |
writer.stop(); |
|
686 |
} |
|
687 |
||
688 |
private void normalStop() { |
|
56184
1c7b4d7140e2
http-client-branch: fix SSL hang issue where errors not reported correctly
michaelm
parents:
56101
diff
changeset
|
689 |
stopOnError(null); |
1c7b4d7140e2
http-client-branch: fix SSL hang issue where errors not reported correctly
michaelm
parents:
56101
diff
changeset
|
690 |
} |
1c7b4d7140e2
http-client-branch: fix SSL hang issue where errors not reported correctly
michaelm
parents:
56101
diff
changeset
|
691 |
|
56207
03c89ed2070f
http-client-branch: Fixed SSLFlowDelegate to not call Subscriber.onError directly
michaelm
parents:
56187
diff
changeset
|
692 |
boolean stopped = false; |
03c89ed2070f
http-client-branch: Fixed SSLFlowDelegate to not call Subscriber.onError directly
michaelm
parents:
56187
diff
changeset
|
693 |
|
03c89ed2070f
http-client-branch: Fixed SSLFlowDelegate to not call Subscriber.onError directly
michaelm
parents:
56187
diff
changeset
|
694 |
synchronized private Void stopOnError(Throwable t) { |
03c89ed2070f
http-client-branch: Fixed SSLFlowDelegate to not call Subscriber.onError directly
michaelm
parents:
56187
diff
changeset
|
695 |
if (stopped) |
03c89ed2070f
http-client-branch: Fixed SSLFlowDelegate to not call Subscriber.onError directly
michaelm
parents:
56187
diff
changeset
|
696 |
return null; |
03c89ed2070f
http-client-branch: Fixed SSLFlowDelegate to not call Subscriber.onError directly
michaelm
parents:
56187
diff
changeset
|
697 |
stopped = true; |
48083 | 698 |
reader.stop(); |
699 |
writer.stop(); |
|
56184
1c7b4d7140e2
http-client-branch: fix SSL hang issue where errors not reported correctly
michaelm
parents:
56101
diff
changeset
|
700 |
return null; |
48083 | 701 |
} |
702 |
||
703 |
private void cleanList(List<ByteBuffer> l) { |
|
704 |
synchronized (l) { |
|
705 |
Iterator<ByteBuffer> iter = l.iterator(); |
|
706 |
while (iter.hasNext()) { |
|
707 |
ByteBuffer b = iter.next(); |
|
708 |
if (!b.hasRemaining() && b != SENTINEL) { |
|
709 |
iter.remove(); |
|
710 |
} |
|
711 |
} |
|
712 |
} |
|
713 |
} |
|
714 |
||
715 |
/** |
|
716 |
* States for handshake. We avoid races when accessing/updating the AtomicInt |
|
717 |
* because updates always schedule an additional call to both the read() |
|
718 |
* and write() functions. |
|
719 |
*/ |
|
720 |
private static final int NOT_HANDSHAKING = 0; |
|
721 |
private static final int HANDSHAKING = 1; |
|
722 |
private static final int INIT = 2; |
|
723 |
private static final int DOING_TASKS = 4; // bit added to above state |
|
724 |
private static final ByteBuffer HS_TRIGGER = ByteBuffer.allocate(0); |
|
725 |
||
726 |
private static final int READER = 1; |
|
727 |
private static final int WRITER = 2; |
|
728 |
||
729 |
private static String states(AtomicInteger state) { |
|
730 |
int s = state.get(); |
|
731 |
StringBuilder sb = new StringBuilder(); |
|
732 |
int x = s & ~DOING_TASKS; |
|
733 |
switch (x) { |
|
734 |
case NOT_HANDSHAKING: |
|
735 |
sb.append(" NOT_HANDSHAKING "); |
|
736 |
break; |
|
737 |
case HANDSHAKING: |
|
738 |
sb.append(" HANDSHAKING "); |
|
739 |
break; |
|
740 |
case INIT: |
|
741 |
sb.append(" INIT "); |
|
742 |
break; |
|
743 |
default: |
|
744 |
throw new InternalError(); |
|
745 |
} |
|
746 |
if ((s & DOING_TASKS) > 0) |
|
747 |
sb.append("|DOING_TASKS"); |
|
748 |
return sb.toString(); |
|
749 |
} |
|
750 |
||
751 |
private void resumeActivity() { |
|
752 |
reader.schedule(); |
|
753 |
writer.schedule(); |
|
754 |
} |
|
755 |
||
756 |
final AtomicInteger handshakeState; |
|
757 |
final ConcurrentLinkedQueue<String> stateList = new ConcurrentLinkedQueue<>(); |
|
758 |
||
759 |
private void doHandshake(EngineResult r, int caller) { |
|
760 |
int s = handshakeState.getAndAccumulate(HANDSHAKING, (current, update) -> update | (current & DOING_TASKS)); |
|
761 |
stateList.add(r.handshakeStatus().toString()); |
|
762 |
stateList.add(Integer.toString(caller)); |
|
763 |
switch (r.handshakeStatus()) { |
|
764 |
case NEED_TASK: |
|
765 |
if ((s & DOING_TASKS) > 0) // someone else was doing tasks |
|
766 |
return; |
|
767 |
List<Runnable> tasks = obtainTasks(); |
|
768 |
executeTasks(tasks); |
|
769 |
break; |
|
770 |
case NEED_WRAP: |
|
771 |
writer.addData(HS_TRIGGER); |
|
772 |
break; |
|
773 |
case NEED_UNWRAP: |
|
774 |
case NEED_UNWRAP_AGAIN: |
|
775 |
// do nothing else |
|
776 |
break; |
|
777 |
default: |
|
778 |
throw new InternalError("Unexpected handshake status:" |
|
779 |
+ r.handshakeStatus()); |
|
780 |
} |
|
781 |
} |
|
782 |
||
783 |
private List<Runnable> obtainTasks() { |
|
784 |
List<Runnable> l = new ArrayList<>(); |
|
785 |
Runnable r; |
|
786 |
while ((r = engine.getDelegatedTask()) != null) { |
|
787 |
l.add(r); |
|
788 |
} |
|
789 |
return l; |
|
790 |
} |
|
791 |
||
792 |
private void executeTasks(List<Runnable> tasks) { |
|
793 |
exec.execute(() -> { |
|
56044
a8423a38386e
http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
56035
diff
changeset
|
794 |
try { |
a8423a38386e
http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
56035
diff
changeset
|
795 |
handshakeState.getAndUpdate((current) -> current | DOING_TASKS); |
a8423a38386e
http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
56035
diff
changeset
|
796 |
List<Runnable> nextTasks = tasks; |
a8423a38386e
http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
56035
diff
changeset
|
797 |
do { |
a8423a38386e
http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
56035
diff
changeset
|
798 |
nextTasks.forEach(Runnable::run); |
48083 | 799 |
if (engine.getHandshakeStatus() == HandshakeStatus.NEED_TASK) { |
800 |
nextTasks = obtainTasks(); |
|
56044
a8423a38386e
http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
56035
diff
changeset
|
801 |
} else { |
a8423a38386e
http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
56035
diff
changeset
|
802 |
break; |
a8423a38386e
http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
56035
diff
changeset
|
803 |
} |
a8423a38386e
http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
56035
diff
changeset
|
804 |
} while (true); |
a8423a38386e
http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
56035
diff
changeset
|
805 |
handshakeState.getAndUpdate((current) -> current & ~DOING_TASKS); |
a8423a38386e
http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
56035
diff
changeset
|
806 |
writer.addData(HS_TRIGGER); |
a8423a38386e
http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
56035
diff
changeset
|
807 |
resumeActivity(); |
a8423a38386e
http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
56035
diff
changeset
|
808 |
} catch (Throwable t) { |
a8423a38386e
http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
56035
diff
changeset
|
809 |
handleError(t); |
a8423a38386e
http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
56035
diff
changeset
|
810 |
} |
48083 | 811 |
}); |
812 |
} |
|
813 |
||
814 |
// FIXME: acknowledge a received CLOSE request from peer |
|
815 |
EngineResult doClosure(EngineResult r) throws IOException { |
|
816 |
debug.log(Level.DEBUG, |
|
817 |
"doClosure(%s): %s [isOutboundDone: %s, isInboundDone: %s]", |
|
818 |
r.result, engine.getHandshakeStatus(), |
|
819 |
engine.isOutboundDone(), engine.isInboundDone()); |
|
820 |
if (engine.getHandshakeStatus() == HandshakeStatus.NEED_WRAP) { |
|
821 |
// we have received TLS close_notify and need to send |
|
822 |
// an acknowledgement back. We're calling doHandshake |
|
823 |
// to finish the close handshake. |
|
824 |
if (engine.isInboundDone() && !engine.isOutboundDone()) { |
|
825 |
debug.log(Level.DEBUG, "doClosure: close_notify received"); |
|
826 |
close_notify_received = true; |
|
827 |
doHandshake(r, READER); |
|
828 |
} |
|
829 |
} |
|
830 |
return r; |
|
831 |
} |
|
832 |
||
833 |
/** |
|
834 |
* Returns the upstream Flow.Subscriber of the reading (incoming) side. |
|
835 |
* This flow must be given the encrypted data read from upstream (eg socket) |
|
836 |
* before it is decrypted. |
|
837 |
*/ |
|
838 |
public Flow.Subscriber<List<ByteBuffer>> upstreamReader() { |
|
839 |
return reader; |
|
840 |
} |
|
841 |
||
842 |
/** |
|
843 |
* Returns the upstream Flow.Subscriber of the writing (outgoing) side. |
|
844 |
* This flow contains the plaintext data before it is encrypted. |
|
845 |
*/ |
|
846 |
public Flow.Subscriber<List<ByteBuffer>> upstreamWriter() { |
|
847 |
return writer; |
|
848 |
} |
|
849 |
||
850 |
public boolean resumeReader() { |
|
851 |
return reader.signalScheduling(); |
|
852 |
} |
|
853 |
||
854 |
public void resetReaderDemand() { |
|
855 |
reader.resetDownstreamDemand(); |
|
856 |
} |
|
857 |
||
858 |
static class EngineResult { |
|
859 |
final SSLEngineResult result; |
|
860 |
final ByteBuffer destBuffer; |
|
861 |
||
862 |
// normal result |
|
863 |
EngineResult(SSLEngineResult result) { |
|
864 |
this(result, null); |
|
865 |
} |
|
866 |
||
867 |
EngineResult(SSLEngineResult result, ByteBuffer destBuffer) { |
|
868 |
this.result = result; |
|
869 |
this.destBuffer = destBuffer; |
|
870 |
} |
|
871 |
||
872 |
// Special result used to trigger handshaking in constructor |
|
873 |
static EngineResult INIT = |
|
874 |
new EngineResult( |
|
875 |
new SSLEngineResult(SSLEngineResult.Status.OK, HandshakeStatus.NEED_WRAP, 0, 0)); |
|
876 |
||
877 |
boolean handshaking() { |
|
878 |
HandshakeStatus s = result.getHandshakeStatus(); |
|
879 |
return s != HandshakeStatus.FINISHED |
|
880 |
&& s != HandshakeStatus.NOT_HANDSHAKING |
|
881 |
&& result.getStatus() != Status.CLOSED; |
|
882 |
} |
|
883 |
||
884 |
boolean needUnwrap() { |
|
885 |
HandshakeStatus s = result.getHandshakeStatus(); |
|
886 |
return s == HandshakeStatus.NEED_UNWRAP; |
|
887 |
} |
|
888 |
||
889 |
||
890 |
int bytesConsumed() { |
|
891 |
return result.bytesConsumed(); |
|
892 |
} |
|
893 |
||
894 |
int bytesProduced() { |
|
895 |
return result.bytesProduced(); |
|
896 |
} |
|
897 |
||
898 |
SSLEngineResult.HandshakeStatus handshakeStatus() { |
|
899 |
return result.getHandshakeStatus(); |
|
900 |
} |
|
901 |
||
902 |
SSLEngineResult.Status status() { |
|
903 |
return result.getStatus(); |
|
904 |
} |
|
905 |
} |
|
906 |
||
907 |
public ByteBuffer getNetBuffer() { |
|
908 |
return ByteBuffer.allocate(engine.getSession().getPacketBufferSize()); |
|
909 |
} |
|
910 |
||
911 |
private ByteBuffer getAppBuffer() { |
|
912 |
return ByteBuffer.allocate(engine.getSession().getApplicationBufferSize()); |
|
913 |
} |
|
914 |
||
915 |
final String dbgString() { |
|
916 |
return "SSLFlowDelegate(" + tubeName + ")"; |
|
917 |
} |
|
918 |
} |