コード例 #1
0
        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));
        }
コード例 #2
0
        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);
        }