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 // Global constants. | 7 // Global constants. |
8 class _Const { | 8 class _Const { |
9 // Bytes for "HTTP". | 9 // Bytes for "HTTP". |
10 static const HTTP = const [72, 84, 84, 80]; | 10 static const HTTP = const [72, 84, 84, 80]; |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 pause(); | 116 pause(); |
117 if (oldResumeCompleter != null) oldResumeCompleter.complete(); | 117 if (oldResumeCompleter != null) oldResumeCompleter.complete(); |
118 subscription.resume(); | 118 subscription.resume(); |
119 subscription.onData(controller.add); | 119 subscription.onData(controller.add); |
120 subscription.onDone(controller.close); | 120 subscription.onDone(controller.close); |
121 subscription.onError(controller.addError); | 121 subscription.onError(controller.addError); |
122 } | 122 } |
123 } | 123 } |
124 | 124 |
125 StreamSubscription<List<int>> listen(void onData(List<int> event), | 125 StreamSubscription<List<int>> listen(void onData(List<int> event), |
126 {void onError(AsyncError error), | 126 {void onError(error), |
127 void onDone(), | 127 void onDone(), |
128 bool unsubscribeOnError}) { | 128 bool unsubscribeOnError}) { |
129 return controller.stream.listen( | 129 return controller.stream.listen( |
130 onData, | 130 onData, |
131 onError: onError, | 131 onError: onError, |
132 onDone: onDone, | 132 onDone: onDone, |
133 unsubscribeOnError: unsubscribeOnError); | 133 unsubscribeOnError: unsubscribeOnError); |
134 } | 134 } |
135 | 135 |
136 void resume() { | 136 void resume() { |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
209 | 209 |
210 _HttpParser._(this._requestParser) { | 210 _HttpParser._(this._requestParser) { |
211 _controller = new StreamController<_HttpIncoming>( | 211 _controller = new StreamController<_HttpIncoming>( |
212 onSubscriptionStateChange: _updateParsePauseState, | 212 onSubscriptionStateChange: _updateParsePauseState, |
213 onPauseStateChange: _updateParsePauseState); | 213 onPauseStateChange: _updateParsePauseState); |
214 _reset(); | 214 _reset(); |
215 } | 215 } |
216 | 216 |
217 | 217 |
218 StreamSubscription<_HttpIncoming> listen(void onData(_HttpIncoming event), | 218 StreamSubscription<_HttpIncoming> listen(void onData(_HttpIncoming event), |
219 {void onError(AsyncError error), | 219 {void onError(error), |
220 void onDone(), | 220 void onDone(), |
221 bool unsubscribeOnError}) { | 221 bool unsubscribeOnError}) { |
222 return _controller.stream.listen(onData, | 222 return _controller.stream.listen(onData, |
223 onError: onError, | 223 onError: onError, |
224 onDone: onDone, | 224 onDone: onDone, |
225 unsubscribeOnError: unsubscribeOnError); | 225 unsubscribeOnError: unsubscribeOnError); |
226 } | 226 } |
227 | 227 |
228 Future<_HttpParser> addStream(Stream<List<int>> stream) { | 228 Future<_HttpParser> addStream(Stream<List<int>> stream) { |
229 // Listen to the stream and handle data accordingly. When a | 229 // Listen to the stream and handle data accordingly. When a |
(...skipping 444 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
674 break; | 674 break; |
675 | 675 |
676 default: | 676 default: |
677 // Should be unreachable. | 677 // Should be unreachable. |
678 assert(false); | 678 assert(false); |
679 break; | 679 break; |
680 } | 680 } |
681 } | 681 } |
682 } catch (e, s) { | 682 } catch (e, s) { |
683 _state = _State.FAILURE; | 683 _state = _State.FAILURE; |
684 error(new AsyncError(e, s)); | 684 error(e, s); |
685 } | 685 } |
686 | 686 |
687 _parserCalled = false; | 687 _parserCalled = false; |
688 if (_buffer != null && _index == _buffer.length) { | 688 if (_buffer != null && _index == _buffer.length) { |
689 // If all data is parsed release the buffer and resume receiving | 689 // If all data is parsed release the buffer and resume receiving |
690 // data. | 690 // data. |
691 _releaseBuffer(); | 691 _releaseBuffer(); |
692 if (_state != _State.UPGRADED && _state != _State.FAILURE) { | 692 if (_state != _State.UPGRADED && _state != _State.FAILURE) { |
693 _socketSubscription.resume(); | 693 _socketSubscription.resume(); |
694 } | 694 } |
(...skipping 11 matching lines...) Expand all Loading... |
706 void _onDone() { | 706 void _onDone() { |
707 // onDone cancles the subscription. | 707 // onDone cancles the subscription. |
708 _socketSubscription = null; | 708 _socketSubscription = null; |
709 if (_state == _State.CLOSED || _state == _State.FAILURE) return; | 709 if (_state == _State.CLOSED || _state == _State.FAILURE) return; |
710 | 710 |
711 if (_incoming != null) { | 711 if (_incoming != null) { |
712 if (_state != _State.UPGRADED && | 712 if (_state != _State.UPGRADED && |
713 !(_state == _State.START && !_requestParser) && | 713 !(_state == _State.START && !_requestParser) && |
714 !(_state == _State.BODY && !_chunked && _transferLength == -1)) { | 714 !(_state == _State.BODY && !_chunked && _transferLength == -1)) { |
715 _bodyController.addError( | 715 _bodyController.addError( |
716 new AsyncError( | 716 new HttpParserException( |
717 new HttpParserException( | 717 "Connection closed while receiving data")); |
718 "Connection closed while receiving data"))); | |
719 } | 718 } |
720 _closeIncoming(); | 719 _closeIncoming(); |
721 _controller.close(); | 720 _controller.close(); |
722 return; | 721 return; |
723 } | 722 } |
724 // If the connection is idle the HTTP stream is closed. | 723 // If the connection is idle the HTTP stream is closed. |
725 if (_state == _State.START) { | 724 if (_state == _State.START) { |
726 if (!_requestParser) { | 725 if (!_requestParser) { |
727 error( | 726 error(new HttpParserException( |
728 new AsyncError( | 727 "Connection closed before full header was received")); |
729 new HttpParserException( | |
730 "Connection closed before full header was received"))); | |
731 } | 728 } |
732 _controller.close(); | 729 _controller.close(); |
733 return; | 730 return; |
734 } | 731 } |
735 | 732 |
736 if (_state == _State.UPGRADED) { | 733 if (_state == _State.UPGRADED) { |
737 _controller.close(); | 734 _controller.close(); |
738 return; | 735 return; |
739 } | 736 } |
740 | 737 |
741 if (_state < _State.FIRST_BODY_STATE) { | 738 if (_state < _State.FIRST_BODY_STATE) { |
742 _state = _State.FAILURE; | 739 _state = _State.FAILURE; |
743 // Report the error through the error callback if any. Otherwise | 740 // Report the error through the error callback if any. Otherwise |
744 // throw the error. | 741 // throw the error. |
745 error( | 742 error(new HttpParserException( |
746 new AsyncError( | 743 "Connection closed before full header was received")); |
747 new HttpParserException( | |
748 "Connection closed before full header was received"))); | |
749 _controller.close(); | 744 _controller.close(); |
750 return; | 745 return; |
751 } | 746 } |
752 | 747 |
753 if (!_chunked && _transferLength == -1) { | 748 if (!_chunked && _transferLength == -1) { |
754 _state = _State.CLOSED; | 749 _state = _State.CLOSED; |
755 } else { | 750 } else { |
756 _state = _State.FAILURE; | 751 _state = _State.FAILURE; |
757 // Report the error through the error callback if any. Otherwise | 752 // Report the error through the error callback if any. Otherwise |
758 // throw the error. | 753 // throw the error. |
759 error( | 754 error(new HttpParserException( |
760 new AsyncError( | 755 "Connection closed before full body was received")); |
761 new HttpParserException( | |
762 "Connection closed before full body was received"))); | |
763 } | 756 } |
764 _controller.close(); | 757 _controller.close(); |
765 } | 758 } |
766 | 759 |
767 void _onError(e) { | 760 void _onError(e) { |
768 _controller.addError(e); | 761 _controller.addError(e); |
769 } | 762 } |
770 | 763 |
771 String get version { | 764 String get version { |
772 switch (_httpVersion) { | 765 switch (_httpVersion) { |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
924 } | 917 } |
925 } else { | 918 } else { |
926 if (_controller.hasListener && !_controller.isPaused) { | 919 if (_controller.hasListener && !_controller.isPaused) { |
927 _continueParsing(); | 920 _continueParsing(); |
928 } else { | 921 } else { |
929 _pauseParsing(); | 922 _pauseParsing(); |
930 } | 923 } |
931 } | 924 } |
932 } | 925 } |
933 | 926 |
934 void error(error) { | 927 void error(error, [stackTrace]) { |
935 if (_socketSubscription != null) _socketSubscription.cancel(); | 928 if (_socketSubscription != null) _socketSubscription.cancel(); |
936 _state = _State.FAILURE; | 929 _state = _State.FAILURE; |
937 _controller.addError(error); | 930 _controller.addError(error, stackTrace); |
938 _controller.close(); | 931 _controller.close(); |
939 } | 932 } |
940 | 933 |
941 // State. | 934 // State. |
942 bool _parserCalled = false; | 935 bool _parserCalled = false; |
943 | 936 |
944 // The data that is currently being parsed. | 937 // The data that is currently being parsed. |
945 List<int> _buffer; | 938 List<int> _buffer; |
946 int _index; | 939 int _index; |
947 | 940 |
(...skipping 27 matching lines...) Expand all Loading... |
975 StreamController<_HttpIncoming> _controller; | 968 StreamController<_HttpIncoming> _controller; |
976 StreamController<List<int>> _bodyController; | 969 StreamController<List<int>> _bodyController; |
977 } | 970 } |
978 | 971 |
979 | 972 |
980 class HttpParserException implements Exception { | 973 class HttpParserException implements Exception { |
981 const HttpParserException([String this.message = ""]); | 974 const HttpParserException([String this.message = ""]); |
982 String toString() => "HttpParserException: $message"; | 975 String toString() => "HttpParserException: $message"; |
983 final String message; | 976 final String message; |
984 } | 977 } |
OLD | NEW |