@@ -9,6 +9,7 @@ import 'package:collection/collection.dart';
9
9
import 'package:http_parser/http_parser.dart' ;
10
10
11
11
import 'body.dart' ;
12
+ import 'content_type.dart' ;
12
13
import 'http_unmodifiable_map.dart' ;
13
14
import 'utils.dart' ;
14
15
@@ -80,10 +81,12 @@ abstract class Message {
80
81
/// If not set, `null` .
81
82
int get contentLength {
82
83
if (_contentLengthCache != null ) return _contentLengthCache;
83
- if (! headers.containsKey ('content-length' )) return null ;
84
- _contentLengthCache = int .parse (headers['content-length' ]);
84
+ var contentLengthHeader = getHeader (headers, 'content-length' );
85
+ if (contentLengthHeader == null ) return null ;
86
+ _contentLengthCache = int .parse (contentLengthHeader);
85
87
return _contentLengthCache;
86
88
}
89
+
87
90
int _contentLengthCache;
88
91
89
92
/// The MIME type declared in [headers] .
@@ -92,11 +95,7 @@ abstract class Message {
92
95
/// the MIME type, without any Content-Type parameters.
93
96
///
94
97
/// If [headers] doesn't have a Content-Type header, this will be `null` .
95
- String get mimeType {
96
- var contentType = _contentType;
97
- if (contentType == null ) return null ;
98
- return contentType.mimeType;
99
- }
98
+ String get mimeType => _contentType? .mimeType;
100
99
101
100
/// The encoding of the body returned by [read] .
102
101
///
@@ -105,22 +104,19 @@ abstract class Message {
105
104
///
106
105
/// If [headers] doesn't have a Content-Type header or it specifies an
107
106
/// encoding that [dart:convert] doesn't support, this will be `null` .
108
- Encoding get encoding {
109
- var contentType = _contentType;
110
- if (contentType == null ) return null ;
111
- if (! contentType.parameters.containsKey ('charset' )) return null ;
112
- return Encoding .getByName (contentType.parameters['charset' ]);
113
- }
107
+ Encoding get encoding => encodingForMediaType (_contentType);
114
108
115
109
/// The parsed version of the Content-Type header in [headers] .
116
110
///
117
111
/// This is cached for efficient access.
118
112
MediaType get _contentType {
119
113
if (_contentTypeCache != null ) return _contentTypeCache;
120
- if (! headers.containsKey ('content-type' )) return null ;
121
- _contentTypeCache = new MediaType .parse (headers['content-type' ]);
114
+ var contentLengthHeader = getHeader (headers, 'content-type' );
115
+ if (contentLengthHeader == null ) return null ;
116
+ _contentTypeCache = new MediaType .parse (contentLengthHeader);
122
117
return _contentTypeCache;
123
118
}
119
+
124
120
MediaType _contentTypeCache;
125
121
126
122
/// Returns the message body as byte chunks.
0 commit comments