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:collection'; |
9 import 'dart:convert'; | 9 import 'dart:convert'; |
10 | 10 |
11 // TODO(kevmoo): use UnmodifiableMapView from SDK once 1.4 ships | 11 // TODO(kevmoo): use UnmodifiableMapView from SDK once 1.4 ships |
12 import 'package:collection/wrappers.dart' as pc; | 12 import 'package:collection/wrappers.dart' as pc; |
13 import 'package:http_parser/http_parser.dart'; | 13 import 'package:http_parser/http_parser.dart'; |
14 import 'package:stack_trace/stack_trace.dart'; | 14 import 'package:stack_trace/stack_trace.dart'; |
15 | 15 |
16 /// Represents logic shared between [Request] and [Response]. | 16 /// Represents logic shared between [Request] and [Response]. |
17 abstract class Message { | 17 abstract class Message { |
18 /// The HTTP headers. | 18 /// The HTTP headers. |
19 /// | 19 /// |
20 /// The value is immutable. | 20 /// The value is immutable. |
21 final Map<String, String> headers; | 21 final Map<String, String> headers; |
22 | 22 |
23 /// The streaming body of the message. | 23 /// The streaming body of the message. |
24 /// | 24 /// |
25 /// This can be read via [read] or [readAsString]. | 25 /// This can be read via [read] or [readAsString]. |
26 final Stream<List<int>> _body; | 26 final Stream<List<int>> _body; |
27 | 27 |
| 28 /// Extra context parameters for handlers to pass data to subsequent handlers. |
| 29 /// Note: for requests this means downstream handlers. |
| 30 /// For responses this means upstream handlers. |
| 31 /// |
| 32 /// The value is immutable. |
| 33 final Map<String, Object> context; |
| 34 |
| 35 |
28 /// Creates a new [Message]. | 36 /// Creates a new [Message]. |
29 /// | 37 /// |
30 /// If [headers] is `null`, it is treated as empty. | 38 /// If [headers] is `null`, it is treated as empty. |
31 Message(this._body, {Map<String, String> headers}) | 39 /// If [context] is `null`, it is treated as empty. |
32 : this.headers = _getIgnoreCaseMapView(headers); | 40 Message(this._body, {Map<String, String> headers, |
| 41 Map<String, Object> context : const {}}) |
| 42 : this.headers = _getIgnoreCaseMapView(headers), |
| 43 this.context = context; |
33 | 44 |
34 /// The contents of the content-length field in [headers]. | 45 /// The contents of the content-length field in [headers]. |
35 /// | 46 /// |
36 /// If not set, `null`. | 47 /// If not set, `null`. |
37 int get contentLength { | 48 int get contentLength { |
38 if (_contentLengthCache != null) return _contentLengthCache; | 49 if (_contentLengthCache != null) return _contentLengthCache; |
39 if (!headers.containsKey('content-length')) return null; | 50 if (!headers.containsKey('content-length')) return null; |
40 _contentLengthCache = int.parse(headers['content-length']); | 51 _contentLengthCache = int.parse(headers['content-length']); |
41 return _contentLengthCache; | 52 return _contentLengthCache; |
42 } | 53 } |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 // TODO(kevmoo) generalize this model with a 'canonical map' to align with | 115 // TODO(kevmoo) generalize this model with a 'canonical map' to align with |
105 // similiar implementation in http pkg [BaseRequest]. | 116 // similiar implementation in http pkg [BaseRequest]. |
106 var map = new LinkedHashMap<String, String>( | 117 var map = new LinkedHashMap<String, String>( |
107 equals: (key1, key2) => key1.toLowerCase() == key2.toLowerCase(), | 118 equals: (key1, key2) => key1.toLowerCase() == key2.toLowerCase(), |
108 hashCode: (key) => key.toLowerCase().hashCode); | 119 hashCode: (key) => key.toLowerCase().hashCode); |
109 | 120 |
110 map.addAll(headers); | 121 map.addAll(headers); |
111 | 122 |
112 return new pc.UnmodifiableMapView<String, String>(map); | 123 return new pc.UnmodifiableMapView<String, String>(map); |
113 } | 124 } |
OLD | NEW |