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 request; | 5 library request; |
6 | 6 |
| 7 import 'dart:async'; |
7 import 'dart:io'; | 8 import 'dart:io'; |
8 import 'dart:scalarlist'; | 9 import 'dart:scalarlist'; |
9 import 'dart:uri'; | 10 import 'dart:uri'; |
10 | 11 |
11 import 'base_request.dart'; | 12 import 'base_request.dart'; |
| 13 import 'byte_stream.dart'; |
12 import 'utils.dart'; | 14 import 'utils.dart'; |
13 | 15 |
14 /// An HTTP request where the entire request body is known in advance. | 16 /// An HTTP request where the entire request body is known in advance. |
15 class Request extends BaseRequest { | 17 class Request extends BaseRequest { |
16 /// The size of the request body, in bytes. This is calculated from | 18 /// The size of the request body, in bytes. This is calculated from |
17 /// [bodyBytes]. | 19 /// [bodyBytes]. |
18 /// | 20 /// |
19 /// The content length cannot be set for [Request], since it's automatically | 21 /// The content length cannot be set for [Request], since it's automatically |
20 /// calculated from [bodyBytes]. | 22 /// calculated from [bodyBytes]. |
21 int get contentLength => bodyBytes.length; | 23 int get contentLength => bodyBytes.length; |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 | 126 |
125 this.body = mapToQuery(fields); | 127 this.body = mapToQuery(fields); |
126 } | 128 } |
127 | 129 |
128 /// Creates a new HTTP request. | 130 /// Creates a new HTTP request. |
129 Request(String method, Uri url) | 131 Request(String method, Uri url) |
130 : super(method, url), | 132 : super(method, url), |
131 _defaultEncoding = Encoding.UTF_8, | 133 _defaultEncoding = Encoding.UTF_8, |
132 _bodyBytes = new Uint8List(0); | 134 _bodyBytes = new Uint8List(0); |
133 | 135 |
134 /// Freeze all mutable fields and return an [InputStream] containing the | 136 /// Freezes all mutable fields and returns a single-subscription [ByteStream] |
135 /// request body. | 137 /// containing the request body. |
136 InputStream finalize() { | 138 ByteStream finalize() { |
137 super.finalize(); | 139 super.finalize(); |
138 | 140 return new ByteStream.fromBytes(bodyBytes); |
139 var stream = new ListInputStream(); | |
140 stream.write(bodyBytes); | |
141 stream.markEndOfStream(); | |
142 return stream; | |
143 } | 141 } |
144 | 142 |
145 /// The `Content-Type` header of the request (if it exists) as a | 143 /// The `Content-Type` header of the request (if it exists) as a |
146 /// [ContentType]. | 144 /// [ContentType]. |
147 ContentType get _contentType { | 145 ContentType get _contentType { |
148 var contentType = headers[HttpHeaders.CONTENT_TYPE]; | 146 var contentType = headers[HttpHeaders.CONTENT_TYPE]; |
149 if (contentType == null) return null; | 147 if (contentType == null) return null; |
150 return new ContentType.fromString(contentType); | 148 return new ContentType.fromString(contentType); |
151 } | 149 } |
152 | 150 |
153 set _contentType(ContentType value) { | 151 set _contentType(ContentType value) { |
154 headers[HttpHeaders.CONTENT_TYPE] = value.toString(); | 152 headers[HttpHeaders.CONTENT_TYPE] = value.toString(); |
155 } | 153 } |
156 | 154 |
157 /// Throw an error if this request has been finalized. | 155 /// Throw an error if this request has been finalized. |
158 void _checkFinalized() { | 156 void _checkFinalized() { |
159 if (!finalized) return; | 157 if (!finalized) return; |
160 throw new StateError("Can't modify a finalized Request."); | 158 throw new StateError("Can't modify a finalized Request."); |
161 } | 159 } |
162 } | 160 } |
OLD | NEW |