| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 library response; | |
| 6 | |
| 7 import 'dart:io'; | |
| 8 import 'dart:scalarlist'; | |
| 9 | |
| 10 import 'base_response.dart'; | |
| 11 import 'streamed_response.dart'; | |
| 12 import 'utils.dart'; | |
| 13 | |
| 14 /// An HTTP response where the entire response body is known in advance. | |
| 15 class Response extends BaseResponse { | |
| 16 /// The bytes comprising the body of this response. | |
| 17 final Uint8List bodyBytes; | |
| 18 | |
| 19 /// 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 /// 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 /// | |
| 24 /// [RFC 2616]: http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html | |
| 25 String get body => decodeString(bodyBytes, _encodingForHeaders(headers)); | |
| 26 | |
| 27 /// Creates a new HTTP response with a string body. | |
| 28 Response( | |
| 29 String body, | |
| 30 int statusCode, | |
| 31 {Map<String, String> headers: const <String>{}, | |
| 32 bool isRedirect: false, | |
| 33 bool persistentConnection: true, | |
| 34 String reasonPhrase}) | |
| 35 : this.bytes( | |
| 36 encodeString(body, _encodingForHeaders(headers)), | |
| 37 statusCode, | |
| 38 headers: headers, | |
| 39 isRedirect: isRedirect, | |
| 40 persistentConnection: persistentConnection, | |
| 41 reasonPhrase: reasonPhrase); | |
| 42 | |
| 43 /// Create a new HTTP response with a byte array body. | |
| 44 Response.bytes( | |
| 45 List<int> bodyBytes, | |
| 46 int statusCode, | |
| 47 {Map<String, String> headers: const <String>{}, | |
| 48 bool isRedirect: false, | |
| 49 bool persistentConnection: true, | |
| 50 String reasonPhrase}) | |
| 51 : bodyBytes = toUint8List(bodyBytes), | |
| 52 super( | |
| 53 statusCode, | |
| 54 bodyBytes.length, | |
| 55 headers: headers, | |
| 56 isRedirect: isRedirect, | |
| 57 persistentConnection: persistentConnection, | |
| 58 reasonPhrase: reasonPhrase); | |
| 59 | |
| 60 /// Creates a new HTTP response by waiting for the full body to become | |
| 61 /// available from a [StreamedResponse]. | |
| 62 static Future<Response> fromStream(StreamedResponse response) { | |
| 63 return consumeInputStream(response.stream).transform((body) { | |
| 64 return new Response.bytes( | |
| 65 body, | |
| 66 response.statusCode, | |
| 67 headers: response.headers, | |
| 68 isRedirect: response.isRedirect, | |
| 69 persistentConnection: response.persistentConnection, | |
| 70 reasonPhrase: response.reasonPhrase); | |
| 71 }); | |
| 72 } | |
| 73 } | |
| 74 | |
| 75 /// 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 | |
| 77 /// if that charset is unknown. | |
| 78 Encoding _encodingForHeaders(Map<String, String> headers) => | |
| 79 encodingForCharset(_contentTypeForHeaders(headers).charset); | |
| 80 | |
| 81 /// Returns the [ContentType] object for the given headers. Defaults to | |
| 82 /// `application/octet-stream`. | |
| 83 ContentType _contentTypeForHeaders(Map<String, String> headers) { | |
| 84 var contentType = headers[HttpHeaders.CONTENT_TYPE]; | |
| 85 if (contentType != null) return new ContentType.fromString(contentType); | |
| 86 return new ContentType("application", "octet-stream"); | |
| 87 } | |
| OLD | NEW |