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 base_request; | 5 library base_request; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:collection'; | 8 import 'dart:collection'; |
9 | 9 |
10 import 'byte_stream.dart'; | 10 import 'byte_stream.dart'; |
11 import 'client.dart'; | 11 import 'client.dart'; |
12 import 'streamed_response.dart'; | 12 import 'streamed_response.dart'; |
13 import 'utils.dart'; | 13 import 'utils.dart'; |
14 | 14 |
15 /// The base class for HTTP requests. | 15 /// The base class for HTTP requests. |
16 /// | 16 /// |
17 /// Subclasses of [BaseRequest] can be constructed manually and passed to | 17 /// Subclasses of [BaseRequest] can be constructed manually and passed to |
18 /// [BaseClient.send], which allows the user to provide fine-grained control | 18 /// [BaseClient.send], which allows the user to provide fine-grained control |
19 /// over the request properties. However, usually it's easier to use convenience | 19 /// over the request properties. However, usually it's easier to use convenience |
20 /// methods like [get] or [BaseClient.get]. | 20 /// methods like [get] or [BaseClient.get]. |
21 abstract class BaseRequest { | 21 abstract class BaseRequest { |
22 /// The HTTP method of the request. Most commonly "GET" or "POST", less | 22 /// The HTTP method of the request. Most commonly "GET" or "POST", less |
23 /// commonly "HEAD", "PUT", or "DELETE". Non-standard method names are also | 23 /// commonly "HEAD", "PUT", or "DELETE". Non-standard method names are also |
24 /// supported. | 24 /// supported. |
25 final String method; | 25 final String method; |
26 | 26 |
27 /// The URL to which the request will be sent. | 27 /// The URL to which the request will be sent. |
28 final Uri url; | 28 final Uri url; |
29 | 29 |
30 /// The size of the request body, in bytes. This defaults to -1, which | 30 /// The size of the request body, in bytes. |
31 /// indicates that the size of the request is not known in advance. | 31 /// |
| 32 /// This defaults to `null`, which indicates that the size of the request is |
| 33 /// not known in advance. |
32 int get contentLength => _contentLength; | 34 int get contentLength => _contentLength; |
33 int _contentLength = -1; | 35 int _contentLength; |
34 | 36 |
35 set contentLength(int value) { | 37 set contentLength(int value) { |
| 38 if (value != null && value < 0) { |
| 39 throw new ArgumentError("Invalid content length $value."); |
| 40 } |
36 _checkFinalized(); | 41 _checkFinalized(); |
37 _contentLength = value; | 42 _contentLength = value; |
38 } | 43 } |
39 | 44 |
40 /// Whether a persistent connection should be maintained with the server. | 45 /// Whether a persistent connection should be maintained with the server. |
41 /// Defaults to true. | 46 /// Defaults to true. |
42 bool get persistentConnection => _persistentConnection; | 47 bool get persistentConnection => _persistentConnection; |
43 bool _persistentConnection = true; | 48 bool _persistentConnection = true; |
44 | 49 |
45 set persistentConnection(bool value) { | 50 set persistentConnection(bool value) { |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 /// Sends this request. | 108 /// Sends this request. |
104 /// | 109 /// |
105 /// This automatically initializes a new [Client] and closes that client once | 110 /// This automatically initializes a new [Client] and closes that client once |
106 /// the request is complete. If you're planning on making multiple requests to | 111 /// the request is complete. If you're planning on making multiple requests to |
107 /// the same server, you should use a single [Client] for all of those | 112 /// the same server, you should use a single [Client] for all of those |
108 /// requests. | 113 /// requests. |
109 Future<StreamedResponse> send() { | 114 Future<StreamedResponse> send() { |
110 var client = new Client(); | 115 var client = new Client(); |
111 return client.send(this).then((response) { | 116 return client.send(this).then((response) { |
112 var stream = onDone(response.stream, client.close); | 117 var stream = onDone(response.stream, client.close); |
| 118 var contentLength = response.contentLength < 0 ? |
| 119 null : response.contentLength; |
113 return new StreamedResponse( | 120 return new StreamedResponse( |
114 new ByteStream(stream), | 121 new ByteStream(stream), |
115 response.statusCode, | 122 response.statusCode, |
116 response.contentLength, | 123 contentLength: contentLength, |
117 request: response.request, | 124 request: response.request, |
118 headers: response.headers, | 125 headers: response.headers, |
119 isRedirect: response.isRedirect, | 126 isRedirect: response.isRedirect, |
120 persistentConnection: response.persistentConnection, | 127 persistentConnection: response.persistentConnection, |
121 reasonPhrase: response.reasonPhrase); | 128 reasonPhrase: response.reasonPhrase); |
122 }).catchError((e) { | 129 }).catchError((e) { |
123 client.close(); | 130 client.close(); |
124 throw e; | 131 throw e; |
125 }); | 132 }); |
126 } | 133 } |
127 | 134 |
128 // Throws an error if this request has been finalized. | 135 // Throws an error if this request has been finalized. |
129 void _checkFinalized() { | 136 void _checkFinalized() { |
130 if (!finalized) return; | 137 if (!finalized) return; |
131 throw new StateError("Can't modify a finalized Request."); | 138 throw new StateError("Can't modify a finalized Request."); |
132 } | 139 } |
133 | 140 |
134 String toString() => "$method $url"; | 141 String toString() => "$method $url"; |
135 } | 142 } |
OLD | NEW |