Ejemplo n.º 1
0
        private ExDateTime GetReceivedDate(Stream mimeStream, bool useSentTime)
        {
            ExDateTime?exDateTime = null;

            try
            {
                using (MimeReader mimeReader = new MimeReader(new SuppressCloseStream(mimeStream)))
                {
                    if (mimeReader.ReadNextPart())
                    {
                        while (mimeReader.HeaderReader.ReadNextHeader())
                        {
                            if (mimeReader.HeaderReader.HeaderId == HeaderId.Received)
                            {
                                ReceivedHeader receivedHeader = Header.ReadFrom(mimeReader.HeaderReader) as ReceivedHeader;
                                if (receivedHeader != null && receivedHeader.Date != null)
                                {
                                    DateTime dateTime = this.ToDateTime(receivedHeader.Date);
                                    return(new ExDateTime(ExTimeZone.UtcTimeZone, dateTime));
                                }
                            }
                            if (useSentTime && mimeReader.HeaderReader.HeaderId == HeaderId.Date)
                            {
                                DateHeader dateHeader = Header.ReadFrom(mimeReader.HeaderReader) as DateHeader;
                                if (dateHeader != null)
                                {
                                    exDateTime = new ExDateTime?(new ExDateTime(ExTimeZone.UtcTimeZone, dateHeader.DateTime));
                                }
                            }
                        }
                    }
                }
            }
            finally
            {
                mimeStream.Seek(0L, SeekOrigin.Begin);
            }
            if (exDateTime != null)
            {
                return(exDateTime.Value);
            }
            return(ExDateTime.MinValue);
        }
 private void ProcessResponse()
 {
     string responseContentType = this.RequestExecutor.ResponseContentType;
     HttpStatusCode statusCode = this.RequestExecutor.StatusCode;
     string boundary;
     bool flag = this.IsMultiPartMime(responseContentType, out boundary);
     if (statusCode != HttpStatusCode.OK || (!responseContentType.StartsWith("application/json") && !flag))
     {
         this.m_requestStatus = ClientRequestStatus.CompletedException;
         throw new ClientRequestException(Resources.GetString("RequestUnexpectedResponseWithContentTypeAndStatus", new object[]
         {
             responseContentType,
             statusCode
         }));
     }
     this.m_context.ProcessingResponse = true;
     try
     {
         using (Stream responseStream = this.RequestExecutor.GetResponseStream())
         {
             MimeReader mimeReader = null;
             Stream responseStream2 = responseStream;
             if (flag)
             {
                 mimeReader = new MimeReader(responseStream, boundary);
                 if (!mimeReader.ReadNextPart())
                 {
                     throw new ClientRequestException(Resources.GetString("RequestUnexpectedResponseWithContentTypeAndStatus", new object[]
                     {
                         responseContentType,
                         statusCode
                     }));
                 }
                 responseStream2 = mimeReader.GetContentStream();
             }
             this.ProcessResponseStream(responseStream2);
             if (flag)
             {
                 while (mimeReader.ReadNextPart())
                 {
                     int num = 2000;
                     int num2 = num;
                     MimeHeaders mimeHeaders = mimeReader.ReadHeaders(num, ref num2);
                     if (mimeHeaders.ContentID != null && !string.IsNullOrEmpty(mimeHeaders.ContentID.Value))
                     {
                         string text = mimeHeaders.ContentID.Value;
                         if (text.StartsWith("<", StringComparison.Ordinal) && text.EndsWith(">", StringComparison.Ordinal))
                         {
                             text = text.Substring(1, text.Length - 2);
                         }
                         ChunkStreamBuilder chunkStreamBuilder = new ChunkStreamBuilder();
                         chunkStreamBuilder.CopyFrom(mimeReader.GetContentStream());
                         this.Context.AddStream(text, chunkStreamBuilder);
                     }
                 }
             }
             this.m_requestStatus = ClientRequestStatus.CompletedSuccess;
         }
     }
     catch (Exception ex)
     {
         this.m_requestStatus = ClientRequestStatus.CompletedException;
         throw;
     }
     finally
     {
         this.m_context.ProcessingResponse = false;
         this.RequestExecutor.Dispose();
     }
 }
