private static ParseInfo Parse(byte[] buffer, int offset, int length)
        {
            // First Line
            HttpParser parser = new HttpParser(buffer, offset, length);
            string method;
            string uri;
            HttpVersion version;
            parser.Token(out method).Space().RequestUri(out uri).Space().Version(out version).CarriageReturnLineFeed();
            ParseInfo parseInfo = new ParseInfo
                                  {
                                      Length = length,
                                      Version = version,
                                      Method = method == null ? null : new HttpRequestMethod(method),
                                      Uri = uri,
                                  };
            if (!parser.Success)
                return parseInfo;

            int firstLineLength = parser.Offset - offset;

            // Header
            int? endHeaderOffset;
            HttpHeader header = new HttpHeader(GetHeaderFields(out endHeaderOffset, buffer, offset + firstLineLength, length - firstLineLength));
            parseInfo.Header = header;
            if (endHeaderOffset == null)
                return parseInfo;

            int headerLength = endHeaderOffset.Value - offset - firstLineLength;

            // Body
            Datagram body = ParseBody(buffer, offset + firstLineLength + headerLength, length - firstLineLength - headerLength, IsBodyPossible(header), header);
            parseInfo.Body = body;
            parseInfo.Length = firstLineLength + headerLength + body.Length;
            return parseInfo;
        }
        private static ParseInfo Parse(byte[] buffer, int offset, int length)
        {
            // First Line
            HttpParser parser = new HttpParser(buffer, offset, length);
            HttpVersion version;
            uint? statusCode;
            Datagram reasonPhrase;
            parser.Version(out version).Space().DecimalNumber(3, out statusCode).Space().SkipSpaces().ReasonPhrase(out reasonPhrase).CarriageReturnLineFeed();
            ParseInfo parseInfo = new ParseInfo
                                  {
                                      Length = length,
                                      Version = version,
                                      StatusCode = statusCode,
                                      ReasonPhrase = reasonPhrase
                                  };
            if (!parser.Success)
                return parseInfo;

            int firstLineLength = parser.Offset - offset;

            // Header
            int? endHeaderOffset;
            HttpHeader header = new HttpHeader(GetHeaderFields(out endHeaderOffset, buffer, offset + firstLineLength, length - firstLineLength));
            parseInfo.Header = header;
            if (endHeaderOffset == null)
                return parseInfo;

            int headerLength = endHeaderOffset.Value - offset - firstLineLength;

            // Body
            Datagram body = ParseBody(buffer, offset + firstLineLength + headerLength, length - firstLineLength - headerLength, IsBodyPossible(statusCode.Value), header);
            parseInfo.Body = body;
            parseInfo.Length = firstLineLength + headerLength + body.Length;
            return parseInfo;
        }
        internal HttpContentLengthField(byte[] fieldValue)
            :base(FieldName, fieldValue)
        {
            HttpParser parser = new HttpParser(fieldValue);
            uint? contentLength;
            parser.DecimalNumber(out contentLength);
            if (!parser.Success)
                return;

            ContentLength = contentLength;
        }
        internal HttpContentLengthField(byte[] fieldValue)
            : base("Content-Length", (IList <byte>)fieldValue)
        {
            HttpParser httpParser = new HttpParser(fieldValue);
            uint?      number;

            httpParser.DecimalNumber(out number);
            if (!httpParser.Success)
            {
                return;
            }
            this.ContentLength = number;
        }
Exemple #5
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;
        }
Exemple #6
0
        private static ParseInfo Parse(byte[] buffer, int offset, int length)
        {
            // First Line
            HttpParser  parser = new HttpParser(buffer, offset, length);
            string      method;
            string      uri;
            HttpVersion version;

            parser.Token(out method).Space().RequestUri(out uri).Space().Version(out version).CarriageReturnLineFeed();
            ParseInfo parseInfo = new ParseInfo
            {
                Length  = length,
                Version = version,
                Method  = method == null ? null : new HttpRequestMethod(method),
                Uri     = uri,
            };

            if (!parser.Success)
            {
                return(parseInfo);
            }

            int firstLineLength = parser.Offset - offset;

            // Header
            int?       endHeaderOffset;
            HttpHeader header = new HttpHeader(GetHeaderFields(out endHeaderOffset, buffer, offset + firstLineLength, length - firstLineLength));

            parseInfo.Header = header;
            if (endHeaderOffset == null)
            {
                return(parseInfo);
            }

            int headerLength = endHeaderOffset.Value - offset - firstLineLength;

            // Body
            Datagram body = ParseBody(buffer, offset + firstLineLength + headerLength, length - firstLineLength - headerLength, IsBodyPossible(header), header);

            parseInfo.Body   = body;
            parseInfo.Length = firstLineLength + headerLength + body.Length;
            return(parseInfo);
        }
        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;
//                    HttpHeader trailerHeader = new HttpHeader(
                    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;
            }
//            Datagram content = new Datagram(contentBuffer);

            return(new Datagram(buffer, offset, parser.Offset - offset));
        }
        private static ParseInfo Parse(byte[] buffer, int offset, int length)
        {
            // First Line
            HttpParser  parser = new HttpParser(buffer, offset, length);
            HttpVersion version;
            uint?       statusCode;
            Datagram    reasonPhrase;

            parser.Version(out version).Space().DecimalNumber(3, out statusCode).Space().SkipSpaces().ReasonPhrase(out reasonPhrase).CarriageReturnLineFeed();
            ParseInfo parseInfo = new ParseInfo
            {
                Length       = length,
                Version      = version,
                StatusCode   = statusCode,
                ReasonPhrase = reasonPhrase
            };

            if (!parser.Success)
            {
                return(parseInfo);
            }

            int firstLineLength = parser.Offset - offset;

            // Header
            int?       endHeaderOffset;
            HttpHeader header = new HttpHeader(GetHeaderFields(out endHeaderOffset, buffer, offset + firstLineLength, length - firstLineLength));

            parseInfo.Header = header;
            if (endHeaderOffset == null)
            {
                return(parseInfo);
            }

            int headerLength = endHeaderOffset.Value - offset - firstLineLength;

            // Body
            Datagram body = ParseBody(buffer, offset + firstLineLength + headerLength, length - firstLineLength - headerLength, IsBodyPossible(statusCode.Value), header);

            parseInfo.Body   = body;
            parseInfo.Length = firstLineLength + headerLength + body.Length;
            return(parseInfo);
        }
        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);
        }
        internal static List <KeyValuePair <string, IEnumerable <byte> > > GetHeaderFields(out int?endOffset, byte[] buffer, int offset, int length)
        {
            endOffset = new int?();
            List <KeyValuePair <string, IEnumerable <byte> > > list = new List <KeyValuePair <string, IEnumerable <byte> > >();
            HttpParser httpParser = new HttpParser(buffer, offset, length);

            while (httpParser.Success)
            {
                if (httpParser.IsCarriageReturnLineFeed())
                {
                    endOffset = new int?(httpParser.Offset + 2);
                    break;
                }
                string             token;
                IEnumerable <byte> fieldValue;
                httpParser.Token(out token).Colon().FieldValue(out fieldValue).CarriageReturnLineFeed();
                if (httpParser.Success)
                {
                    list.Add(new KeyValuePair <string, IEnumerable <byte> >(token, fieldValue));
                }
            }
            return(list);
        }
Exemple #11
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;
            //                    HttpHeader trailerHeader = new HttpHeader(
                        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;
            }
            //            Datagram content = new Datagram(contentBuffer);

            return new Datagram(buffer, offset, parser.Offset - offset);
        }
Exemple #12
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;
 }