-
Notifications
You must be signed in to change notification settings - Fork 366
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
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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]) { | ||
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) => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was going to use it when creating the There was a problem hiding this comment. Choose a reason for hiding this commentThe 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}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'; | ||
|
||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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]. | ||
|
@@ -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]. | ||
/// | ||
|
@@ -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. | ||
|
There was a problem hiding this comment.
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 returnnull
if the encoding isn't found rather than havingfallback
parameters.