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 3 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
30 changes: 30 additions & 0 deletions lib/src/content_type.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// 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 [fallback] if [charset] is null or if no [Encoding] was found that
/// corresponds to [charset].
Encoding encodingForCharset(String charset, [Encoding fallback = LATIN1]) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that we have ??, it's probably simpler to just return null if the encoding isn't found rather than having fallback parameters.

if (charset == null) return fallback;
var encoding = Encoding.getByName(charset);
return encoding == null ? fallback : encoding;
}

/// Determines the encoding from the media [type].
///
/// Returns [fallback] if the charset is not specified in the [type] or if no
/// [Encoding] was found that corresponds to the `charset`.
Encoding encodingForMediaType(MediaType type, [Encoding fallback = LATIN1]) {
if (type == null) return fallback;
return encodingForCharset(type.parameters['charset'], fallback);
}

/// Modifies the media [type]'s [encoding].
MediaType modifyEncoding(MediaType type, Encoding encoding) =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem to be used anywhere, and I'm not sure it would be worth factoring out even if it were...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was going to use it when creating the content-type header from a body and also its in the multipart stuff.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It just doesn't seem like it provides substantial value over just inlining the definition.

type.change(parameters: <String, String>{'charset': encoding.name});
28 changes: 11 additions & 17 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,11 @@ 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']);
return _contentLengthCache;
var contentLengthHeader = getHeader(headers, 'content-length');
if (contentLengthHeader == null) return null;
return _contentLengthCache = int.parse(contentLengthHeader);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We generally avoid using the return value of assignment expressions.

}

int _contentLengthCache;

/// The MIME type declared in [headers].
Expand All @@ -92,11 +94,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 +103,18 @@ 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, null);

/// 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']);
return _contentTypeCache;
var contentLengthHeader = getHeader(headers, 'content-type');
if (contentLengthHeader == null) return null;
return _contentTypeCache = new MediaType.parse(contentLengthHeader);
}

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