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

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

Issue 22872012: Remove Encoding-enum from dart:io and add interface in dart:convert. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Fix typo. Created 7 years, 3 months 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
« no previous file with comments | « pkg/http/lib/src/multipart_file.dart ('k') | pkg/http/lib/src/response.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, 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 request; 5 library request;
6 6
7 import 'dart:convert';
7 import 'dart:io'; 8 import 'dart:io';
8 import 'dart:typed_data'; 9 import 'dart:typed_data';
9 10
10 import 'base_request.dart'; 11 import 'base_request.dart';
11 import 'byte_stream.dart'; 12 import 'byte_stream.dart';
12 import 'utils.dart'; 13 import 'utils.dart';
13 14
14 /// An HTTP request where the entire request body is known in advance. 15 /// An HTTP request where the entire request body is known in advance.
15 class Request extends BaseRequest { 16 class Request extends BaseRequest {
16 /// The size of the request body, in bytes. This is calculated from 17 /// The size of the request body, in bytes. This is calculated from
(...skipping 12 matching lines...) Expand all
29 /// [body]. This is only used if [encoding] hasn't been manually set and if 30 /// [body]. This is only used if [encoding] hasn't been manually set and if
30 /// the content-type header has no encoding information. 31 /// the content-type header has no encoding information.
31 Encoding _defaultEncoding; 32 Encoding _defaultEncoding;
32 33
33 /// The encoding used for the request. This encoding is used when converting 34 /// The encoding used for the request. This encoding is used when converting
34 /// between [bodyBytes] and [body]. 35 /// between [bodyBytes] and [body].
35 /// 36 ///
36 /// If the request has a `Content-Type` header and that header has a `charset` 37 /// If the request has a `Content-Type` header and that header has a `charset`
37 /// parameter, that parameter's value is used as the encoding. Otherwise, if 38 /// parameter, that parameter's value is used as the encoding. Otherwise, if
38 /// [encoding] has been set manually, that encoding is used. If that hasn't 39 /// [encoding] has been set manually, that encoding is used. If that hasn't
39 /// been set either, this defaults to [Encoding.UTF_8]. 40 /// been set either, this defaults to [UTF8].
40 /// 41 ///
41 /// If the `charset` parameter's value is not a known [Encoding], reading this 42 /// If the `charset` parameter's value is not a known [Encoding], reading this
42 /// will throw a [FormatException]. 43 /// will throw a [FormatException].
43 /// 44 ///
44 /// If the request has a `Content-Type` header, setting this will set the 45 /// If the request has a `Content-Type` header, setting this will set the
45 /// charset parameter on that header. 46 /// charset parameter on that header.
46 Encoding get encoding { 47 Encoding get encoding {
47 if (_contentType == null || _contentType.charset == null) { 48 if (_contentType == null || _contentType.charset == null) {
48 return _defaultEncoding; 49 return _defaultEncoding;
49 } 50 }
(...skipping 26 matching lines...) Expand all
76 _bodyBytes = toUint8List(value); 77 _bodyBytes = toUint8List(value);
77 } 78 }
78 79
79 /// The body of the request as a string. This is converted to and from 80 /// The body of the request as a string. This is converted to and from
80 /// [bodyBytes] using [encoding]. 81 /// [bodyBytes] using [encoding].
81 /// 82 ///
82 /// When this is set, if the request does not yet have a `Content-Type` 83 /// When this is set, if the request does not yet have a `Content-Type`
83 /// header, one will be added with the type `text/plain`. Then the `charset` 84 /// header, one will be added with the type `text/plain`. Then the `charset`
84 /// parameter of the `Content-Type` header (whether new or pre-existing) will 85 /// parameter of the `Content-Type` header (whether new or pre-existing) will
85 /// be set to [encoding] if it wasn't already set. 86 /// be set to [encoding] if it wasn't already set.
86 String get body => decodeString(bodyBytes, encoding); 87 String get body => encoding.decode(bodyBytes);
87 88
88 set body(String value) { 89 set body(String value) {
89 bodyBytes = encodeString(value, encoding); 90 bodyBytes = encoding.encode(value);
90 var contentType = _contentType; 91 var contentType = _contentType;
91 if (contentType == null) { 92 if (contentType == null) {
92 contentType = new ContentType("text", "plain", charset: encoding.name); 93 contentType = new ContentType("text", "plain", charset: encoding.name);
93 } else if (contentType.charset == null) { 94 } else if (contentType.charset == null) {
94 contentType = new ContentType(contentType.primaryType, 95 contentType = new ContentType(contentType.primaryType,
95 contentType.subType, 96 contentType.subType,
96 charset: encoding.name, 97 charset: encoding.name,
97 parameters: contentType.parameters); 98 parameters: contentType.parameters);
98 } 99 }
99 _contentType = contentType; 100 _contentType = contentType;
(...skipping 30 matching lines...) Expand all
130 throw new StateError('Cannot set the body fields of a Request with ' 131 throw new StateError('Cannot set the body fields of a Request with '
131 'content-type "${_contentType.value}".'); 132 'content-type "${_contentType.value}".');
132 } 133 }
133 134
134 this.body = mapToQuery(fields); 135 this.body = mapToQuery(fields);
135 } 136 }
136 137
137 /// Creates a new HTTP request. 138 /// Creates a new HTTP request.
138 Request(String method, Uri url) 139 Request(String method, Uri url)
139 : super(method, url), 140 : super(method, url),
140 _defaultEncoding = Encoding.UTF_8, 141 _defaultEncoding = UTF8,
141 _bodyBytes = new Uint8List(0); 142 _bodyBytes = new Uint8List(0);
142 143
143 /// Freezes all mutable fields and returns a single-subscription [ByteStream] 144 /// Freezes all mutable fields and returns a single-subscription [ByteStream]
144 /// containing the request body. 145 /// containing the request body.
145 ByteStream finalize() { 146 ByteStream finalize() {
146 super.finalize(); 147 super.finalize();
147 return new ByteStream.fromBytes(bodyBytes); 148 return new ByteStream.fromBytes(bodyBytes);
148 } 149 }
149 150
150 /// The `Content-Type` header of the request (if it exists) as a 151 /// The `Content-Type` header of the request (if it exists) as a
151 /// [ContentType]. 152 /// [ContentType].
152 ContentType get _contentType { 153 ContentType get _contentType {
153 var contentType = headers[HttpHeaders.CONTENT_TYPE]; 154 var contentType = headers[HttpHeaders.CONTENT_TYPE];
154 if (contentType == null) return null; 155 if (contentType == null) return null;
155 return ContentType.parse(contentType); 156 return ContentType.parse(contentType);
156 } 157 }
157 158
158 set _contentType(ContentType value) { 159 set _contentType(ContentType value) {
159 headers[HttpHeaders.CONTENT_TYPE] = value.toString(); 160 headers[HttpHeaders.CONTENT_TYPE] = value.toString();
160 } 161 }
161 162
162 /// Throw an error if this request has been finalized. 163 /// Throw an error if this request has been finalized.
163 void _checkFinalized() { 164 void _checkFinalized() {
164 if (!finalized) return; 165 if (!finalized) return;
165 throw new StateError("Can't modify a finalized Request."); 166 throw new StateError("Can't modify a finalized Request.");
166 } 167 }
167 } 168 }
OLDNEW
« no previous file with comments | « pkg/http/lib/src/multipart_file.dart ('k') | pkg/http/lib/src/response.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698