Chromium Code Reviews

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

Issue 12440002: Make instances of HeaderValue and ContentType immutable (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Updated pkg/http Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012, 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:async'; 7 import 'dart:async';
8 import 'dart:io'; 8 import 'dart:io';
9 import 'dart:scalarlist'; 9 import 'dart:scalarlist';
10 import 'dart:uri'; 10 import 'dart:uri';
11 11
(...skipping 38 matching lines...)
50 return _defaultEncoding; 50 return _defaultEncoding;
51 } 51 }
52 return requiredEncodingForCharset(_contentType.charset); 52 return requiredEncodingForCharset(_contentType.charset);
53 } 53 }
54 54
55 set encoding(Encoding value) { 55 set encoding(Encoding value) {
56 _checkFinalized(); 56 _checkFinalized();
57 _defaultEncoding = value; 57 _defaultEncoding = value;
58 var contentType = _contentType; 58 var contentType = _contentType;
59 if (contentType != null) { 59 if (contentType != null) {
60 contentType.charset = value.name; 60 contentType = new ContentType(contentType.primaryType,
61 contentType.subType,
62 value.name);
nweiz 2013/03/05 20:43:18 Same here.
Søren Gjesse 2013/03/06 13:23:30 Done.
61 _contentType = contentType; 63 _contentType = contentType;
62 } 64 }
63 } 65 }
64 66
65 // TODO(nweiz): make this return a read-only view 67 // TODO(nweiz): make this return a read-only view
66 /// The bytes comprising the body of the request. This is converted to and 68 /// The bytes comprising the body of the request. This is converted to and
67 /// from [body] using [encoding]. 69 /// from [body] using [encoding].
68 /// 70 ///
69 /// This list should only be set, not be modified in place. 71 /// This list should only be set, not be modified in place.
70 Uint8List get bodyBytes => _bodyBytes; 72 Uint8List get bodyBytes => _bodyBytes;
71 Uint8List _bodyBytes; 73 Uint8List _bodyBytes;
72 74
73 set bodyBytes(List<int> value) { 75 set bodyBytes(List<int> value) {
74 _checkFinalized(); 76 _checkFinalized();
75 _bodyBytes = toUint8List(value); 77 _bodyBytes = toUint8List(value);
76 } 78 }
77 79
78 /// 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
79 /// [bodyBytes] using [encoding]. 81 /// [bodyBytes] using [encoding].
80 /// 82 ///
81 /// 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`
82 /// 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`
83 /// 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
84 /// be set to [encoding] if it wasn't already set. 86 /// be set to [encoding] if it wasn't already set.
85 String get body => decodeString(bodyBytes, encoding); 87 String get body => decodeString(bodyBytes, encoding);
86 88
87 set body(String value) { 89 set body(String value) {
88 bodyBytes = encodeString(value, encoding); 90 bodyBytes = encodeString(value, encoding);
89 var contentType = _contentType; 91 var contentType = _contentType;
90 if (contentType == null) contentType = new ContentType("text", "plain"); 92 if (contentType == null) {
91 if (contentType.charset == null) contentType.charset = encoding.name; 93 contentType = new ContentType("text", "plain", encoding.name);
94 } else if (contentType.charset == null) {
95 contentType = new ContentType(contentType.primaryType,
96 contentType.subType,
97 encoding.name);
nweiz 2013/03/05 20:43:18 And here.
Søren Gjesse 2013/03/06 13:23:30 Done.
98 }
92 _contentType = contentType; 99 _contentType = contentType;
93 } 100 }
94 101
95 /// The form-encoded fields in the body of the request as a map from field 102 /// The form-encoded fields in the body of the request as a map from field
96 /// names to values. The form-encoded body is converted to and from 103 /// names to values. The form-encoded body is converted to and from
97 /// [bodyBytes] using [encoding] (in the same way as [body]). 104 /// [bodyBytes] using [encoding] (in the same way as [body]).
98 /// 105 ///
99 /// If the request doesn't have a `Content-Type` header of 106 /// If the request doesn't have a `Content-Type` header of
100 /// `application/x-www-form-urlencoded`, reading this will throw a 107 /// `application/x-www-form-urlencoded`, reading this will throw a
101 /// [StateError]. 108 /// [StateError].
(...skipping 49 matching lines...)
151 set _contentType(ContentType value) { 158 set _contentType(ContentType value) {
152 headers[HttpHeaders.CONTENT_TYPE] = value.toString(); 159 headers[HttpHeaders.CONTENT_TYPE] = value.toString();
153 } 160 }
154 161
155 /// Throw an error if this request has been finalized. 162 /// Throw an error if this request has been finalized.
156 void _checkFinalized() { 163 void _checkFinalized() {
157 if (!finalized) return; 164 if (!finalized) return;
158 throw new StateError("Can't modify a finalized Request."); 165 throw new StateError("Can't modify a finalized Request.");
159 } 166 }
160 } 167 }
OLDNEW

Powered by Google App Engine