Beispiel #1
0
            private void parse(Stream stream, long start, long finish)
            {
                byte[] eol2 = new byte[m_EOL.Length << 1];
                Array.Copy(m_EOL, eol2, m_EOL.Length); Array.Copy(m_EOL, 0, eol2, m_EOL.Length, m_EOL.Length);

                long eol2PosAfterHeader = StreamHelpers.Find(stream, eol2, start, finish);

                if (eol2PosAfterHeader == StreamHelpers.NOT_FOUND_POS)
                {
                    throw new NFXException(StringConsts.MULTIPART_DOUBLE_EOL_ISNT_FOUND_AFTER_HEADER_ERROR + this.GetType().Name + ".Parse");
                }

                string str = StreamHelpers.GetString(stream, start, eol2PosAfterHeader - 1);

                m_Parameters = MultiPartParameters.Parse(str, m_EOL, m_Encoding);

                m_Content       = new byte[finish - eol2PosAfterHeader - eol2.Length + 1];
                stream.Position = eol2PosAfterHeader + eol2.Length;
                stream.Read(m_Content, 0, m_Content.Length);

                m_Content       = new byte[finish - (eol2PosAfterHeader + eol2.Length) + 1];
                stream.Position = eol2PosAfterHeader + eol2.Length;
                stream.Read(m_Content, 0, m_Content.Length);

                if (m_Parameters.IsField)
                {
                    m_ContentString = m_Encoding.GetString(m_Content);
                }
            }
Beispiel #2
0
            public static MultiPart CreateFile(string name, string fieldName, string contentType,
                                               byte[] content, byte[] eol, Encoding encoding = null)
            {
                var parameters = MultiPartParameters.CreateFile(name, fieldName, contentType, eol);
                var part       = new MultiPart(parameters, eol, encoding);

                part.m_Content = content;
                return(part);
            }
Beispiel #3
0
            public static MultiPart CreateField(string fieldName, string fieldValue, byte[] eol, Encoding encoding = null)
            {
                var parameters = MultiPartParameters.CreateField(fieldName, eol);
                var part       = new MultiPart(parameters, eol, encoding);

                part.m_Content       = part.m_Encoding.GetBytes(fieldValue);
                part.m_ContentString = fieldValue;
                return(part);
            }
Beispiel #4
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);
            }
Beispiel #5
0
 private MultiPart(MultiPartParameters parameters, byte[] eol, Encoding encoding = null) : this(encoding)
 {
     m_EOL        = eol ?? EOL_CRLF_BYTES;
     m_Parameters = parameters;
 }
Beispiel #6
0
          private void parse(Stream stream, long start, long finish, byte[] eol, Encoding encoding)
          {
            byte[] eol2 = new byte[eol.Length << 1];
            Array.Copy(eol, eol2, eol.Length); Array.Copy(eol, 0, eol2, eol.Length, eol.Length);

            long eol2PosAfterHeader = StreamHelpers.Find(stream, eol2, start, finish);

            if (eol2PosAfterHeader == StreamHelpers.NOT_FOUND_POS)
              throw new NFXException(StringConsts.MULTIPART_DOUBLE_EOL_ISNT_FOUND_AFTER_HEADER_ERROR + this.GetType().Name + ".Parse");

            string str = StreamHelpers.GetString(stream, start, eol2PosAfterHeader-1);

            m_Parameters = MultiPartParameters.Parse(str, eol);

            m_Content = new byte[finish - eol2PosAfterHeader - eol2.Length + 1];
            stream.Position = eol2PosAfterHeader + eol2.Length;
            stream.Read(m_Content, 0, m_Content.Length);

            m_Content = new byte[finish - (eol2PosAfterHeader + eol2.Length) + 1];
            stream.Position = eol2PosAfterHeader + eol2.Length;
            stream.Read(m_Content, 0, m_Content.Length);

            if (m_Parameters.IsField)
              m_ContentString = encoding.GetString(m_Content);
          }
Beispiel #7
0
 private MultiPart(MultiPartParameters parameters, byte[] eol, Encoding encoding = null)
 {
   m_Parameters = parameters;
   m_EOL = eol;
 }
Beispiel #8
0
 private MultiPart(MultiPartParameters parameters, byte[] eol, Encoding encoding = null)
     : this(encoding)
 {
     m_EOL = eol ?? EOL_CRLF_BYTES;
     m_Parameters = parameters;
 }
Beispiel #9
0
 private MultiPart(MultiPartParameters parameters, byte[] eol, Encoding encoding = null)
 {
     m_Parameters = parameters;
     m_EOL        = eol;
 }