示例#1
0
        /// <summary>
        /// Parse each line in lines as a <see cref="Header"/>.
        /// </summary>
        /// <param name="lines">The lines to parse.</param>
        /// <returns>The parsed enumeration of <see cref="Header"/> instances.</returns>
        public static IEnumerable <Header> ReadHeaders(IEnumerable <StringSegment> lines)
        {
            if (lines == null)
            {
                throw new ArgumentNullException("lines");
            }

            Header header = null;

            foreach (StringSegment line in lines)
            {
                if (line.IsEmpty)
                {
                    if (header != null)
                    {
                        yield return(header);

                        header = null;
                    }
                    //
                    // Done with the header section. Onto the Body, so we're done
                    //
                    break;
                }

                if (MimeStandard.IsWhitespace(line[0]))
                {
                    // Line folding. This line belongs to the current header
                    if (header == null)
                    {
                        throw new MimeException(MimeError.InvalidHeader);
                    }
                    header.AppendSourceText(line);
                }
                else
                {
                    if (header != null)
                    {
                        yield return(header);
                    }

                    header = new Header(line);
                }
            }

            if (header != null)
            {
                yield return(header);
            }
        }
示例#2
0
        /// <summary>
        /// Parses the supplied <paramref name="lines"/> into constituent <see cref="MimePart"/> instances
        /// </summary>
        /// <remarks>
        /// This is the main parser interface. The <see cref="MimePart"/> instances expected to be returned are <see cref="Header"/> (one for each header), and <see cref="Body"/>
        /// </remarks>
        /// <param name="lines">The enumeration of <see cref="StringSegment"/> lines to parse</param>
        /// <returns>An enumeration of the constituent <see cref="MimePart"/> instances parsed from the <paramref name="lines"/></returns>
        public static IEnumerable <MimePart> ReadMimeParts(IEnumerable <StringSegment> lines)
        {
            if (lines == null)
            {
                throw new ArgumentNullException("lines");
            }

            MimePartType expectedPartType = MimePartType.Header;
            Header       header           = null;
            Body         body             = null;

            foreach (StringSegment line in lines)
            {
                switch (expectedPartType)
                {
                default:
                    throw new MimeException(MimeError.Unexpected);

                case MimePartType.Header:
                    if (line.IsEmpty)
                    {
                        if (header != null)
                        {
                            yield return(header);

                            header = null;
                        }

                        yield return(new MimePart(MimePartType.HeaderBoundary, line));

                        //
                        // Done with the header section. Onto the Body
                        //
                        expectedPartType = MimePartType.Body;
                        break;
                    }

                    if (MimeStandard.IsWhitespace(line[0]))
                    {
                        // Line folding. This line belongs to the current header
                        if (header == null)
                        {
                            throw new MimeException(MimeError.InvalidHeader);
                        }
                        header.AppendSourceText(line);
                        break;
                    }

                    if (header != null)
                    {
                        yield return(header);
                    }
                    header = new Header(line);
                    break;

                case MimePartType.Body:
                    if (body == null)
                    {
                        body = new Body(line);
                    }
                    else
                    {
                        body.AppendSourceText(line);
                    }
                    break;
                }
            }

            if (header != null)
            {
                yield return(header);
            }
            if (body != null)
            {
                yield return(body);
            }
        }