Exemple #1
0
            public static MultiPart Parse(byte[] buf, int length, Encoding encoding = null)
            {
                var p = new MultiPart(encoding);

                if (buf[0] == LF)
                {
                    p.m_EOL = EOL_LF_BYTES;
                }
                else if (buf[0] == CR && buf[1] == LF)
                {
                    p.m_EOL = EOL_CRLF_BYTES;
                }
                else
                {
                    throw new NFXException(StringConsts.MULTIPART_NO_LF_NOR_CRLF_ISNT_FOUND_ERROR + typeof(MultiPart).Name + ".Parse");
                }

                int eolLength       = p.m_EOL.Length;
                int doubleEOLLength = eolLength << 1;

                byte[] doubleEOL = new byte[doubleEOLLength];
                Array.Copy(p.m_EOL, 0, doubleEOL, 0, eolLength); Array.Copy(p.m_EOL, 0, doubleEOL, eolLength, eolLength);

                int separatorPos = StreamHelpers.FindIndex(buf, 0, length, doubleEOL);

                if (separatorPos == StreamHelpers.NOT_FOUND_POS)
                {
                    throw new NFXException(StringConsts.MULTIPART_DOUBLE_EOL_ISNT_FOUND_AFTER_HEADER_ERROR + typeof(MultiPart).Name + ".Parse");
                }

                string prmsStr = p.m_Encoding.GetString(buf, 0, separatorPos);

                p.m_Parameters = MultiPartParameters.Parse(prmsStr, p.m_EOL, p.m_Encoding);

                for (int iEOL = 0, iBuf = length - eolLength; iEOL < eolLength; iEOL++, iBuf++)
                {
                    if (p.m_EOL[iEOL] != buf[iBuf])
                    {
                        throw new NFXException("Invalid content EOL. " + typeof(MultiPart) + ".Parse()");
                    }
                }

                int contentLength = length - separatorPos - doubleEOLLength - eolLength;

                p.m_Content = new byte[contentLength];
                Array.Copy(buf, separatorPos + doubleEOLLength, p.m_Content, 0, contentLength);

                if (p.Parameters.IsField)
                {
                    p.m_ContentString = p.m_Encoding.GetString(p.m_Content);
                }

                return(p);
            }