Skip to content

Commit 7b0eb3b

Browse files
donny-dontnex3
authored andcommitted
Add content type library (#124)
1 parent 4171b73 commit 7b0eb3b

File tree

3 files changed

+36
-33
lines changed

3 files changed

+36
-33
lines changed

lib/src/content_type.dart

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
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.
4+
5+
import 'dart:convert';
6+
7+
import 'package:http_parser/http_parser.dart';
8+
9+
/// Returns the [Encoding] that corresponds to [charset].
10+
///
11+
/// Returns `null` if [charset] is `null` or if no [Encoding] was found that
12+
/// corresponds to [charset].
13+
Encoding encodingForCharset(String charset) {
14+
if (charset == null) return null;
15+
return Encoding.getByName(charset);
16+
}
17+
18+
/// Determines the encoding from the media [type].
19+
///
20+
/// Returns `null` if the charset is not specified in the [type] or if no
21+
/// [Encoding] was found that corresponds to the `charset`.
22+
Encoding encodingForMediaType(MediaType type) {
23+
if (type == null) return null;
24+
return encodingForCharset(type.parameters['charset']);
25+
}

lib/src/message.dart

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import 'package:collection/collection.dart';
99
import 'package:http_parser/http_parser.dart';
1010

1111
import 'body.dart';
12+
import 'content_type.dart';
1213
import 'http_unmodifiable_map.dart';
1314
import 'utils.dart';
1415

@@ -80,10 +81,12 @@ abstract class Message {
8081
/// If not set, `null`.
8182
int get contentLength {
8283
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);
8587
return _contentLengthCache;
8688
}
89+
8790
int _contentLengthCache;
8891

8992
/// The MIME type declared in [headers].
@@ -92,11 +95,7 @@ abstract class Message {
9295
/// the MIME type, without any Content-Type parameters.
9396
///
9497
/// 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;
10099

101100
/// The encoding of the body returned by [read].
102101
///
@@ -105,22 +104,19 @@ abstract class Message {
105104
///
106105
/// If [headers] doesn't have a Content-Type header or it specifies an
107106
/// 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);
114108

115109
/// The parsed version of the Content-Type header in [headers].
116110
///
117111
/// This is cached for efficient access.
118112
MediaType get _contentType {
119113
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);
122117
return _contentTypeCache;
123118
}
119+
124120
MediaType _contentTypeCache;
125121

126122
/// Returns the message body as byte chunks.

lib/src/utils.dart

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -52,24 +52,6 @@ List<String> split1(String toSplit, String pattern) {
5252
];
5353
}
5454

55-
/// Returns the [Encoding] that corresponds to [charset]. Returns [fallback] if
56-
/// [charset] is null or if no [Encoding] was found that corresponds to
57-
/// [charset].
58-
Encoding encodingForCharset(String charset, [Encoding fallback = LATIN1]) {
59-
if (charset == null) return fallback;
60-
var encoding = Encoding.getByName(charset);
61-
return encoding == null ? fallback : encoding;
62-
}
63-
64-
65-
/// Returns the [Encoding] that corresponds to [charset]. Throws a
66-
/// [FormatException] if no [Encoding] was found that corresponds to [charset].
67-
/// [charset] may not be null.
68-
Encoding requiredEncodingForCharset(String charset) {
69-
var encoding = Encoding.getByName(charset);
70-
if (encoding != null) return encoding;
71-
throw new FormatException('Unsupported encoding "$charset".');
72-
}
7355

7456
/// A regular expression that matches strings that are composed entirely of
7557
/// ASCII-compatible characters.

0 commit comments

Comments
 (0)