Buffer-oriented parsing of HTML form URL-ended, also known as application/x-www-form-urlencoded, data.
コード例 #1
0
        public void ParseBufferCorrectly(string segment, string name, string value)
        {
            for (int index = 1; index < Iterations; index++)
            {
                List <string> segments = new List <string>();
                for (int cnt = 0; cnt < index; cnt++)
                {
                    segments.Add(segment);
                }

                byte[] data = CreateBuffer(segments.ToArray());
                for (var cnt = 1; cnt <= data.Length; cnt++)
                {
                    ICollection <KeyValuePair <string, string> > collection;
                    FormUrlEncodedParser parser = CreateParser(data.Length + 1, out collection);
                    Assert.NotNull(parser);

                    int         totalBytesConsumed;
                    ParserState state = ParseBufferInSteps(parser, data, cnt, out totalBytesConsumed);
                    Assert.Equal(ParserState.Done, state);
                    Assert.Equal(data.Length, totalBytesConsumed);

                    Assert.Equal(index, collection.Count());
                    foreach (KeyValuePair <string, string> element in collection)
                    {
                        Assert.Equal(name, element.Key);
                        Assert.Equal(value, element.Value);
                    }
                }
            }
        }
コード例 #2
0
        internal static ParserState ParseBufferInSteps(
            FormUrlEncodedParser 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 == buffer.Length - size
                    );
                totalBytesConsumed += bytesConsumed;

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

            return(state);
        }
コード例 #3
0
        public void ParseBufferThrowsOnNullBuffer()
        {
            ICollection <KeyValuePair <string, string> > collection;
            FormUrlEncodedParser parser = CreateParser(128, out collection);
            int bytesConsumed           = 0;

            Assert.ThrowsArgumentNull(() => { parser.ParseBuffer(null, 0, ref bytesConsumed, false); }, "buffer");
        }
コード例 #4
0
        public void FormUrlEncodedParserThrowsOnInvalidSize()
        {
            Assert.ThrowsArgumentGreaterThanOrEqualTo(() => { new FormUrlEncodedParser(CreateCollection(), MinMessageSize - 1); }, "maxMessageSize", MinMessageSize.ToString(), MinMessageSize - 1);

            FormUrlEncodedParser parser = new FormUrlEncodedParser(CreateCollection(), MinMessageSize);

            Assert.NotNull(parser);

            parser = new FormUrlEncodedParser(CreateCollection(), MinMessageSize + 1);
            Assert.NotNull(parser);
        }
コード例 #5
0
        public void HeaderParserDataTooBig()
        {
            byte[] data = CreateBuffer("N=V");
            ICollection <KeyValuePair <string, string> > collection;
            FormUrlEncodedParser parser = CreateParser(MinMessageSize, out collection);

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

            Assert.Equal(ParserState.DataTooBig, state);
            Assert.Equal(MinMessageSize, bytesConsumed);
        }
コード例 #6
0
        public void ParseBufferHandlesEmptyBuffer()
        {
            byte[] data = CreateBuffer();
            ICollection <KeyValuePair <string, string> > collection;
            FormUrlEncodedParser parser = CreateParser(MinMessageSize, out collection);

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

            Assert.Equal(ParserState.Done, state);
            Assert.Equal(data.Length, bytesConsumed);
            Assert.Equal(0, collection.Count());
        }
コード例 #7
0
        internal static ParserState ParseBufferInSteps(FormUrlEncodedParser 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 == buffer.Length - size);
                totalBytesConsumed += bytesConsumed;

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

            return state;
        }
コード例 #8
0
        /// <summary>
        /// Reads all name-value pairs encoded as HTML Form URL encoded data and add them to 
        /// a collection as UNescaped URI strings.
        /// </summary>
        /// <param name="input">Stream to read from.</param>
        /// <param name="bufferSize">Size of the buffer used to read the contents.</param>
        /// <returns>Collection of name-value pairs.</returns>
        private static IEnumerable<KeyValuePair<string, string>> ReadFormUrlEncoded(Stream input, int bufferSize)
        {
            Contract.Assert(input != null, "input stream cannot be null");
            Contract.Assert(bufferSize >= MinBufferSize, "buffer size cannot be less than MinBufferSize");

            byte[] data = new byte[bufferSize];

            int bytesRead;
            bool isFinal = false;
            List<KeyValuePair<string, string>> result = new List<KeyValuePair<string, string>>();
            FormUrlEncodedParser parser = new FormUrlEncodedParser(result, Int64.MaxValue);
            ParserState state;

            while (true)
            {
                try
                {
                    bytesRead = input.Read(data, 0, data.Length);
                    if (bytesRead == 0)
                    {
                        isFinal = true;
                    }
                }
                catch (Exception e)
                {
                    throw Error.InvalidOperation(e, Properties.Resources.ErrorReadingFormUrlEncodedStream);
                }

                int bytesConsumed = 0;
                state = parser.ParseBuffer(data, bytesRead, ref bytesConsumed, isFinal);
                if (state != ParserState.NeedMoreData && state != ParserState.Done)
                {
                    throw Error.InvalidOperation(Properties.Resources.FormUrlEncodedParseError, bytesConsumed);
                }

                if (isFinal)
                {
                    return result;
                }
            }
        }
コード例 #9
0
        public void FormUrlEncodedParserThrowsOnInvalidSize()
        {
            Assert.ThrowsArgumentGreaterThanOrEqualTo(() => { new FormUrlEncodedParser(CreateCollection(), MinMessageSize - 1); }, "maxMessageSize", MinMessageSize.ToString(), MinMessageSize - 1);

            FormUrlEncodedParser parser = new FormUrlEncodedParser(CreateCollection(), MinMessageSize);
            Assert.NotNull(parser);

            parser = new FormUrlEncodedParser(CreateCollection(), MinMessageSize + 1);
            Assert.NotNull(parser);
        }