示例#1
0
        internal HttpContentLengthField(byte[] fieldValue)
            : base(FieldName, fieldValue)
        {
            HttpParser parser = new HttpParser(fieldValue);
            uint?      contentLength;

            parser.DecimalNumber(out contentLength);
            if (!parser.Success)
            {
                return;
            }

            ContentLength = contentLength;
        }
示例#2
0
        private static Datagram ParseChunkedBody(byte[] buffer, int offset, int length)
        {
            List <Datagram> contentData = new List <Datagram>();
            HttpParser      parser      = new HttpParser(buffer, offset, length);
            uint?           chunkSize;

            while (parser.HexadecimalNumber(out chunkSize).SkipChunkExtensions().CarriageReturnLineFeed().Success)
            {
                uint chunkSizeValue = chunkSize.Value;
                if (chunkSizeValue == 0)
                {
                    int?endOffset;
                    GetHeaderFields(out endOffset, buffer, parser.Offset, offset + length - parser.Offset);
                    if (endOffset != null)
                    {
                        parser.Skip(endOffset.Value - parser.Offset);
                    }
                    break;
                }

                int actualChunkSize = (int)Math.Min(chunkSizeValue, offset + length - parser.Offset);
                contentData.Add(new Datagram(buffer, parser.Offset, actualChunkSize));
                parser.Skip(actualChunkSize);
                parser.CarriageReturnLineFeed();
            }

            int contentLength = contentData.Sum(datagram => datagram.Length);

            byte[] contentBuffer       = new byte[contentLength];
            int    contentBufferOffset = 0;

            foreach (Datagram datagram in contentData)
            {
                datagram.Write(contentBuffer, contentBufferOffset);
                contentBufferOffset += datagram.Length;
            }

            return(new Datagram(buffer, offset, parser.Offset - offset));
        }
示例#3
0
        internal static List <KeyValuePair <string, IEnumerable <byte> > > GetHeaderFields(out int?endOffset, byte[] buffer, int offset, int length)
        {
            endOffset = null;
            var        headerFields = new List <KeyValuePair <string, IEnumerable <byte> > >();
            HttpParser parser       = new HttpParser(buffer, offset, length);

            while (parser.Success)
            {
                if (parser.IsCarriageReturnLineFeed())
                {
                    endOffset = parser.Offset + 2;
                    break;
                }
                string             fieldName;
                IEnumerable <byte> fieldValue;
                parser.Token(out fieldName).Colon().FieldValue(out fieldValue).CarriageReturnLineFeed();
                if (parser.Success)
                {
                    headerFields.Add(new KeyValuePair <string, IEnumerable <byte> >(fieldName, fieldValue));
                }
            }
            return(headerFields);
        }