Line data Source code
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 : import 'dart:async';
6 :
7 : import 'byte_stream.dart';
8 : import 'base_request.dart';
9 :
10 : /// An HTTP request where the request body is sent asynchronously after the
11 : /// connection has been established and the headers have been sent.
12 : ///
13 : /// When the request is sent via [BaseClient.send], only the headers and
14 : /// whatever data has already been written to [StreamedRequest.stream] will be
15 : /// sent immediately. More data will be sent as soon as it's written to
16 : /// [StreamedRequest.sink], and when the sink is closed the request will end.
17 : class StreamedRequest extends BaseRequest {
18 : /// The sink to which to write data that will be sent as the request body.
19 : /// This may be safely written to before the request is sent; the data will be
20 : /// buffered.
21 : ///
22 : /// Closing this signals the end of the request.
23 0 : EventSink<List<int>> get sink => _controller.sink;
24 :
25 : /// The controller for [sink], from which [BaseRequest] will read data for
26 : /// [finalize].
27 : final StreamController<List<int>> _controller;
28 :
29 : /// Creates a new streaming request.
30 : StreamedRequest(String method, Uri url)
31 0 : : _controller = new StreamController<List<int>>(sync: true),
32 0 : super(method, url);
33 :
34 : /// Freezes all mutable fields other than [stream] and returns a
35 : /// single-subscription [ByteStream] that emits the data being written to
36 : /// [sink].
37 : ByteStream finalize() {
38 0 : super.finalize();
39 0 : return new ByteStream(_controller.stream);
40 : }
41 : }
|