Ejemplo n.º 3
0
        public static void Convert(Stream source, Stream destination)
        {
            int i = 0;

            byte[][] array  = null;
            byte[]   array2 = null;
            using (Stream stream = new SuppressCloseStream(source))
            {
                using (MimeReader mimeReader = new MimeReader(stream, true, DecodingOptions.Default, MimeLimits.Unlimited, true, false))
                {
                    while (mimeReader.ReadNextPart())
                    {
                        while (i >= mimeReader.Depth)
                        {
                            byte[] array3 = array[--i];
                            if (array3 != null)
                            {
                                destination.Write(array3, 0, array3.Length - 2);
                                destination.Write(MimeString.TwoDashesCRLF, 0, MimeString.TwoDashesCRLF.Length);
                            }
                        }
                        if (i > 0)
                        {
                            byte[] array3 = array[i - 1];
                            if (array3 != null)
                            {
                                destination.Write(array3, 0, array3.Length);
                            }
                        }
                        HeaderList        headerList        = HeaderList.ReadFrom(mimeReader);
                        ContentTypeHeader contentTypeHeader = headerList.FindFirst(HeaderId.ContentType) as ContentTypeHeader;
                        bool flag;
                        bool flag2;
                        bool flag3;
                        EightToSevenBitConverter.Analyse(contentTypeHeader, out flag, out flag2, out flag3);
                        Header header = headerList.FindFirst(HeaderId.ContentTransferEncoding);
                        if (flag2 || flag)
                        {
                            if (header != null)
                            {
                                headerList.RemoveChild(header);
                            }
                            headerList.WriteTo(destination);
                            byte[] array3;
                            if (flag)
                            {
                                array3 = null;
                                destination.Write(MimeString.CrLf, 0, MimeString.CrLf.Length);
                            }
                            else
                            {
                                array3 = MimePart.GetBoundary(contentTypeHeader);
                            }
                            if (array == null)
                            {
                                array = new byte[4][];
                            }
                            else if (array.Length == i)
                            {
                                byte[][] array4 = new byte[array.Length * 2][];
                                Array.Copy(array, 0, array4, 0, i);
                                array = array4;
                            }
                            array[i++] = array3;
                        }
                        else
                        {
                            Stream stream2 = null;
                            try
                            {
                                stream2 = mimeReader.GetRawContentReadStream();
                                if (header != null && stream2 != null)
                                {
                                    ContentTransferEncoding encodingType = MimePart.GetEncodingType(header.FirstRawToken);
                                    if (encodingType == ContentTransferEncoding.EightBit || encodingType == ContentTransferEncoding.Binary)
                                    {
                                        if (flag3)
                                        {
                                            header.RawValue = MimeString.QuotedPrintable;
                                            stream2         = new EncoderStream(stream2, new QPEncoder(), EncoderStreamAccess.Read);
                                        }
                                        else
                                        {
                                            header.RawValue = MimeString.Base64;
                                            stream2         = new EncoderStream(stream2, new Base64Encoder(), EncoderStreamAccess.Read);
                                        }
                                    }
                                }
                                headerList.WriteTo(destination);
                                destination.Write(MimeString.CrLf, 0, MimeString.CrLf.Length);
                                if (stream2 != null)
                                {
                                    DataStorage.CopyStreamToStream(stream2, destination, long.MaxValue, ref array2);
                                }
                            }
                            finally
                            {
                                if (stream2 != null)
                                {
                                    stream2.Dispose();
                                }
                            }
                        }
                    }
                    while (i > 0)
                    {
                        byte[] array3 = array[--i];
                        if (array3 != null)
                        {
                            destination.Write(array3, 0, array3.Length - 2);
                            destination.Write(MimeString.TwoDashesCRLF, 0, MimeString.TwoDashesCRLF.Length);
                        }
                    }
                }
            }
        }