| 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 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 227 * object will be [:true:] indicating that from now on the protocol is | 227 * object will be [:true:] indicating that from now on the protocol is |
| 228 * not HTTP anymore and no more callbacks will happen, that is | 228 * not HTTP anymore and no more callbacks will happen, that is |
| 229 * [:dataReceived:] and [:dataEnd:] are not called in this case as | 229 * [:dataReceived:] and [:dataEnd:] are not called in this case as |
| 230 * there is no more HTTP data. After the upgrade the method | 230 * there is no more HTTP data. After the upgrade the method |
| 231 * [:readUnparsedData:] can be used to read any remaining bytes in the | 231 * [:readUnparsedData:] can be used to read any remaining bytes in the |
| 232 * HTTP parser which are part of the protocol the connection is | 232 * HTTP parser which are part of the protocol the connection is |
| 233 * upgrading to. These bytes cannot be processed by the HTTP parser | 233 * upgrading to. These bytes cannot be processed by the HTTP parser |
| 234 * and should be handled according to whatever protocol is being | 234 * and should be handled according to whatever protocol is being |
| 235 * upgraded to. | 235 * upgraded to. |
| 236 */ | 236 */ |
| 237 class _HttpParser | 237 class _HttpParser extends Stream<_HttpIncoming> { |
| 238 extends Stream<_HttpIncoming> | |
| 239 implements StreamConsumer<List<int>> { | |
| 240 // State. | 238 // State. |
| 241 bool _parserCalled = false; | 239 bool _parserCalled = false; |
| 242 | 240 |
| 243 // The data that is currently being parsed. | 241 // The data that is currently being parsed. |
| 244 Uint8List _buffer; | 242 Uint8List _buffer; |
| 245 int _index; | 243 int _index; |
| 246 | 244 |
| 247 final bool _requestParser; | 245 final bool _requestParser; |
| 248 int _state; | 246 int _state; |
| 249 int _httpVersionIndex; | 247 int _httpVersionIndex; |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 308 StreamSubscription<_HttpIncoming> listen(void onData(_HttpIncoming event), | 306 StreamSubscription<_HttpIncoming> listen(void onData(_HttpIncoming event), |
| 309 {Function onError, | 307 {Function onError, |
| 310 void onDone(), | 308 void onDone(), |
| 311 bool cancelOnError}) { | 309 bool cancelOnError}) { |
| 312 return _controller.stream.listen(onData, | 310 return _controller.stream.listen(onData, |
| 313 onError: onError, | 311 onError: onError, |
| 314 onDone: onDone, | 312 onDone: onDone, |
| 315 cancelOnError: cancelOnError); | 313 cancelOnError: cancelOnError); |
| 316 } | 314 } |
| 317 | 315 |
| 318 Future<_HttpParser> addStream(Stream<List<int>> stream) { | 316 void listenToStream(Stream<List<int>> stream) { |
| 319 // Listen to the stream and handle data accordingly. When a | 317 // Listen to the stream and handle data accordingly. When a |
| 320 // _HttpIncoming is created, _dataPause, _dataResume, _dataDone is | 318 // _HttpIncoming is created, _dataPause, _dataResume, _dataDone is |
| 321 // given to provide a way of controlling the parser. | 319 // given to provide a way of controlling the parser. |
| 322 // TODO(ajohnsen): Remove _dataPause, _dataResume and _dataDone and clean up | 320 // TODO(ajohnsen): Remove _dataPause, _dataResume and _dataDone and clean up |
| 323 // how the _HttpIncoming signals the parser. | 321 // how the _HttpIncoming signals the parser. |
| 324 var completer = new Completer(); | |
| 325 _socketSubscription = stream.listen( | 322 _socketSubscription = stream.listen( |
| 326 _onData, | 323 _onData, |
| 327 onError: _onError, | 324 onError: _onError, |
| 328 onDone: () { | 325 onDone: _onDone); |
| 329 completer.complete(this); | |
| 330 }); | |
| 331 return completer.future; | |
| 332 } | |
| 333 | |
| 334 Future<_HttpParser> close() { | |
| 335 _onDone(); | |
| 336 return new Future.value(this); | |
| 337 } | 326 } |
| 338 | 327 |
| 339 void _parse() { | 328 void _parse() { |
| 340 try { | 329 try { |
| 341 _doParse(); | 330 _doParse(); |
| 342 } catch (e, s) { | 331 } catch (e, s) { |
| 343 _state = _State.FAILURE; | 332 _state = _State.FAILURE; |
| 344 _reportError(e, s); | 333 _reportError(e, s); |
| 345 } | 334 } |
| 346 } | 335 } |
| (...skipping 693 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1040 } | 1029 } |
| 1041 } | 1030 } |
| 1042 | 1031 |
| 1043 void _reportError(error, [stackTrace]) { | 1032 void _reportError(error, [stackTrace]) { |
| 1044 if (_socketSubscription != null) _socketSubscription.cancel(); | 1033 if (_socketSubscription != null) _socketSubscription.cancel(); |
| 1045 _state = _State.FAILURE; | 1034 _state = _State.FAILURE; |
| 1046 _controller.addError(error, stackTrace); | 1035 _controller.addError(error, stackTrace); |
| 1047 _controller.close(); | 1036 _controller.close(); |
| 1048 } | 1037 } |
| 1049 } | 1038 } |
| OLD | NEW |