Remove DataUri.
Closes #6
R=kevmoo@google.com
Review URL: https://codereview.chromium.org//1516133002 .
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 51e21cb..9b54230 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,6 +1,10 @@
-## 1.1.0
+## 2.0.0
-* Added a `DataUri` class for encoding and decoding data URIs.
+* Removed the `DataUri` class. It's redundant with the `Uri.data` getter that's
+ coming in Dart 1.14, and the `DataUri.data` field in particular was an invalid
+ override of that field.
+
+## 1.1.0
* The MIME spec says that media types and their parameter names are
case-insensitive. Accordingly, `MediaType` now uses a case-insensitive map for
diff --git a/lib/http_parser.dart b/lib/http_parser.dart
index a03c84b..77c75c0 100644
--- a/lib/http_parser.dart
+++ b/lib/http_parser.dart
@@ -6,7 +6,6 @@
export 'src/authentication_challenge.dart';
export 'src/case_insensitive_map.dart';
-export 'src/data_uri.dart';
export 'src/http_date.dart';
export 'src/media_type.dart';
export 'src/web_socket.dart';
diff --git a/lib/src/data_uri.dart b/lib/src/data_uri.dart
deleted file mode 100644
index b8cdee9..0000000
--- a/lib/src/data_uri.dart
+++ /dev/null
@@ -1,328 +0,0 @@
-// Copyright (c) 2015, 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:convert/convert.dart';
-import 'package:crypto/crypto.dart';
-import 'package:string_scanner/string_scanner.dart';
-
-import 'media_type.dart';
-import 'scan.dart';
-import 'utils.dart';
-
-/// Like [whitespace] from scan.dart, except that it matches URI-encoded
-/// whitespace rather than literal characters.
-final _whitespace = new RegExp(r'(?:(?:%0D%0A)?(?:%20|%09)+)*');
-
-/// A converter for percent encoding strings using UTF-8.
-final _utf8Percent = UTF8.fuse(percent);
-
-/// A class representing a `data:` URI that provides access to its [mediaType]
-/// and the [data] it contains.
-///
-/// Data can be encoded as a `data:` URI using [encode] or [encodeString], and
-/// decoded using [decode].
-///
-/// This implementation is based on [RFC 2397][rfc], but as that RFC is
-/// [notoriously ambiguous][ambiguities], some judgment calls have been made.
-/// This class tries to match browsers' data URI logic, to ensure that it can
-/// losslessly parse its own output, and to accept as much input as it can make
-/// sense of. A balance has been struck between these goals so that while none
-/// of them have been accomplished perfectly, all of them are close enough for
-/// practical use.
-///
-/// [rfc]: http://tools.ietf.org/html/rfc2397
-/// [ambiguities]: https://simonsapin.github.io/data-urls/
-///
-/// Some particular notes on the behavior:
-///
-/// * When encoding, all characters that are not [reserved][] in the type,
-/// subtype, parameter names, and parameter values of media types are
-/// percent-encoded using UTF-8.
-///
-/// * When decoding, the type, subtype, parameter names, and parameter values of
-/// media types are percent-decoded using UTF-8. Parameter values are allowed
-/// to contain non-token characters once decoded, but the other tokens are
-/// not.
-///
-/// * As per the spec, quoted-string parameters are not supported when decoding.
-///
-/// * Query components are included in the decoding algorithm, but fragments are
-/// not.
-///
-/// * Invalid media types and parameters will raise exceptions when decoding.
-/// This is standard for Dart parsers but contrary to browser behavior.
-///
-/// * The URL and filename-safe base64 alphabet is accepted when decoding but
-/// never emitted when encoding, since browsers don't support it.
-///
-/// [lws]: https://tools.ietf.org/html/rfc2616#section-2.2
-/// [reserved]: https://tools.ietf.org/html/rfc3986#section-2.2
-class DataUri implements Uri {
- /// The inner URI to which all [Uri] methods are forwarded.
- final Uri _inner;
-
- /// The byte data contained in the data URI.
- final List<int> data;
-
- /// The media type declared for the data URI.
- ///
- /// This defaults to `text/plain;charset=US-ASCII`.
- final MediaType mediaType;
-
- /// The encoding declared by the `charset` parameter in [mediaType].
- ///
- /// If [mediaType] has no `charset` parameter, this defaults to [ASCII]. If
- /// the `charset` parameter declares an encoding that can't be found using
- /// [Encoding.getByName], this returns `null`.
- Encoding get declaredEncoding {
- var charset = mediaType.parameters["charset"];
- return charset == null ? ASCII : Encoding.getByName(charset);
- }
-
- /// Creates a new data URI with the given [mediaType] and [data].
- ///
- /// If [base64] is `true` (the default), the data is base64-encoded;
- /// otherwise, it's percent-encoded.
- ///
- /// If [encoding] is passed or [mediaType] declares a `charset` parameter,
- /// [data] is encoded using that encoding. Otherwise, it's encoded using
- /// [UTF8] or [ASCII] depending on whether it contains any non-ASCII
- /// characters.
- ///
- /// Throws [ArgumentError] if [mediaType] and [encoding] disagree on the
- /// encoding, and an [UnsupportedError] if [mediaType] defines an encoding
- /// that's not supported by [Encoding.getByName].
- factory DataUri.encodeString(String data, {bool base64: true,
- MediaType mediaType, Encoding encoding}) {
- if (mediaType == null) mediaType = new MediaType("text", "plain");
-
- var charset = mediaType.parameters["charset"];
- var bytes;
- if (encoding != null) {
- if (charset == null) {
- mediaType = mediaType.change(parameters: {"charset": encoding.name});
- } else if (Encoding.getByName(charset) != encoding) {
- throw new ArgumentError("Media type charset '$charset' disagrees with "
- "encoding '${encoding.name}'.");
- }
- bytes = encoding.encode(data);
- } else if (charset != null) {
- encoding = Encoding.getByName(charset);
- if (encoding == null) {
- throw new UnsupportedError(
- 'Unsupported media type charset "$charset".');
- }
- bytes = encoding.encode(data);
- } else if (data.codeUnits.every((codeUnit) => codeUnit < 0x80)) {
- // If the data is pure ASCII, don't bother explicitly defining a charset.
- bytes = data.codeUnits;
- } else {
- // If the data isn't pure ASCII, default to UTF-8.
- bytes = UTF8.encode(data);
- mediaType = mediaType.change(parameters: {"charset": "utf-8"});
- }
-
- return new DataUri.encode(bytes, base64: base64, mediaType: mediaType);
- }
-
- /// Creates a new data URI with the given [mediaType] and [data].
- ///
- /// If [base64] is `true` (the default), the data is base64-encoded;
- /// otherwise, it's percent-encoded.
- factory DataUri.encode(List<int> data, {bool base64: true,
- MediaType mediaType}) {
- mediaType ??= new MediaType('text', 'plain');
-
- var buffer = new StringBuffer();
-
- // Manually stringify the media type because [section 3][rfc] requires that
- // parameter values should have non-token characters URL-escaped rather than
- // emitting them as quoted-strings. This also allows us to omit text/plain
- // if possible.
- //
- // [rfc]: http://tools.ietf.org/html/rfc2397#section-3
- if (mediaType.type != 'text' || mediaType.subtype != 'plain') {
- buffer.write(_utf8Percent.encode(mediaType.type));
- buffer.write("/");
- buffer.write(_utf8Percent.encode(mediaType.subtype));
- }
-
- mediaType.parameters.forEach((attribute, value) {
- buffer.write(";${_utf8Percent.encode(attribute)}=");
- buffer.write(_utf8Percent.encode(value));
- });
-
- if (base64) {
- buffer.write(";base64,");
- // *Don't* use the URL-safe encoding scheme, since browsers don't actually
- // support it.
- buffer.write(CryptoUtils.bytesToBase64(data));
- } else {
- buffer.write(",");
- buffer.write(percent.encode(data));
- }
-
- return new DataUri._(data, mediaType,
- new Uri(scheme: 'data', path: buffer.toString()));
- }
-
- /// Decodes [uri] to make its [data] and [mediaType] available.
- ///
- /// [uri] may be a [Uri] or a [String].
- ///
- /// Throws an [ArgumentError] if [uri] is an invalid type or has a scheme
- /// other than `data:`. Throws a [FormatException] if parsing fails.
- factory DataUri.decode(uri) {
- if (uri is String) {
- uri = Uri.parse(uri);
- } else if (uri is! Uri) {
- throw new ArgumentError.value(uri, "uri", "Must be a String or a Uri.");
- }
-
- if (uri.scheme != 'data') {
- throw new ArgumentError.value(uri, "uri", "Can only decode a data: URI.");
- }
-
- return wrapFormatException("data URI", uri.toString(), () {
- // Remove the fragment, as per https://simonsapin.github.io/data-urls/.
- // TODO(nweiz): Use Uri.removeFragment once sdk#24593 is fixed.
- var string = uri.toString();
- var fragment = string.indexOf('#');
- if (fragment != -1) string = string.substring(0, fragment);
- var scanner = new StringScanner(string);
- scanner.expect('data:');
-
- // Manually scan the media type for three reasons:
- //
- // * Media type parameter values that aren't valid tokens are URL-encoded
- // rather than quoted.
- //
- // * The media type may be omitted without omitting the parameters.
- //
- // * We need to be able to stop once we reach `;base64,`, even though at
- // first it looks like a parameter.
- var type;
- var subtype;
- var implicitType = false;
- if (scanner.scan(token)) {
- type = _verifyToken(scanner);
- scanner.expect('/');
- subtype = _expectToken(scanner);
- } else {
- type = 'text';
- subtype = 'plain';
- implicitType = true;
- }
-
- // Scan the parameters, up through ";base64" or a comma.
- var parameters = {};
- var base64 = false;
- while (scanner.scan(';')) {
- var attribute = _expectToken(scanner);
-
- if (attribute != 'base64') {
- scanner.expect('=');
- } else if (!scanner.scan('=')) {
- base64 = true;
- break;
- }
-
- // Don't use [_expectToken] because the value uses percent-encoding to
- // escape non-token characters.
- scanner.expect(token);
- parameters[attribute] = _utf8Percent.decode(scanner.lastMatch[0]);
- }
- scanner.expect(',');
-
- if (implicitType && parameters.isEmpty) {
- parameters = {"charset": "US-ASCII"};
- }
-
- var mediaType = new MediaType(type, subtype, parameters);
-
- var data = base64
- ? CryptoUtils.base64StringToBytes(scanner.rest)
- : percent.decode(scanner.rest);
-
- return new DataUri._(data, mediaType, uri);
- });
- }
-
- /// Returns the percent-decoded value of the last MIME token scanned by
- /// [scanner].
- ///
- /// Throws a [FormatException] if it's not a valid token after
- /// percent-decoding.
- static String _verifyToken(StringScanner scanner) {
- var value = _utf8Percent.decode(scanner.lastMatch[0]);
- if (!value.contains(nonToken)) return value;
- scanner.error("Invalid token.");
- return null;
- }
-
- /// Scans [scanner] through a MIME token and returns its percent-decoded
- /// value.
- ///
- /// Throws a [FormatException] if it's not a valid token after
- /// percent-decoding.
- static String _expectToken(StringScanner scanner) {
- scanner.expect(token, name: "a token");
- return _verifyToken(scanner);
- }
-
- DataUri._(this.data, this.mediaType, this._inner);
-
- /// Returns the decoded [data] decoded using [encoding].
- ///
- /// [encoding] defaults to [declaredEncoding]. If the declared encoding isn't
- /// supported by [Encoding.getByName] and [encoding] isn't passed, this throws
- /// an [UnsupportedError].
- String dataAsString({Encoding encoding}) {
- encoding ??= declaredEncoding;
- if (encoding == null) {
- throw new UnsupportedError(
- 'Unsupported media type charset '
- '"${mediaType.parameters["charset"]}".');
- }
-
- return encoding.decode(data);
- }
-
- String get scheme => _inner.scheme;
- String get authority => _inner.authority;
- String get userInfo => _inner.userInfo;
- String get host => _inner.host;
- int get port => _inner.port;
- String get path => _inner.path;
- String get query => _inner.query;
- String get fragment => _inner.fragment;
- Uri replace({String scheme, String userInfo, String host, int port,
- String path, Iterable<String> pathSegments, String query,
- Map<String, String> queryParameters, String fragment}) =>
- _inner.replace(
- scheme: scheme, userInfo: userInfo, host: host, port: port,
- path: path, pathSegments: pathSegments, query: query,
- queryParameters: queryParameters, fragment: fragment);
- Uri removeFragment() => _inner.removeFragment();
- List<String> get pathSegments => _inner.pathSegments;
- Map<String, String> get queryParameters => _inner.queryParameters;
- Uri normalizePath() => _inner.normalizePath();
- bool get isAbsolute => _inner.isAbsolute;
- Uri resolve(String reference) => _inner.resolve(reference);
- Uri resolveUri(Uri reference) => _inner.resolveUri(reference);
- bool get hasScheme => _inner.hasScheme;
- bool get hasAuthority => _inner.hasAuthority;
- bool get hasPort => _inner.hasPort;
- bool get hasQuery => _inner.hasQuery;
- bool get hasFragment => _inner.hasFragment;
- bool get hasEmptyPath => _inner.hasEmptyPath;
- bool get hasAbsolutePath => _inner.hasAbsolutePath;
- String get origin => _inner.origin;
- String toFilePath({bool windows}) => _inner.toFilePath(windows: windows);
- String toString() => _inner.toString();
- bool operator==(other) => _inner == other;
- int get hashCode => _inner.hashCode;
-}
diff --git a/pubspec.yaml b/pubspec.yaml
index a78597f..930db8f 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -1,17 +1,15 @@
name: http_parser
-version: 1.1.0
+version: 2.0.0
author: "Dart Team <misc@dartlang.org>"
homepage: https://github.com/dart-lang/http_parser
description: >
A platform-independent package for parsing and serializing HTTP formats.
dependencies:
- convert: "^1.0.0"
collection: ">=0.9.1 <2.0.0"
crypto: "^0.9.0"
source_span: "^1.0.0"
string_scanner: ">=0.0.0 <0.2.0"
dev_dependencies:
- charcode: "^1.1.0"
test: "^0.12.0"
environment:
- sdk: ">=1.12.0 <2.0.0"
+ sdk: ">=1.8.0 <2.0.0"
diff --git a/test/data_uri_test.dart b/test/data_uri_test.dart
deleted file mode 100644
index da97cfe..0000000
--- a/test/data_uri_test.dart
+++ /dev/null
@@ -1,241 +0,0 @@
-// Copyright (c) 2015, 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:charcode/charcode.dart';
-import 'package:http_parser/http_parser.dart';
-import 'package:test/test.dart';
-
-void main() {
- group("encode", () {
- test("base64-encodes data by default", () {
- var uri = new DataUri.encode([1, 2, 3, 4]);
- expect(uri.toString(), equals("data:;base64,AQIDBA=="));
- });
-
- test("doesn't use URL-safe base64 encoding", () {
- var uri = new DataUri.encode([0xFB, 0xFF]);
- expect(uri.toString(), equals("data:;base64,+/8="));
- });
-
- test("percent-encodes data if base64 is disabled", () {
- var uri = new DataUri.encode([$a, $B, $plus, $slash, 0xFF],
- base64: false);
- expect(uri.toString(), equals("data:,aB%2B%2F%FF"));
- });
-
- test("includes a media type and its parameters", () {
- var mediaType = new MediaType('text', 'html', {
- 'foo': 'bar',
- 'baz': 'bang'
- });
- var uri = new DataUri.encode([], mediaType: mediaType);
- expect(uri.toString(), equals('data:text/html;foo=bar;baz=bang;base64,'));
- });
-
- test("percent-encodes the media type and its parameters", () {
- var mediaType = new MediaType('te=xt', 'ht%ml', {'f;oo': 'ba,r'});
- var uri = new DataUri.encode([], mediaType: mediaType);
- expect(uri.toString(),
- equals('data:te%3Dxt/ht%25ml;f%3Boo=ba%2Cr;base64,'));
- });
-
- test("UTF-8 encodes non-ASCII characters", () {
- var mediaType = new MediaType('tëxt', 'ћtml', {'føo': 'bår'});
- var uri = new DataUri.encode([], mediaType: mediaType);
- expect(uri.toString(),
- equals('data:t%C3%ABxt/%D1%9Btml;f%C3%B8o=b%C3%A5r;base64,'));
- });
-
- test("doesn't include a text/plain media type", () {
- var mediaType = new MediaType('text', 'plain', {'foo': 'bar'});
- var uri = new DataUri.encode([], mediaType: mediaType);
- expect(uri.toString(), equals('data:;foo=bar;base64,'));
- });
-
- group("with a string", () {
- test("defaults to ASCII if it's sufficient", () {
- var uri = new DataUri.encodeString('foo');
- expect(uri.toString(), equals("data:;base64,Zm9v"));
- });
-
- test("defaults to UTF-8 encoding if it's needed", () {
- var uri = new DataUri.encodeString('føo');
- expect(uri.toString(), equals("data:;charset=utf-8;base64,ZsO4bw=="));
- });
-
- test("obeys a passed encoding", () {
- var uri = new DataUri.encodeString('føo', encoding: LATIN1);
- expect(uri.toString(), equals("data:;charset=iso-8859-1;base64,Zvhv"));
- });
-
- test("obeys a media type encoding", () {
- var mediaType = new MediaType('text', 'plain',
- {'charset': 'iso-8859-1'});
- var uri = new DataUri.encodeString('føo', mediaType: mediaType);
- expect(uri.toString(), equals("data:;charset=iso-8859-1;base64,Zvhv"));
- });
-
- test("obeys a passed encoding that matches a media type encoding", () {
- var mediaType = new MediaType('text', 'plain',
- {'charset': 'iso-8859-1'});
- var uri = new DataUri.encodeString('føo',
- encoding: LATIN1, mediaType: mediaType);
- expect(uri.toString(), equals("data:;charset=iso-8859-1;base64,Zvhv"));
- });
-
- test("throws if a media type encoding is unsupported", () {
- var mediaType = new MediaType('text', 'plain', {'charset': 'fblthp'});
- expect(() => new DataUri.encodeString('føo', mediaType: mediaType),
- throwsUnsupportedError);
- });
-
- test("throws if a passed encoding disagrees with a media type encoding",
- () {
- var mediaType = new MediaType('text', 'plain', {'charset': 'utf-8'});
- expect(() {
- new DataUri.encodeString('føo',
- encoding: LATIN1, mediaType: mediaType);
- }, throwsArgumentError);
- });
- });
- });
-
- group("decode", () {
- test("decodes a base64 URI", () {
- var uri = new DataUri.decode("data:;base64,AQIDBA==");
- expect(uri.data, equals([1, 2, 3, 4]));
- });
-
- test("decodes a percent-encoded URI", () {
- var uri = new DataUri.decode("data:,aB%2B%2F%FF");
- expect(uri.data, equals([$a, $B, $plus, $slash, 0xFF]));
- });
-
- test("decodes a media type and its parameters", () {
- var uri = new DataUri.decode("data:text/html;foo=bar;baz=bang;base64,");
- expect(uri.data, isEmpty);
- expect(uri.mediaType.type, equals('text'));
- expect(uri.mediaType.subtype, equals('html'));
- expect(uri.mediaType.parameters, equals({
- 'foo': 'bar',
- 'baz': 'bang'
- }));
- });
-
- test("defaults to a text/plain media type", () {
- var uri = new DataUri.decode("data:;base64,");
- expect(uri.mediaType.type, equals('text'));
- expect(uri.mediaType.subtype, equals('plain'));
- expect(uri.mediaType.parameters, equals({'charset': 'US-ASCII'}));
- });
-
- test("defaults to a text/plain media type with parameters", () {
- var uri = new DataUri.decode("data:;foo=bar;base64,");
- expect(uri.mediaType.type, equals('text'));
- expect(uri.mediaType.subtype, equals('plain'));
- expect(uri.mediaType.parameters, equals({'foo': 'bar'}));
- });
-
- test("percent-decodes the media type and its parameters", () {
- var uri = new DataUri.decode(
- 'data:te%78t/ht%6Dl;f%6Fo=ba%2Cr;base64,');
- expect(uri.mediaType.type, equals('text'));
- expect(uri.mediaType.subtype, equals('html'));
- expect(uri.mediaType.parameters, equals({'foo': 'ba,r'}));
- });
-
- test("assumes the URI is UTF-8", () {
- var uri = new DataUri.decode(
- 'data:t%C3%ABxt/%D1%9Btml;f%C3%B8o=b%C3%A5r;base64,');
- expect(uri.mediaType.type, equals('tëxt'));
- expect(uri.mediaType.subtype, equals('ћtml'));
- expect(uri.mediaType.parameters, equals({'føo': 'bår'}));
- });
-
- test("allows a parameter named base64", () {
- var uri = new DataUri.decode("data:;base64=no,foo");
- expect(uri.mediaType.parameters, equals({'base64': 'no'}));
- expect(uri.data, equals([$f, $o, $o]));
- });
-
- test("includes the query", () {
- var uri = new DataUri.decode("data:,a?b=c");
- expect(uri.data, equals([$a, $question, $b, $equal, $c]));
- });
-
- test("doesn't include the fragment", () {
- var uri = new DataUri.decode("data:,a#b=c");
- expect(uri.data, equals([$a]));
- });
-
- test("supports the URL-safe base64 alphabet", () {
- var uri = new DataUri.decode("data:;base64,-_8%3D");
- expect(uri.data, equals([0xFB, 0xFF]));
- });
-
- group("forbids", () {
- test("a parameter with the wrong type", () {
- expect(() => new DataUri.decode(12), throwsArgumentError);
- });
-
- test("a parameter with the wrong scheme", () {
- expect(() => new DataUri.decode("http:;base64,"), throwsArgumentError);
- });
-
- test("non-token characters in invalid positions", () {
- expect(() => new DataUri.decode("data:text//plain;base64,"),
- throwsFormatException);
- expect(() => new DataUri.decode("data:text/plain;;base64,"),
- throwsFormatException);
- expect(() => new DataUri.decode("data:text/plain;/base64,"),
- throwsFormatException);
- expect(() => new DataUri.decode("data:text/plain;,"),
- throwsFormatException);
- expect(() => new DataUri.decode("data:text/plain;base64;"),
- throwsFormatException);
- expect(() => new DataUri.decode("data:text/plain;foo=bar=baz;base64,"),
- throwsFormatException);
- });
-
- test("encoded non-token characters in invalid positions", () {
- expect(() => new DataUri.decode("data:te%2Cxt/plain;base64,"),
- throwsFormatException);
- expect(() => new DataUri.decode("data:text/pl%2Cain;base64,"),
- throwsFormatException);
- expect(() => new DataUri.decode("data:text/plain;f%2Coo=bar;base64,"),
- throwsFormatException);
- });
- });
- });
-
- group("dataAsString", () {
- test("decodes the data as ASCII by default", () {
- var uri = new DataUri.decode("data:;base64,Zm9v");
- expect(uri.dataAsString(), equals("foo"));
-
- uri = new DataUri.decode("data:;base64,ZsO4bw==");
- expect(() => uri.dataAsString(), throwsFormatException);
- });
-
- test("decodes the data using the declared charset", () {
- var uri = new DataUri.decode("data:;charset=iso-8859-1;base64,ZsO4bw==");
- expect(uri.dataAsString(), equals("føo"));
- });
-
- test("throws if the charset isn't supported", () {
- var uri = new DataUri.decode("data:;charset=fblthp;base64,ZsO4bw==");
- expect(() => uri.dataAsString(), throwsUnsupportedError);
- });
-
- test("uses the given encoding in preference to the declared charset", () {
- var uri = new DataUri.decode("data:;charset=fblthp;base64,ZsO4bw==");
- expect(uri.dataAsString(encoding: UTF8), equals("føo"));
-
- uri = new DataUri.decode("data:;charset=utf-8;base64,ZsO4bw==");
- expect(uri.dataAsString(encoding: LATIN1), equals("føo"));
- });
- });
-}