| 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 /// Extra context that can be used by for middleware and handlers. |
| 24 /// |
| 25 /// For requests, this is used to pass data to inner middleware and handlers; |
| 26 /// for responses, it's used to pass data to outer middleware and handlers. |
| 27 /// |
| 28 /// Context properties that are used by a particular package should begin with |
| 29 /// that package's name followed by a period. For example, if [logRequests] |
| 30 /// wanted to take a prefix, its property name would be `"shelf.prefix"`, |
| 31 /// since it's in the `shelf` package. |
| 32 /// |
| 33 /// The value is immutable. |
| 34 final Map<String, Object> context; |
| 35 |
| 23 /// The streaming body of the message. | 36 /// The streaming body of the message. |
| 24 /// | 37 /// |
| 25 /// This can be read via [read] or [readAsString]. | 38 /// This can be read via [read] or [readAsString]. |
| 26 final Stream<List<int>> _body; | 39 final Stream<List<int>> _body; |
| 27 | 40 |
| 28 /// Creates a new [Message]. | 41 /// Creates a new [Message]. |
| 29 /// | 42 /// |
| 30 /// If [headers] is `null`, it is treated as empty. | 43 /// If [headers] is `null`, it is treated as empty. |
| 31 Message(this._body, {Map<String, String> headers}) | 44 Message(this._body, {Map<String, String> headers, |
| 32 : this.headers = _getIgnoreCaseMapView(headers); | 45 Map<String, Object> context}) |
| 46 : this.headers = _getIgnoreCaseMapView(headers), |
| 47 this.context = new pc.UnmodifiableMapView( |
| 48 context == null ? const {} : new Map.from(context)); |
| 33 | 49 |
| 34 /// The contents of the content-length field in [headers]. | 50 /// The contents of the content-length field in [headers]. |
| 35 /// | 51 /// |
| 36 /// If not set, `null`. | 52 /// If not set, `null`. |
| 37 int get contentLength { | 53 int get contentLength { |
| 38 if (_contentLengthCache != null) return _contentLengthCache; | 54 if (_contentLengthCache != null) return _contentLengthCache; |
| 39 if (!headers.containsKey('content-length')) return null; | 55 if (!headers.containsKey('content-length')) return null; |
| 40 _contentLengthCache = int.parse(headers['content-length']); | 56 _contentLengthCache = int.parse(headers['content-length']); |
| 41 return _contentLengthCache; | 57 return _contentLengthCache; |
| 42 } | 58 } |
| (...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 | 120 // TODO(kevmoo) generalize this model with a 'canonical map' to align with |
| 105 // similiar implementation in http pkg [BaseRequest]. | 121 // similiar implementation in http pkg [BaseRequest]. |
| 106 var map = new LinkedHashMap<String, String>( | 122 var map = new LinkedHashMap<String, String>( |
| 107 equals: (key1, key2) => key1.toLowerCase() == key2.toLowerCase(), | 123 equals: (key1, key2) => key1.toLowerCase() == key2.toLowerCase(), |
| 108 hashCode: (key) => key.toLowerCase().hashCode); | 124 hashCode: (key) => key.toLowerCase().hashCode); |
| 109 | 125 |
| 110 map.addAll(headers); | 126 map.addAll(headers); |
| 111 | 127 |
| 112 return new pc.UnmodifiableMapView<String, String>(map); | 128 return new pc.UnmodifiableMapView<String, String>(map); |
| 113 } | 129 } |
| OLD | NEW |