Exemple #1
0
        internal static int GetRangeItemLength(string input, int startIndex, out RangeItemHeaderValue parsedValue)
        {
            Contract.Requires(startIndex >= 0);

            // This parser parses number ranges: e.g. '1-2', '1-', '-2'.

            parsedValue = null;

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

            // Caller must remove leading whitespaces. If not, we'll return 0.
            int current = startIndex;

            // Try parse the first value of a value pair.
            int fromStartIndex = current;
            int fromLength     = HttpRuleParser.GetNumberLength(input, current, false);

            if (fromLength > HttpRuleParser.MaxInt64Digits)
            {
                return(0);
            }

            current = current + fromLength;
            current = current + HttpRuleParser.GetWhitespaceLength(input, current);

            // Afer the first value, the '-' character must follow.
            if ((current == input.Length) || (input[current] != '-'))
            {
                // We need a '-' character otherwise this can't be a valid range.
                return(0);
            }

            current++; // skip the '-' character
            current = current + HttpRuleParser.GetWhitespaceLength(input, current);

            int toStartIndex = current;
            int toLength     = 0;

            // If we didn't reach the end of the string, try parse the second value of the range.
            if (current < input.Length)
            {
                toLength = HttpRuleParser.GetNumberLength(input, current, false);

                if (toLength > HttpRuleParser.MaxInt64Digits)
                {
                    return(0);
                }

                current = current + toLength;
                current = current + HttpRuleParser.GetWhitespaceLength(input, current);
            }

            if ((fromLength == 0) && (toLength == 0))
            {
                return(0); // At least one value must be provided in order to be a valid range.
            }

            // Try convert first value to int64
            long from = 0;

            if ((fromLength > 0) && !HeaderUtilities.TryParseInt64(input.Substring(fromStartIndex, fromLength), out from))
            {
                return(0);
            }

            // Try convert second value to int64
            long to = 0;

            if ((toLength > 0) && !HeaderUtilities.TryParseInt64(input.Substring(toStartIndex, toLength), out to))
            {
                return(0);
            }

            // 'from' must not be greater than 'to'
            if ((fromLength > 0) && (toLength > 0) && (from > to))
            {
                return(0);
            }

            parsedValue = new RangeItemHeaderValue((fromLength == 0 ? (long?)null : (long?)from),
                                                   (toLength == 0 ? (long?)null : (long?)to));
            return(current - startIndex);
        }
Exemple #2
0
 /// <summary>确定指定的 <see cref="T:System.Object" /> 是否等于当前的 <see cref="T:System.Net.Http.Headers.MediaTypeHeaderValue" /> 对象。</summary>
 /// <param name="obj">要与当前对象进行比较的对象。</param>
 /// <returns>如果指定的 <see cref="T:System.Object" /> 等于当前的对象,则为 <see langword="true" />;否则为 <see langword="false" />。</returns>
 public override bool Equals(object obj)
 {
     return(obj is MediaTypeHeaderValue mediaTypeHeaderValue && string.Equals(this._mediaType, mediaTypeHeaderValue._mediaType, StringComparison.OrdinalIgnoreCase) && HeaderUtilities.AreEqualCollections <NameValueHeaderValue>(this._parameters, mediaTypeHeaderValue._parameters));
 }
Exemple #3
0
 public override void Validate(string item) => HeaderUtilities.CheckValidToken(item, nameof(item));
 private static void CheckNameValueFormat(string name, string?value)
 {
     HeaderUtilities.CheckValidToken(name, nameof(name));
     CheckValueFormat(value);
 }
Exemple #5
0
 private static void CheckIsValidToken(string item)
 {
     HeaderUtilities.CheckValidToken(item, nameof(item));
 }
Exemple #6
0
        internal static int GetRetryConditionLength(string?input, int startIndex, out object?parsedValue)
        {
            Debug.Assert(startIndex >= 0);

            parsedValue = null;

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

            int current = startIndex;

            // Caller must remove leading whitespace.
            DateTimeOffset date         = DateTimeOffset.MinValue;
            int            deltaSeconds = -1; // use -1 to indicate that the value was not set. 'delta' values are always >=0

            // We either have a timespan or a date/time value. Determine which one we have by looking at the first char.
            // If it is a number, we have a timespan, otherwise we assume we have a date.
            char firstChar = input[current];

            if ((firstChar >= '0') && (firstChar <= '9'))
            {
                int deltaStartIndex = current;
                int deltaLength     = HttpRuleParser.GetNumberLength(input, current, false);

                // The value must be in the range 0..2^31
                if ((deltaLength == 0) || (deltaLength > HttpRuleParser.MaxInt32Digits))
                {
                    return(0);
                }

                current = current + deltaLength;
                current = current + HttpRuleParser.GetWhitespaceLength(input, current);

                // RetryConditionHeaderValue only allows 1 value. There must be no delimiter/other chars after 'delta'
                if (current != input.Length)
                {
                    return(0);
                }

                if (!HeaderUtilities.TryParseInt32(input, deltaStartIndex, deltaLength, out deltaSeconds))
                {
                    return(0); // int.TryParse() may return 'false' if the value has 10 digits and is > Int32.MaxValue.
                }
            }
            else
            {
                if (!HttpDateParser.TryParse(input.AsSpan(current), out date))
                {
                    return(0);
                }

                // If we got a valid date, then the parser consumed the whole string (incl. trailing whitespace).
                current = input.Length;
            }

            RetryConditionHeaderValue result = new RetryConditionHeaderValue();

            if (deltaSeconds == -1) // we didn't change delta, so we must have found a date.
            {
                result._date = date;
            }
            else
            {
                result._delta = new TimeSpan(0, 0, deltaSeconds);
            }

            parsedValue = result;
            return(current - startIndex);
        }
Exemple #7
0
 private static void CheckNameValueFormat(string name, string value)
 {
     HeaderUtilities.CheckValidToken(name, "name");
     NameValueHeaderValue.CheckValueFormat(value);
 }
Exemple #8
0
 internal static bool AreEqualCollections <T>(ObjectCollection <T> x, ObjectCollection <T> y) where T : class
 {
     return(HeaderUtilities.AreEqualCollections <T>(x, y, (IEqualityComparer <T>)null));
 }
 public TransferCodingHeaderValue(string value)
 {
     HeaderUtilities.CheckValidToken(value, nameof(value));
     _value = value;
 }
 public AuthenticationHeaderValue(string scheme, string?parameter)
 {
     HeaderUtilities.CheckValidToken(scheme, nameof(scheme));
     _scheme    = scheme;
     _parameter = parameter;
 }
Exemple #11
0
 public ProductInfoHeaderValue(string comment)
 {
     HeaderUtilities.CheckValidComment(comment, "comment");
     this.comment = comment;
 }
Exemple #12
0
 private static void ValidateToken(HttpHeaderValueCollection <string> collection, string value)
 {
     HeaderUtilities.CheckValidToken(value, "item");
 }
Exemple #13
0
 internal static bool TryParseInt32(string value, out int result)
 {
     return(HeaderUtilities.TryParseInt32(value, 0, value.Length, out result));
 }
Exemple #14
0
 public ProductInfoHeaderValue(string comment)
 {
     HeaderUtilities.CheckValidComment(comment, nameof(comment));
     _comment = comment;
 }
Exemple #15
0
        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 #16
0
        public StringWithQualityHeaderValue(string value)
        {
            HeaderUtilities.CheckValidToken(value, "value");

            _value = value;
        }
Exemple #17
0
 public TransferCodingHeaderValue(string value)
 {
     HeaderUtilities.CheckValidToken(value, "value");
     this.value = value;
 }