| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 library streamed_request; | |
| 6 | |
| 7 import 'dart:io'; | |
| 8 import 'dart:uri'; | |
| 9 | |
| 10 import 'base_request.dart'; | |
| 11 | |
| 12 /// An HTTP request where the request body is sent asynchronously after the | |
| 13 /// connection has been established and the headers have been sent. | |
| 14 /// | |
| 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 | |
| 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 | |
| 19 /// end. | |
| 20 class StreamedRequest extends BaseRequest { | |
| 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 | |
| 23 /// buffered. | |
| 24 /// | |
| 25 /// Closing this signals the end of the request. | |
| 26 final OutputStream stream; | |
| 27 | |
| 28 /// The stream from which the [BaseClient] will read the data in [stream] once | |
| 29 /// the request has been finalized. | |
| 30 final ListInputStream _inputStream; | |
| 31 | |
| 32 /// Creates a new streaming request. | |
| 33 StreamedRequest(String method, Uri url) | |
| 34 : super(method, url), | |
| 35 stream = new ListOutputStream(), | |
| 36 _inputStream = new ListInputStream() { | |
| 37 // TODO(nweiz): pipe errors from the output stream to the input stream once | |
| 38 // issue 3657 is fixed | |
| 39 stream.onData = () => _inputStream.write(stream.read()); | |
| 40 stream.onClosed = _inputStream.markEndOfStream; | |
| 41 } | |
| 42 | |
| 43 /// Freezes all mutable fields other than [stream] and returns an [InputStream
] | |
| 44 /// that emits the data being written to [stream]. | |
| 45 InputStream finalize() { | |
| 46 super.finalize(); | |
| 47 return _inputStream; | |
| 48 } | |
| 49 } | |
| OLD | NEW |