Example #1
0
        public void ReturnExpectPosWhenSubsenquenceFound()
        {
            var sub  = new byte[] { 15, 20, 22, 23 };
            var sub1 = new byte[] { 11, 20, 22, 23, 15, 15, 20, 22, 23, 222, 111, 100 };
            var full = new byte[] { 11, 20, 22, 23, 15, 15, 20, 22, 23, 222, 111, 100 };

            int pos  = SubsenquenceFinder.Search(full, sub);
            int pos1 = SubsenquenceFinder.Search(full, sub1);

            Assert.AreEqual(pos, 5);
            Assert.AreEqual(pos1, 0);
        }
Example #2
0
        public void ReturnNegativeOneWhenSubsenquenceNotFound()
        {
            var sub  = new byte[] { 15, 20, 22, 23 };
            var sub1 = new byte[] { 11, 20, 22, 23, 15, 15, 21, 22, 23, 222, 111, 100, 2 };
            var full = new byte[] { 11, 20, 22, 23, 15, 15, 21, 22, 23, 222, 111, 100 };

            int pos  = SubsenquenceFinder.Search(full, sub);
            int pos1 = SubsenquenceFinder.Search(full, sub1);

            Assert.AreEqual(pos, -1);
            Assert.AreEqual(pos, -1);
        }
Example #3
0
        private async Task ParseFilePartAsync(RebufferableStreamReader reader, Dictionary <string, string> partHeaders)
        {
            //先使用内存存储吧,遇到大文件肯定是不行的
            MemoryStream ms         = new MemoryStream();
            var          curBuffer  = new byte[this.BinaryBufferSize];
            var          prevBuffer = new byte[this.BinaryBufferSize];

            int curLength  = 0;
            int prevLength = await reader.ReadAsync(prevBuffer, 0, prevBuffer.Length);

            int endPos = -1;

            do
            {
                curLength = await reader.ReadAsync(curBuffer, 0, curBuffer.Length);

                var fullBuffer = new Byte[prevLength + curLength];
                Buffer.BlockCopy(prevBuffer, 0, fullBuffer, 0, prevLength);
                Buffer.BlockCopy(curBuffer, 0, fullBuffer, prevLength, curLength);

                int endBoundaryPos = SubsenquenceFinder.Search(fullBuffer, endBoundaryBytes);
                int boundaryPos    = SubsenquenceFinder.Search(fullBuffer, boundaryBytes);

                if (endBoundaryPos >= 0 && boundaryPos >= 0)
                {
                    if (boundaryPos < endBoundaryPos)
                    {
                        endPos = boundaryPos;
                    }
                    else
                    {
                        endPos = endBoundaryPos;
                        this.readEndBoundary = true;
                    }
                }
                else if (boundaryPos >= 0 && endBoundaryPos < 0)
                {
                    endPos = boundaryPos;
                }
                else if (endBoundaryPos >= 0 && boundaryPos < 0)
                {
                    endPos = endBoundaryPos;
                    this.readEndBoundary = true;
                }

                if (endPos != -1)
                {
                    //这里endPos减2的原因是去除CRLF
                    ms.Write(fullBuffer, 0, endPos - 2);
                    var rebuffer = new byte[fullBuffer.Length - endPos];
                    Buffer.BlockCopy(fullBuffer, endPos, rebuffer, 0, rebuffer.Length);
                    reader.Rebuffer(rebuffer);
                    ms.Flush();
                    ms.Position = 0;
                    FilePart filePart = new FilePart()
                    {
                        Filename = partHeaders["filename"],
                        Name     = partHeaders["name"],
                        Data     = ms
                    };

                    this.Files.Add(filePart);
                    return;
                }

                ms.Write(prevBuffer, 0, prevLength);
                ms.Flush();
                prevBuffer = curBuffer;
                prevLength = curLength;
            }while (prevLength > 0);

            if (endPos == -1)
            {
                ms.Close();
                throw new HttpRequestParseException("http报文实体格式错误");
            }
        }