Exemplo n.º 1
0
        private static ParserState ParseBufferInSteps(
            HttpRequestLineParser parser,
            byte[] buffer,
            int readsize,
            out int totalBytesConsumed
            )
        {
            ParserState state = ParserState.Invalid;

            totalBytesConsumed = 0;
            while (totalBytesConsumed <= buffer.Length)
            {
                int    size        = Math.Min(buffer.Length - totalBytesConsumed, readsize);
                byte[] parseBuffer = new byte[size];
                Buffer.BlockCopy(buffer, totalBytesConsumed, parseBuffer, 0, size);

                int bytesConsumed = 0;
                state = parser.ParseBuffer(parseBuffer, parseBuffer.Length, ref bytesConsumed);
                totalBytesConsumed += bytesConsumed;

                if (state != ParserState.NeedMoreData)
                {
                    return(state);
                }
            }

            return(state);
        }
 public void RequestLineParserNullBuffer()
 {
     HttpUnsortedRequest requestLine = new HttpUnsortedRequest();
     HttpRequestLineParser parser = new HttpRequestLineParser(requestLine, ParserData.MinRequestLineSize);
     Assert.NotNull(parser);
     int bytesConsumed = 0;
     Assert.ThrowsArgumentNull(() => { parser.ParseBuffer(null, 0, ref bytesConsumed); }, "buffer");
 }
Exemplo n.º 3
0
        public void RequestLineParserNullBuffer()
        {
            HttpUnsortedRequest   requestLine = new HttpUnsortedRequest();
            HttpRequestLineParser parser      = new HttpRequestLineParser(requestLine, ParserData.MinRequestLineSize);

            Assert.NotNull(parser);
            int bytesConsumed = 0;

            Assert.ThrowsArgumentNull(() => { parser.ParseBuffer(null, 0, ref bytesConsumed); }, "buffer");
        }
        public void RequestLineParserMinimumBuffer()
        {
            byte[] data = CreateBuffer("G", "/", "HTTP/1.1");
            HttpUnsortedRequest requestLine = new HttpUnsortedRequest();
            HttpRequestLineParser parser = new HttpRequestLineParser(requestLine, ParserData.MinRequestLineSize);
            Assert.NotNull(parser);

            int bytesConsumed = 0;
            ParserState state = parser.ParseBuffer(data, data.Length, ref bytesConsumed);
            Assert.Equal(ParserState.Done, state);
            Assert.Equal(data.Length, bytesConsumed);

            ValidateResult(requestLine, "G", "/", new Version("1.1"));
        }
Exemplo n.º 5
0
        public void RequestLineParserMinimumBuffer()
        {
            byte[] data = CreateBuffer("G", "/", "HTTP/1.1");
            HttpUnsortedRequest   requestLine = new HttpUnsortedRequest();
            HttpRequestLineParser parser      = new HttpRequestLineParser(requestLine, ParserData.MinRequestLineSize);

            Assert.NotNull(parser);

            int         bytesConsumed = 0;
            ParserState state         = parser.ParseBuffer(data, data.Length, ref bytesConsumed);

            Assert.Equal(ParserState.Done, state);
            Assert.Equal(data.Length, bytesConsumed);

            ValidateResult(requestLine, "G", "/", new Version("1.1"));
        }
        private static ParserState ParseBufferInSteps(HttpRequestLineParser parser, byte[] buffer, int readsize, out int totalBytesConsumed)
        {
            ParserState state = ParserState.Invalid;
            totalBytesConsumed = 0;
            while (totalBytesConsumed <= buffer.Length)
            {
                int size = Math.Min(buffer.Length - totalBytesConsumed, readsize);
                byte[] parseBuffer = new byte[size];
                Buffer.BlockCopy(buffer, totalBytesConsumed, parseBuffer, 0, size);

                int bytesConsumed = 0;
                state = parser.ParseBuffer(parseBuffer, parseBuffer.Length, ref bytesConsumed);
                totalBytesConsumed += bytesConsumed;

                if (state != ParserState.NeedMoreData)
                {
                    return state;
                }
            }

            return state;
        }
Exemplo n.º 7
0
        public ParserState ParseBuffer(
            byte[] buffer,
            int bytesReady,
            ref int bytesConsumed)
        {
            if (buffer == null)
            {
                throw Error.ArgumentNull("buffer");
            }

            ParserState parseStatus    = ParserState.NeedMoreData;
            ParserState subParseStatus = ParserState.NeedMoreData;

            switch (_requestStatus)
            {
            case HttpRequestState.RequestLine:
                try
                {
                    subParseStatus = _requestLineParser.ParseBuffer(buffer, bytesReady, ref bytesConsumed);
                }
                catch (Exception)
                {
                    subParseStatus = ParserState.Invalid;
                }

                if (subParseStatus == ParserState.Done)
                {
                    _requestStatus = HttpRequestState.RequestHeaders;
                    subParseStatus = ParserState.NeedMoreData;
                    goto case HttpRequestState.RequestHeaders;
                }
                else if (subParseStatus != ParserState.NeedMoreData)
                {
                    // Report error - either Invalid or DataTooBig
                    parseStatus = subParseStatus;
                    break;
                }

                break;     // read more data

            case HttpRequestState.RequestHeaders:
                if (bytesConsumed >= bytesReady)
                {
                    // we already can tell we need more data
                    break;
                }

                try
                {
                    subParseStatus = _headerParser.ParseBuffer(buffer, bytesReady, ref bytesConsumed);
                }
                catch (Exception)
                {
                    subParseStatus = ParserState.Invalid;
                }

                if (subParseStatus == ParserState.Done)
                {
                    parseStatus = subParseStatus;
                }
                else if (subParseStatus != ParserState.NeedMoreData)
                {
                    parseStatus = subParseStatus;
                    break;
                }

                break;     // need more data
            }

            return(parseStatus);
        }