Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add content type library #124

Merged
merged 5 commits into from
Oct 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions lib/src/content_type.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'dart:convert';

import 'package:http_parser/http_parser.dart';

/// Returns the [Encoding] that corresponds to [charset].
///
/// Returns `null` if [charset] is `null` or if no [Encoding] was found that
/// corresponds to [charset].
Encoding encodingForCharset(String charset) {
if (charset == null) return null;
return Encoding.getByName(charset);
}

/// Determines the encoding from the media [type].
///
/// Returns `null` if the charset is not specified in the [type] or if no
/// [Encoding] was found that corresponds to the `charset`.
Encoding encodingForMediaType(MediaType type) {
if (type == null) return null;
return encodingForCharset(type.parameters['charset']);
}
26 changes: 11 additions & 15 deletions lib/src/message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:collection/collection.dart';
import 'package:http_parser/http_parser.dart';

import 'body.dart';
import 'content_type.dart';
import 'http_unmodifiable_map.dart';
import 'utils.dart';

Expand Down Expand Up @@ -80,10 +81,12 @@ abstract class Message {
/// If not set, `null`.
int get contentLength {
if (_contentLengthCache != null) return _contentLengthCache;
if (!headers.containsKey('content-length')) return null;
_contentLengthCache = int.parse(headers['content-length']);
var contentLengthHeader = getHeader(headers, 'content-length');
if (contentLengthHeader == null) return null;
_contentLengthCache = int.parse(contentLengthHeader);
return _contentLengthCache;
}

int _contentLengthCache;

/// The MIME type declared in [headers].
Expand All @@ -92,11 +95,7 @@ abstract class Message {
/// the MIME type, without any Content-Type parameters.
///
/// If [headers] doesn't have a Content-Type header, this will be `null`.
String get mimeType {
var contentType = _contentType;
if (contentType == null) return null;
return contentType.mimeType;
}
String get mimeType => _contentType?.mimeType;

/// The encoding of the body returned by [read].
///
Expand All @@ -105,22 +104,19 @@ abstract class Message {
///
/// If [headers] doesn't have a Content-Type header or it specifies an
/// encoding that [dart:convert] doesn't support, this will be `null`.
Encoding get encoding {
var contentType = _contentType;
if (contentType == null) return null;
if (!contentType.parameters.containsKey('charset')) return null;
return Encoding.getByName(contentType.parameters['charset']);
}
Encoding get encoding => encodingForMediaType(_contentType);

/// The parsed version of the Content-Type header in [headers].
///
/// This is cached for efficient access.
MediaType get _contentType {
if (_contentTypeCache != null) return _contentTypeCache;
if (!headers.containsKey('content-type')) return null;
_contentTypeCache = new MediaType.parse(headers['content-type']);
var contentLengthHeader = getHeader(headers, 'content-type');
if (contentLengthHeader == null) return null;
_contentTypeCache = new MediaType.parse(contentLengthHeader);
return _contentTypeCache;
}

MediaType _contentTypeCache;

/// Returns the message body as byte chunks.
Expand Down
18 changes: 0 additions & 18 deletions lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,6 @@ List<String> split1(String toSplit, String pattern) {
];
}

/// Returns the [Encoding] that corresponds to [charset]. Returns [fallback] if
/// [charset] is null or if no [Encoding] was found that corresponds to
/// [charset].
Encoding encodingForCharset(String charset, [Encoding fallback = LATIN1]) {
if (charset == null) return fallback;
var encoding = Encoding.getByName(charset);
return encoding == null ? fallback : encoding;
}


/// Returns the [Encoding] that corresponds to [charset]. Throws a
/// [FormatException] if no [Encoding] was found that corresponds to [charset].
/// [charset] may not be null.
Encoding requiredEncodingForCharset(String charset) {
var encoding = Encoding.getByName(charset);
if (encoding != null) return encoding;
throw new FormatException('Unsupported encoding "$charset".');
}

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