Exemple #1
0
 private bool WriteChunk(byte[] source)
 {
     if (IsTransforming)
     {
         if (!Transform.Write(source))
         {
             return(false);
         }
         if (Transform.BufferedReadable > 0)
         {
             return(HandleReadable(Transform.Read()));
         }
         return(true);
     }
     return(HandleReadable(source));
 }
Exemple #2
0
 protected sealed override bool HandleWritable(byte[] source)
 {
     if (IsTransforming)
     {
         if (!Transform.Write(source))
         {
             return(false);
         }
         if (Transform.BufferedReadable > 0)
         {
             return(WriteChunk(Transform.Read()));
         }
         return(true);
     }
     return(WriteChunk(source));
 }
Exemple #3
0
        protected sealed override bool HandleWritable(byte[] source)
        {
            if (Malformed)
            {
                return(false);
            }
            char c = '\0'; ulong length, i = 0, sourceLength = (ulong)source.LongLength;

            for (; i < sourceLength;)
            {
                switch (State)
                {
                case ParseState.Dormant: return(false);

                case ParseState.Binary:
                    ContentLength = EncodedContentLength += sourceLength;
                    if (!WriteChunk(source))
                    {
                        return(false);
                    }
                    if (Type.Value.Length.HasValue && Type.Value.Length == ContentLength)
                    {
                        return(Finish());
                    }
                    return(true);

                case ParseState.Chunked_Length:
                    EncodedContentLength++;
                    c = (char)source[i++];
                    if (c == CR)
                    {
                        State = ParseState.Chunked_LengthLf;
                    }
                    else
                    {
                        ChunkLengthString = ChunkLengthString == null?c.ToString() : ChunkLengthString + c;
                    }
                    break;

                case ParseState.Chunked_LengthLf:
                    EncodedContentLength++;
                    c = (char)source[i++];
                    if (c != LF)
                    {
                        return(!(Malformed = true));
                    }
                    ChunkIndex = 0;
                    if (!ulong.TryParse(ChunkLengthString, NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture, out length))
                    {
                        return(!(Malformed = true));
                    }
                    ChunkLengthString = null;
                    State             = (ChunkLength = length) == 0 ? ParseState.Chunked_Trailer : ParseState.Chunked_ChunkData;
                    break;

                case ParseState.Chunked_ChunkData:
                    length = Math.Min(ChunkLength - ChunkIndex, sourceLength - i);
                    WriteChunk(PrimitiveBuffer.Slice(source, i, i += length));
                    ChunkIndex += length; ContentLength += length; EncodedContentLength += length;
                    if (ChunkIndex >= ChunkLength)
                    {
                        State = ParseState.Chunked_ChunkCr;
                    }
                    break;

                case ParseState.Chunked_ChunkCr:
                    EncodedContentLength++;
                    c = (char)source[i++];
                    if (c != CR)
                    {
                        return(!(Malformed = true));
                    }
                    State = ParseState.Chunked_ChunkLf;
                    break;

                case ParseState.Chunked_ChunkLf:
                    EncodedContentLength++;
                    c = (char)source[i++];
                    if (c != LF)
                    {
                        return(!(Malformed = true));
                    }
                    ChunkIndex = 0;
                    State      = ParseState.Chunked_Length;
                    break;

                case ParseState.Chunked_Trailer:
                    while ((c = (char)source[i++]) != CR)
                    {
                        EncodedContentLength++;
                    }
                    State = ParseState.Chunked_Lf;
                    break;

                case ParseState.Chunked_Lf:
                    EncodedContentLength++;
                    c = (char)source[i++];
                    if (c != LF)
                    {
                        return(!(Malformed = true));
                    }
                    ExcessStore.Write(source, i);
                    return(Finish());
                }
            }
            return(true);
        }