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 | |
Bob Nystrom
2014/03/17 17:59:19
Why this change?
I think most users expect ints t
nweiz
2014/03/17 19:36:01
[String.indexOf] is a holdover from JavaScript whi
Bob Nystrom
2014/03/17 21:08:45
Sure, and millions of people know and expect that
nweiz
2014/03/17 21:25:09
Every JavaScript developer expects objects to be m
kevmoo
2014/03/17 21:30:53
I agree with this logic.
For helpers accessing th
Bob Nystrom
2014/03/17 21:32:05
Sure, and Dart was designed expressly to not surpr
nweiz
2014/03/17 21:57:48
Dart was designed in the tradition of Java and Jav
| |
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 < 0) throw new ArgumentError("Invalid content length $value."); | |
kevmoo
2014/03/17 17:53:25
Is setting null valid here? Will the `<` blow up o
nweiz
2014/03/17 19:36:01
Good catch. Done.
| |
36 _checkFinalized(); | 39 _checkFinalized(); |
37 _contentLength = value; | 40 _contentLength = value; |
38 } | 41 } |
39 | 42 |
40 /// Whether a persistent connection should be maintained with the server. | 43 /// Whether a persistent connection should be maintained with the server. |
41 /// Defaults to true. | 44 /// Defaults to true. |
42 bool get persistentConnection => _persistentConnection; | 45 bool get persistentConnection => _persistentConnection; |
43 bool _persistentConnection = true; | 46 bool _persistentConnection = true; |
44 | 47 |
45 set persistentConnection(bool value) { | 48 set persistentConnection(bool value) { |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
106 /// the request is complete. If you're planning on making multiple requests to | 109 /// 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 | 110 /// the same server, you should use a single [Client] for all of those |
108 /// requests. | 111 /// requests. |
109 Future<StreamedResponse> send() { | 112 Future<StreamedResponse> send() { |
110 var client = new Client(); | 113 var client = new Client(); |
111 return client.send(this).then((response) { | 114 return client.send(this).then((response) { |
112 var stream = onDone(response.stream, client.close); | 115 var stream = onDone(response.stream, client.close); |
113 return new StreamedResponse( | 116 return new StreamedResponse( |
114 new ByteStream(stream), | 117 new ByteStream(stream), |
115 response.statusCode, | 118 response.statusCode, |
116 response.contentLength, | 119 response.contentLength == null ? -1 : response.contentLength, |
117 request: response.request, | 120 request: response.request, |
118 headers: response.headers, | 121 headers: response.headers, |
119 isRedirect: response.isRedirect, | 122 isRedirect: response.isRedirect, |
120 persistentConnection: response.persistentConnection, | 123 persistentConnection: response.persistentConnection, |
121 reasonPhrase: response.reasonPhrase); | 124 reasonPhrase: response.reasonPhrase); |
122 }).catchError((e) { | 125 }).catchError((e) { |
123 client.close(); | 126 client.close(); |
124 throw e; | 127 throw e; |
125 }); | 128 }); |
126 } | 129 } |
127 | 130 |
128 // Throws an error if this request has been finalized. | 131 // Throws an error if this request has been finalized. |
129 void _checkFinalized() { | 132 void _checkFinalized() { |
130 if (!finalized) return; | 133 if (!finalized) return; |
131 throw new StateError("Can't modify a finalized Request."); | 134 throw new StateError("Can't modify a finalized Request."); |
132 } | 135 } |
133 | 136 |
134 String toString() => "$method $url"; | 137 String toString() => "$method $url"; |
135 } | 138 } |
OLD | NEW |