Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(144)

Side by Side Diff: pkg/http/lib/src/response.dart

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

Powered by Google App Engine
This is Rietveld 408576698