OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 library mime.bound_multipart_stream; | 4 library mime.bound_multipart_stream; |
5 | 5 |
6 import 'dart:async'; | 6 import 'dart:async'; |
7 import 'dart:convert'; | 7 import 'dart:convert'; |
8 | 8 |
9 import 'mime_shared.dart'; | 9 import 'mime_shared.dart'; |
10 import 'char_code.dart'; | 10 import 'char_code.dart'; |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 | 98 |
99 // Current index in the data buffer. If index is negative then it | 99 // Current index in the data buffer. If index is negative then it |
100 // is the index into the artificial prefix of the boundary string. | 100 // is the index into the artificial prefix of the boundary string. |
101 int _index; | 101 int _index; |
102 List<int> _buffer; | 102 List<int> _buffer; |
103 | 103 |
104 BoundMultipartStream(this._boundary, Stream<List<int>> stream) { | 104 BoundMultipartStream(this._boundary, Stream<List<int>> stream) { |
105 _controller = new StreamController( | 105 _controller = new StreamController( |
106 sync: true, | 106 sync: true, |
107 onPause: _pauseStream, | 107 onPause: _pauseStream, |
108 onResume:_resumeStream, | 108 onResume: _resumeStream, |
109 onCancel: () { | 109 onCancel: () { |
110 _controllerState = _CONTROLLER_STATE_CANCELED; | 110 _controllerState = _CONTROLLER_STATE_CANCELED; |
111 _tryPropagateControllerState(); | 111 _tryPropagateControllerState(); |
112 }, | 112 }, |
113 onListen: () { | 113 onListen: () { |
114 _controllerState = _CONTROLLER_STATE_ACTIVE; | 114 _controllerState = _CONTROLLER_STATE_ACTIVE; |
115 _subscription = stream.listen( | 115 _subscription = stream.listen( |
116 (data) { | 116 (data) { |
117 assert(_buffer == null); | 117 assert(_buffer == null); |
118 _subscription.pause(); | 118 _subscription.pause(); |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
196 if (_state == _CONTENT && _boundaryIndex == 0) { | 196 if (_state == _CONTENT && _boundaryIndex == 0) { |
197 contentStartIndex = 0; | 197 contentStartIndex = 0; |
198 } else { | 198 } else { |
199 contentStartIndex = null; | 199 contentStartIndex = null; |
200 } | 200 } |
201 // The data to parse might be "artificially" prefixed with a | 201 // The data to parse might be "artificially" prefixed with a |
202 // partial match of the boundary. | 202 // partial match of the boundary. |
203 boundaryPrefix = _boundaryIndex; | 203 boundaryPrefix = _boundaryIndex; |
204 | 204 |
205 while ((_index < _buffer.length) && _state != _FAIL && _state != _DONE) { | 205 while ((_index < _buffer.length) && _state != _FAIL && _state != _DONE) { |
206 if (_multipartController != null && _multipartController.isPaused) { | |
207 return; | |
208 } | |
209 int byte; | 206 int byte; |
210 if (_index < 0) { | 207 if (_index < 0) { |
211 byte = _boundary[boundaryPrefix + _index]; | 208 byte = _boundary[boundaryPrefix + _index]; |
212 } else { | 209 } else { |
213 byte = _buffer[_index]; | 210 byte = _buffer[_index]; |
214 } | 211 } |
215 switch (_state) { | 212 switch (_state) { |
216 case _START: | 213 case _START: |
217 if (byte == _boundary[_boundaryIndex]) { | 214 if (byte == _boundary[_boundaryIndex]) { |
218 _boundaryIndex++; | 215 _boundaryIndex++; |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
308 _headerField.add(_toLowerCase(byte)); | 305 _headerField.add(_toLowerCase(byte)); |
309 _state = _HEADER_FIELD; | 306 _state = _HEADER_FIELD; |
310 } | 307 } |
311 } | 308 } |
312 break; | 309 break; |
313 | 310 |
314 case _HEADER_ENDING: | 311 case _HEADER_ENDING: |
315 _expectByteValue(byte, CharCode.LF); | 312 _expectByteValue(byte, CharCode.LF); |
316 _multipartController = new StreamController( | 313 _multipartController = new StreamController( |
317 sync: true, | 314 sync: true, |
| 315 onListen: () { |
| 316 if (_subscription.isPaused) _subscription.resume(); |
| 317 }, |
318 onPause: _subscription.pause, | 318 onPause: _subscription.pause, |
319 onResume: () { | 319 onResume: _subscription.resume); |
320 _subscription.resume(); | |
321 _parse(); | |
322 }); | |
323 _controller.add( | 320 _controller.add( |
324 new _MimeMultipart(_headers, _multipartController.stream)); | 321 new _MimeMultipart(_headers, _multipartController.stream)); |
325 _headers = null; | 322 _headers = null; |
326 _state = _CONTENT; | 323 _state = _CONTENT; |
327 contentStartIndex = _index + 1; | 324 contentStartIndex = _index + 1; |
328 break; | 325 break; |
329 | 326 |
330 case _CONTENT: | 327 case _CONTENT: |
331 if (byte == _boundary[_boundaryIndex]) { | 328 if (byte == _boundary[_boundaryIndex]) { |
332 _boundaryIndex++; | 329 _boundaryIndex++; |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
389 } | 386 } |
390 | 387 |
391 // Resume if at end. | 388 // Resume if at end. |
392 if (_index == _buffer.length) { | 389 if (_index == _buffer.length) { |
393 _buffer = null; | 390 _buffer = null; |
394 _index = null; | 391 _index = null; |
395 _subscription.resume(); | 392 _subscription.resume(); |
396 } | 393 } |
397 } | 394 } |
398 } | 395 } |
OLD | NEW |