OLD | NEW |
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...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 charset: value.name, |
| 63 parameters: contentType.parameters); |
61 _contentType = contentType; | 64 _contentType = contentType; |
62 } | 65 } |
63 } | 66 } |
64 | 67 |
65 // TODO(nweiz): make this return a read-only view | 68 // TODO(nweiz): make this return a read-only view |
66 /// The bytes comprising the body of the request. This is converted to and | 69 /// The bytes comprising the body of the request. This is converted to and |
67 /// from [body] using [encoding]. | 70 /// from [body] using [encoding]. |
68 /// | 71 /// |
69 /// This list should only be set, not be modified in place. | 72 /// This list should only be set, not be modified in place. |
70 Uint8List get bodyBytes => _bodyBytes; | 73 Uint8List get bodyBytes => _bodyBytes; |
71 Uint8List _bodyBytes; | 74 Uint8List _bodyBytes; |
72 | 75 |
73 set bodyBytes(List<int> value) { | 76 set bodyBytes(List<int> value) { |
74 _checkFinalized(); | 77 _checkFinalized(); |
75 _bodyBytes = toUint8List(value); | 78 _bodyBytes = toUint8List(value); |
76 } | 79 } |
77 | 80 |
78 /// The body of the request as a string. This is converted to and from | 81 /// The body of the request as a string. This is converted to and from |
79 /// [bodyBytes] using [encoding]. | 82 /// [bodyBytes] using [encoding]. |
80 /// | 83 /// |
81 /// When this is set, if the request does not yet have a `Content-Type` | 84 /// 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` | 85 /// 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 | 86 /// parameter of the `Content-Type` header (whether new or pre-existing) will |
84 /// be set to [encoding] if it wasn't already set. | 87 /// be set to [encoding] if it wasn't already set. |
85 String get body => decodeString(bodyBytes, encoding); | 88 String get body => decodeString(bodyBytes, encoding); |
86 | 89 |
87 set body(String value) { | 90 set body(String value) { |
88 bodyBytes = encodeString(value, encoding); | 91 bodyBytes = encodeString(value, encoding); |
89 var contentType = _contentType; | 92 var contentType = _contentType; |
90 if (contentType == null) contentType = new ContentType("text", "plain"); | 93 if (contentType == null) { |
91 if (contentType.charset == null) contentType.charset = encoding.name; | 94 contentType = new ContentType("text", "plain", charset: encoding.name); |
| 95 } else if (contentType.charset == null) { |
| 96 contentType = new ContentType(contentType.primaryType, |
| 97 contentType.subType, |
| 98 charset: encoding.name, |
| 99 parameters: contentType.parameters); |
| 100 } |
92 _contentType = contentType; | 101 _contentType = contentType; |
93 } | 102 } |
94 | 103 |
95 /// The form-encoded fields in the body of the request as a map from field | 104 /// 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 | 105 /// names to values. The form-encoded body is converted to and from |
97 /// [bodyBytes] using [encoding] (in the same way as [body]). | 106 /// [bodyBytes] using [encoding] (in the same way as [body]). |
98 /// | 107 /// |
99 /// If the request doesn't have a `Content-Type` header of | 108 /// If the request doesn't have a `Content-Type` header of |
100 /// `application/x-www-form-urlencoded`, reading this will throw a | 109 /// `application/x-www-form-urlencoded`, reading this will throw a |
101 /// [StateError]. | 110 /// [StateError]. |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 set _contentType(ContentType value) { | 160 set _contentType(ContentType value) { |
152 headers[HttpHeaders.CONTENT_TYPE] = value.toString(); | 161 headers[HttpHeaders.CONTENT_TYPE] = value.toString(); |
153 } | 162 } |
154 | 163 |
155 /// Throw an error if this request has been finalized. | 164 /// Throw an error if this request has been finalized. |
156 void _checkFinalized() { | 165 void _checkFinalized() { |
157 if (!finalized) return; | 166 if (!finalized) return; |
158 throw new StateError("Can't modify a finalized Request."); | 167 throw new StateError("Can't modify a finalized Request."); |
159 } | 168 } |
160 } | 169 } |
OLD | NEW |