Exemplo n.º 1
0
        public unsafe MimeHeader ParseHeader(Byte[] data)
        {
            var header = new MimeHeaderBufferByteArray();

            header.Add(data, true);
            return(ParseHeader(header));
        }
Exemplo n.º 2
0
        private unsafe MimeHeader ParseHeader(MimeHeaderBufferByteArray headerPointer)
        {
            MimeHeader header = null;

            fixed(Byte *headerPtr = headerPointer.Data)
            {
                Byte *current = headerPtr;
                Byte *end     = headerPtr + headerPointer.Length * sizeof(Byte);

                while (*current == (Byte)' ')
                {
                    current++;
                }
                Byte *start = current;

                while (*current != (Byte)':')
                {
                    current++;
                    if (current == end)
                    {
                        var line = Encoding.UTF8.GetString(headerPointer.ToArray());
                        throw new InvalidMimeFormatException(line);
                    }
                }
                String key = this.Encoding.GetString(CreateNewBytes(new IntPtr(start), current - start));

                switch (key.ToLower())
                {
                case "content-type": header = new ContentType(); break;

                case "content-disposition": header = new ContentDisposition(); break;

                default: header = new MimeHeader(); break;
                }
                header.Key = key;

                current++;

                while (*current == (Byte)' ')
                {
                    current++;
                }
                header.RawValue = this.Encoding.GetString(CreateNewBytes(new IntPtr(current), end - current));

                if (header.HasParameter == false)
                {
                    header.Value = _Rfc2047Converter.Decode(current, end);
                }
                header.RawData = headerPointer.ToArray();
                ParseContentType(header as ContentType);
                ParseContentDisposition(header as ContentDisposition);
            }

            return(header);
        }
Exemplo n.º 3
0
        private List <MimeContent> ReadMimeContent(MimeStreamBuffer context, Byte[] boundary)
        {
            List <MimeContent>        l             = new List <MimeContent>();
            MimeContent               mc            = null;
            MimeHeaderBufferByteArray headerPointer = _HeaderBuffer;
            var bodyBuffer     = new MimeContentByteArray();
            var state          = MimeContentParserState.Boundary;
            var boundaryResult = CheckBoundaryResult.None;
            var isEndOfBody    = false;

            Byte[]           childBoundary = null;
            Byte[]           line          = null;
            MimeStreamBuffer cx            = context;

            mc = new MimeContent();
            headerPointer.Clear();
            while (true)
            {
                if (context == null)
                {
                    this.ReadFromStream(cx);
                }
                context = null;

                while (true)
                {
                    switch (state)
                    {
                    case MimeContentParserState.Boundary:
                        #region
                    {
                        line = cx.ReadLine();
                        if (line.Length == 0)
                        {
                            continue;
                        }
                        this.AddToRawData(line);
                        if (line.Length == 2 && line[0] == 13 && line[1] == 10)
                        {
                            continue;
                        }

                        bodyBuffer.AddBoundaryLine(line);
                        if (IsEndByNewLine(line) == true)
                        {
                            boundaryResult = bodyBuffer.CheckBoundary(boundary);
                            switch (boundaryResult)
                            {
                            case CheckBoundaryResult.None:
                            {
                                var lineText = this.Encoding.GetString(line);
                                throw new InvalidMimeFormatException(lineText);
                            }

                            case CheckBoundaryResult.Boundary:
                            {
                                headerPointer.Clear();
                                state = MimeContentParserState.Header;
                            }
                            break;

                            case CheckBoundaryResult.EndBoundary: break;

                            default: throw new InvalidOperationException();
                            }
                        }
                    }
                    break;

                        #endregion
                    case MimeContentParserState.Header:
                        #region
                    {
                        headerPointer = cx.ReadHeader(headerPointer);
                        if (headerPointer.IsEmptyNewLine() == true)
                        {
                            var bb = headerPointer.ToArray();
                            this.AddToRawData(bb);
                            bodyBuffer.AddHeaderLine(bb);
                            bodyBuffer.HeaderLength = bodyBuffer.Length;
                            headerPointer.Clear();

                            if (mc.ContentType != null && String.IsNullOrEmpty(mc.ContentType.Boundary) == false)
                            {
                                childBoundary = CreateBoundary("--" + mc.ContentType.Boundary);
                                state         = MimeContentParserState.ChildContent;
                            }
                            else
                            {
                                state = MimeContentParserState.Body;
                            }
                        }
                        else if (headerPointer.IsEnd == true)
                        {
                            var bb = headerPointer.ToArray();
                            this.AddToRawData(bb);
                            //New MimeHeader
                            var header = ParseHeader(headerPointer);
                            mc.Headers.Add(header);
                            headerPointer.Clear();
                            bodyBuffer.AddHeaderLine(bb);
                        }
                    }
                    break;

                        #endregion
                    case MimeContentParserState.Body:
                        #region
                    {
                        var bodyLine = cx.ReadBody(boundary, out boundaryResult, out isEndOfBody);
                        bodyBuffer.AddBodyLine(bodyLine);
                        if (boundaryResult != CheckBoundaryResult.None || isEndOfBody == true)
                        {
                            this.SetMimeContentBody(mc, bodyBuffer);
                            l.Add(mc);

                            headerPointer.Clear();
                            bodyBuffer.Clear();
                            mc = new MimeContent();

                            state = MimeContentParserState.Boundary;
                        }
                    }
                    break;

                        #endregion
                    case MimeContentParserState.ChildContent:
                        #region
                    {
                        var bodyLine = cx.ReadBody(childBoundary, out boundaryResult, out isEndOfBody);
                        bodyBuffer.AddBodyLine(bodyLine);
                        if (boundaryResult != CheckBoundaryResult.None || isEndOfBody == true)
                        {
                            if (bodyBuffer.Length > 0)
                            {
                                this.AddToRawDataWithNewline(bodyBuffer.GetBodyArray());
                            }

                            mc.Contents.AddRange(ReadMimeContent(cx, childBoundary));

                            bodyBuffer.Clear();

                            state = MimeContentParserState.Body;
                        }
                    }
                    break;

                        #endregion
                    default: throw new InvalidOperationException();
                    }
                    if (cx.IsEnd() == true)
                    {
                        break;
                    }
                    if (isEndOfBody == true || boundaryResult == CheckBoundaryResult.EndBoundary)
                    {
                        break;
                    }
                }
                if (isEndOfBody == true || boundaryResult == CheckBoundaryResult.EndBoundary || cx.EndOfStream == true)
                {
                    break;
                }
            }
            if (boundaryResult == CheckBoundaryResult.EndBoundary)
            {
                this.AddToRawData(boundary);
                this.AddToRawData(new Byte[] { 45, 45 });
            }
            return(l);
        }
