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:convert'; | 8 import 'dart:convert'; |
9 | 9 |
10 import 'package:collection/wrappers.dart'; | 10 import 'package:collection/wrappers.dart'; |
11 import 'package:http_parser/http_parser.dart'; | 11 import 'package:http_parser/http_parser.dart'; |
12 import 'package:stack_trace/stack_trace.dart'; | 12 import 'package:stack_trace/stack_trace.dart'; |
13 | 13 |
14 /// Represents logic shared between [Request] and [Response]. | 14 /// Represents logic shared between [Request] and [Response]. |
15 abstract class Message { | 15 abstract class Message { |
16 /// The HTTP headers. | 16 /// The HTTP headers. |
17 /// | 17 /// |
18 /// The value is immutable. | 18 /// The value is immutable. |
19 final Map<String, String> headers; | 19 final Map<String, String> headers; |
20 | 20 |
21 /// The streaming body of the message. | 21 /// The streaming body of the message. |
22 /// | 22 /// |
23 /// This can be read via [read] or [readAsString]. | 23 /// This can be read via [read] or [readAsString]. |
24 final Stream<List<int>> _body; | 24 final Stream<List<int>> _body; |
25 | 25 |
26 Message(UnmodifiableMapView<String, String> headers, this._body) | 26 /// Extra context parameters for handlers to pass data to subsequent handlers. |
27 : this.headers = headers; | 27 /// Note: for requests this means downstream handlers. |
| 28 /// For responses this means upstream handlers. |
| 29 /// |
| 30 /// The value is immutable. |
| 31 final Map<String, Object> context; |
| 32 |
| 33 |
| 34 Message(UnmodifiableMapView<String, String> headers, this._body, |
| 35 [ UnmodifiableMapView<String, Object> context ]) |
| 36 : this.headers = headers, |
| 37 this.context = context; |
28 | 38 |
29 /// The contents of the content-length field in [headers]. | 39 /// The contents of the content-length field in [headers]. |
30 /// | 40 /// |
31 /// If not set, `null`. | 41 /// If not set, `null`. |
32 int get contentLength { | 42 int get contentLength { |
33 if (_contentLengthCache != null) return _contentLengthCache; | 43 if (_contentLengthCache != null) return _contentLengthCache; |
34 if (!headers.containsKey('content-length')) return null; | 44 if (!headers.containsKey('content-length')) return null; |
35 _contentLengthCache = int.parse(headers['content-length']); | 45 _contentLengthCache = int.parse(headers['content-length']); |
36 return _contentLengthCache; | 46 return _contentLengthCache; |
37 } | 47 } |
(...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 | 95 /// 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. | 96 /// doesn't exist or doesn't have a "charset" parameter, UTF-8 is used. |
87 /// | 97 /// |
88 /// This calls [read] internally, which can only be called once. | 98 /// This calls [read] internally, which can only be called once. |
89 Future<String> readAsString([Encoding encoding]) { | 99 Future<String> readAsString([Encoding encoding]) { |
90 if (encoding == null) encoding = this.encoding; | 100 if (encoding == null) encoding = this.encoding; |
91 if (encoding == null) encoding = UTF8; | 101 if (encoding == null) encoding = UTF8; |
92 return Chain.track(encoding.decodeStream(read())); | 102 return Chain.track(encoding.decodeStream(read())); |
93 } | 103 } |
94 } | 104 } |
OLD | NEW |