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 596 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
607 ((_secureFilter != null && | 607 ((_secureFilter != null && |
608 _secureFilter.buffers[READ_PLAINTEXT].length > 0) || | 608 _secureFilter.buffers[READ_PLAINTEXT].length > 0) || |
609 _socketClosedRead)) { | 609 _socketClosedRead)) { |
610 // We might not have no underlying socket to set off read events. | 610 // We might not have no underlying socket to set off read events. |
611 Timer.run(_readHandler); | 611 Timer.run(_readHandler); |
612 } | 612 } |
613 } | 613 } |
614 | 614 |
615 List<int> read([int len]) { | 615 List<int> read([int len]) { |
616 if (_closedRead) { | 616 if (_closedRead) { |
617 throw new SocketIOException("Reading from a closed socket"); | 617 throw new SocketException("Reading from a closed socket"); |
618 } | 618 } |
619 if (_status != CONNECTED) { | 619 if (_status != CONNECTED) { |
620 return null; | 620 return null; |
621 } | 621 } |
622 var buffer = _secureFilter.buffers[READ_PLAINTEXT]; | 622 var buffer = _secureFilter.buffers[READ_PLAINTEXT]; |
623 _readEncryptedData(); | 623 _readEncryptedData(); |
624 int toRead = buffer.length; | 624 int toRead = buffer.length; |
625 if (len != null) { | 625 if (len != null) { |
626 if (len is! int || len < 0) { | 626 if (len is! int || len < 0) { |
627 throw new ArgumentError( | 627 throw new ArgumentError( |
(...skipping 28 matching lines...) Expand all Loading... |
656 } | 656 } |
657 | 657 |
658 return result; | 658 return result; |
659 } | 659 } |
660 | 660 |
661 // Write the data to the socket, and flush it as much as possible | 661 // Write the data to the socket, and flush it as much as possible |
662 // until it would block. If the write would block, _writeEncryptedData sets | 662 // until it would block. If the write would block, _writeEncryptedData sets |
663 // up handlers to flush the pipeline when possible. | 663 // up handlers to flush the pipeline when possible. |
664 int write(List<int> data, [int offset, int bytes]) { | 664 int write(List<int> data, [int offset, int bytes]) { |
665 if (_closedWrite) { | 665 if (_closedWrite) { |
666 _controller.addError(new SocketIOException("Writing to a closed socket")); | 666 _controller.addError(new SocketException("Writing to a closed socket")); |
667 return 0; | 667 return 0; |
668 } | 668 } |
669 if (_status != CONNECTED) return 0; | 669 if (_status != CONNECTED) return 0; |
670 | 670 |
671 if (offset == null) offset = 0; | 671 if (offset == null) offset = 0; |
672 if (bytes == null) bytes = data.length - offset; | 672 if (bytes == null) bytes = data.length - offset; |
673 | 673 |
674 var buffer = _secureFilter.buffers[WRITE_PLAINTEXT]; | 674 var buffer = _secureFilter.buffers[WRITE_PLAINTEXT]; |
675 if (bytes > buffer.free) { | 675 if (bytes > buffer.free) { |
676 bytes = buffer.free; | 676 bytes = buffer.free; |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
743 if (_status == CLOSED) { | 743 if (_status == CLOSED) { |
744 return; | 744 return; |
745 } else if (_status == HANDSHAKE) { | 745 } else if (_status == HANDSHAKE) { |
746 try { | 746 try { |
747 _secureHandshake(); | 747 _secureHandshake(); |
748 if (_status != HANDSHAKE) _readHandler(); | 748 if (_status != HANDSHAKE) _readHandler(); |
749 } catch (e) { _reportError(e, "RawSecureSocket error"); } | 749 } catch (e) { _reportError(e, "RawSecureSocket error"); } |
750 } else { | 750 } else { |
751 if (_status != CONNECTED) { | 751 if (_status != CONNECTED) { |
752 // Cannot happen. | 752 // Cannot happen. |
753 throw new SocketIOException("Internal SocketIO Error"); | 753 throw new SocketException("Internal SocketIO Error"); |
754 } | 754 } |
755 try { | 755 try { |
756 _readEncryptedData(); | 756 _readEncryptedData(); |
757 } catch (e) { _reportError(e, "RawSecureSocket error"); } | 757 } catch (e) { _reportError(e, "RawSecureSocket error"); } |
758 if (!_filterReadEmpty) { | 758 if (!_filterReadEmpty) { |
759 if (_readEventsEnabled) { | 759 if (_readEventsEnabled) { |
760 if (_secureFilter.buffers[READ_PLAINTEXT].length > 0) { | 760 if (_secureFilter.buffers[READ_PLAINTEXT].length > 0) { |
761 _controller.add(RawSocketEvent.READ); | 761 _controller.add(RawSocketEvent.READ); |
762 } | 762 } |
763 if (_socketClosedRead) { | 763 if (_socketClosedRead) { |
(...skipping 12 matching lines...) Expand all Loading... |
776 _close(); | 776 _close(); |
777 } | 777 } |
778 } | 778 } |
779 | 779 |
780 void _errorHandler(e) { | 780 void _errorHandler(e) { |
781 _reportError(e, 'Error on underlying RawSocket'); | 781 _reportError(e, 'Error on underlying RawSocket'); |
782 } | 782 } |
783 | 783 |
784 void _reportError(e, String message) { | 784 void _reportError(e, String message) { |
785 // TODO(whesse): Call _reportError from all internal functions that throw. | 785 // TODO(whesse): Call _reportError from all internal functions that throw. |
786 if (e is SocketIOException) { | 786 if (e is SocketException) { |
787 e = new SocketIOException('$message (${e.message})', e.osError); | 787 e = new SocketException('$message (${e.message})', e.osError); |
788 } else if (e is OSError) { | 788 } else if (e is OSError) { |
789 e = new SocketIOException(message, e); | 789 e = new SocketException(message, e); |
790 } else { | 790 } else { |
791 e = new SocketIOException('$message (${e.toString()})', null); | 791 e = new SocketException('$message (${e.toString()})', null); |
792 } | 792 } |
793 if (_connectPending) { | 793 if (_connectPending) { |
794 _handshakeComplete.completeError(e); | 794 _handshakeComplete.completeError(e); |
795 } else { | 795 } else { |
796 _controller.addError(e); | 796 _controller.addError(e); |
797 } | 797 } |
798 _close(); | 798 _close(); |
799 } | 799 } |
800 | 800 |
801 void _closeHandler() { | 801 void _closeHandler() { |
802 if (_status == CONNECTED) { | 802 if (_status == CONNECTED) { |
803 if (_closedRead) return; | 803 if (_closedRead) return; |
804 _socketClosedRead = true; | 804 _socketClosedRead = true; |
805 if (_filterReadEmpty) { | 805 if (_filterReadEmpty) { |
806 _closedRead = true; | 806 _closedRead = true; |
807 _controller.add(RawSocketEvent.READ_CLOSED); | 807 _controller.add(RawSocketEvent.READ_CLOSED); |
808 if (_socketClosedWrite) { | 808 if (_socketClosedWrite) { |
809 _close(); | 809 _close(); |
810 } | 810 } |
811 } | 811 } |
812 } else if (_status == HANDSHAKE) { | 812 } else if (_status == HANDSHAKE) { |
813 _reportError( | 813 _reportError( |
814 new SocketIOException('Connection terminated during handshake'), | 814 new SocketException('Connection terminated during handshake'), |
815 'handshake error'); | 815 'handshake error'); |
816 } | 816 } |
817 } | 817 } |
818 | 818 |
819 void _secureHandshake() { | 819 void _secureHandshake() { |
820 _readEncryptedData(); | 820 _readEncryptedData(); |
821 _secureFilter.handshake(); | 821 _secureFilter.handshake(); |
822 _writeEncryptedData(); | 822 _writeEncryptedData(); |
823 } | 823 } |
824 | 824 |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
973 void destroy(); | 973 void destroy(); |
974 void handshake(); | 974 void handshake(); |
975 void init(); | 975 void init(); |
976 X509Certificate get peerCertificate; | 976 X509Certificate get peerCertificate; |
977 int processBuffer(int bufferIndex); | 977 int processBuffer(int bufferIndex); |
978 void registerBadCertificateCallback(Function callback); | 978 void registerBadCertificateCallback(Function callback); |
979 void registerHandshakeCompleteCallback(Function handshakeCompleteHandler); | 979 void registerHandshakeCompleteCallback(Function handshakeCompleteHandler); |
980 | 980 |
981 List<_ExternalBuffer> get buffers; | 981 List<_ExternalBuffer> get buffers; |
982 } | 982 } |
OLD | NEW |