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'; | |
9 import 'dart:convert'; | 8 import 'dart:convert'; |
10 | 9 |
11 // TODO(kevmoo): use UnmodifiableMapView from SDK once 1.4 ships | |
12 import 'package:collection/wrappers.dart' as pc; | |
13 import 'package:http_parser/http_parser.dart'; | 10 import 'package:http_parser/http_parser.dart'; |
14 import 'package:stack_trace/stack_trace.dart'; | 11 import 'package:stack_trace/stack_trace.dart'; |
15 | 12 |
| 13 import 'shelf_unmodifiable_map.dart'; |
| 14 |
16 /// Represents logic shared between [Request] and [Response]. | 15 /// Represents logic shared between [Request] and [Response]. |
17 abstract class Message { | 16 abstract class Message { |
18 /// The HTTP headers. | 17 /// The HTTP headers. |
19 /// | 18 /// |
20 /// The value is immutable. | 19 /// The value is immutable. |
21 final Map<String, String> headers; | 20 final Map<String, String> headers; |
22 | 21 |
23 /// Extra context that can be used by for middleware and handlers. | 22 /// Extra context that can be used by for middleware and handlers. |
24 /// | 23 /// |
25 /// For requests, this is used to pass data to inner middleware and handlers; | 24 /// For requests, this is used to pass data to inner middleware and handlers; |
(...skipping 10 matching lines...) Expand all Loading... |
36 /// The streaming body of the message. | 35 /// The streaming body of the message. |
37 /// | 36 /// |
38 /// This can be read via [read] or [readAsString]. | 37 /// This can be read via [read] or [readAsString]. |
39 final Stream<List<int>> _body; | 38 final Stream<List<int>> _body; |
40 | 39 |
41 /// Creates a new [Message]. | 40 /// Creates a new [Message]. |
42 /// | 41 /// |
43 /// If [headers] is `null`, it is treated as empty. | 42 /// If [headers] is `null`, it is treated as empty. |
44 Message(this._body, {Map<String, String> headers, | 43 Message(this._body, {Map<String, String> headers, |
45 Map<String, Object> context}) | 44 Map<String, Object> context}) |
46 : this.headers = _getIgnoreCaseMapView(headers), | 45 : this.headers = new ShelfUnmodifiableMap<String>(headers, |
47 this.context = new pc.UnmodifiableMapView( | 46 ignoreKeyCase: true), |
48 context == null ? const {} : new Map.from(context)); | 47 this.context = new ShelfUnmodifiableMap<Object>(context, |
| 48 ignoreKeyCase: false); |
49 | 49 |
50 /// The contents of the content-length field in [headers]. | 50 /// The contents of the content-length field in [headers]. |
51 /// | 51 /// |
52 /// If not set, `null`. | 52 /// If not set, `null`. |
53 int get contentLength { | 53 int get contentLength { |
54 if (_contentLengthCache != null) return _contentLengthCache; | 54 if (_contentLengthCache != null) return _contentLengthCache; |
55 if (!headers.containsKey('content-length')) return null; | 55 if (!headers.containsKey('content-length')) return null; |
56 _contentLengthCache = int.parse(headers['content-length']); | 56 _contentLengthCache = int.parse(headers['content-length']); |
57 return _contentLengthCache; | 57 return _contentLengthCache; |
58 } | 58 } |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 /// If [encoding] is passed, that's used to decode the body. | 105 /// If [encoding] is passed, that's used to decode the body. |
106 /// Otherwise the encoding is taken from the Content-Type header. If that | 106 /// Otherwise the encoding is taken from the Content-Type header. If that |
107 /// doesn't exist or doesn't have a "charset" parameter, UTF-8 is used. | 107 /// doesn't exist or doesn't have a "charset" parameter, UTF-8 is used. |
108 /// | 108 /// |
109 /// This calls [read] internally, which can only be called once. | 109 /// This calls [read] internally, which can only be called once. |
110 Future<String> readAsString([Encoding encoding]) { | 110 Future<String> readAsString([Encoding encoding]) { |
111 if (encoding == null) encoding = this.encoding; | 111 if (encoding == null) encoding = this.encoding; |
112 if (encoding == null) encoding = UTF8; | 112 if (encoding == null) encoding = UTF8; |
113 return Chain.track(encoding.decodeStream(read())); | 113 return Chain.track(encoding.decodeStream(read())); |
114 } | 114 } |
| 115 |
| 116 /// Creates a new [Message] by copying existing values and applying specified |
| 117 /// changes. |
| 118 Message change({Map<String, String> headers, Map<String, Object> context}); |
115 } | 119 } |
116 | |
117 /// Creates on an unmodifiable map of [headers] with case-insensitive access. | |
118 Map<String, String> _getIgnoreCaseMapView(Map<String, String> headers) { | |
119 if (headers == null) return const {}; | |
120 // TODO(kevmoo) generalize this model with a 'canonical map' to align with | |
121 // similiar implementation in http pkg [BaseRequest]. | |
122 var map = new LinkedHashMap<String, String>( | |
123 equals: (key1, key2) => key1.toLowerCase() == key2.toLowerCase(), | |
124 hashCode: (key) => key.toLowerCase().hashCode); | |
125 | |
126 map.addAll(headers); | |
127 | |
128 return new pc.UnmodifiableMapView<String, String>(map); | |
129 } | |
OLD | NEW |