MediaType.parse constructor

MediaType.parse(
  1. String mediaType
)

Parses a media type.

This will throw a FormatError if the media type is invalid.

Implementation

factory MediaType.parse(String mediaType) {
  final scanner = StringScanner(
    mediaType,
  );
  scanner.scan(
    whitespace,
  );
  scanner.expect(
    token,
  );
  final type = scanner.lastMatch![0]!;
  scanner.expect(
    '/',
  );
  scanner.expect(
    token,
  );
  final subtype = scanner.lastMatch![0]!;
  scanner.scan(
    whitespace,
  );

  final parameters = <String, String>{};
  while (scanner.scan(';')) {
    scanner.scan(
      whitespace,
    );
    scanner.expect(
      token,
    );
    final attribute = scanner.lastMatch![0]!;
    scanner.expect(
      '=',
    );

    String value;
    if (scanner.scan(token)) {
      value = scanner.lastMatch![0]!;
    } else {
      value = expectQuotedString(
        scanner,
      );
    }

    scanner.scan(
      whitespace,
    );
    parameters[attribute] = value;
  }

  scanner.expectDone();
  return MediaType(
    type,
    subtype,
    parameters: parameters,
  );
}