private static bool TrySetOptionalTokenList(NameValueHeaderValue nameValue, ref bool boolField,
                                                    ref ObjectCollection <string>?destination)
        {
            Debug.Assert(nameValue != null);

            if (nameValue.Value == null)
            {
                boolField = true;
                return(true);
            }

            // We need the string to be at least 3 chars long: 2x quotes and at least 1 character. Also make sure we
            // have a quoted string. Note that NameValueHeaderValue will never have leading/trailing whitespace.
            string valueString = nameValue.Value;

            if ((valueString.Length < 3) || (valueString[0] != '\"') || (valueString[valueString.Length - 1] != '\"'))
            {
                return(false);
            }

            // We have a quoted string. Now verify that the string contains a list of valid tokens separated by ','.
            int  current            = 1;                      // skip the initial '"' character.
            int  maxLength          = valueString.Length - 1; // -1 because we don't want to parse the final '"'.
            bool separatorFound     = false;
            int  originalValueCount = destination == null ? 0 : destination.Count;

            while (current < maxLength)
            {
                current = HeaderUtilities.GetNextNonEmptyOrWhitespaceIndex(valueString, current, true,
                                                                           out separatorFound);

                if (current == maxLength)
                {
                    break;
                }

                int tokenLength = HttpRuleParser.GetTokenLength(valueString, current);

                if (tokenLength == 0)
                {
                    // We already skipped whitespace and separators. If we don't have a token it must be an invalid
                    // character.
                    return(false);
                }

                destination ??= new ObjectCollection <string>(s_checkIsValidToken);
                destination.Add(valueString.Substring(current, tokenLength));

                current = current + tokenLength;
            }

            // After parsing a valid token list, we expect to have at least one value
            if ((destination != null) && (destination.Count > originalValueCount))
            {
                boolField = true;
                return(true);
            }

            return(false);
        }
Ejemplo n.º 2
0
        internal static void SetQuality(
            ObjectCollection <NameValueHeaderValue> parameters,
            double?value)
        {
            NameValueHeaderValue valueHeaderValue = NameValueHeaderValue.Find(parameters, "q");

            if (value.HasValue)
            {
                double?nullable = value;
                double num1     = 0.0;
                if (!(nullable.GetValueOrDefault() < num1 & nullable.HasValue))
                {
                    nullable = value;
                    double num2 = 1.0;
                    if (!(nullable.GetValueOrDefault() > num2 & nullable.HasValue))
                    {
                        string str = value.Value.ToString("0.0##", (IFormatProvider)NumberFormatInfo.InvariantInfo);
                        if (valueHeaderValue != null)
                        {
                            valueHeaderValue.Value = str;
                            return;
                        }
                        parameters.Add(new NameValueHeaderValue("q", str));
                        return;
                    }
                }
                throw new ArgumentOutOfRangeException(nameof(value));
            }
            if (valueHeaderValue == null)
            {
                return;
            }
            parameters.Remove(valueHeaderValue);
        }
Ejemplo n.º 3
0
        internal static ObjectCollection <NameValueHeaderValue>?Clone(this ObjectCollection <NameValueHeaderValue>?source)
        {
            if (source == null)
            {
                return(null);
            }

            var copy = new ObjectCollection <NameValueHeaderValue>();

            foreach (NameValueHeaderValue item in source)
            {
                copy.Add(new NameValueHeaderValue(item));
            }

            return(copy);
        }
Ejemplo n.º 4
0
        internal static int GetNameValueListLength(
            string input,
            int startIndex,
            char delimiter,
            ObjectCollection <NameValueHeaderValue> nameValueCollection)
        {
            if (string.IsNullOrEmpty(input) || startIndex >= input.Length)
            {
                return(0);
            }
            int startIndex1 = startIndex + HttpRuleParser.GetWhitespaceLength(input, startIndex);
            int index;

            while (true)
            {
                NameValueHeaderValue parsedValue = (NameValueHeaderValue)null;
                int nameValueLength = NameValueHeaderValue.GetNameValueLength(input, startIndex1, NameValueHeaderValue.s_defaultNameValueCreator, out parsedValue);
                if (nameValueLength != 0)
                {
                    nameValueCollection.Add(parsedValue);
                    int startIndex2 = startIndex1 + nameValueLength;
                    index = startIndex2 + HttpRuleParser.GetWhitespaceLength(input, startIndex2);
                    if (index != input.Length && (int)input[index] == (int)delimiter)
                    {
                        int startIndex3 = index + 1;
                        startIndex1 = startIndex3 + HttpRuleParser.GetWhitespaceLength(input, startIndex3);
                    }
                    else
                    {
                        goto label_6;
                    }
                }
                else
                {
                    break;
                }
            }
            return(0);

label_6:
            return(index - startIndex);
        }
Ejemplo n.º 5
0
        // Returns the length of a name/value list, separated by 'delimiter'. E.g. "a=b, c=d, e=f" adds 3
        // name/value pairs to 'nameValueCollection' if 'delimiter' equals ','.
        internal static int GetNameValueListLength(string input, int startIndex, char delimiter,
                                                   ObjectCollection <NameValueHeaderValue> nameValueCollection)
        {
            Debug.Assert(nameValueCollection != null);
            Debug.Assert(startIndex >= 0);

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

            int current = startIndex + HttpRuleParser.GetWhitespaceLength(input, startIndex);

            while (true)
            {
                NameValueHeaderValue parameter = null;
                int nameValueLength            = NameValueHeaderValue.GetNameValueLength(input, current,
                                                                                         s_defaultNameValueCreator, out parameter);

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

                nameValueCollection.Add(parameter);
                current = current + nameValueLength;
                current = current + HttpRuleParser.GetWhitespaceLength(input, current);

                if ((current == input.Length) || (input[current] != delimiter))
                {
                    // We're done and we have at least one valid name/value pair.
                    return(current - startIndex);
                }

                // input[current] is 'delimiter'. Skip the delimiter and whitespace and try to parse again.
                current++; // skip delimiter.
                current = current + HttpRuleParser.GetWhitespaceLength(input, current);
            }
        }
Ejemplo n.º 6
0
        internal static void SetQuality(ObjectCollection <NameValueHeaderValue> parameters, double?value)
        {
            Debug.Assert(parameters != null);

            NameValueHeaderValue qualityParameter = NameValueHeaderValue.Find(parameters, qualityName);

            if (value.HasValue)
            {
                // Note that even if we check the value here, we can't prevent a user from adding an invalid quality
                // value using Parameters.Add(). Even if we would prevent the user from adding an invalid value
                // using Parameters.Add() he could always add invalid values using HttpHeaders.AddWithoutValidation().
                // So this check is really for convenience to show users that they're trying to add an invalid
                // value.
                if ((value < 0) || (value > 1))
                {
                    throw new ArgumentOutOfRangeException(nameof(value));
                }

                string qualityString = ((double)value).ToString("0.0##", NumberFormatInfo.InvariantInfo);
                if (qualityParameter != null)
                {
                    qualityParameter.Value = qualityString;
                }
                else
                {
                    parameters.Add(new NameValueHeaderValue(qualityName, qualityString));
                }
            }
            else
            {
                // Remove quality parameter
                if (qualityParameter != null)
                {
                    parameters.Remove(qualityParameter);
                }
            }
        }