private MimePart ReadMimePart(string uri)
 {
     MimePart part = null;
     if ((uri == null) || (uri.Length == 0))
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(System.Runtime.Serialization.SR.GetString("MtomInvalidEmptyURI")));
     }
     string key = null;
     if (uri.StartsWith(MimeGlobals.ContentIDScheme, StringComparison.Ordinal))
     {
         key = string.Format(CultureInfo.InvariantCulture, "<{0}>", new object[] { Uri.UnescapeDataString(uri.Substring(MimeGlobals.ContentIDScheme.Length)) });
     }
     else if (uri.StartsWith("<", StringComparison.Ordinal))
     {
         key = uri;
     }
     if (key == null)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(System.Runtime.Serialization.SR.GetString("MtomInvalidCIDUri", new object[] { uri })));
     }
     if ((this.mimeParts == null) || !this.mimeParts.TryGetValue(key, out part))
     {
         while ((part == null) && this.mimeReader.ReadNextPart())
         {
             MimeHeaders headers = this.mimeReader.ReadHeaders(this.maxBufferSize, ref this.bufferRemaining);
             Stream contentStream = this.mimeReader.GetContentStream();
             if (contentStream == null)
             {
                 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(System.Runtime.Serialization.SR.GetString("MtomMessageInvalidContentInMimePart")));
             }
             ContentIDHeader header = (headers == null) ? null : headers.ContentID;
             if ((header == null) || (header.Value == null))
             {
                 int count = 0x100;
                 byte[] buffer = new byte[count];
                 while (contentStream.Read(buffer, 0, count) > 0)
                 {
                 }
             }
             else
             {
                 string str2 = headers.ContentID.Value;
                 MimePart part2 = new MimePart(contentStream, headers);
                 if (this.mimeParts == null)
                 {
                     this.mimeParts = new Dictionary<string, MimePart>();
                 }
                 this.mimeParts.Add(str2, part2);
                 if (str2.Equals(key))
                 {
                     part = part2;
                 }
                 else
                 {
                     part2.GetBuffer(this.maxBufferSize, ref this.bufferRemaining);
                 }
             }
         }
         if (part == null)
         {
             throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(System.Runtime.Serialization.SR.GetString("MtomPartNotFound", new object[] { uri })));
         }
     }
     else if (part.ReferencedFromInfoset)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(System.Runtime.Serialization.SR.GetString("MtomMimePartReferencedMoreThanOnce", new object[] { key })));
     }
     part.ReferencedFromInfoset = true;
     return part;
 }
Exemplo n.º 2
0
        MimePart ReadMimePart(string uri)
        {
            MimePart part = null;

            if (uri == null || uri.Length == 0)
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.GetString(SR.MtomInvalidEmptyURI)));

            string contentID = null;
            if (uri.StartsWith(MimeGlobals.ContentIDScheme, StringComparison.Ordinal))
                contentID = String.Format(CultureInfo.InvariantCulture, "<{0}>", Uri.UnescapeDataString(uri.Substring(MimeGlobals.ContentIDScheme.Length)));
            else if (uri.StartsWith("<", StringComparison.Ordinal))
                contentID = uri;

            if (contentID == null)
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.GetString(SR.MtomInvalidCIDUri, uri)));

            if (mimeParts != null && mimeParts.TryGetValue(contentID, out part))
            {
                if (part.ReferencedFromInfoset)
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.GetString(SR.MtomMimePartReferencedMoreThanOnce, contentID)));
            }
            else
            {
                int maxMimeParts = AppSettings.MaxMimeParts;
                while (part == null && mimeReader.ReadNextPart())
                {
                    MimeHeaders headers = mimeReader.ReadHeaders(this.maxBufferSize, ref this.bufferRemaining);
                    Stream contentStream = mimeReader.GetContentStream();
                    if (contentStream == null)
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.GetString(SR.MtomMessageInvalidContentInMimePart)));

                    ContentIDHeader contentIDHeader = (headers == null) ? null : headers.ContentID;
                    if (contentIDHeader == null || contentIDHeader.Value == null)
                    {
                        // Skip content if Content-ID header is not present
                        int size = 256;
                        byte[] bytes = new byte[size];

                        int read = 0;
                        do
                        {
                            read = contentStream.Read(bytes, 0, size);
                        }
                        while (read > 0);
                        continue;
                    }

                    string currentContentID = headers.ContentID.Value;
                    MimePart currentPart = new MimePart(contentStream, headers);
                    if (mimeParts == null)
                        mimeParts = new Dictionary<string, MimePart>();

                    mimeParts.Add(currentContentID, currentPart);

                    if (mimeParts.Count > maxMimeParts)
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.GetString(SR.MaxMimePartsExceeded, maxMimeParts, AppSettings.MaxMimePartsAppSettingsString)));

                    if (currentContentID.Equals(contentID))
                        part = currentPart;
                    else
                        currentPart.GetBuffer(this.maxBufferSize, ref this.bufferRemaining);
                }

                if (part == null)
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.GetString(SR.MtomPartNotFound, uri)));
            }

            part.ReferencedFromInfoset = true;
            return part;
        }