Example #1
0
        private ChunkStream.State GetChunkSize(byte[] buffer, ref int offset, int size)
        {
            char c = '\0';

            while (offset < size)
            {
                c = (char)buffer[offset++];
                if (c == '\r')
                {
                    if (this.sawCR)
                    {
                        ChunkStream.ThrowProtocolViolation("2 CR found");
                    }
                    this.sawCR = true;
                }
                else
                {
                    if (this.sawCR && c == '\n')
                    {
                        break;
                    }
                    if (c == ' ')
                    {
                        this.gotit = true;
                    }
                    if (!this.gotit)
                    {
                        this.saved.Append(c);
                    }
                    if (this.saved.Length > 20)
                    {
                        ChunkStream.ThrowProtocolViolation("chunk size too long.");
                    }
                }
            }
            if (!this.sawCR || c != '\n')
            {
                if (offset < size)
                {
                    ChunkStream.ThrowProtocolViolation("Missing \\n");
                }
                try
                {
                    if (this.saved.Length > 0)
                    {
                        this.chunkSize = int.Parse(ChunkStream.RemoveChunkExtension(this.saved.ToString()), NumberStyles.HexNumber);
                    }
                }
                catch (Exception)
                {
                    ChunkStream.ThrowProtocolViolation("Cannot parse chunk size.");
                }
                return(ChunkStream.State.None);
            }
            this.chunkRead = 0;
            try
            {
                this.chunkSize = int.Parse(ChunkStream.RemoveChunkExtension(this.saved.ToString()), NumberStyles.HexNumber);
            }
            catch (Exception)
            {
                ChunkStream.ThrowProtocolViolation("Cannot parse chunk size.");
            }
            if (this.chunkSize == 0)
            {
                this.trailerState = 2;
                return(ChunkStream.State.Trailer);
            }
            return(ChunkStream.State.Body);
        }