Exemple #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="FormUrlEncodedParser"/> class.
        /// </summary>
        /// <param name="nameValuePairs">The collection to which name value pairs are added as they are parsed.</param>
        /// <param name="maxMessageSize">Maximum length of all the individual name value pairs.</param>
        public FormUrlEncodedParser(ICollection <KeyValuePair <string, string> > nameValuePairs, long maxMessageSize)
        {
            // The minimum length which would be an empty buffer
            if (maxMessageSize < MinMessageSize)
            {
                throw Error.ArgumentMustBeGreaterThanOrEqualTo("maxMessageSize", maxMessageSize, MinMessageSize);
            }

            if (nameValuePairs == null)
            {
                throw Error.ArgumentNull("nameValuePairs");
            }

            _nameValuePairs       = nameValuePairs;
            _maxMessageSize       = maxMessageSize;
            _currentNameValuePair = new CurrentNameValuePair();
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="FormUrlEncodedParser"/> class.
        /// </summary>
        /// <param name="nameValuePairs">The collection to which name value pairs are added as they are parsed.</param>
        /// <param name="maxMessageSize">Maximum length of all the individual name value pairs.</param>
        public FormUrlEncodedParser(ICollection<KeyValuePair<string, string>> nameValuePairs, long maxMessageSize)
        {
            // The minimum length which would be an empty buffer
            if (maxMessageSize < MinMessageSize)
            {
                throw Error.ArgumentMustBeGreaterThanOrEqualTo("maxMessageSize", maxMessageSize, MinMessageSize);
            }

            if (nameValuePairs == null)
            {
                throw Error.ArgumentNull("nameValuePairs");
            }

            _nameValuePairs = nameValuePairs;
            _maxMessageSize = maxMessageSize;
            _currentNameValuePair = new CurrentNameValuePair();
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="FormUrlEncodedParser"/> class.
        /// </summary>
        /// <param name="nameValuePairs">The collection to which name value pairs are added as they are parsed.</param>
        /// <param name="maxMessageSize">Maximum length of all the individual name value pairs.</param>
        public FormUrlEncodedParser(ICollection<KeyValuePair<string, string>> nameValuePairs, long maxMessageSize)
        {
            // The minimum length which would be an empty buffer
            if (maxMessageSize < MinMessageSize)
            {
                throw new ArgumentOutOfRangeException("maxMessageSize", maxMessageSize, RS.Format(Properties.Resources.ArgumentMustBeGreaterThanOrEqualTo, MinMessageSize));
            }

            if (nameValuePairs == null)
            {
                throw new ArgumentNullException("nameValuePairs");
            }

            _nameValuePairs = nameValuePairs;
            _maxMessageSize = maxMessageSize;
            _currentNameValuePair = new CurrentNameValuePair();
        }
Exemple #4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="FormUrlEncodedParser"/> class.
        /// </summary>
        /// <param name="nameValuePairs">The collection to which name value pairs are added as they are parsed.</param>
        /// <param name="maxMessageSize">Maximum length of all the individual name value pairs.</param>
        public FormUrlEncodedParser(ICollection <KeyValuePair <string, string> > nameValuePairs, long maxMessageSize)
        {
            // The minimum length which would be an empty buffer
            if (maxMessageSize < MinMessageSize)
            {
                throw new ArgumentOutOfRangeException("maxMessageSize", maxMessageSize, RS.Format(Properties.Resources.ArgumentMustBeGreaterThanOrEqualTo, MinMessageSize));
            }

            if (nameValuePairs == null)
            {
                throw new ArgumentNullException("nameValuePairs");
            }

            _nameValuePairs       = nameValuePairs;
            _maxMessageSize       = maxMessageSize;
            _currentNameValuePair = new CurrentNameValuePair();
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="FormUrlEncodedParser"/> class.
        /// </summary>
        /// <param name="nameValuePairs">The collection to which name value pairs are added as they are parsed.</param>
        /// <param name="maxMessageSize">Maximum length of all the individual name value pairs.</param>
        public FormUrlEncodedParser(ICollection<KeyValuePair<string, string>> nameValuePairs, long maxMessageSize)
        {
            // The minimum length which would be an empty buffer
            if (maxMessageSize < MinMessageSize)
            {
                throw new ArgumentOutOfRangeException("maxMessageSize", maxMessageSize, "The thing was out of range... and stuff.");
            }

            if (nameValuePairs == null)
            {
                throw new ArgumentNullException("nameValuePairs");
            }

            _nameValuePairs = nameValuePairs;
            _maxMessageSize = maxMessageSize;
            _currentNameValuePair = new CurrentNameValuePair();
        }
        private static ParserState ParseNameValuePairs(
            byte[] buffer,
            int bytesReady,
            ref int bytesConsumed,
            ref NameValueState nameValueState,
            long maximumLength,
            ref long totalBytesConsumed,
            CurrentNameValuePair currentNameValuePair,
            ICollection <KeyValuePair <string, string> > nameValuePairs
            )
        {
            Contract.Assert(
                (bytesReady - bytesConsumed) >= 0,
                "ParseNameValuePairs()|(inputBufferLength - bytesParsed) < 0"
                );
            Contract.Assert(
                maximumLength <= 0 || totalBytesConsumed <= maximumLength,
                "ParseNameValuePairs()|Headers already read exceeds limit."
                );

            // Remember where we started.
            int initialBytesParsed = bytesConsumed;
            int segmentStart;

            // Set up parsing status with what will happen if we exceed the buffer.
            ParserState parseStatus  = ParserState.DataTooBig;
            long        effectiveMax =
                maximumLength <= 0
                    ? Int64.MaxValue
                    : maximumLength - totalBytesConsumed + initialBytesParsed;

            if (bytesReady < effectiveMax)
            {
                parseStatus  = ParserState.NeedMoreData;
                effectiveMax = bytesReady;
            }

            Contract.Assert(
                bytesConsumed < effectiveMax,
                "We have already consumed more than the max buffer length."
                );

            switch (nameValueState)
            {
            case NameValueState.Name:
                segmentStart = bytesConsumed;
                while (buffer[bytesConsumed] != '=' && buffer[bytesConsumed] != '&')
                {
                    if (++bytesConsumed == effectiveMax)
                    {
                        string name = Encoding.UTF8.GetString(
                            buffer,
                            segmentStart,
                            bytesConsumed - segmentStart
                            );
                        currentNameValuePair.Name.Append(name);
                        goto quit;
                    }
                }

                if (bytesConsumed > segmentStart)
                {
                    string name = Encoding.UTF8.GetString(
                        buffer,
                        segmentStart,
                        bytesConsumed - segmentStart
                        );
                    currentNameValuePair.Name.Append(name);
                }

                // Check if we got name=value or just name
                if (buffer[bytesConsumed] == '=')
                {
                    // Move part the '='
                    nameValueState = NameValueState.Value;
                    if (++bytesConsumed == effectiveMax)
                    {
                        goto quit;
                    }

                    goto case NameValueState.Value;
                }
                else
                {
                    // Copy parsed name-only to collection
                    currentNameValuePair.CopyNameOnlyTo(nameValuePairs);

                    // Move past the '&' but stay in same state
                    if (++bytesConsumed == effectiveMax)
                    {
                        goto quit;
                    }

                    goto case NameValueState.Name;
                }

            case NameValueState.Value:
                segmentStart = bytesConsumed;
                while (buffer[bytesConsumed] != '&')
                {
                    if (++bytesConsumed == effectiveMax)
                    {
                        string value = Encoding.UTF8.GetString(
                            buffer,
                            segmentStart,
                            bytesConsumed - segmentStart
                            );
                        currentNameValuePair.Value.Append(value);
                        goto quit;
                    }
                }

                if (bytesConsumed > segmentStart)
                {
                    string value = Encoding.UTF8.GetString(
                        buffer,
                        segmentStart,
                        bytesConsumed - segmentStart
                        );
                    currentNameValuePair.Value.Append(value);
                }

                // Copy parsed name value pair to collection
                currentNameValuePair.CopyTo(nameValuePairs);

                // Move past the '&'
                nameValueState = NameValueState.Name;
                if (++bytesConsumed == effectiveMax)
                {
                    goto quit;
                }

                goto case NameValueState.Name;
            }

quit:
            totalBytesConsumed += bytesConsumed - initialBytesParsed;
            return(parseStatus);
        }
        private static ParserState ParseNameValuePairs(
            byte[] buffer,
            int bytesReady,
            ref int bytesConsumed,
            ref NameValueState nameValueState,
            long maximumLength,
            ref long totalBytesConsumed,
            CurrentNameValuePair currentNameValuePair,
            ICollection<KeyValuePair<string, string>> nameValuePairs)
        {
            Contract.Assert((bytesReady - bytesConsumed) >= 0, "ParseNameValuePairs()|(inputBufferLength - bytesParsed) < 0");
            Contract.Assert(maximumLength <= 0 || totalBytesConsumed <= maximumLength, "ParseNameValuePairs()|Headers already read exceeds limit.");

            // Remember where we started.
            int initialBytesParsed = bytesConsumed;
            int segmentStart;

            // Set up parsing status with what will happen if we exceed the buffer.
            ParserState parseStatus = ParserState.DataTooBig;
            long effectiveMax = maximumLength <= 0 ? Int64.MaxValue : maximumLength - totalBytesConsumed + initialBytesParsed;
            if (bytesReady < effectiveMax)
            {
                parseStatus = ParserState.NeedMoreData;
                effectiveMax = bytesReady;
            }

            Contract.Assert(bytesConsumed < effectiveMax, "We have already consumed more than the max buffer length.");

            switch (nameValueState)
            {
                case NameValueState.Name:
                    segmentStart = bytesConsumed;
                    while (buffer[bytesConsumed] != '=' && buffer[bytesConsumed] != '&')
                    {
                        if (++bytesConsumed == effectiveMax)
                        {
                            string name = Encoding.UTF8.GetString(buffer, segmentStart, bytesConsumed - segmentStart);
                            currentNameValuePair.Name.Append(name);
                            goto quit;
                        }
                    }

                    if (bytesConsumed > segmentStart)
                    {
                        string name = Encoding.UTF8.GetString(buffer, segmentStart, bytesConsumed - segmentStart);
                        currentNameValuePair.Name.Append(name);
                    }

                    // Check if we got name=value or just name
                    if (buffer[bytesConsumed] == '=')
                    {
                        // Move part the '='
                        nameValueState = NameValueState.Value;
                        if (++bytesConsumed == effectiveMax)
                        {
                            goto quit;
                        }

                        goto case NameValueState.Value;
                    }
                    else
                    {
                        // Copy parsed name-only to collection
                        currentNameValuePair.CopyNameOnlyTo(nameValuePairs);

                        // Move past the '&' but stay in same state
                        if (++bytesConsumed == effectiveMax)
                        {
                            goto quit;
                        }

                        goto case NameValueState.Name;
                    }

                case NameValueState.Value:
                    segmentStart = bytesConsumed;
                    while (buffer[bytesConsumed] != '&')
                    {
                        if (++bytesConsumed == effectiveMax)
                        {
                            string value = Encoding.UTF8.GetString(buffer, segmentStart, bytesConsumed - segmentStart);
                            currentNameValuePair.Value.Append(value);
                            goto quit;
                        }
                    }

                    if (bytesConsumed > segmentStart)
                    {
                        string value = Encoding.UTF8.GetString(buffer, segmentStart, bytesConsumed - segmentStart);
                        currentNameValuePair.Value.Append(value);
                    }

                    // Copy parsed name value pair to collection
                    currentNameValuePair.CopyTo(nameValuePairs);

                    // Move past the '&'
                    nameValueState = NameValueState.Name;
                    if (++bytesConsumed == effectiveMax)
                    {
                        goto quit;
                    }

                    goto case NameValueState.Name;
            }

        quit:
            totalBytesConsumed += bytesConsumed - initialBytesParsed;
            return parseStatus;
        }