Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 shelf.message; | 5 library shelf.message; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:collection'; | |
| 8 import 'dart:convert'; | 9 import 'dart:convert'; |
| 9 | 10 |
| 10 import 'package:collection/wrappers.dart'; | 11 // TODO(kevmoo): use UnmodifiableMapView from SDK once 1.4 ships |
| 12 import 'package:collection/wrappers.dart' as pc show UnmodifiableMapView; | |
|
nweiz
2014/04/08 20:23:09
There's never a reason to use "show" and "as" in t
kevmoo
2014/04/08 21:41:13
Done.
| |
| 11 import 'package:http_parser/http_parser.dart'; | 13 import 'package:http_parser/http_parser.dart'; |
| 12 import 'package:stack_trace/stack_trace.dart'; | 14 import 'package:stack_trace/stack_trace.dart'; |
| 13 | 15 |
| 14 /// Represents logic shared between [Request] and [Response]. | 16 /// Represents logic shared between [Request] and [Response]. |
| 15 abstract class Message { | 17 class Message { |
| 16 /// The HTTP headers. | 18 /// The HTTP headers. |
| 17 /// | 19 /// |
| 18 /// The value is immutable. | 20 /// The value is immutable. |
| 19 final Map<String, String> headers; | 21 final Map<String, String> headers; |
| 20 | 22 |
| 21 /// The streaming body of the message. | 23 /// The streaming body of the message. |
| 22 /// | 24 /// |
| 23 /// This can be read via [read] or [readAsString]. | 25 /// This can be read via [read] or [readAsString]. |
| 24 final Stream<List<int>> _body; | 26 final Stream<List<int>> _body; |
| 25 | 27 |
| 26 Message(UnmodifiableMapView<String, String> headers, this._body) | 28 Message(Map<String, String> headers, this._body) |
| 27 : this.headers = headers; | 29 : this.headers = _getIgnoreCaseMapView(headers); |
| 28 | 30 |
| 29 /// The contents of the content-length field in [headers]. | 31 /// The contents of the content-length field in [headers]. |
| 30 /// | 32 /// |
| 31 /// If not set, `null`. | 33 /// If not set, `null`. |
| 32 int get contentLength { | 34 int get contentLength { |
| 33 if (_contentLengthCache != null) return _contentLengthCache; | 35 if (_contentLengthCache != null) return _contentLengthCache; |
| 34 if (!headers.containsKey('content-length')) return null; | 36 if (!headers.containsKey('content-length')) return null; |
| 35 _contentLengthCache = int.parse(headers['content-length']); | 37 _contentLengthCache = int.parse(headers['content-length']); |
| 36 return _contentLengthCache; | 38 return _contentLengthCache; |
| 37 } | 39 } |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 85 /// Otherwise the encoding is taken from the Content-Type header. If that | 87 /// Otherwise the encoding is taken from the Content-Type header. If that |
| 86 /// doesn't exist or doesn't have a "charset" parameter, UTF-8 is used. | 88 /// doesn't exist or doesn't have a "charset" parameter, UTF-8 is used. |
| 87 /// | 89 /// |
| 88 /// This calls [read] internally, which can only be called once. | 90 /// This calls [read] internally, which can only be called once. |
| 89 Future<String> readAsString([Encoding encoding]) { | 91 Future<String> readAsString([Encoding encoding]) { |
| 90 if (encoding == null) encoding = this.encoding; | 92 if (encoding == null) encoding = this.encoding; |
| 91 if (encoding == null) encoding = UTF8; | 93 if (encoding == null) encoding = UTF8; |
| 92 return Chain.track(encoding.decodeStream(read())); | 94 return Chain.track(encoding.decodeStream(read())); |
| 93 } | 95 } |
| 94 } | 96 } |
| 97 | |
| 98 pc.UnmodifiableMapView _getIgnoreCaseMapView(Map headers) { | |
|
nweiz
2014/04/08 20:23:09
Document this.
kevmoo
2014/04/08 21:41:13
Done.
| |
| 99 var map = new LinkedHashMap( | |
| 100 equals: (key1, key2) => key1.toLowerCase() == key2.toLowerCase(), | |
| 101 hashCode: (key) => key.toLowerCase().hashCode); | |
|
nweiz
2014/04/08 20:23:09
Add a TODO to use a canonicalized map once somethi
kevmoo
2014/04/08 21:41:13
Done.
| |
| 102 | |
| 103 map.addAll(headers); | |
| 104 | |
| 105 // TODO(kevmoo): use UnmodifiableMapView from SDK once 1.4 ships | |
|
nweiz
2014/04/08 20:23:09
You don't need this TODO twice in the same file.
kevmoo
2014/04/08 21:41:13
Done.
| |
| 106 return new pc.UnmodifiableMapView(map); | |
| 107 } | |
| OLD | NEW |