示例#1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="mime_content"></param>
        /// <returns></returns>
        public byte[] SerializeMimeContent(MimeContent mime_content)
        {
            MemoryStream _contentStream = new MemoryStream();

            this.SerializeMimeContent(mime_content, _contentStream);
            return(_contentStream.ToArray());
        }
示例#2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="in_content"></param>
        /// <param name="out_stream"></param>
        public void SerializeMimeContent(MimeContent in_content, Stream out_stream)
        {
            byte[] _writeHelper;

            //
            // Prepare some bytes written more than once
            //
            byte[] _boundaryBytes = ParserEncoding.GetBytes("--" + in_content.Boundary);

            //
            // Write every part into the stream
            //
            foreach (var item in in_content.Parts)
            {
                //
                // First of all write the boundary
                //
                out_stream.Write(CarriageReturnLineFeed, 0, CarriageReturnLineFeed.Length);
                out_stream.Write(_boundaryBytes, 0, _boundaryBytes.Length);
                out_stream.Write(CarriageReturnLineFeed, 0, 2);

                //
                // Write the content-type for the current element
                //
                var _builder = new StringBuilder();
                _builder.Append(String.Format("Content-Type: {0}", item.ContentType));
                if (!String.IsNullOrEmpty(item.CharSet))
                {
                    _builder.Append(String.Format("; charset={0}", item.CharSet));
                }
                _builder.Append(new char[] { '\r', '\n' });

                if (!String.IsNullOrEmpty(item.TransferEncoding))
                {
                    _builder.Append(String.Format("Content-Transfer-Encoding: {0}", item.TransferEncoding));
                    _builder.Append(new char[] { '\r', '\n' });
                }

                _builder.Append(String.Format("Content-ID: {0}", item.ContentId));

                _writeHelper = ParserEncoding.GetBytes(_builder.ToString());
                out_stream.Write(_writeHelper, 0, _writeHelper.Length);

                out_stream.Write(CarriageReturnLineFeed, 0, CarriageReturnLineFeed.Length);
                out_stream.Write(CarriageReturnLineFeed, 0, CarriageReturnLineFeed.Length);

                //
                // Write the actual content
                //
                out_stream.Write(item.Content, 0, item.Content.Length);
            }

            //
            // Write one last content boundary
            //
            out_stream.Write(CarriageReturnLineFeed, 0, CarriageReturnLineFeed.Length);
            out_stream.Write(_boundaryBytes, 0, _boundaryBytes.Length);
            out_stream.Write(EndOfPartsDelimeter, 0, EndOfPartsDelimeter.Length);
            out_stream.Write(CarriageReturnLineFeed, 0, CarriageReturnLineFeed.Length);
        }
示例#3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="http_content_type"></param>
        /// <param name="binary_content"></param>
        /// <returns></returns>
        public MimeContent DeserializeMimeContent(string http_content_type, byte[] binary_content)
        {
            //
            // First of all parse the http content type
            //
            string _mimeType = null, _mimeBoundary = null, _mimeStart = null;

            ParseHttpContentTypeHeader(http_content_type, ref _mimeType, ref _mimeBoundary, ref _mimeStart);

            //
            // Create the mime-content
            //
            MimeContent _content = new MimeContent()
            {
                Boundary = _mimeBoundary
            };

            //
            // Start finding the parts in the mime message
            // Note: in MIME RFC a "--" represents the end of something
            //
            var _endBoundaryHelperNdx = 0;

            byte[] _mimeBoundaryBytes = ParserEncoding.GetBytes("--" + _mimeBoundary);
            for (int i = 0; i < binary_content.Length; i++)
            {
                if (AreArrayPartsForTextEqual(_mimeBoundaryBytes, 0, binary_content, i, _mimeBoundaryBytes.Length))
                {
                    _endBoundaryHelperNdx = i + _mimeBoundaryBytes.Length;
                    if ((_endBoundaryHelperNdx + 1) < binary_content.Length)
                    {
                        // The end of the MIME-message is the boundary followed by "--"
                        if (binary_content[_endBoundaryHelperNdx] == '-' && binary_content[_endBoundaryHelperNdx + 1] == '-')
                        {
                            break;
                        }
                    }
                    else
                    {
                        throw new ProxyException("Invalid MIME content parsed, premature End-Of-File detected!");
                    }

                    // Start reading the mime part after the boundary
                    MimePart _part = ReadMimePart(binary_content, ref i, _mimeBoundaryBytes);
                    if (_part != null)
                    {
                        _content.Parts.Add(_part);
                    }
                }
            }

            //
            // Finally return the ready-to-use object model
            //
            _content.SetAsStartPart(_mimeStart);
            return(_content);
        }