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 response; | 5 library response; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:convert'; |
8 import 'dart:io'; | 9 import 'dart:io'; |
9 import 'dart:typed_data'; | 10 import 'dart:typed_data'; |
10 | 11 |
11 import 'base_request.dart'; | 12 import 'base_request.dart'; |
12 import 'base_response.dart'; | 13 import 'base_response.dart'; |
13 import 'streamed_response.dart'; | 14 import 'streamed_response.dart'; |
14 import 'utils.dart'; | 15 import 'utils.dart'; |
15 | 16 |
16 /// An HTTP response where the entire response body is known in advance. | 17 /// An HTTP response where the entire response body is known in advance. |
17 class Response extends BaseResponse { | 18 class Response extends BaseResponse { |
18 /// The bytes comprising the body of this response. | 19 /// The bytes comprising the body of this response. |
19 final Uint8List bodyBytes; | 20 final Uint8List bodyBytes; |
20 | 21 |
21 /// The body of the response as a string. This is converted from [bodyBytes] | 22 /// The body of the response as a string. This is converted from [bodyBytes] |
22 /// using the `charset` parameter of the `Content-Type` header field, if | 23 /// using the `charset` parameter of the `Content-Type` header field, if |
23 /// available. If it's unavailable or if the encoding name is unknown, | 24 /// available. If it's unavailable or if the encoding name is unknown, |
24 /// [Encoding.ISO_8859_1] is used by default, as per [RFC 2616][]. | 25 /// [LATIN1] is used by default, as per [RFC 2616][]. |
25 /// | 26 /// |
26 /// [RFC 2616]: http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html | 27 /// [RFC 2616]: http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html |
27 String get body => decodeString(bodyBytes, _encodingForHeaders(headers)); | 28 String get body => _encodingForHeaders(headers).decode(bodyBytes); |
28 | 29 |
29 /// Creates a new HTTP response with a string body. | 30 /// Creates a new HTTP response with a string body. |
30 Response( | 31 Response( |
31 String body, | 32 String body, |
32 int statusCode, | 33 int statusCode, |
33 {BaseRequest request, | 34 {BaseRequest request, |
34 Map<String, String> headers: const {}, | 35 Map<String, String> headers: const {}, |
35 bool isRedirect: false, | 36 bool isRedirect: false, |
36 bool persistentConnection: true, | 37 bool persistentConnection: true, |
37 String reasonPhrase}) | 38 String reasonPhrase}) |
38 : this.bytes( | 39 : this.bytes( |
39 encodeString(body, _encodingForHeaders(headers)), | 40 _encodingForHeaders(headers).encode(body), |
40 statusCode, | 41 statusCode, |
41 request: request, | 42 request: request, |
42 headers: headers, | 43 headers: headers, |
43 isRedirect: isRedirect, | 44 isRedirect: isRedirect, |
44 persistentConnection: persistentConnection, | 45 persistentConnection: persistentConnection, |
45 reasonPhrase: reasonPhrase); | 46 reasonPhrase: reasonPhrase); |
46 | 47 |
47 /// Create a new HTTP response with a byte array body. | 48 /// Create a new HTTP response with a byte array body. |
48 Response.bytes( | 49 Response.bytes( |
49 List<int> bodyBytes, | 50 List<int> bodyBytes, |
(...skipping 23 matching lines...) Expand all Loading... |
73 request: response.request, | 74 request: response.request, |
74 headers: response.headers, | 75 headers: response.headers, |
75 isRedirect: response.isRedirect, | 76 isRedirect: response.isRedirect, |
76 persistentConnection: response.persistentConnection, | 77 persistentConnection: response.persistentConnection, |
77 reasonPhrase: response.reasonPhrase); | 78 reasonPhrase: response.reasonPhrase); |
78 }); | 79 }); |
79 } | 80 } |
80 } | 81 } |
81 | 82 |
82 /// Returns the encoding to use for a response with the given headers. This | 83 /// Returns the encoding to use for a response with the given headers. This |
83 /// defaults to [Encoding.ISO_8859_1] if the headers don't specify a charset or | 84 /// defaults to [LATIN1] if the headers don't specify a charset or |
84 /// if that charset is unknown. | 85 /// if that charset is unknown. |
85 Encoding _encodingForHeaders(Map<String, String> headers) => | 86 Encoding _encodingForHeaders(Map<String, String> headers) => |
86 encodingForCharset(_contentTypeForHeaders(headers).charset); | 87 encodingForCharset(_contentTypeForHeaders(headers).charset); |
87 | 88 |
88 /// Returns the [ContentType] object for the given headers. Defaults to | 89 /// Returns the [ContentType] object for the given headers. Defaults to |
89 /// `application/octet-stream`. | 90 /// `application/octet-stream`. |
90 ContentType _contentTypeForHeaders(Map<String, String> headers) { | 91 ContentType _contentTypeForHeaders(Map<String, String> headers) { |
91 var contentType = headers[HttpHeaders.CONTENT_TYPE]; | 92 var contentType = headers[HttpHeaders.CONTENT_TYPE]; |
92 if (contentType != null) return ContentType.parse(contentType); | 93 if (contentType != null) return ContentType.parse(contentType); |
93 return new ContentType("application", "octet-stream"); | 94 return new ContentType("application", "octet-stream"); |
94 } | 95 } |
OLD | NEW |