Exemplo n.º 4
0
        private unsafe void ReadHeaderAndMessageBody(MimeMessage message, MimeStreamBuffer context)
        {
            MimeHeaderBufferByteArray headerPointer = _HeaderBuffer;
            var     bodyBuffer  = new MimeContentByteArray();
            Boolean endOfHeader = false;
            Boolean endOfBody   = false;

            Byte[] boundary       = null;
            var    boundaryResult = CheckBoundaryResult.None;
            var    isEndOfBody    = false;
            var    firstLine      = true;
            var    cx             = context;

            headerPointer.Clear();
            while (true)
            {
                this.ReadFromStream(cx);

                while (true)
                {
                    if (endOfHeader == false)
                    {
                        #region Header
                        headerPointer = cx.ReadHeader(headerPointer);
                        if (headerPointer.IsEnd == false)
                        {
                            break;
                        }
                        if (firstLine == true && headerPointer.IsOkResponseLine() == true)
                        {
                            firstLine = false;
                            headerPointer.Clear();
                            continue;
                        }
                        firstLine = false;
                        var l = Encoding.UTF8.GetString(headerPointer.ToArray());
                        if (endOfHeader == false && headerPointer.IsEmptyNewLine() == true)
                        {
                            this.AddToRawData(headerPointer.ToArray());
                            headerPointer.Clear();
                            endOfHeader = true;

                            if (String.IsNullOrEmpty(message.ContentType.Boundary) == false && boundary == null)
                            {
                                boundary = CreateBoundary("--" + message.ContentType.Boundary);
                            }
                            continue;
                        }
                        if (headerPointer.IsEnd == true)
                        {
                            //New MimeHeader
                            var bb = headerPointer.ToArray();
                            this.AddToRawData(bb);

                            var header = ParseHeader(headerPointer);
                            message.Headers.Add(header);
                            headerPointer.Clear();
                        }
                        #endregion
                    }
                    else
                    {
                        #region Body
                        Byte[] bodyLine = null;
                        bodyLine = cx.ReadBody(boundary, out boundaryResult, out isEndOfBody);
                        bodyBuffer.AddBodyLine(bodyLine);
                        if (boundaryResult != CheckBoundaryResult.None || isEndOfBody == true)
                        {
                            endOfBody = true;
                            var bb = bodyBuffer.GetBodyArray();
                            if (bb.Length == 0)
                            {
                                break;
                            }
                            message.TextBeforeMimeContent = this.Encoding.GetString(bb);
                            this.AddToRawDataWithNewline(bb);
                            if (message.ContentType.IsText == false)
                            {
                                break;
                            }

                            this.SetMimeMessageBodyText(message, bb);
                            break;
                        }
                        #endregion
                    }
                    if (cx.IsEnd() == true || cx.EndOfStream == true)
                    {
                        break;
                    }
                }
                if (endOfBody == true || cx.EndOfStream == true)
                {
                    break;
                }
            }
        }
Exemplo n.º 5
0
        public MimeHeaderBufferByteArray ReadHeader(MimeHeaderBufferByteArray header)
        {
            this._Start = this._Current;
            Byte* start = this._Start;
            Byte* lastOfLine = this._Current;

            if (this._Current == this._End) { return header; }
            if (*this._Current == 10)// \n
            {
                this._Current++;
                header.Add(MimeParser.CreateNewBytes(new IntPtr(start), this._Current - start), true);
                return header;
            }
            //Find LineFeed char
            while (true)
            {
                // \n
                while (*this._Current != 10) { this._Current++; }

                if (this._Current == this._End)
                {
                    header.Add(MimeParser.CreateNewBytes(new IntPtr(start), this._End - start), false);
                    return header;
                }
                lastOfLine = this._Current - 2;

                this._Current++;
                //Empty line is start of body
                if (lastOfLine - this._Start < 1)
                {
                    header.Add(MimeParser.CreateNewBytes(new IntPtr(start), this._Current - start), true);
                    return header;
                }

                if (*this._Current != 9 && *this._Current != 32)// \t or white space
                {
                    header.Add(MimeParser.CreateNewBytes(new IntPtr(start), this._Current - start), true);
                    return header;
                }
            }
        }