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