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 721 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
732 case _State.CHUNKED_BODY_DONE_LF: | 732 case _State.CHUNKED_BODY_DONE_LF: |
733 _expect(byte, _CharCode.LF); | 733 _expect(byte, _CharCode.LF); |
734 _reset(); | 734 _reset(); |
735 _closeIncoming(); | 735 _closeIncoming(); |
736 break; | 736 break; |
737 | 737 |
738 case _State.BODY: | 738 case _State.BODY: |
739 // The body is not handled one byte at a time but in blocks. | 739 // The body is not handled one byte at a time but in blocks. |
740 _index--; | 740 _index--; |
741 int dataAvailable = _buffer.length - _index; | 741 int dataAvailable = _buffer.length - _index; |
742 List<int> data; | 742 if (_remainingContent >= 0 && dataAvailable > _remainingContent) { |
743 if (_remainingContent == -1 || | 743 dataAvailable = _remainingContent; |
744 dataAvailable <= _remainingContent) { | |
745 if (_index == 0) { | |
746 data = _buffer; | |
747 } else { | |
748 data = new Uint8List.view(_buffer.buffer, | |
749 _index, | |
750 dataAvailable); | |
751 } | |
752 } else { | |
753 data = new Uint8List.view(_buffer.buffer, | |
754 _index, | |
755 _remainingContent); | |
756 } | 744 } |
| 745 // Always present the data as a view. This way we can handle all |
| 746 // cases like this, and the user will not experince different data |
| 747 // typed (which could lead to polymorphic user code). |
| 748 List<int> data = new Uint8List.view(_buffer.buffer, |
| 749 _buffer.offsetInBytes + _index, |
| 750 dataAvailable); |
757 _bodyController.add(data); | 751 _bodyController.add(data); |
758 if (_remainingContent != -1) { | 752 if (_remainingContent != -1) { |
759 _remainingContent -= data.length; | 753 _remainingContent -= data.length; |
760 } | 754 } |
761 _index += data.length; | 755 _index += data.length; |
762 if (_remainingContent == 0) { | 756 if (_remainingContent == 0) { |
763 if (!_chunked) { | 757 if (!_chunked) { |
764 _reset(); | 758 _reset(); |
765 _closeIncoming(); | 759 _closeIncoming(); |
766 } else { | 760 } else { |
(...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1029 } | 1023 } |
1030 } | 1024 } |
1031 | 1025 |
1032 void _reportError(error, [stackTrace]) { | 1026 void _reportError(error, [stackTrace]) { |
1033 if (_socketSubscription != null) _socketSubscription.cancel(); | 1027 if (_socketSubscription != null) _socketSubscription.cancel(); |
1034 _state = _State.FAILURE; | 1028 _state = _State.FAILURE; |
1035 _controller.addError(error, stackTrace); | 1029 _controller.addError(error, stackTrace); |
1036 _controller.close(); | 1030 _controller.close(); |
1037 } | 1031 } |
1038 } | 1032 } |
OLD | NEW |