| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 library streamed_request; | 5 library streamed_request; |
| 6 | 6 |
| 7 import 'dart:io'; | 7 import 'dart:io'; |
| 8 import 'dart:uri'; | 8 import 'dart:uri'; |
| 9 | 9 |
| 10 import 'base_request.dart'; | 10 import 'base_request.dart'; |
| 11 | 11 |
| 12 /// An HTTP request where the request body is sent asynchronously after the | 12 /// An HTTP request where the request body is sent asynchronously after the |
| 13 /// connection has been established and the headers have been sent. | 13 /// connection has been established and the headers have been sent. |
| 14 /// | 14 /// |
| 15 /// When the request is sent via [BaseClient.send], only the headers and | 15 /// When the request is sent via [BaseClient.send], only the headers and |
| 16 /// whatever data has already been written to [StreamedRequest.stream] will be | 16 /// whatever data has already been written to [StreamedRequest.stream] will be |
| 17 /// sent immediately. More data will be sent as soon as it's written to | 17 /// sent immediately. More data will be sent as soon as it's written to |
| 18 /// [StreamedRequest.stream], and when the stream is closed the request will | 18 /// [StreamedRequest.stream], and when the stream is closed the request will |
| 19 /// end. | 19 /// end. |
| 20 class StreamedRequest extends BaseRequest { | 20 class StreamedRequest extends BaseRequest { |
| 21 /// The stream to which to write data that will be sent as the request body. | 21 /// The stream to which to write data that will be sent as the request body. |
| 22 /// This may be safely written to before the request is sent; the data will be | 22 /// This may be safely written to before the request is sent; the data will be |
| 23 /// buffered. | 23 /// buffered. |
| 24 /// | 24 /// |
| 25 /// Closing this signals the end of the request. | 25 /// Closing this signals the end of the request. |
| 26 OutputStream get stream => _outputStream; | 26 final OutputStream stream; |
| 27 | |
| 28 /// [stream], stored as a [ListOutputStream]. | |
| 29 final ListOutputStream _outputStream; | |
| 30 | 27 |
| 31 /// The stream from which the [BaseClient] will read the data in [stream] once | 28 /// The stream from which the [BaseClient] will read the data in [stream] once |
| 32 /// the request has been finalized. | 29 /// the request has been finalized. |
| 33 final ListInputStream _inputStream; | 30 final ListInputStream _inputStream; |
| 34 | 31 |
| 35 /// Creates a new streaming request. | 32 /// Creates a new streaming request. |
| 36 StreamedRequest(String method, Uri url) | 33 StreamedRequest(String method, Uri url) |
| 37 : super(method, url), | 34 : super(method, url), |
| 38 _outputStream = new ListOutputStream(), | 35 stream = new ListOutputStream(), |
| 39 _inputStream = new ListInputStream() { | 36 _inputStream = new ListInputStream() { |
| 37 ListOutputStream outputStream = stream; |
| 40 // TODO(nweiz): pipe errors from the output stream to the input stream once | 38 // TODO(nweiz): pipe errors from the output stream to the input stream once |
| 41 // issue 3657 is fixed | 39 // issue 3657 is fixed |
| 42 _outputStream.onData = () => _inputStream.write(_outputStream.read()); | 40 outputStream.onData = () => _inputStream.write(outputStream.read()); |
| 43 _outputStream.onClosed = _inputStream.markEndOfStream; | 41 outputStream.onClosed = _inputStream.markEndOfStream; |
| 44 } | 42 } |
| 45 | 43 |
| 46 /// Freezes all mutable fields other than [stream] and returns an [InputStream
] | 44 /// Freezes all mutable fields other than [stream] and returns an [InputStream
] |
| 47 /// that emits the data being written to [stream]. | 45 /// that emits the data being written to [stream]. |
| 48 InputStream finalize() { | 46 InputStream finalize() { |
| 49 super.finalize(); | 47 super.finalize(); |
| 50 return _inputStream; | 48 return _inputStream; |
| 51 } | 49 } |
| 52 } | 50 } |
| OLD | NEW |