public static ContentDisposition Parse(string disposition) { Guard.ArgumentNotNullOrEmptyString(disposition, "disposition"); var offset = 0; var type = MailBnfHelper.ReadToken(disposition, ref offset); if (string.IsNullOrEmpty(type)) { throw new FormatException("Content disposition invalid"); } var parameters = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase); while (MailBnfHelper.SkipCfws(disposition, ref offset)) { if (disposition[offset++] != ';') { throw new FormatException("Content disposition invalid"); } if (!MailBnfHelper.SkipCfws(disposition, ref offset)) { break; } var key = MailBnfHelper.ReadParameterAttribute(disposition, ref offset); if (string.IsNullOrEmpty(key)) { throw new FormatException("Content disposition invalid"); } if (offset >= disposition.Length || disposition[offset++] != '=') { throw new FormatException("Content disposition invalid"); } if (!MailBnfHelper.SkipCfws(disposition, ref offset)) { throw new FormatException("Content disposition invalid"); } var value = disposition[offset] == '"' ? MailBnfHelper.ReadQuotedString(disposition, ref offset, null) : MailBnfHelper.ReadToken(disposition, ref offset); if (value == null) { throw new FormatException("Content disposition invalid"); } parameters.Add(key, value); } return(new ContentDisposition(disposition, type, parameters)); }
private static IDictionary <string, string> Parse(string type, int offset) { var parameters = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase); while (MailBnfHelper.SkipCfws(type, ref offset)) { if (type[offset++] != ';') { throw new FormatException("Content type invalid"); } if (!MailBnfHelper.SkipCfws(type, ref offset)) { break; } var key = MailBnfHelper.ReadParameterAttribute(type, ref offset); if (string.IsNullOrEmpty(key)) { throw new FormatException("Content type invalid"); } if (offset >= type.Length || type[offset++] != '=') { throw new FormatException("Content type invalid"); } if (!MailBnfHelper.SkipCfws(type, ref offset)) { throw new FormatException("Content type invalid"); } var value = type[offset] == '"' ? MailBnfHelper.ReadQuotedString(type, ref offset, null) : MailBnfHelper.ReadToken(type, ref offset); if (value == null) { throw new FormatException("Content type invalid"); } parameters.Add(key, value); } return(parameters); }
public ContentType(string type) { Guard.ArgumentNotNullOrEmptyString(type, "type"); _type = type; var offset = 0; _mediaType = MailBnfHelper.ReadToken(_type, ref offset); if (string.IsNullOrEmpty(_mediaType)) { throw new FormatException("Content type invalid"); } offset++; _subType = MailBnfHelper.ReadToken(_type, ref offset); if (string.IsNullOrEmpty(_subType)) { throw new FormatException("Content type invalid"); } _parameters = Parse(type, offset); }