예제 #1
0
        internal static int GetMediaTypeLength(string input, int startIndex,
                                               Func <MediaTypeHeaderValue> mediaTypeCreator, out MediaTypeHeaderValue parsedValue)
        {
            Contract.Requires(mediaTypeCreator != null);
            Contract.Requires(startIndex >= 0);

            parsedValue = null;

            if (string.IsNullOrEmpty(input) || (startIndex >= input.Length))
            {
                return(0);
            }

            // Caller must remove leading whitespace. If not, we'll return 0.
            string mediaType       = null;
            int    mediaTypeLength = MediaTypeHeaderValue.GetMediaTypeExpressionLength(input, startIndex, out mediaType);

            if (mediaTypeLength == 0)
            {
                return(0);
            }

            int current = startIndex + mediaTypeLength;

            current = current + HttpRuleParser.GetWhitespaceLength(input, current);
            MediaTypeHeaderValue mediaTypeHeader = null;

            // If we're not done and we have a parameter delimiter, then we have a list of parameters.
            if ((current < input.Length) && (input[current] == ';'))
            {
                mediaTypeHeader            = mediaTypeCreator();
                mediaTypeHeader._mediaType = mediaType;

                current++; // skip delimiter.
                int parameterLength = NameValueHeaderValue.GetNameValueListLength(input, current, ';',
                                                                                  (ObjectCollection <NameValueHeaderValue>)mediaTypeHeader.Parameters);

                if (parameterLength == 0)
                {
                    return(0);
                }

                parsedValue = mediaTypeHeader;
                return(current + parameterLength - startIndex);
            }

            // We have a media type without parameters.
            mediaTypeHeader            = mediaTypeCreator();
            mediaTypeHeader._mediaType = mediaType;
            parsedValue = mediaTypeHeader;
            return(current - startIndex);
        }
예제 #2
0
        internal static int GetTransferCodingLength(string input, int startIndex,
                                                    Func <TransferCodingHeaderValue> transferCodingCreator, out TransferCodingHeaderValue parsedValue)
        {
            Contract.Requires(transferCodingCreator != null);
            Contract.Requires(startIndex >= 0);

            parsedValue = null;

            if (string.IsNullOrEmpty(input) || (startIndex >= input.Length))
            {
                return(0);
            }

            // Caller must remove leading whitespace. If not, we'll return 0.
            int valueLength = HttpRuleParser.GetTokenLength(input, startIndex);

            if (valueLength == 0)
            {
                return(0);
            }

            string value   = input.Substring(startIndex, valueLength);
            int    current = startIndex + valueLength;

            current = current + HttpRuleParser.GetWhitespaceLength(input, current);
            TransferCodingHeaderValue transferCodingHeader = null;

            // If we're not done and we have a parameter delimiter, then we have a list of parameters.
            if ((current < input.Length) && (input[current] == ';'))
            {
                transferCodingHeader        = transferCodingCreator();
                transferCodingHeader._value = value;

                current++; // skip delimiter.
                int parameterLength = NameValueHeaderValue.GetNameValueListLength(input, current, ';',
                                                                                  (ObjectCollection <NameValueHeaderValue>)transferCodingHeader.Parameters);

                if (parameterLength == 0)
                {
                    return(0);
                }

                parsedValue = transferCodingHeader;
                return(current + parameterLength - startIndex);
            }

            // We have a transfer coding without parameters.
            transferCodingHeader        = transferCodingCreator();
            transferCodingHeader._value = value;
            parsedValue = transferCodingHeader;
            return(current - startIndex);
        }
예제 #3
0
        internal static int GetNameValueWithParametersLength(string input, int startIndex, out object parsedValue)
        {
            Contract.Requires(input != null);
            Contract.Requires(startIndex >= 0);

            parsedValue = null;

            if (string.IsNullOrEmpty(input) || (startIndex >= input.Length))
            {
                return(0);
            }

            NameValueHeaderValue nameValue = null;
            int nameValueLength            = NameValueHeaderValue.GetNameValueLength(input, startIndex,
                                                                                     s_nameValueCreator, out nameValue);

            if (nameValueLength == 0)
            {
                return(0);
            }

            int current = startIndex + nameValueLength;

            current = current + HttpRuleParser.GetWhitespaceLength(input, current);
            NameValueWithParametersHeaderValue nameValueWithParameters =
                nameValue as NameValueWithParametersHeaderValue;

            Debug.Assert(nameValueWithParameters != null);

            // So far we have a valid name/value pair. Check if we have also parameters for the name/value pair. If
            // yes, parse parameters. E.g. something like "name=value; param1=value1; param2=value2".
            if ((current < input.Length) && (input[current] == ';'))
            {
                current++; // skip delimiter.
                int parameterLength = NameValueHeaderValue.GetNameValueListLength(input, current, ';',
                                                                                  (ObjectCollection <NameValueHeaderValue>)nameValueWithParameters.Parameters);

                if (parameterLength == 0)
                {
                    return(0);
                }

                parsedValue = nameValueWithParameters;
                return(current + parameterLength - startIndex);
            }

            // We have a name/value pair without parameters.
            parsedValue = nameValueWithParameters;
            return(current - startIndex);
        }
        internal static int GetDispositionTypeLength(string input, int startIndex, out object parsedValue)
        {
            Contract.Requires(startIndex >= 0);

            parsedValue = null;

            if (string.IsNullOrEmpty(input) || (startIndex >= input.Length))
            {
                return(0);
            }

            // Caller must remove leading whitespace. If not, we'll return 0.
            string dispositionType       = null;
            int    dispositionTypeLength = GetDispositionTypeExpressionLength(input, startIndex, out dispositionType);

            if (dispositionTypeLength == 0)
            {
                return(0);
            }

            int current = startIndex + dispositionTypeLength;

            current = current + HttpRuleParser.GetWhitespaceLength(input, current);
            ContentDispositionHeaderValue contentDispositionHeader = new ContentDispositionHeaderValue();

            contentDispositionHeader._dispositionType = dispositionType;

            // If we're not done and we have a parameter delimiter, then we have a list of parameters.
            if ((current < input.Length) && (input[current] == ';'))
            {
                current++; // Skip delimiter.
                int parameterLength = NameValueHeaderValue.GetNameValueListLength(input, current, ';',
                                                                                  (ObjectCollection <NameValueHeaderValue>)contentDispositionHeader.Parameters);

                if (parameterLength == 0)
                {
                    return(0);
                }

                parsedValue = contentDispositionHeader;
                return(current + parameterLength - startIndex);
            }

            // We have a ContentDisposition header without parameters.
            parsedValue = contentDispositionHeader;
            return(current - startIndex);
        }