Пример #1
0
        private static Datagram ParseChunkedBody(byte[] buffer, int offset, int length)
        {
            List <Datagram> list       = new List <Datagram>();
            HttpParser      httpParser = new HttpParser(buffer, offset, length);
            uint?           number;

            while (httpParser.HexadecimalNumber(out number).SkipChunkExtensions().CarriageReturnLineFeed().Success)
            {
                uint num1 = number.Value;
                if ((int)num1 == 0)
                {
                    int?endOffset;
                    HttpDatagram.GetHeaderFields(out endOffset, buffer, httpParser.Offset, offset + length - httpParser.Offset);
                    if (endOffset.HasValue)
                    {
                        httpParser.Skip(endOffset.Value - httpParser.Offset);
                        break;
                    }
                    break;
                }
                int num2 = (int)Math.Min((long)num1, (long)(offset + length - httpParser.Offset));
                list.Add(new Datagram(buffer, httpParser.Offset, num2));
                httpParser.Skip(num2);
                httpParser.CarriageReturnLineFeed();
            }
            byte[] buffer1 = new byte[Enumerable.Sum <Datagram>((IEnumerable <Datagram>)list, (Func <Datagram, int>)(datagram => datagram.Length))];
            int    offset1 = 0;

            foreach (Datagram datagram in list)
            {
                datagram.Write(buffer1, offset1);
                offset1 += datagram.Length;
            }
            return(new Datagram(buffer, offset, httpParser.Offset - offset));
        }
Пример #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);
        }
        private static HttpResponseDatagram.ParseInfo Parse(byte[] buffer, int offset, int length)
        {
            HttpParser  httpParser = new HttpParser(buffer, offset, length);
            HttpVersion version;
            uint?       number;
            Datagram    reasonPhrase;

            httpParser.Version(out version).Space().DecimalNumber(3, out number).Space().SkipSpaces().ReasonPhrase(out reasonPhrase).CarriageReturnLineFeed();
            HttpResponseDatagram.ParseInfo parseInfo1 = new HttpResponseDatagram.ParseInfo();
            parseInfo1.Length       = length;
            parseInfo1.Version      = version;
            parseInfo1.StatusCode   = number;
            parseInfo1.ReasonPhrase = reasonPhrase;
            HttpResponseDatagram.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, HttpResponseDatagram.IsBodyPossible(number.Value), header);

            parseInfo2.Body   = datagram;
            parseInfo2.Length = num1 + num2 + datagram.Length;
            return(parseInfo2);
        }