Example #1
0
        // Helper to invoke parser around a query string
        private static IEnumerable <KeyValuePair <string, string> > ParseQueryString(string query)
        {
            List <KeyValuePair <string, string> > result = new List <KeyValuePair <string, string> >();

            if (String.IsNullOrWhiteSpace(query))
            {
                return(result);
            }

            if (query.Length > 0 && query[0] == '?')
            {
                query = query.Substring(1);
            }

            byte[] bytes = System.Text.Encoding.UTF8.GetBytes(query);

            FormUrlEncodedParser parser = new FormUrlEncodedParser(result, Int64.MaxValue);

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

            if (state != ParserState.Done)
            {
                throw new InvalidOperationException(RS.Format(Properties.Resources.FormUrlEncodedParseError, bytesConsumed));
            }

            return(result);
        }
Example #2
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);
                }
            }
        }
        // Helper to invoke parser around a query string
        private static IEnumerable <KeyValuePair <string, string> > ParseQueryString(string query)
        {
            List <KeyValuePair <string, string> > result = new List <KeyValuePair <string, string> >();

            if (String.IsNullOrWhiteSpace(query))
            {
                return(result);
            }

            byte[] bytes = Encoding.UTF8.GetBytes(query);

            FormUrlEncodedParser parser = new FormUrlEncodedParser(result, Int64.MaxValue);

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

            if (state != ParserState.Done)
            {
                throw Error.InvalidOperation(Resources.FormUrlEncodedParseError, bytesConsumed);
            }

            return(result);
        }