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;
        }
Exemple #2
0
        private static HttpRequestDatagram.ParseInfo Parse(byte[] buffer, int offset, int length)
        {
            HttpParser  httpParser = new HttpParser(buffer, offset, length);
            string      token;
            string      uri;
            HttpVersion version;

            httpParser.Token(out token).Space().RequestUri(out uri).Space().Version(out version).CarriageReturnLineFeed();
            HttpRequestDatagram.ParseInfo parseInfo1 = new HttpRequestDatagram.ParseInfo();
            parseInfo1.Length  = length;
            parseInfo1.Version = version;
            parseInfo1.Method  = token == null ? (HttpRequestMethod)null : new HttpRequestMethod(token);
            parseInfo1.Uri     = uri;
            HttpRequestDatagram.ParseInfo parseInfo2 = parseInfo1;
            if (!httpParser.Success)
            {
                return(parseInfo2);
            }
            int        num1 = httpParser.Offset - offset;
            int?       endOffset;
            HttpHeader header = new HttpHeader((IEnumerable <KeyValuePair <string, IEnumerable <byte> > >)HttpDatagram.GetHeaderFields(out endOffset, buffer, offset + num1, length - num1));

            parseInfo2.Header = header;
            if (!endOffset.HasValue)
            {
                return(parseInfo2);
            }
            int      num2     = endOffset.Value - offset - num1;
            Datagram datagram = HttpDatagram.ParseBody(buffer, offset + num1 + num2, length - num1 - num2, HttpRequestDatagram.IsBodyPossible(header), header);

            parseInfo2.Body   = datagram;
            parseInfo2.Length = num1 + num2 + datagram.Length;
            return(parseInfo2);
        }
Exemple #3
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);
        }
        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 #6
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;
 }