public void Add(string name, MimeContent value)
 {
     AddWithoutValidate(name, value);
 }
 public void Set(string name, MimeContent value)
 {
     for (int i = 0; i < Count; i++)
     {
         MimeContent mime = (MimeContent)_mime[i];
         if (mime.Name == name)
         {
             _mime[i] = mime;
             return;
         }
     }
     Add(name, value);
 }
 protected void AddWithoutValidate(string name, MimeContent value)
 {
     _mime.Add(value);
 }
示例#4
0
        public MimeContent GetNextContent()
        {
            string key = "";
            string value = "";

            MimeContent mime = new MimeContent();
            MemoryStream ms = new MemoryStream();

            do
            {
                switch (state)
                {
                    case MimeParserState.ReadBoundary:
                        if (buffer[idx] == '\r')
                            idx++;
                        else if (buffer[idx] != '\n')
                            value += (char)buffer[idx++];
                        else
                        {
                            idx++;
                            value = "";
                            state = MimeParserState.ReadHeaderKey;
                        }
                        break;
                    case MimeParserState.ReadHeaderKey:
                        if (buffer[idx] == '\r')
                            idx++;
                        else if (buffer[idx] == '\n')
                        {
                            idx++;
                            state = MimeParserState.ReadContent;
                        }
                        else if (buffer[idx] == ':')
                            idx++;
                        else if (buffer[idx] != ' ')
                            key += (char)buffer[idx++];
                        else
                        {
                            idx++;
                            value = "";
                            state = MimeParserState.ReadHeaderValue;
                        }
                        break;
                    case MimeParserState.ReadHeaderValue:
                        if (buffer[idx] == '\r')
                            idx++;
                        else if (buffer[idx] != '\n')
                            value += (char)buffer[idx++];
                        else
                        {
                            idx++;
                            mime.Headers.Add(key, value);
                            key = "";
                            state = MimeParserState.ReadHeaderKey;
                        }
                        break;
                    case MimeParserState.ReadContent:

                        if (buffer[idx] == '\n' && idx < buffer.Length - _boundaryBytes.Length - 2)
                        {
                            // detect if next line is boundary line or content

                            bool foundBound = true;

                            for (int i = 0; i < _boundaryBytes.Length; i++)
                            {
                                if (_boundaryBytes[i] != buffer[idx + i + 1])
                                {
                                    foundBound = false;
                                    break;
                                }
                            }

                            if (!foundBound)
                            {
                                ms.WriteByte(buffer[idx++]);
                                continue;
                            }
                            else
                            {
                                idx++;
                                state = MimeParserState.ReadBoundary;

                                if (ms.ToArray().Length > 1)
                                {
                                    mime.Content = new byte[ms.ToArray().Length - 1];
                                    Array.Copy(ms.ToArray(), mime.Content, ms.ToArray().Length - 1);
                                }

                                return mime;
                            }
                        }
                        else
                        {
                            ms.WriteByte(buffer[idx++]);
                        }

                        break;
                }
            }
            while (idx < buffer.Length);

            return null;
        }