Exemplo n.º 1
0
        /// <summary>Read a parameter for a media type/range.</summary>
        /// <param name="text">Text to read from.</param>
        /// <param name="textIndex">Pointer in text.</param>
        /// <param name="parameters">Array with parameters to grow as necessary.</param>
        private static void ReadMediaTypeParameter(string text, ref int textIndex, ref KeyValuePair <string, string>[] parameters)
        {
            int startIndex = textIndex;

            if (ReadToken(text, ref textIndex))
            {
                throw HttpProcessUtils.CreateParsingException(Resource.HttpProcessUtility_MediaTypeMissingValue);
            }

            string parameterName = text.Substring(startIndex, textIndex - startIndex);

            if (text[textIndex] != '=')
            {
                throw HttpProcessUtils.CreateParsingException(Resource.HttpProcessUtility_MediaTypeMissingValue);
            }

            textIndex++;

            string parameterValue = ReadQuotedParameterValue(parameterName, text, ref textIndex);

            // Add the parameter name/value pair to the list.
            if (parameters == null)
            {
                parameters = new KeyValuePair <string, string> [1];
            }
            else
            {
                KeyValuePair <string, string>[] grow = new KeyValuePair <string, string> [parameters.Length + 1];
                Array.Copy(parameters, grow, parameters.Length);
                parameters = grow;
            }

            parameters[parameters.Length - 1] = new KeyValuePair <string, string>(parameterName, parameterValue);
        }
