Exemple #1
0
        internal static int GetHashCode(ObjectCollection <NameValueHeaderValue> values)
        {
            if ((values == null) || (values.Count == 0))
            {
                return(0);
            }

            int result = 0;

            foreach (var value in values)
            {
                result = result ^ value.GetHashCode();
            }
            return(result);
        }
Exemple #2
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);
        }
Exemple #3
0
 internal static NameValueHeaderValue Find(
     ObjectCollection <NameValueHeaderValue> values,
     string name)
 {
     if (values == null || values.Count == 0)
     {
         return((NameValueHeaderValue)null);
     }
     foreach (NameValueHeaderValue valueHeaderValue in values)
     {
         if (string.Equals(valueHeaderValue.Name, name, StringComparison.OrdinalIgnoreCase))
         {
             return(valueHeaderValue);
         }
     }
     return((NameValueHeaderValue)null);
 }
        internal static NameValueHeaderValue?Find(ObjectCollection <NameValueHeaderValue>?values, string name)
        {
            Debug.Assert((name != null) && (name.Length > 0));

            if ((values == null) || (values.Count == 0))
            {
                return(null);
            }

            foreach (var value in values)
            {
                if (string.Equals(value.Name, name, StringComparison.OrdinalIgnoreCase))
                {
                    return(value);
                }
            }
            return(null);
        }
Exemple #5
0
        internal static double?GetQuality(ObjectCollection <NameValueHeaderValue> parameters)
        {
            NameValueHeaderValue valueHeaderValue = NameValueHeaderValue.Find(parameters, "q");

            if (valueHeaderValue != null)
            {
                double result = 0.0;
                if (double.TryParse(valueHeaderValue.Value, NumberStyles.AllowDecimalPoint, (IFormatProvider)NumberFormatInfo.InvariantInfo, out result))
                {
                    return(new double?(result));
                }
                if (NetEventSource.IsEnabled)
                {
                    NetEventSource.Error((object)null, (object)SR.Format(SR.net_http_log_headers_invalid_quality, (object)valueHeaderValue.Value), nameof(GetQuality));
                }
            }
            return(new double?());
        }
        private static void AppendValues(StringBuilder sb, ObjectCollection <string> values)
        {
            bool first = true;

            foreach (string value in values)
            {
                if (first)
                {
                    first = false;
                }
                else
                {
                    sb.Append(", ");
                }

                sb.Append(value);
            }
        }
Exemple #7
0
 internal static bool AreEqualCollections <T>(
     ObjectCollection <T> x,
     ObjectCollection <T> y,
     IEqualityComparer <T> comparer)
     where T : class
 {
     if (x == null)
     {
         return(y == null || y.Count == 0);
     }
     if (y == null)
     {
         return(x.Count == 0);
     }
     if (x.Count != y.Count)
     {
         return(false);
     }
     if (x.Count == 0)
     {
         return(true);
     }
     bool[] flagArray = new bool[x.Count];
     foreach (T x1 in x)
     {
         int  index = 0;
         bool flag  = false;
         foreach (T y1 in y)
         {
             if (!flagArray[index] && (comparer == null && x1.Equals((object)y1) || comparer != null && comparer.Equals(x1, y1)))
             {
                 flagArray[index] = true;
                 flag             = true;
                 break;
             }
             ++index;
         }
         if (!flag)
         {
             return(false);
         }
     }
     return(true);
 }
        internal static double?GetQuality(ObjectCollection <NameValueHeaderValue> parameters)
        {
            Debug.Assert(parameters != null);

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

            if (qualityParameter != null)
            {
                // Note that the RFC requires decimal '.' regardless of the culture. I.e. using ',' as decimal
                // separator is considered invalid (even if the current culture would allow it).
                double qualityValue = 0;
                if (double.TryParse(qualityParameter.Value, NumberStyles.AllowDecimalPoint,
                                    NumberFormatInfo.InvariantInfo, out qualityValue))
                {
                    return(qualityValue);
                }
            }
            return(null);
        }
Exemple #9
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);
        }
        internal static void ToString(ObjectCollection <NameValueHeaderValue>?values, char separator, bool leadingSeparator,
                                      StringBuilder destination)
        {
            Debug.Assert(destination != null);

            if ((values == null) || (values.Count == 0))
            {
                return;
            }

            foreach (var value in values)
            {
                if (leadingSeparator || (destination.Length > 0))
                {
                    destination.Append(separator);
                    destination.Append(' ');
                }
                value.AddToStringBuilder(destination);
            }
        }
Exemple #11
0
 internal static void ToString(
     ObjectCollection <NameValueHeaderValue> values,
     char separator,
     bool leadingSeparator,
     StringBuilder destination)
 {
     if (values == null || values.Count == 0)
     {
         return;
     }
     foreach (NameValueHeaderValue valueHeaderValue in values)
     {
         if (leadingSeparator || destination.Length > 0)
         {
             destination.Append(separator);
             destination.Append(' ');
         }
         valueHeaderValue.AddToStringBuilder(destination);
     }
 }
        // 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;
                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);
            }
        }
Exemple #13
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);
                }
            }
        }
