Exemplo n.º 1
0
        public override void Add(string name, string value)
        {
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }
            if (name == string.Empty)
            {
                throw new ArgumentException(string.Format(Strings.net_emptystringcall, nameof(name)), nameof(name));
            }
            if (value == string.Empty)
            {
                throw new ArgumentException(string.Format(Strings.net_emptystringcall, nameof(value)), nameof(name));
            }

            MailBnfHelper.ValidateHeaderName(name);

            // normalize the case of well known headers
            name = MailHeaderInfo.NormalizeCase(name);

            MailHeaderID id = MailHeaderInfo.GetID(name);

            value = value.Normalize(NormalizationForm.FormC);

            if (id == MailHeaderID.ContentType && _part != null)
            {
                _part.ContentType.Set(value.ToLower(CultureInfo.InvariantCulture), this);
            }
            else if (id == MailHeaderID.ContentDisposition && _part is MimePart)
            {
                ((MimePart)_part).ContentDisposition.Set(value.ToLower(CultureInfo.InvariantCulture), this);
            }
            else
            {
                InternalAdd(name, value);
            }
        }
Exemplo n.º 2
0
        private void WriteAndFold(string value, int charsAlreadyOnLine, bool allowUnicode)
        {
            int lastSpace = 0, startOfLine = 0;

            for (int index = 0; index < value.Length; index++)
            {
                // When we find a FWS (CRLF) copy it as is.
                if (MailBnfHelper.IsFWSAt(value, index)) // At the first char of "\r\n " or "\r\n\t"
                {
                    index += 2;                          // Skip the FWS
                    _bufferBuilder.Append(value, startOfLine, index - startOfLine, allowUnicode);
                    // Reset for the next line
                    startOfLine        = index;
                    lastSpace          = index;
                    charsAlreadyOnLine = 0;
                }
                // When we pass the line length limit, and know where there was a space to fold at, fold there
                else if (((index - startOfLine) > (_lineLength - charsAlreadyOnLine)) && lastSpace != startOfLine)
                {
                    _bufferBuilder.Append(value, startOfLine, lastSpace - startOfLine, allowUnicode);
                    _bufferBuilder.Append(s_crlf);
                    startOfLine        = lastSpace;
                    charsAlreadyOnLine = 0;
                }
                // Mark a foldable space.  If we go over the line length limit, fold here.
                else if (value[index] == MailBnfHelper.Space || value[index] == MailBnfHelper.Tab)
                {
                    lastSpace = index;
                }
            }
            // Write any remaining data to the buffer.
            if (value.Length - startOfLine > 0)
            {
                _bufferBuilder.Append(value, startOfLine, value.Length - startOfLine, allowUnicode);
            }
        }
Exemplo n.º 3
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;
        }
Exemplo n.º 4
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);
            }
        }