Пример #1
0
        private void ParseValue()
        {
            int offset = 0;

            try
            {
                // the disposition MUST be the first parameter in the string
                _dispositionType = MailBnfHelper.ReadToken(_disposition, ref offset, null);

                // disposition MUST not be empty
                if (string.IsNullOrEmpty(_dispositionType))
                {
                    throw new FormatException(Strings.MailHeaderFieldMalformedHeader);
                }

                // now we know that there are parameters so we must initialize or clear
                // and parse
                if (_parameters == null)
                {
                    _parameters = new TrackingValidationObjectDictionary(s_validators);
                }
                else
                {
                    _parameters.Clear();
                }

                while (MailBnfHelper.SkipCFWS(_disposition, ref offset))
                {
                    // ensure that the separator charactor is present
                    if (_disposition[offset++] != ';')
                    {
                        throw new FormatException(string.Format(Strings.MailHeaderFieldInvalidCharacter, _disposition[offset - 1]));
                    }

                    // skip whitespace and see if there's anything left to parse or if we're done
                    if (!MailBnfHelper.SkipCFWS(_disposition, ref offset))
                    {
                        break;
                    }

                    string paramAttribute = MailBnfHelper.ReadParameterAttribute(_disposition, ref offset, null);
                    string paramValue;

                    // verify the next character after the parameter is correct
                    if (_disposition[offset++] != '=')
                    {
                        throw new FormatException(Strings.MailHeaderFieldMalformedHeader);
                    }

                    if (!MailBnfHelper.SkipCFWS(_disposition, ref offset))
                    {
                        // parameter was at end of string and has no value
                        // this is not valid
                        throw new FormatException(Strings.ContentDispositionInvalid);
                    }

                    paramValue = _disposition[offset] == '"' ?
                                 MailBnfHelper.ReadQuotedString(_disposition, ref offset, null) :
                                 MailBnfHelper.ReadToken(_disposition, ref offset, null);

                    // paramValue could potentially still be empty if it was a valid quoted string that
                    // contained no inner value.  this is invalid
                    if (string.IsNullOrEmpty(paramAttribute) || string.IsNullOrEmpty(paramValue))
                    {
                        throw new FormatException(Strings.ContentDispositionInvalid);
                    }

                    // if validation is needed, the parameters dictionary will have a validator registered
                    // for the parameter that is being set so no additional formatting checks are needed here
                    Parameters.Add(paramAttribute, paramValue);
                }
            }
            catch (FormatException exception)
            {
                // it's possible that something in MailBNFHelper could throw so ensure that we catch it and wrap it
                // so that the exception has the correct text
                throw new FormatException(Strings.ContentDispositionInvalid, exception);
            }

            _parameters.IsChanged = false;
        }
Пример #2
0
        // Helper methods.

        private void ParseValue()
        {
            int       offset    = 0;
            Exception exception = null;

            try
            {
                _mediaType = MailBnfHelper.ReadToken(_type, ref offset, null);
                if (_mediaType == null || _mediaType.Length == 0 || offset >= _type.Length || _type[offset++] != '/')
                {
                    exception = new FormatException(Strings.ContentTypeInvalid);
                }

                if (exception == null)
                {
                    _subType = MailBnfHelper.ReadToken(_type, ref offset, null);
                    if (_subType == null || _subType.Length == 0)
                    {
                        exception = new FormatException(Strings.ContentTypeInvalid);
                    }
                }

                if (exception == null)
                {
                    while (MailBnfHelper.SkipCFWS(_type, ref offset))
                    {
                        if (_type[offset++] != ';')
                        {
                            exception = new FormatException(Strings.ContentTypeInvalid);
                            break;
                        }

                        if (!MailBnfHelper.SkipCFWS(_type, ref offset))
                        {
                            break;
                        }

                        string paramAttribute = MailBnfHelper.ReadParameterAttribute(_type, ref offset, null);

                        if (paramAttribute == null || paramAttribute.Length == 0)
                        {
                            exception = new FormatException(Strings.ContentTypeInvalid);
                            break;
                        }

                        string paramValue;
                        if (offset >= _type.Length || _type[offset++] != '=')
                        {
                            exception = new FormatException(Strings.ContentTypeInvalid);
                            break;
                        }

                        if (!MailBnfHelper.SkipCFWS(_type, ref offset))
                        {
                            exception = new FormatException(Strings.ContentTypeInvalid);
                            break;
                        }

                        paramValue = _type[offset] == '"' ?
                                     MailBnfHelper.ReadQuotedString(_type, ref offset, null) :
                                     MailBnfHelper.ReadToken(_type, ref offset, null);

                        if (paramValue == null)
                        {
                            exception = new FormatException(Strings.ContentTypeInvalid);
                            break;
                        }

                        _parameters.Add(paramAttribute, paramValue);
                    }
                }
                _parameters.IsChanged = false;
            }
            catch (FormatException)
            {
                throw new FormatException(Strings.ContentTypeInvalid);
            }

            if (exception != null)
            {
                throw new FormatException(Strings.ContentTypeInvalid);
            }
        }