OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 part of dart.io; | 5 part of dart.io; |
6 | 6 |
7 /** | 7 /** |
8 * A high-level class for communicating securely over a TCP socket, using | 8 * A high-level class for communicating securely over a TCP socket, using |
9 * TLS and SSL. The [SecureSocket] exposes both a [Stream] and an | 9 * TLS and SSL. The [SecureSocket] exposes both a [Stream] and an |
10 * [IOSink] interface, making it ideal for using together with | 10 * [IOSink] interface, making it ideal for using together with |
(...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
283 _status = HANDSHAKE; | 283 _status = HANDSHAKE; |
284 _secureHandshake(); | 284 _secureHandshake(); |
285 }) | 285 }) |
286 .catchError((error) { | 286 .catchError((error) { |
287 _handshakeComplete.completeError(error); | 287 _handshakeComplete.completeError(error); |
288 _close(); | 288 _close(); |
289 }); | 289 }); |
290 } | 290 } |
291 | 291 |
292 StreamSubscription listen(void onData(RawSocketEvent data), | 292 StreamSubscription listen(void onData(RawSocketEvent data), |
293 {void onError(AsyncError error), | 293 {void onError(error), |
294 void onDone(), | 294 void onDone(), |
295 bool cancelOnError}) { | 295 bool cancelOnError}) { |
296 if (_writeEventsEnabled) { | 296 if (_writeEventsEnabled) { |
297 _writeEventsEnabled = false; | 297 _writeEventsEnabled = false; |
298 _controller.add(RawSocketEvent.WRITE); | 298 _controller.add(RawSocketEvent.WRITE); |
299 } | 299 } |
300 return _stream.listen(onData, | 300 return _stream.listen(onData, |
301 onError: onError, | 301 onError: onError, |
302 onDone: onDone, | 302 onDone: onDone, |
303 cancelOnError: cancelOnError); | 303 cancelOnError: cancelOnError); |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
459 } | 459 } |
460 | 460 |
461 return result; | 461 return result; |
462 } | 462 } |
463 | 463 |
464 // Write the data to the socket, and flush it as much as possible | 464 // Write the data to the socket, and flush it as much as possible |
465 // until it would block. If the write would block, _writeEncryptedData sets | 465 // until it would block. If the write would block, _writeEncryptedData sets |
466 // up handlers to flush the pipeline when possible. | 466 // up handlers to flush the pipeline when possible. |
467 int write(List<int> data, [int offset, int bytes]) { | 467 int write(List<int> data, [int offset, int bytes]) { |
468 if (_closedWrite) { | 468 if (_closedWrite) { |
469 _controller.addError(new AsyncError(new SocketIOException( | 469 _controller.addError(new SocketIOException("Writing to a closed socket")); |
470 "Writing to a closed socket"))); | |
471 return 0; | 470 return 0; |
472 } | 471 } |
473 if (_status != CONNECTED) return 0; | 472 if (_status != CONNECTED) return 0; |
474 | 473 |
475 if (offset == null) offset = 0; | 474 if (offset == null) offset = 0; |
476 if (bytes == null) bytes = data.length - offset; | 475 if (bytes == null) bytes = data.length - offset; |
477 | 476 |
478 var buffer = _secureFilter.buffers[WRITE_PLAINTEXT]; | 477 var buffer = _secureFilter.buffers[WRITE_PLAINTEXT]; |
479 if (bytes > buffer.free) { | 478 if (bytes > buffer.free) { |
480 bytes = buffer.free; | 479 bytes = buffer.free; |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
562 void _doneHandler() { | 561 void _doneHandler() { |
563 if (_filterReadEmpty) { | 562 if (_filterReadEmpty) { |
564 _close(); | 563 _close(); |
565 } | 564 } |
566 } | 565 } |
567 | 566 |
568 void _errorHandler(e) { | 567 void _errorHandler(e) { |
569 _reportError(e, 'Error on underlying RawSocket'); | 568 _reportError(e, 'Error on underlying RawSocket'); |
570 } | 569 } |
571 | 570 |
572 void _reportError(error, String message) { | 571 void _reportError(e, String message) { |
573 // TODO(whesse): Call _reportError from all internal functions that throw. | 572 // TODO(whesse): Call _reportError from all internal functions that throw. |
574 var e; | 573 if (e is SocketIOException) { |
575 if (error is AsyncError) { | 574 e = new SocketIOException('$message (${e.message})', e.osError); |
576 e = error; | |
577 } else if (error is SocketIOException) { | |
578 e = new SocketIOException('$message (${error.message})', error.osError); | |
579 } else if (error is OSError) { | 575 } else if (error is OSError) { |
580 e = new SocketIOException(message, error); | 576 e = new SocketIOException(message, e); |
581 } else { | 577 } else { |
582 e = new SocketIOException('$message (${error.toString()})', null); | 578 e = new SocketIOException('$message (${e.toString()})', null); |
583 } | 579 } |
584 if (_connectPending) { | 580 if (_connectPending) { |
585 _handshakeComplete.completeError(e); | 581 _handshakeComplete.completeError(e); |
586 } else { | 582 } else { |
587 _controller.addError(e); | 583 _controller.addError(e); |
588 } | 584 } |
589 _close(); | 585 _close(); |
590 } | 586 } |
591 | 587 |
592 void _closeHandler() { | 588 void _closeHandler() { |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
759 void destroy(); | 755 void destroy(); |
760 void handshake(); | 756 void handshake(); |
761 void init(); | 757 void init(); |
762 X509Certificate get peerCertificate; | 758 X509Certificate get peerCertificate; |
763 int processBuffer(int bufferIndex); | 759 int processBuffer(int bufferIndex); |
764 void registerBadCertificateCallback(Function callback); | 760 void registerBadCertificateCallback(Function callback); |
765 void registerHandshakeCompleteCallback(Function handshakeCompleteHandler); | 761 void registerHandshakeCompleteCallback(Function handshakeCompleteHandler); |
766 | 762 |
767 List<_ExternalBuffer> get buffers; | 763 List<_ExternalBuffer> get buffers; |
768 } | 764 } |
OLD | NEW |