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 final Map<String, List<String>> _headers; | 8 final Map<String, List<String>> _headers; |
9 final String protocolVersion; | 9 final String protocolVersion; |
10 | 10 |
(...skipping 23 matching lines...) Expand all Loading... |
34 List<String> values = _headers[name]; | 34 List<String> values = _headers[name]; |
35 if (values == null) return null; | 35 if (values == null) return null; |
36 if (values.length > 1) { | 36 if (values.length > 1) { |
37 throw new HttpException("More than one value for header $name"); | 37 throw new HttpException("More than one value for header $name"); |
38 } | 38 } |
39 return values[0]; | 39 return values[0]; |
40 } | 40 } |
41 | 41 |
42 void add(String name, value) { | 42 void add(String name, value) { |
43 _checkMutable(); | 43 _checkMutable(); |
44 var lowerCaseName = name.toLowerCase(); | 44 _addAll(name.toLowerCase(), value); |
| 45 } |
| 46 |
| 47 void _addAll(String name, value) { |
45 if (value is List) { | 48 if (value is List) { |
46 for (int i = 0; i < value.length; i++) { | 49 for (int i = 0; i < value.length; i++) { |
47 _add(lowerCaseName, value[i]); | 50 _add(name, value[i]); |
48 } | 51 } |
49 } else { | 52 } else { |
50 _add(lowerCaseName, value); | 53 _add(name, value); |
51 } | 54 } |
52 } | 55 } |
53 | 56 |
54 void set(String name, Object value) { | 57 void set(String name, Object value) { |
| 58 _checkMutable(); |
55 name = name.toLowerCase(); | 59 name = name.toLowerCase(); |
56 _checkMutable(); | 60 _headers.remove(name); |
57 removeAll(name); | 61 _addAll(name, value); |
58 add(name, value); | |
59 } | 62 } |
60 | 63 |
61 void remove(String name, Object value) { | 64 void remove(String name, Object value) { |
62 _checkMutable(); | 65 _checkMutable(); |
63 name = name.toLowerCase(); | 66 name = name.toLowerCase(); |
64 List<String> values = _headers[name]; | 67 List<String> values = _headers[name]; |
65 if (values != null) { | 68 if (values != null) { |
66 int index = values.indexOf(value); | 69 int index = values.indexOf(value); |
67 if (index != -1) { | 70 if (index != -1) { |
68 values.removeRange(index, index + 1); | 71 values.removeRange(index, index + 1); |
(...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
335 _headers[name] = values; | 338 _headers[name] = values; |
336 } | 339 } |
337 if (value is DateTime) { | 340 if (value is DateTime) { |
338 values.add(HttpDate.format(value)); | 341 values.add(HttpDate.format(value)); |
339 } else { | 342 } else { |
340 values.add(value.toString()); | 343 values.add(value.toString()); |
341 } | 344 } |
342 } | 345 } |
343 | 346 |
344 void _set(String name, String value) { | 347 void _set(String name, String value) { |
345 name = name.toLowerCase(); | 348 assert(name == name.toLowerCase()); |
346 List<String> values = new List<String>(); | 349 List<String> values = new List<String>(); |
347 _headers[name] = values; | 350 _headers[name] = values; |
348 values.add(value); | 351 values.add(value); |
349 } | 352 } |
350 | 353 |
351 _checkMutable() { | 354 _checkMutable() { |
352 if (!_mutable) throw new HttpException("HTTP headers are not mutable"); | 355 if (!_mutable) throw new HttpException("HTTP headers are not mutable"); |
353 } | 356 } |
354 | 357 |
355 _updateHostHeader() { | 358 _updateHostHeader() { |
(...skipping 487 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
843 void clear() { | 846 void clear() { |
844 throw new UnsupportedError("Cannot modify an unmodifiable map"); | 847 throw new UnsupportedError("Cannot modify an unmodifiable map"); |
845 } | 848 } |
846 void forEach(void f(K key, V value)) => _map.forEach(f); | 849 void forEach(void f(K key, V value)) => _map.forEach(f); |
847 Iterable<K> get keys => _map.keys; | 850 Iterable<K> get keys => _map.keys; |
848 Iterable<V> get values => _map.values; | 851 Iterable<V> get values => _map.values; |
849 int get length => _map.length; | 852 int get length => _map.length; |
850 bool get isEmpty => _map.isEmpty; | 853 bool get isEmpty => _map.isEmpty; |
851 bool get isNotEmpty => _map.isNotEmpty; | 854 bool get isNotEmpty => _map.isNotEmpty; |
852 } | 855 } |
OLD | NEW |