Line data Source code
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 : import 'dart:async';
6 : import 'dart:convert';
7 : import 'dart:typed_data';
8 :
9 : import 'package:http_parser/http_parser.dart';
10 :
11 : import 'base_request.dart';
12 : import 'base_response.dart';
13 : import 'streamed_response.dart';
14 : import 'utils.dart';
15 :
16 : /// An HTTP response where the entire response body is known in advance.
17 : class Response extends BaseResponse {
18 : /// The bytes comprising the body of this response.
19 : final Uint8List bodyBytes;
20 :
21 : /// 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 : /// available. If it's unavailable or if the encoding name is unknown,
24 : /// [LATIN1] is used by default, as per [RFC 2616][].
25 : ///
26 : /// [RFC 2616]: http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html
27 0 : String get body => _encodingForHeaders(headers).decode(bodyBytes);
28 :
29 : /// Creates a new HTTP response with a string body.
30 : Response(
31 : String body,
32 : int statusCode,
33 : {BaseRequest request,
34 : Map<String, String> headers: const {},
35 : bool isRedirect: false,
36 : bool persistentConnection: true,
37 : String reasonPhrase})
38 0 : : this.bytes(
39 0 : _encodingForHeaders(headers).encode(body),
40 : statusCode,
41 : request: request,
42 : headers: headers,
43 : isRedirect: isRedirect,
44 : persistentConnection: persistentConnection,
45 : reasonPhrase: reasonPhrase);
46 :
47 : /// Create a new HTTP response with a byte array body.
48 : Response.bytes(
49 : List<int> bodyBytes,
50 : int statusCode,
51 : {BaseRequest request,
52 : Map<String, String> headers: const {},
53 : bool isRedirect: false,
54 : bool persistentConnection: true,
55 : String reasonPhrase})
56 0 : : bodyBytes = toUint8List(bodyBytes),
57 0 : super(
58 : statusCode,
59 0 : contentLength: bodyBytes.length,
60 : request: request,
61 : headers: headers,
62 : isRedirect: isRedirect,
63 : persistentConnection: persistentConnection,
64 : reasonPhrase: reasonPhrase);
65 :
66 : /// Creates a new HTTP response by waiting for the full body to become
67 : /// available from a [StreamedResponse].
68 : static Future<Response> fromStream(StreamedResponse response) {
69 0 : return response.stream.toBytes().then((body) {
70 0 : return new Response.bytes(
71 : body,
72 0 : response.statusCode,
73 0 : request: response.request,
74 0 : headers: response.headers,
75 0 : isRedirect: response.isRedirect,
76 0 : persistentConnection: response.persistentConnection,
77 0 : reasonPhrase: response.reasonPhrase);
78 : });
79 : }
80 : }
81 :
82 : /// Returns the encoding to use for a response with the given headers. This
83 : /// defaults to [LATIN1] if the headers don't specify a charset or
84 : /// if that charset is unknown.
85 : Encoding _encodingForHeaders(Map<String, String> headers) =>
86 0 : encodingForCharset(_contentTypeForHeaders(headers).parameters['charset']);
87 :
88 : /// Returns the [MediaType] object for the given headers's content-type.
89 : ///
90 : /// Defaults to `application/octet-stream`.
91 : MediaType _contentTypeForHeaders(Map<String, String> headers) {
92 0 : var contentType = headers['content-type'];
93 0 : if (contentType != null) return new MediaType.parse(contentType);
94 0 : return new MediaType("application", "octet-stream");
95 : }
|