Exemple #14
0
        internal static double?GetQuality(ObjectCollection <NameValueHeaderValue> parameters)
        {
            Debug.Assert(parameters != null);

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

            if (qualityParameter != null)
            {
                // Note that the RFC requires decimal '.' regardless of the culture. I.e. using ',' as decimal
                // separator is considered invalid (even if the current culture would allow it).
                double qualityValue = 0;
                if (double.TryParse(qualityParameter.Value, NumberStyles.AllowDecimalPoint,
                                    NumberFormatInfo.InvariantInfo, out qualityValue))
                {
                    return(qualityValue);
                }
                // If the stored value is an invalid quality value, just return null and log a warning.
                if (NetEventSource.IsEnabled)
                {
                    NetEventSource.Error(null, SR.Format(SR.net_http_log_headers_invalid_quality, qualityParameter.Value));
                }
            }
            return(null);
        }
Exemple #15
0
        internal static double?GetQuality(ObjectCollection <NameValueHeaderValue> parameters)
        {
            Contract.Requires(parameters != null);

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

            if (qualityParameter != null)
            {
                // Note that the RFC requires decimal '.' regardless of the culture. I.e. using ',' as decimal
                // separator is considered invalid (even if the current culture would allow it).
                double qualityValue = 0;
                if (double.TryParse(qualityParameter.Value, NumberStyles.AllowDecimalPoint,
                                    NumberFormatInfo.InvariantInfo, out qualityValue))
                {
                    return(qualityValue);
                }
                // If the stored value is an invalid quality value, just return null and log a warning.
                if (Logging.On)
                {
                    Logging.PrintError(Logging.Http, string.Format(System.Globalization.CultureInfo.InvariantCulture, SR.net_http_log_headers_invalid_quality, qualityParameter.Value));
                }
            }
            return(null);
        }
Exemple #16
0
        private CacheControlHeaderValue(CacheControlHeaderValue source)
        {
            Debug.Assert(source != null);

            _noCache         = source._noCache;
            _noStore         = source._noStore;
            _maxAge          = source._maxAge;
            _sharedMaxAge    = source._sharedMaxAge;
            _maxStale        = source._maxStale;
            _maxStaleLimit   = source._maxStaleLimit;
            _minFresh        = source._minFresh;
            _noTransform     = source._noTransform;
            _onlyIfCached    = source._onlyIfCached;
            _publicField     = source._publicField;
            _privateField    = source._privateField;
            _mustRevalidate  = source._mustRevalidate;
            _proxyRevalidate = source._proxyRevalidate;

            if (source._noCacheHeaders != null)
            {
                foreach (var noCacheHeader in source._noCacheHeaders)
                {
                    NoCacheHeaders.Add(noCacheHeader);
                }
            }

            if (source._privateHeaders != null)
            {
                foreach (var privateHeader in source._privateHeaders)
                {
                    PrivateHeaders.Add(privateHeader);
                }
            }

            _extensions = source._extensions.Clone();
        }
Exemple #17
0
        internal static bool AreEqualCollections <T>(ObjectCollection <T>?x, ObjectCollection <T>?y, IEqualityComparer <T>?comparer) where T : class
        {
            if (x == null)
            {
                return((y == null) || (y.Count == 0));
            }

            if (y == null)
            {
                return(x.Count == 0);
            }

            if (x.Count != y.Count)
            {
                return(false);
            }

            if (x.Count == 0)
            {
                return(true);
            }

            // We have two unordered lists. So comparison is an O(n*m) operation which is expensive. Usually
            // headers have 1-2 parameters (if any), so this comparison shouldn't be too expensive.
            bool[] alreadyFound = new bool[x.Count];
            int    i            = 0;

            foreach (var xItem in x)
            {
                Debug.Assert(xItem != null);

                i = 0;
                bool found = false;
                foreach (var yItem in y)
                {
                    if (!alreadyFound[i])
                    {
                        if (((comparer == null) && xItem.Equals(yItem)) ||
                            ((comparer != null) && comparer.Equals(xItem, yItem)))
                        {
                            alreadyFound[i] = true;
                            found           = true;
                            break;
                        }
                    }
                    i++;
                }

                if (!found)
                {
                    return(false);
                }
            }

            // Since we never re-use a "found" value in 'y', we expect 'alreadyFound' to have all fields set to 'true'.
            // Otherwise the two collections can't be equal and we should not get here.
            Debug.Assert(Array.TrueForAll(alreadyFound, value => value),
                         "Expected all values in 'alreadyFound' to be true since collections are considered equal.");

            return(true);
        }
Exemple #18
0
 internal static bool AreEqualCollections <T>(ObjectCollection <T>?x, ObjectCollection <T>?y) where T : class
 {
     return(AreEqualCollections(x, y, null));
 }
 protected NameValueWithParametersHeaderValue(NameValueWithParametersHeaderValue source)
     : base(source)
 {
     _parameters = source._parameters.Clone();
 }
        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);
                }

                if (destination == null)
                {
                    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);
        }
Exemple #21
0
 internal static bool AreEqualCollections <T>(ObjectCollection <T> x, ObjectCollection <T> y) where T : class
 {
     return(HeaderUtilities.AreEqualCollections <T>(x, y, (IEqualityComparer <T>)null));
 }