OLD | NEW |
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 part of dart.io; | 5 part of dart.io; |
6 | 6 |
7 class _HttpHeaders implements HttpHeaders { | 7 class _HttpHeaders implements HttpHeaders { |
8 _HttpHeaders(String this.protocolVersion) | 8 _HttpHeaders(String this.protocolVersion) |
9 : _headers = new Map<String, List<String>>(); | 9 : _headers = new Map<String, List<String>>(); |
10 | 10 |
11 List<String> operator[](String name) { | 11 List<String> operator[](String name) { |
12 name = name.toLowerCase(); | 12 name = name.toLowerCase(); |
13 return _headers[name]; | 13 return _headers[name]; |
14 } | 14 } |
15 | 15 |
16 String value(String name) { | 16 String value(String name) { |
17 name = name.toLowerCase(); | 17 name = name.toLowerCase(); |
18 List<String> values = _headers[name]; | 18 List<String> values = _headers[name]; |
19 if (values == null) return null; | 19 if (values == null) return null; |
20 if (values.length > 1) { | 20 if (values.length > 1) { |
21 throw new HttpException("More than one value for header $name"); | 21 throw new HttpException("More than one value for header $name"); |
22 } | 22 } |
23 return values[0]; | 23 return values[0]; |
24 } | 24 } |
25 | 25 |
26 void add(String name, value) { | 26 void add(String name, value) { |
27 _checkMutable(); | 27 _checkMutable(); |
| 28 var lowerCaseName = name.toLowerCase(); |
28 if (value is List) { | 29 if (value is List) { |
29 for (int i = 0; i < value.length; i++) { | 30 for (int i = 0; i < value.length; i++) { |
30 _add(name, value[i]); | 31 _add(lowerCaseName, value[i]); |
31 } | 32 } |
32 } else { | 33 } else { |
33 _add(name, value); | 34 _add(lowerCaseName, value); |
34 } | 35 } |
35 } | 36 } |
36 | 37 |
37 void set(String name, Object value) { | 38 void set(String name, Object value) { |
38 name = name.toLowerCase(); | 39 name = name.toLowerCase(); |
39 _checkMutable(); | 40 _checkMutable(); |
40 removeAll(name); | 41 removeAll(name); |
41 add(name, value); | 42 add(name, value); |
42 } | 43 } |
43 | 44 |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
200 } else { | 201 } else { |
201 return null; | 202 return null; |
202 } | 203 } |
203 } | 204 } |
204 | 205 |
205 void set contentType(ContentType contentType) { | 206 void set contentType(ContentType contentType) { |
206 _checkMutable(); | 207 _checkMutable(); |
207 _set(HttpHeaders.CONTENT_TYPE, contentType.toString()); | 208 _set(HttpHeaders.CONTENT_TYPE, contentType.toString()); |
208 } | 209 } |
209 | 210 |
| 211 // [name] must be a lower-case version of the name. |
210 void _add(String name, value) { | 212 void _add(String name, value) { |
211 var lowerCaseName = name.toLowerCase(); | |
212 // TODO(sgjesse): Add immutable state throw HttpException is immutable. | 213 // TODO(sgjesse): Add immutable state throw HttpException is immutable. |
213 if (lowerCaseName == HttpHeaders.CONTENT_LENGTH) { | 214 if (name == HttpHeaders.CONTENT_LENGTH) { |
214 if (value is int) { | 215 if (value is int) { |
215 contentLength = value; | 216 contentLength = value; |
216 } else if (value is String) { | 217 } else if (value is String) { |
217 contentLength = int.parse(value); | 218 contentLength = int.parse(value); |
218 } else { | 219 } else { |
219 throw new HttpException("Unexpected type for header named $name"); | 220 throw new HttpException("Unexpected type for header named $name"); |
220 } | 221 } |
221 } else if (lowerCaseName == HttpHeaders.TRANSFER_ENCODING) { | 222 } else if (name == HttpHeaders.TRANSFER_ENCODING) { |
222 if (value == "chunked") { | 223 if (value == "chunked") { |
223 chunkedTransferEncoding = true; | 224 chunkedTransferEncoding = true; |
224 } else { | 225 } else { |
225 _addValue(lowerCaseName, value); | 226 _addValue(name, value); |
226 } | 227 } |
227 } else if (lowerCaseName == HttpHeaders.DATE) { | 228 } else if (name == HttpHeaders.DATE) { |
228 if (value is DateTime) { | 229 if (value is DateTime) { |
229 date = value; | 230 date = value; |
230 } else if (value is String) { | 231 } else if (value is String) { |
231 _set(HttpHeaders.DATE, value); | 232 _set(HttpHeaders.DATE, value); |
232 } else { | 233 } else { |
233 throw new HttpException("Unexpected type for header named $name"); | 234 throw new HttpException("Unexpected type for header named $name"); |
234 } | 235 } |
235 } else if (lowerCaseName == HttpHeaders.EXPIRES) { | 236 } else if (name == HttpHeaders.EXPIRES) { |
236 if (value is DateTime) { | 237 if (value is DateTime) { |
237 expires = value; | 238 expires = value; |
238 } else if (value is String) { | 239 } else if (value is String) { |
239 _set(HttpHeaders.EXPIRES, value); | 240 _set(HttpHeaders.EXPIRES, value); |
240 } else { | 241 } else { |
241 throw new HttpException("Unexpected type for header named $name"); | 242 throw new HttpException("Unexpected type for header named $name"); |
242 } | 243 } |
243 } else if (lowerCaseName == HttpHeaders.IF_MODIFIED_SINCE) { | 244 } else if (name == HttpHeaders.IF_MODIFIED_SINCE) { |
244 if (value is DateTime) { | 245 if (value is DateTime) { |
245 ifModifiedSince = value; | 246 ifModifiedSince = value; |
246 } else if (value is String) { | 247 } else if (value is String) { |
247 _set(HttpHeaders.IF_MODIFIED_SINCE, value); | 248 _set(HttpHeaders.IF_MODIFIED_SINCE, value); |
248 } else { | 249 } else { |
249 throw new HttpException("Unexpected type for header named $name"); | 250 throw new HttpException("Unexpected type for header named $name"); |
250 } | 251 } |
251 } else if (lowerCaseName == HttpHeaders.HOST) { | 252 } else if (name == HttpHeaders.HOST) { |
252 if (value is String) { | 253 if (value is String) { |
253 int pos = value.indexOf(":"); | 254 int pos = value.indexOf(":"); |
254 if (pos == -1) { | 255 if (pos == -1) { |
255 _host = value; | 256 _host = value; |
256 _port = HttpClient.DEFAULT_HTTP_PORT; | 257 _port = HttpClient.DEFAULT_HTTP_PORT; |
257 } else { | 258 } else { |
258 if (pos > 0) { | 259 if (pos > 0) { |
259 _host = value.substring(0, pos); | 260 _host = value.substring(0, pos); |
260 } else { | 261 } else { |
261 _host = null; | 262 _host = null; |
262 } | 263 } |
263 if (pos + 1 == value.length) { | 264 if (pos + 1 == value.length) { |
264 _port = HttpClient.DEFAULT_HTTP_PORT; | 265 _port = HttpClient.DEFAULT_HTTP_PORT; |
265 } else { | 266 } else { |
266 try { | 267 try { |
267 _port = int.parse(value.substring(pos + 1)); | 268 _port = int.parse(value.substring(pos + 1)); |
268 } on FormatException catch (e) { | 269 } on FormatException catch (e) { |
269 _port = null; | 270 _port = null; |
270 } | 271 } |
271 } | 272 } |
272 } | 273 } |
273 _set(HttpHeaders.HOST, value); | 274 _set(HttpHeaders.HOST, value); |
274 } else { | 275 } else { |
275 throw new HttpException("Unexpected type for header named $name"); | 276 throw new HttpException("Unexpected type for header named $name"); |
276 } | 277 } |
277 } else if (lowerCaseName == HttpHeaders.CONTENT_TYPE) { | 278 } else if (name == HttpHeaders.CONTENT_TYPE) { |
278 _set(HttpHeaders.CONTENT_TYPE, value); | 279 _set(HttpHeaders.CONTENT_TYPE, value); |
279 } else { | 280 } else { |
280 _addValue(lowerCaseName, value); | 281 _addValue(name, value); |
281 } | 282 } |
282 } | 283 } |
283 | 284 |
284 void _addValue(String name, Object value) { | 285 void _addValue(String name, Object value) { |
285 List<String> values = _headers[name]; | 286 List<String> values = _headers[name]; |
286 if (values == null) { | 287 if (values == null) { |
287 values = new List<String>(); | 288 values = new List<String>(); |
288 _headers[name] = values; | 289 _headers[name] = values; |
289 } | 290 } |
290 if (value is DateTime) { | 291 if (value is DateTime) { |
(...skipping 536 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
827 void clear() { | 828 void clear() { |
828 throw new UnsupportedError("Cannot modify an unmodifiable map"); | 829 throw new UnsupportedError("Cannot modify an unmodifiable map"); |
829 } | 830 } |
830 void forEach(void f(K key, V value)) => _map.forEach(f); | 831 void forEach(void f(K key, V value)) => _map.forEach(f); |
831 Iterable<K> get keys => _map.keys; | 832 Iterable<K> get keys => _map.keys; |
832 Iterable<V> get values => _map.values; | 833 Iterable<V> get values => _map.values; |
833 int get length => _map.length; | 834 int get length => _map.length; |
834 bool get isEmpty => _map.isEmpty; | 835 bool get isEmpty => _map.isEmpty; |
835 bool get isNotEmpty => _map.isNotEmpty; | 836 bool get isNotEmpty => _map.isNotEmpty; |
836 } | 837 } |
OLD | NEW |