Chromium Code Reviews| 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:io'; | 7 import 'dart:io'; |
| 8 import 'dart:scalarlist'; | 8 import 'dart:scalarlist'; |
| 9 | 9 |
| 10 import 'base_request.dart'; | |
| 10 import 'base_response.dart'; | 11 import 'base_response.dart'; |
| 11 import 'streamed_response.dart'; | 12 import 'streamed_response.dart'; |
| 12 import 'utils.dart'; | 13 import 'utils.dart'; |
| 13 | 14 |
| 14 /// An HTTP response where the entire response body is known in advance. | 15 /// An HTTP response where the entire response body is known in advance. |
| 15 class Response extends BaseResponse { | 16 class Response extends BaseResponse { |
| 16 /// The bytes comprising the body of this response. | 17 /// The bytes comprising the body of this response. |
| 17 final Uint8List bodyBytes; | 18 final Uint8List bodyBytes; |
| 18 | 19 |
| 19 /// The body of the response as a string. This is converted from [bodyBytes] | 20 /// The body of the response as a string. This is converted from [bodyBytes] |
| 20 /// using the `charset` parameter of the `Content-Type` header field, if | 21 /// using the `charset` parameter of the `Content-Type` header field, if |
| 21 /// available. If it's unavailable or if the encoding name is unknown, | 22 /// available. If it's unavailable or if the encoding name is unknown, |
| 22 /// [Encoding.ISO_8859_1] is used by default, as per [RFC 2616][]. | 23 /// [Encoding.ISO_8859_1] is used by default, as per [RFC 2616][]. |
| 23 /// | 24 /// |
| 24 /// [RFC 2616]: http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html | 25 /// [RFC 2616]: http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html |
| 25 String get body => decodeString(bodyBytes, _encodingForHeaders(headers)); | 26 String get body => decodeString(bodyBytes, _encodingForHeaders(headers)); |
| 26 | 27 |
| 27 /// Creates a new HTTP response with a string body. | 28 /// Creates a new HTTP response with a string body. |
| 28 Response( | 29 Response( |
| 29 String body, | 30 String body, |
| 30 int statusCode, | 31 int statusCode, |
| 31 {Map<String, String> headers: const <String>{}, | 32 {BaseRequest request: null, |
|
Bob Nystrom
2012/11/29 23:57:04
null is the default default value, so you can omit
nweiz
2012/11/30 00:08:54
Done.
| |
| 33 Map<String, String> headers: const <String>{}, | |
| 32 bool isRedirect: false, | 34 bool isRedirect: false, |
| 33 bool persistentConnection: true, | 35 bool persistentConnection: true, |
| 34 String reasonPhrase}) | 36 String reasonPhrase}) |
| 35 : this.bytes( | 37 : this.bytes( |
| 36 encodeString(body, _encodingForHeaders(headers)), | 38 encodeString(body, _encodingForHeaders(headers)), |
| 37 statusCode, | 39 statusCode, |
| 40 request: request, | |
| 38 headers: headers, | 41 headers: headers, |
| 39 isRedirect: isRedirect, | 42 isRedirect: isRedirect, |
| 40 persistentConnection: persistentConnection, | 43 persistentConnection: persistentConnection, |
| 41 reasonPhrase: reasonPhrase); | 44 reasonPhrase: reasonPhrase); |
| 42 | 45 |
| 43 /// Create a new HTTP response with a byte array body. | 46 /// Create a new HTTP response with a byte array body. |
| 44 Response.bytes( | 47 Response.bytes( |
| 45 List<int> bodyBytes, | 48 List<int> bodyBytes, |
| 46 int statusCode, | 49 int statusCode, |
| 47 {Map<String, String> headers: const <String>{}, | 50 {BaseRequest request: null, |
| 51 Map<String, String> headers: const <String>{}, | |
| 48 bool isRedirect: false, | 52 bool isRedirect: false, |
| 49 bool persistentConnection: true, | 53 bool persistentConnection: true, |
| 50 String reasonPhrase}) | 54 String reasonPhrase}) |
| 51 : bodyBytes = toUint8List(bodyBytes), | 55 : bodyBytes = toUint8List(bodyBytes), |
| 52 super( | 56 super( |
| 53 statusCode, | 57 statusCode, |
| 54 bodyBytes.length, | 58 bodyBytes.length, |
| 59 request: request, | |
| 55 headers: headers, | 60 headers: headers, |
| 56 isRedirect: isRedirect, | 61 isRedirect: isRedirect, |
| 57 persistentConnection: persistentConnection, | 62 persistentConnection: persistentConnection, |
| 58 reasonPhrase: reasonPhrase); | 63 reasonPhrase: reasonPhrase); |
| 59 | 64 |
| 60 /// Creates a new HTTP response by waiting for the full body to become | 65 /// Creates a new HTTP response by waiting for the full body to become |
| 61 /// available from a [StreamedResponse]. | 66 /// available from a [StreamedResponse]. |
| 62 static Future<Response> fromStream(StreamedResponse response) { | 67 static Future<Response> fromStream(StreamedResponse response) { |
| 63 return consumeInputStream(response.stream).transform((body) { | 68 return consumeInputStream(response.stream).transform((body) { |
| 64 return new Response.bytes( | 69 return new Response.bytes( |
| 65 body, | 70 body, |
| 66 response.statusCode, | 71 response.statusCode, |
| 72 request: response.request, | |
| 67 headers: response.headers, | 73 headers: response.headers, |
| 68 isRedirect: response.isRedirect, | 74 isRedirect: response.isRedirect, |
| 69 persistentConnection: response.persistentConnection, | 75 persistentConnection: response.persistentConnection, |
| 70 reasonPhrase: response.reasonPhrase); | 76 reasonPhrase: response.reasonPhrase); |
| 71 }); | 77 }); |
| 72 } | 78 } |
| 73 } | 79 } |
| 74 | 80 |
| 75 /// Returns the encoding to use for a response with the given headers. This | 81 /// Returns the encoding to use for a response with the given headers. This |
| 76 /// defaults to [Encoding.ISO_8859_1] if the headers don't specify a charset or | 82 /// defaults to [Encoding.ISO_8859_1] if the headers don't specify a charset or |
| 77 /// if that charset is unknown. | 83 /// if that charset is unknown. |
| 78 Encoding _encodingForHeaders(Map<String, String> headers) => | 84 Encoding _encodingForHeaders(Map<String, String> headers) => |
| 79 encodingForCharset(_contentTypeForHeaders(headers).charset); | 85 encodingForCharset(_contentTypeForHeaders(headers).charset); |
| 80 | 86 |
| 81 /// Returns the [ContentType] object for the given headers. Defaults to | 87 /// Returns the [ContentType] object for the given headers. Defaults to |
| 82 /// `application/octet-stream`. | 88 /// `application/octet-stream`. |
| 83 ContentType _contentTypeForHeaders(Map<String, String> headers) { | 89 ContentType _contentTypeForHeaders(Map<String, String> headers) { |
| 84 var contentType = headers[HttpHeaders.CONTENT_TYPE]; | 90 var contentType = headers[HttpHeaders.CONTENT_TYPE]; |
| 85 if (contentType != null) return new ContentType.fromString(contentType); | 91 if (contentType != null) return new ContentType.fromString(contentType); |
| 86 return new ContentType("application", "octet-stream"); | 92 return new ContentType("application", "octet-stream"); |
| 87 } | 93 } |
| OLD | NEW |