示例#1
0
        private ChunkStream.State ReadTrailer(byte[] buffer, ref int offset, int size)
        {
            if (this.trailerState == 2 && (ushort)buffer[offset] == 13 && this.saved.Length == 0)
            {
                offset++;
                if (offset < size && (ushort)buffer[offset] == 10)
                {
                    offset++;
                    return(ChunkStream.State.None);
                }
                offset--;
            }
            int    num  = this.trailerState;
            string text = "\r\n\r";

            while (offset < size && num < 4)
            {
                char c = (char)buffer[offset++];
                if ((num == 0 || num == 2) && c == '\r')
                {
                    num++;
                }
                else if ((num == 1 || num == 3) && c == '\n')
                {
                    num++;
                }
                else if (num > 0)
                {
                    this.saved.Append(text.Substring(0, (this.saved.Length != 0) ? num : (num - 2)));
                    num = 0;
                    if (this.saved.Length > 4196)
                    {
                        ChunkStream.ThrowProtocolViolation("Error reading trailer (too long).");
                    }
                }
            }
            if (num < 4)
            {
                this.trailerState = num;
                if (offset < size)
                {
                    ChunkStream.ThrowProtocolViolation("Error reading trailer.");
                }
                return(ChunkStream.State.Trailer);
            }
            StringReader stringReader = new StringReader(this.saved.ToString());
            string       text2;

            while ((text2 = stringReader.ReadLine()) != null && text2 != "")
            {
                this.headers.Add(text2);
            }
            return(ChunkStream.State.None);
        }
示例#2
0
 private ChunkStream.State ReadCRLF(byte[] buffer, ref int offset, int size)
 {
     if (!this.sawCR)
     {
         if ((ushort)buffer[offset++] != 13)
         {
             ChunkStream.ThrowProtocolViolation("Expecting \\r.");
         }
         this.sawCR = true;
         if (offset == size)
         {
             return(ChunkStream.State.BodyFinished);
         }
     }
     if (this.sawCR && (ushort)buffer[offset++] != 10)
     {
         ChunkStream.ThrowProtocolViolation("Expecting \\n.");
     }
     return(ChunkStream.State.None);
 }
示例#3
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);
        }