Пример #1
0
        public static List <RespInfo> GetInfo(byte[] buffer)
        {
            var result = new List <RespInfo>();

            while (true)
            {
                var respInfo = RespInfo.Parse(buffer);
                if (respInfo == null)
                {
                    break;
                }
                buffer = buffer.Skip(respInfo.BufferCount).ToArray();
                result.Add(respInfo);
                if (buffer.Count() == 0)
                {
                    break;
                }
            }
            return(result);
        }
Пример #2
0
        public static RespInfo Parse(byte[] buffer)
        {
            if (buffer == null)
            {
                return(null);
            }
            var respLineIndex = -1;

            for (int i = 0; i < buffer.Length; i++)
            {
                if (buffer[i] == '\n')
                {
                    respLineIndex = i;
                    break;
                }
            }
            if (respLineIndex == -1)
            {
                return(null);
            }
            RespLineInfo tempRespLine = null;

            try
            {
                tempRespLine = new RespLineInfo(Encoding.ASCII.GetString(buffer, 0, respLineIndex - 1));
            }
            catch
            {
                return(null);
            }
            if (tempRespLine == null)
            {
                return(null);
            }
            buffer = buffer.Skip(respLineIndex + 1).ToArray();
            int dataCount = (respLineIndex + 1);
            int index     = -1;

            for (int i = 0; i < buffer.Length - 3; i++)
            {
                if (buffer[i] == '\r' && buffer[i + 1] == '\n' && buffer[i + 2] == '\r' && buffer[i + 3] == '\n')
                {
                    index = i;
                    break;
                }
            }
            if (index == -1)
            {
                return(null);
            }
            dataCount += (index + 4);
            var str          = Encoding.ASCII.GetString(buffer, 0, index);
            var responseHead = new RespHeadInfo(str);

            if (responseHead == null)
            {
                return(null);
            }
            buffer = buffer.Skip(index + 4).ToArray();
            var clen = responseHead.ContentLength;

            if (clen == -1)
            {
                return(null);
            }
            //不包含Conten-length,有可能是非200,或者是Chunked
            if (clen == 0)
            {
                //解析Checked数据包
                if (responseHead.IsChunkedThrans)
                {
                    int dataleng = 0;
                    int b        = 0;
                    while (true)
                    {
                        int chunkHeadLength = 0;
                        int chunkLeng       = GetChunkLength(buffer, out chunkHeadLength);
                        if (chunkLeng == -1)
                        {
                            return(null);
                        }
                        if (chunkLeng == 0)
                        {
                            dataleng += (chunkHeadLength + 4);
                            if (buffer.Length < dataleng)
                            {
                                return(null);
                            }
                            break;
                        }
                        dataleng += (chunkHeadLength + 4 + chunkLeng);
                        if (buffer.Length < dataleng)
                        {
                            if (b == 2)
                            {
                                return(null);
                            }
                            return(null);
                        }
                        buffer = buffer.Skip(4 + chunkHeadLength + chunkLeng).ToArray();
                        b++;
                    }
                    dataCount += dataleng;
                }
                return(new RespInfo()
                {
                    BufferCount = dataCount, RespLine = tempRespLine, RespHead = responseHead
                });
            }
            else if (clen > 0)
            {
                dataCount += clen;
                if (clen <= buffer.Length)
                {
                    var result = new RespInfo()
                    {
                        BufferCount = dataCount, RespLine = tempRespLine, RespHead = responseHead
                    };
                    return(result);
                }
                return(null);
            }
            return(null);
            //throw new Exception("length parse exception");
        }