author | ssadetsky |
Mon, 16 Oct 2017 08:06:56 -0700 | |
changeset 47387 | 4d711a58bb3b |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
10596
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
7668
diff
changeset
|
2 |
* Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.net.httpserver; |
|
27 |
||
28 |
import java.net.*; |
|
29 |
import java.nio.*; |
|
30 |
import java.io.*; |
|
31 |
import java.nio.channels.*; |
|
32 |
import java.util.concurrent.locks.*; |
|
33 |
import javax.net.ssl.*; |
|
34 |
import javax.net.ssl.SSLEngineResult.*; |
|
35 |
import com.sun.net.httpserver.*; |
|
36 |
||
37 |
/** |
|
38 |
* given a non-blocking SocketChannel, it produces |
|
39 |
* (blocking) streams which encrypt/decrypt the SSL content |
|
40 |
* and handle the SSL handshaking automatically. |
|
41 |
*/ |
|
42 |
||
43 |
class SSLStreams { |
|
44 |
||
45 |
SSLContext sslctx; |
|
46 |
SocketChannel chan; |
|
47 |
TimeSource time; |
|
48 |
ServerImpl server; |
|
49 |
SSLEngine engine; |
|
50 |
EngineWrapper wrapper; |
|
51 |
OutputStream os; |
|
52 |
InputStream is; |
|
53 |
||
54 |
/* held by thread doing the hand-shake on this connection */ |
|
55 |
Lock handshaking = new ReentrantLock(); |
|
56 |
||
57 |
SSLStreams (ServerImpl server, SSLContext sslctx, SocketChannel chan) throws IOException { |
|
58 |
this.server = server; |
|
59 |
this.time= (TimeSource)server; |
|
60 |
this.sslctx= sslctx; |
|
61 |
this.chan= chan; |
|
62 |
InetSocketAddress addr = |
|
63 |
(InetSocketAddress)chan.socket().getRemoteSocketAddress(); |
|
64 |
engine = sslctx.createSSLEngine (addr.getHostName(), addr.getPort()); |
|
65 |
engine.setUseClientMode (false); |
|
66 |
HttpsConfigurator cfg = server.getHttpsConfigurator(); |
|
67 |
configureEngine (cfg, addr); |
|
68 |
wrapper = new EngineWrapper (chan, engine); |
|
69 |
} |
|
70 |
||
71 |
private void configureEngine(HttpsConfigurator cfg, InetSocketAddress addr){ |
|
72 |
if (cfg != null) { |
|
73 |
Parameters params = new Parameters (cfg, addr); |
|
7537
b7d2d2428f31
7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
7271
diff
changeset
|
74 |
//BEGIN_TIGER_EXCLUDE |
2 | 75 |
cfg.configure (params); |
76 |
SSLParameters sslParams = params.getSSLParameters(); |
|
77 |
if (sslParams != null) { |
|
78 |
engine.setSSLParameters (sslParams); |
|
7271 | 79 |
} else |
80 |
//END_TIGER_EXCLUDE |
|
81 |
{ |
|
2 | 82 |
/* tiger compatibility */ |
83 |
if (params.getCipherSuites() != null) { |
|
84 |
try { |
|
85 |
engine.setEnabledCipherSuites ( |
|
86 |
params.getCipherSuites() |
|
87 |
); |
|
88 |
} catch (IllegalArgumentException e) { /* LOG */} |
|
89 |
} |
|
90 |
engine.setNeedClientAuth (params.getNeedClientAuth()); |
|
91 |
engine.setWantClientAuth (params.getWantClientAuth()); |
|
92 |
if (params.getProtocols() != null) { |
|
93 |
try { |
|
94 |
engine.setEnabledProtocols ( |
|
95 |
params.getProtocols() |
|
96 |
); |
|
97 |
} catch (IllegalArgumentException e) { /* LOG */} |
|
98 |
} |
|
99 |
} |
|
100 |
} |
|
101 |
} |
|
102 |
||
103 |
class Parameters extends HttpsParameters { |
|
104 |
InetSocketAddress addr; |
|
105 |
HttpsConfigurator cfg; |
|
106 |
||
107 |
Parameters (HttpsConfigurator cfg, InetSocketAddress addr) { |
|
108 |
this.addr = addr; |
|
109 |
this.cfg = cfg; |
|
110 |
} |
|
111 |
public InetSocketAddress getClientAddress () { |
|
112 |
return addr; |
|
113 |
} |
|
114 |
public HttpsConfigurator getHttpsConfigurator() { |
|
115 |
return cfg; |
|
116 |
} |
|
7271 | 117 |
//BEGIN_TIGER_EXCLUDE |
118 |
SSLParameters params; |
|
2 | 119 |
public void setSSLParameters (SSLParameters p) { |
120 |
params = p; |
|
121 |
} |
|
122 |
SSLParameters getSSLParameters () { |
|
123 |
return params; |
|
124 |
} |
|
7271 | 125 |
//END_TIGER_EXCLUDE |
2 | 126 |
} |
127 |
||
128 |
/** |
|
129 |
* cleanup resources allocated inside this object |
|
130 |
*/ |
|
131 |
void close () throws IOException { |
|
132 |
wrapper.close(); |
|
133 |
} |
|
134 |
||
135 |
/** |
|
136 |
* return the SSL InputStream |
|
137 |
*/ |
|
138 |
InputStream getInputStream () throws IOException { |
|
139 |
if (is == null) { |
|
140 |
is = new InputStream(); |
|
141 |
} |
|
142 |
return is; |
|
143 |
} |
|
144 |
||
145 |
/** |
|
146 |
* return the SSL OutputStream |
|
147 |
*/ |
|
148 |
OutputStream getOutputStream () throws IOException { |
|
149 |
if (os == null) { |
|
150 |
os = new OutputStream(); |
|
151 |
} |
|
152 |
return os; |
|
153 |
} |
|
154 |
||
155 |
SSLEngine getSSLEngine () { |
|
156 |
return engine; |
|
157 |
} |
|
158 |
||
159 |
/** |
|
160 |
* request the engine to repeat the handshake on this session |
|
161 |
* the handshake must be driven by reads/writes on the streams |
|
162 |
* Normally, not necessary to call this. |
|
163 |
*/ |
|
164 |
void beginHandshake() throws SSLException { |
|
165 |
engine.beginHandshake(); |
|
166 |
} |
|
167 |
||
168 |
class WrapperResult { |
|
169 |
SSLEngineResult result; |
|
170 |
||
171 |
/* if passed in buffer was not big enough then the |
|
172 |
* a reallocated buffer is returned here |
|
173 |
*/ |
|
174 |
ByteBuffer buf; |
|
175 |
} |
|
176 |
||
177 |
int app_buf_size; |
|
178 |
int packet_buf_size; |
|
179 |
||
180 |
enum BufType { |
|
181 |
PACKET, APPLICATION |
|
182 |
}; |
|
183 |
||
184 |
private ByteBuffer allocate (BufType type) { |
|
185 |
return allocate (type, -1); |
|
186 |
} |
|
187 |
||
188 |
private ByteBuffer allocate (BufType type, int len) { |
|
189 |
assert engine != null; |
|
190 |
synchronized (this) { |
|
191 |
int size; |
|
192 |
if (type == BufType.PACKET) { |
|
193 |
if (packet_buf_size == 0) { |
|
194 |
SSLSession sess = engine.getSession(); |
|
195 |
packet_buf_size = sess.getPacketBufferSize(); |
|
196 |
} |
|
197 |
if (len > packet_buf_size) { |
|
198 |
packet_buf_size = len; |
|
199 |
} |
|
200 |
size = packet_buf_size; |
|
201 |
} else { |
|
202 |
if (app_buf_size == 0) { |
|
203 |
SSLSession sess = engine.getSession(); |
|
204 |
app_buf_size = sess.getApplicationBufferSize(); |
|
205 |
} |
|
206 |
if (len > app_buf_size) { |
|
207 |
app_buf_size = len; |
|
208 |
} |
|
209 |
size = app_buf_size; |
|
210 |
} |
|
211 |
return ByteBuffer.allocate (size); |
|
212 |
} |
|
213 |
} |
|
214 |
||
215 |
/* reallocates the buffer by :- |
|
216 |
* 1. creating a new buffer double the size of the old one |
|
217 |
* 2. putting the contents of the old buffer into the new one |
|
218 |
* 3. set xx_buf_size to the new size if it was smaller than new size |
|
219 |
* |
|
220 |
* flip is set to true if the old buffer needs to be flipped |
|
221 |
* before it is copied. |
|
222 |
*/ |
|
223 |
private ByteBuffer realloc (ByteBuffer b, boolean flip, BufType type) { |
|
224 |
synchronized (this) { |
|
225 |
int nsize = 2 * b.capacity(); |
|
226 |
ByteBuffer n = allocate (type, nsize); |
|
227 |
if (flip) { |
|
228 |
b.flip(); |
|
229 |
} |
|
230 |
n.put(b); |
|
231 |
b = n; |
|
232 |
} |
|
233 |
return b; |
|
234 |
} |
|
235 |
/** |
|
236 |
* This is a thin wrapper over SSLEngine and the SocketChannel, |
|
237 |
* which guarantees the ordering of wraps/unwraps with respect to the underlying |
|
238 |
* channel read/writes. It handles the UNDER/OVERFLOW status codes |
|
239 |
* It does not handle the handshaking status codes, or the CLOSED status code |
|
240 |
* though once the engine is closed, any attempt to read/write to it |
|
241 |
* will get an exception. The overall result is returned. |
|
242 |
* It functions synchronously/blocking |
|
243 |
*/ |
|
244 |
class EngineWrapper { |
|
245 |
||
246 |
SocketChannel chan; |
|
247 |
SSLEngine engine; |
|
248 |
Object wrapLock, unwrapLock; |
|
249 |
ByteBuffer unwrap_src, wrap_dst; |
|
250 |
boolean closed = false; |
|
251 |
int u_remaining; // the number of bytes left in unwrap_src after an unwrap() |
|
252 |
||
253 |
EngineWrapper (SocketChannel chan, SSLEngine engine) throws IOException { |
|
254 |
this.chan = chan; |
|
255 |
this.engine = engine; |
|
256 |
wrapLock = new Object(); |
|
257 |
unwrapLock = new Object(); |
|
258 |
unwrap_src = allocate(BufType.PACKET); |
|
259 |
wrap_dst = allocate(BufType.PACKET); |
|
260 |
} |
|
261 |
||
262 |
void close () throws IOException { |
|
263 |
} |
|
264 |
||
265 |
/* try to wrap and send the data in src. Handles OVERFLOW. |
|
266 |
* Might block if there is an outbound blockage or if another |
|
267 |
* thread is calling wrap(). Also, might not send any data |
|
268 |
* if an unwrap is needed. |
|
269 |
*/ |
|
270 |
WrapperResult wrapAndSend(ByteBuffer src) throws IOException { |
|
271 |
return wrapAndSendX(src, false); |
|
272 |
} |
|
273 |
||
274 |
WrapperResult wrapAndSendX(ByteBuffer src, boolean ignoreClose) throws IOException { |
|
275 |
if (closed && !ignoreClose) { |
|
276 |
throw new IOException ("Engine is closed"); |
|
277 |
} |
|
278 |
Status status; |
|
279 |
WrapperResult r = new WrapperResult(); |
|
280 |
synchronized (wrapLock) { |
|
281 |
wrap_dst.clear(); |
|
282 |
do { |
|
283 |
r.result = engine.wrap (src, wrap_dst); |
|
284 |
status = r.result.getStatus(); |
|
285 |
if (status == Status.BUFFER_OVERFLOW) { |
|
286 |
wrap_dst = realloc (wrap_dst, true, BufType.PACKET); |
|
287 |
} |
|
288 |
} while (status == Status.BUFFER_OVERFLOW); |
|
289 |
if (status == Status.CLOSED && !ignoreClose) { |
|
290 |
closed = true; |
|
291 |
return r; |
|
292 |
} |
|
293 |
if (r.result.bytesProduced() > 0) { |
|
294 |
wrap_dst.flip(); |
|
295 |
int l = wrap_dst.remaining(); |
|
296 |
assert l == r.result.bytesProduced(); |
|
297 |
while (l>0) { |
|
298 |
l -= chan.write (wrap_dst); |
|
299 |
} |
|
300 |
} |
|
301 |
} |
|
302 |
return r; |
|
303 |
} |
|
304 |
||
305 |
/* block until a complete message is available and return it |
|
306 |
* in dst, together with the Result. dst may have been re-allocated |
|
307 |
* so caller should check the returned value in Result |
|
308 |
* If handshaking is in progress then, possibly no data is returned |
|
309 |
*/ |
|
310 |
WrapperResult recvAndUnwrap(ByteBuffer dst) throws IOException { |
|
311 |
Status status = Status.OK; |
|
312 |
WrapperResult r = new WrapperResult(); |
|
313 |
r.buf = dst; |
|
314 |
if (closed) { |
|
315 |
throw new IOException ("Engine is closed"); |
|
316 |
} |
|
317 |
boolean needData; |
|
318 |
if (u_remaining > 0) { |
|
319 |
unwrap_src.compact(); |
|
320 |
unwrap_src.flip(); |
|
321 |
needData = false; |
|
322 |
} else { |
|
323 |
unwrap_src.clear(); |
|
324 |
needData = true; |
|
325 |
} |
|
326 |
synchronized (unwrapLock) { |
|
7271 | 327 |
int x; |
2 | 328 |
do { |
329 |
if (needData) { |
|
330 |
do { |
|
331 |
x = chan.read (unwrap_src); |
|
7271 | 332 |
} while (x == 0); |
2 | 333 |
if (x == -1) { |
334 |
throw new IOException ("connection closed for reading"); |
|
335 |
} |
|
336 |
unwrap_src.flip(); |
|
337 |
} |
|
338 |
r.result = engine.unwrap (unwrap_src, r.buf); |
|
339 |
status = r.result.getStatus(); |
|
340 |
if (status == Status.BUFFER_UNDERFLOW) { |
|
341 |
if (unwrap_src.limit() == unwrap_src.capacity()) { |
|
342 |
/* buffer not big enough */ |
|
343 |
unwrap_src = realloc ( |
|
344 |
unwrap_src, false, BufType.PACKET |
|
345 |
); |
|
346 |
} else { |
|
347 |
/* Buffer not full, just need to read more |
|
348 |
* data off the channel. Reset pointers |
|
349 |
* for reading off SocketChannel |
|
350 |
*/ |
|
351 |
unwrap_src.position (unwrap_src.limit()); |
|
352 |
unwrap_src.limit (unwrap_src.capacity()); |
|
353 |
} |
|
354 |
needData = true; |
|
355 |
} else if (status == Status.BUFFER_OVERFLOW) { |
|
356 |
r.buf = realloc (r.buf, true, BufType.APPLICATION); |
|
357 |
needData = false; |
|
358 |
} else if (status == Status.CLOSED) { |
|
359 |
closed = true; |
|
360 |
r.buf.flip(); |
|
361 |
return r; |
|
362 |
} |
|
363 |
} while (status != Status.OK); |
|
364 |
} |
|
365 |
u_remaining = unwrap_src.remaining(); |
|
366 |
return r; |
|
367 |
} |
|
368 |
} |
|
369 |
||
370 |
/** |
|
371 |
* send the data in the given ByteBuffer. If a handshake is needed |
|
372 |
* then this is handled within this method. When this call returns, |
|
373 |
* all of the given user data has been sent and any handshake has been |
|
374 |
* completed. Caller should check if engine has been closed. |
|
375 |
*/ |
|
376 |
public WrapperResult sendData (ByteBuffer src) throws IOException { |
|
377 |
WrapperResult r=null; |
|
378 |
while (src.remaining() > 0) { |
|
379 |
r = wrapper.wrapAndSend(src); |
|
380 |
Status status = r.result.getStatus(); |
|
381 |
if (status == Status.CLOSED) { |
|
382 |
doClosure (); |
|
383 |
return r; |
|
384 |
} |
|
385 |
HandshakeStatus hs_status = r.result.getHandshakeStatus(); |
|
386 |
if (hs_status != HandshakeStatus.FINISHED && |
|
387 |
hs_status != HandshakeStatus.NOT_HANDSHAKING) |
|
388 |
{ |
|
389 |
doHandshake(hs_status); |
|
390 |
} |
|
391 |
} |
|
392 |
return r; |
|
393 |
} |
|
394 |
||
395 |
/** |
|
396 |
* read data thru the engine into the given ByteBuffer. If the |
|
397 |
* given buffer was not large enough, a new one is allocated |
|
398 |
* and returned. This call handles handshaking automatically. |
|
399 |
* Caller should check if engine has been closed. |
|
400 |
*/ |
|
401 |
public WrapperResult recvData (ByteBuffer dst) throws IOException { |
|
402 |
/* we wait until some user data arrives */ |
|
403 |
WrapperResult r = null; |
|
404 |
assert dst.position() == 0; |
|
405 |
while (dst.position() == 0) { |
|
406 |
r = wrapper.recvAndUnwrap (dst); |
|
407 |
dst = (r.buf != dst) ? r.buf: dst; |
|
408 |
Status status = r.result.getStatus(); |
|
409 |
if (status == Status.CLOSED) { |
|
410 |
doClosure (); |
|
411 |
return r; |
|
412 |
} |
|
413 |
||
414 |
HandshakeStatus hs_status = r.result.getHandshakeStatus(); |
|
415 |
if (hs_status != HandshakeStatus.FINISHED && |
|
416 |
hs_status != HandshakeStatus.NOT_HANDSHAKING) |
|
417 |
{ |
|
418 |
doHandshake (hs_status); |
|
419 |
} |
|
420 |
} |
|
421 |
dst.flip(); |
|
422 |
return r; |
|
423 |
} |
|
424 |
||
425 |
/* we've received a close notify. Need to call wrap to send |
|
426 |
* the response |
|
427 |
*/ |
|
428 |
void doClosure () throws IOException { |
|
429 |
try { |
|
430 |
handshaking.lock(); |
|
431 |
ByteBuffer tmp = allocate(BufType.APPLICATION); |
|
432 |
WrapperResult r; |
|
433 |
do { |
|
434 |
tmp.clear(); |
|
435 |
tmp.flip (); |
|
436 |
r = wrapper.wrapAndSendX (tmp, true); |
|
437 |
} while (r.result.getStatus() != Status.CLOSED); |
|
438 |
} finally { |
|
439 |
handshaking.unlock(); |
|
440 |
} |
|
441 |
} |
|
442 |
||
443 |
/* do the (complete) handshake after acquiring the handshake lock. |
|
444 |
* If two threads call this at the same time, then we depend |
|
445 |
* on the wrapper methods being idempotent. eg. if wrapAndSend() |
|
446 |
* is called with no data to send then there must be no problem |
|
447 |
*/ |
|
10596
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
7668
diff
changeset
|
448 |
@SuppressWarnings("fallthrough") |
2 | 449 |
void doHandshake (HandshakeStatus hs_status) throws IOException { |
450 |
try { |
|
451 |
handshaking.lock(); |
|
452 |
ByteBuffer tmp = allocate(BufType.APPLICATION); |
|
453 |
while (hs_status != HandshakeStatus.FINISHED && |
|
454 |
hs_status != HandshakeStatus.NOT_HANDSHAKING) |
|
455 |
{ |
|
456 |
WrapperResult r = null; |
|
457 |
switch (hs_status) { |
|
458 |
case NEED_TASK: |
|
459 |
Runnable task; |
|
460 |
while ((task = engine.getDelegatedTask()) != null) { |
|
461 |
/* run in current thread, because we are already |
|
462 |
* running an external Executor |
|
463 |
*/ |
|
464 |
task.run(); |
|
465 |
} |
|
466 |
/* fall thru - call wrap again */ |
|
467 |
case NEED_WRAP: |
|
468 |
tmp.clear(); |
|
469 |
tmp.flip(); |
|
470 |
r = wrapper.wrapAndSend(tmp); |
|
471 |
break; |
|
472 |
||
473 |
case NEED_UNWRAP: |
|
474 |
tmp.clear(); |
|
475 |
r = wrapper.recvAndUnwrap (tmp); |
|
476 |
if (r.buf != tmp) { |
|
477 |
tmp = r.buf; |
|
478 |
} |
|
479 |
assert tmp.position() == 0; |
|
480 |
break; |
|
481 |
} |
|
482 |
hs_status = r.result.getHandshakeStatus(); |
|
483 |
} |
|
484 |
} finally { |
|
485 |
handshaking.unlock(); |
|
486 |
} |
|
487 |
} |
|
488 |
||
489 |
/** |
|
490 |
* represents an SSL input stream. Multiple https requests can |
|
491 |
* be sent over one stream. closing this stream causes an SSL close |
|
492 |
* input. |
|
493 |
*/ |
|
494 |
class InputStream extends java.io.InputStream { |
|
495 |
||
496 |
ByteBuffer bbuf; |
|
497 |
boolean closed = false; |
|
498 |
||
499 |
/* this stream eof */ |
|
500 |
boolean eof = false; |
|
501 |
||
502 |
boolean needData = true; |
|
503 |
||
504 |
InputStream () { |
|
505 |
bbuf = allocate (BufType.APPLICATION); |
|
506 |
} |
|
507 |
||
508 |
public int read (byte[] buf, int off, int len) throws IOException { |
|
509 |
if (closed) { |
|
510 |
throw new IOException ("SSL stream is closed"); |
|
511 |
} |
|
512 |
if (eof) { |
|
513 |
return 0; |
|
514 |
} |
|
515 |
int available=0; |
|
516 |
if (!needData) { |
|
517 |
available = bbuf.remaining(); |
|
518 |
needData = (available==0); |
|
519 |
} |
|
520 |
if (needData) { |
|
521 |
bbuf.clear(); |
|
522 |
WrapperResult r = recvData (bbuf); |
|
523 |
bbuf = r.buf== bbuf? bbuf: r.buf; |
|
524 |
if ((available=bbuf.remaining()) == 0) { |
|
525 |
eof = true; |
|
526 |
return 0; |
|
527 |
} else { |
|
528 |
needData = false; |
|
529 |
} |
|
530 |
} |
|
531 |
/* copy as much as possible from buf into users buf */ |
|
532 |
if (len > available) { |
|
533 |
len = available; |
|
534 |
} |
|
535 |
bbuf.get (buf, off, len); |
|
536 |
return len; |
|
537 |
} |
|
538 |
||
539 |
public int available () throws IOException { |
|
540 |
return bbuf.remaining(); |
|
541 |
} |
|
542 |
||
543 |
public boolean markSupported () { |
|
544 |
return false; /* not possible with SSLEngine */ |
|
545 |
} |
|
546 |
||
547 |
public void reset () throws IOException { |
|
548 |
throw new IOException ("mark/reset not supported"); |
|
549 |
} |
|
550 |
||
551 |
public long skip (long s) throws IOException { |
|
552 |
int n = (int)s; |
|
553 |
if (closed) { |
|
554 |
throw new IOException ("SSL stream is closed"); |
|
555 |
} |
|
556 |
if (eof) { |
|
557 |
return 0; |
|
558 |
} |
|
559 |
int ret = n; |
|
560 |
while (n > 0) { |
|
561 |
if (bbuf.remaining() >= n) { |
|
562 |
bbuf.position (bbuf.position()+n); |
|
563 |
return ret; |
|
564 |
} else { |
|
565 |
n -= bbuf.remaining(); |
|
566 |
bbuf.clear(); |
|
567 |
WrapperResult r = recvData (bbuf); |
|
568 |
bbuf = r.buf==bbuf? bbuf: r.buf; |
|
569 |
} |
|
570 |
} |
|
571 |
return ret; /* not reached */ |
|
572 |
} |
|
573 |
||
574 |
/** |
|
575 |
* close the SSL connection. All data must have been consumed |
|
576 |
* before this is called. Otherwise an exception will be thrown. |
|
577 |
* [Note. May need to revisit this. not quite the normal close() symantics |
|
578 |
*/ |
|
579 |
public void close () throws IOException { |
|
580 |
eof = true; |
|
581 |
engine.closeInbound (); |
|
582 |
} |
|
583 |
||
584 |
public int read (byte[] buf) throws IOException { |
|
585 |
return read (buf, 0, buf.length); |
|
586 |
} |
|
587 |
||
588 |
byte single[] = new byte [1]; |
|
589 |
||
590 |
public int read () throws IOException { |
|
591 |
int n = read (single, 0, 1); |
|
592 |
if (n == 0) { |
|
593 |
return -1; |
|
594 |
} else { |
|
595 |
return single[0] & 0xFF; |
|
596 |
} |
|
597 |
} |
|
598 |
} |
|
599 |
||
600 |
/** |
|
601 |
* represents an SSL output stream. plain text data written to this stream |
|
602 |
* is encrypted by the stream. Multiple HTTPS responses can be sent on |
|
603 |
* one stream. closing this stream initiates an SSL closure |
|
604 |
*/ |
|
605 |
class OutputStream extends java.io.OutputStream { |
|
606 |
ByteBuffer buf; |
|
607 |
boolean closed = false; |
|
608 |
byte single[] = new byte[1]; |
|
609 |
||
610 |
OutputStream() { |
|
611 |
buf = allocate(BufType.APPLICATION); |
|
612 |
} |
|
613 |
||
614 |
public void write(int b) throws IOException { |
|
615 |
single[0] = (byte)b; |
|
616 |
write (single, 0, 1); |
|
617 |
} |
|
618 |
||
619 |
public void write(byte b[]) throws IOException { |
|
620 |
write (b, 0, b.length); |
|
621 |
} |
|
622 |
public void write(byte b[], int off, int len) throws IOException { |
|
623 |
if (closed) { |
|
624 |
throw new IOException ("output stream is closed"); |
|
625 |
} |
|
626 |
while (len > 0) { |
|
627 |
int l = len > buf.capacity() ? buf.capacity() : len; |
|
628 |
buf.clear(); |
|
629 |
buf.put (b, off, l); |
|
630 |
len -= l; |
|
631 |
off += l; |
|
632 |
buf.flip(); |
|
633 |
WrapperResult r = sendData (buf); |
|
634 |
if (r.result.getStatus() == Status.CLOSED) { |
|
635 |
closed = true; |
|
636 |
if (len > 0) { |
|
637 |
throw new IOException ("output stream is closed"); |
|
638 |
} |
|
639 |
} |
|
640 |
} |
|
641 |
} |
|
642 |
||
643 |
public void flush() throws IOException { |
|
644 |
/* no-op */ |
|
645 |
} |
|
646 |
||
647 |
public void close() throws IOException { |
|
648 |
WrapperResult r=null; |
|
649 |
engine.closeOutbound(); |
|
650 |
closed = true; |
|
651 |
HandshakeStatus stat = HandshakeStatus.NEED_WRAP; |
|
652 |
buf.clear(); |
|
653 |
while (stat == HandshakeStatus.NEED_WRAP) { |
|
654 |
r = wrapper.wrapAndSend (buf); |
|
655 |
stat = r.result.getHandshakeStatus(); |
|
656 |
} |
|
657 |
assert r.result.getStatus() == Status.CLOSED; |
|
658 |
} |
|
659 |
} |
|
660 |
} |