Exemplo n.º 2
0
        /// <summary>Reads the type and subtype specifications for a MIME type.</summary>
        /// <param name='text'>Text in which specification exists.</param>
        /// <param name='textIndex'>Pointer into text.</param>
        /// <param name='type'>Type of media found.</param>
        /// <param name='subType'>Subtype of media found.</param>
        private static void ReadMediaTypeAndSubtype(string text, ref int textIndex, out string type, out string subType)
        {
            Debug.Assert(text != null, "text != null");
            int textStart = textIndex;

            if (ReadToken(text, ref textIndex))
            {
                throw HttpProcessUtils.CreateParsingException(Resource.HttpProcessUtility_MediaTypeUnspecified);
            }

            if (text[textIndex] != '/')
            {
                throw HttpProcessUtils.CreateParsingException(Resource.HttpProcessUtility_MediaTypeRequiresSlash);
            }

            type = text.Substring(textStart, textIndex - textStart);
            textIndex++;

            int subTypeStart = textIndex;

            ReadToken(text, ref textIndex);

            if (textIndex == subTypeStart)
            {
                throw HttpProcessUtils.CreateParsingException(Resource.HttpProcessUtility_MediaTypeRequiresSubType);
            }

            subType = text.Substring(subTypeStart, textIndex - subTypeStart);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Reads the numeric part of a quality value substring, normalizing it to 0-1000
        /// rather than the standard 0.000-1.000 ranges.
        /// </summary>
        /// <param name="text">Text to read qvalue from.</param>
        /// <param name="textIndex">Index into text where the qvalue starts.</param>
        /// <param name="qualityValue">After the method executes, the normalized qvalue.</param>
        /// <remarks>
        /// For more information, see RFC 2616.3.8.
        /// </remarks>
        private static void ReadQualityValue(string text, ref int textIndex, out int qualityValue)
        {
            char digit = text[textIndex++];

            if (digit == '0')
            {
                qualityValue = 0;
            }
            else if (digit == '1')
            {
                qualityValue = 1;
            }
            else
            {
                throw HttpProcessUtils.CreateParsingException(Resource.HttpContextServiceHost_MalformedHeaderValue);
            }

            if (textIndex < text.Length && text[textIndex] == '.')
            {
                textIndex++;

                int adjustFactor = 1000;
                while (adjustFactor > 1 && textIndex < text.Length)
                {
                    char c         = text[textIndex];
                    int  charValue = DigitToInt32(c);
                    if (charValue >= 0)
                    {
                        textIndex++;
                        adjustFactor /= 10;
                        qualityValue *= 10;
                        qualityValue += charValue;
                    }
                    else
                    {
                        break;
                    }
                }

                qualityValue = qualityValue *= adjustFactor;
                if (qualityValue > 1000)
                {
                    // Too high of a value in qvalue.
                    throw HttpProcessUtils.CreateParsingException(Resource.HttpContextServiceHost_MalformedHeaderValue);
                }
            }
            else
            {
                qualityValue *= 1000;
            }
        }
Exemplo n.º 4
0
 /// <summary>
 /// Converts the specified character from the ASCII range to a digit.
 /// </summary>
 /// <param name="c">Character to convert.</param>
 /// <returns>
 /// The Int32 value for c, or -1 if it is an element separator.
 /// </returns>
 private static int DigitToInt32(char c)
 {
     if (c >= '0' && c <= '9')
     {
         return((int)(c - '0'));
     }
     else
     {
         if (IsHttpElementSeparator(c))
         {
             return(-1);
         }
         else
         {
             throw HttpProcessUtils.CreateParsingException(Resource.HttpContextServiceHost_MalformedHeaderValue);
         }
     }
 }
Exemplo n.º 5
0
        /// <summary>Returns all MIME types from the specified (non-blank) <paramref name='text' />.</summary>
        /// <param name='text'>Non-blank text, as it appears on an HTTP Accepts header.</param>
        /// <returns>An enumerable object with media type descriptions.</returns>
        private static IEnumerable <MediaType> MimeTypesFromAcceptHeader(string text)
        {
            Debug.Assert(!String.IsNullOrEmpty(text), "!String.IsNullOrEmpty(text)");
            List <MediaType> mediaTypes = new List <MediaType>();
            int textIndex = 0;

            while (!SkipWhitespace(text, ref textIndex))
            {
                string type;
                string subType;
                ReadMediaTypeAndSubtype(text, ref textIndex, out type, out subType);

                KeyValuePair <string, string>[] parameters = null;
                while (!SkipWhitespace(text, ref textIndex))
                {
                    if (text[textIndex] == ',')
                    {
                        textIndex++;
                        break;
                    }

                    if (text[textIndex] != ';')
                    {
                        throw HttpProcessUtils.CreateParsingException(Resource.HttpProcessUtility_MediaTypeRequiresSemicolonBeforeParameter);
                    }

                    textIndex++;
                    if (SkipWhitespace(text, ref textIndex))
                    {
                        // ';' should be a leading separator, but we choose to be a
                        // bit permissive and allow it as a final delimiter as well.
                        break;
                    }

                    ReadMediaTypeParameter(text, ref textIndex, ref parameters);
                }

                mediaTypes.Add(new MediaType(type, subType, parameters));
            }

            return(mediaTypes);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Reads Mime type parameter value for a particular parameter in the Content-Type/Accept headers.
        /// </summary>
        /// <param name="parameterName">Name of parameter.</param>
        /// <param name="headerText">Header text.</param>
        /// <param name="textIndex">Parsing index in <paramref name="headerText"/>.</param>
        /// <returns>String representing the value of the <paramref name="parameterName"/> parameter.</returns>
        private static string ReadQuotedParameterValue(string parameterName, string headerText, ref int textIndex)
        {
            StringBuilder parameterValue = new StringBuilder();

            // Check if the value is quoted.
            bool valueIsQuoted = false;

            if (textIndex < headerText.Length)
            {
                if (headerText[textIndex] == '\"')
                {
                    textIndex++;
                    valueIsQuoted = true;
                }
            }

            while (textIndex < headerText.Length)
            {
                char currentChar = headerText[textIndex];

                if (currentChar == '\\' || currentChar == '\"')
                {
                    if (!valueIsQuoted)
                    {
                        throw HttpProcessUtils.CreateParsingException(string.Format(
                                                                          CultureInfo.InvariantCulture,
                                                                          Resource.HttpProcessUtility_EscapeCharWithoutQuotes,
                                                                          parameterName));
                    }

                    textIndex++;

                    // End of quoted parameter value.
                    if (currentChar == '\"')
                    {
                        valueIsQuoted = false;
                        break;
                    }

                    if (textIndex >= headerText.Length)
                    {
                        throw HttpProcessUtils.CreateParsingException(string.Format(
                                                                          CultureInfo.InvariantCulture,
                                                                          Resource.HttpProcessUtility_EscapeCharAtEnd,
                                                                          parameterName));
                    }

                    currentChar = headerText[textIndex];
                }
                else
                if (!IsHttpToken(currentChar))
                {
                    // If the given character is special, we stop processing.
                    break;
                }

                parameterValue.Append(currentChar);
                textIndex++;
            }

            if (valueIsQuoted)
            {
                throw HttpProcessUtils.CreateParsingException(string.Format(
                                                                  CultureInfo.InvariantCulture,
                                                                  Resource.HttpProcessUtility_ClosingQuoteNotFound,
                                                                  parameterName));
            }

            return(parameterValue.ToString());
        }