Exemplo n.º 1
0
        protected override int GetParsedValueLength(string value, int startIndex, object?storeValue,
                                                    out object?parsedValue)
        {
            CacheControlHeaderValue?temp = null;
            bool isInvalidValue          = true;

            if (storeValue is List <object> list)
            {
                foreach (object item in list)
                {
                    if (item is not HttpHeaders.InvalidValue)
                    {
                        isInvalidValue = false;
                        temp           = item as CacheControlHeaderValue;
                        break;
                    }
                }
            }
            else
            {
                if (storeValue is not HttpHeaders.InvalidValue)
                {
                    isInvalidValue = false;
                    temp           = storeValue as CacheControlHeaderValue;
                }
            }
            Debug.Assert(isInvalidValue || storeValue == null || temp != null, "'storeValue' is not of type CacheControlHeaderValue");

            int resultLength = CacheControlHeaderValue.GetCacheControlLength(value, startIndex, temp, out temp);

            parsedValue = temp;
            return(resultLength);
        }
        internal static int GetCacheControlLength(string?input, int startIndex, CacheControlHeaderValue?storeValue,
                                                  out CacheControlHeaderValue?parsedValue)
        {
            Debug.Assert(startIndex >= 0);

            parsedValue = null;

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

            // Cache-Control header consists of a list of name/value pairs, where the value is optional. So use an
            // instance of NameValueHeaderParser to parse the string.
            int    current = startIndex;
            object?nameValue;
            List <NameValueHeaderValue> nameValueList = new List <NameValueHeaderValue>();

            while (current < input.Length)
            {
                if (!s_nameValueListParser.TryParseValue(input, null, ref current, out nameValue))
                {
                    return(0);
                }

                nameValueList.Add((NameValueHeaderValue)nameValue);
            }

            // If we get here, we were able to successfully parse the string as list of name/value pairs. Now analyze
            // the name/value pairs.

            // Cache-Control is a header supporting lists of values. However, expose the header as an instance of
            // CacheControlHeaderValue. So if we already have an instance of CacheControlHeaderValue, add the values
            // from this string to the existing instances.
            CacheControlHeaderValue?result = storeValue;

            if (result == null)
            {
                result = new CacheControlHeaderValue();
            }

            if (!TrySetCacheControlValues(result, nameValueList))
            {
                return(0);
            }

            // If we had an existing store value and we just updated that instance, return 'null' to indicate that
            // we don't have a new instance of CacheControlHeaderValue, but just updated an existing one. This is the
            // case if we have multiple 'Cache-Control' headers set in a request/response message.
            if (storeValue == null)
            {
                parsedValue = result;
            }

            // If we get here we successfully parsed the whole string.
            return(input.Length - startIndex);
        }
Exemplo n.º 3
0
    /// <summary>
    /// Attempts to parse the specified <paramref name="input"/> as a <see cref="CacheControlHeaderValue"/>.
    /// </summary>
    /// <param name="input">The value to parse.</param>
    /// <param name="parsedValue">The parsed value.</param>
    /// <returns><see langword="true"/> if input is a valid <see cref="CacheControlHeaderValue"/>, otherwise <see langword="false"/>.</returns>
    public static bool TryParse(StringSegment input, [NotNullWhen(true)] out CacheControlHeaderValue?parsedValue)
    {
        var index = 0;

        // Cache-Control is unusual because there are no required values so the parser will succeed for an empty string, but still return null.
        if (Parser.TryParseValue(input, ref index, out parsedValue) && parsedValue != null)
        {
            return(true);
        }
        parsedValue = null;
        return(false);
    }
Exemplo n.º 4
0
        protected override int GetParsedValueLength(string value, int startIndex, object?storeValue,
                                                    out object?parsedValue)
        {
            CacheControlHeaderValue?temp = storeValue as CacheControlHeaderValue;

            Debug.Assert(storeValue == null || temp != null, "'storeValue' is not of type CacheControlHeaderValue");

            int resultLength = CacheControlHeaderValue.GetCacheControlLength(value, startIndex, temp, out temp);

            parsedValue = temp;
            return(resultLength);
        }
        public static bool TryParse(string?input, out CacheControlHeaderValue?parsedValue)
        {
            int index = 0;

            parsedValue = null;

            if (CacheControlHeaderParser.Parser.TryParseValue(input, null, ref index, out object?output))
            {
                parsedValue = (CacheControlHeaderValue?)output;
                return(true);
            }
            return(false);
        }
Exemplo n.º 6
0
    private static int GetCacheControlLength(StringSegment input, int startIndex, out CacheControlHeaderValue?parsedValue)
    {
        Contract.Requires(startIndex >= 0);

        parsedValue = null;

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

        // Cache-Control header consists of a list of name/value pairs, where the value is optional. So use an
        // instance of NameValueHeaderParser to parse the string.
        var current       = startIndex;
        var nameValueList = new List <NameValueHeaderValue>();

        while (current < input.Length)
        {
            if (!NameValueHeaderValue.MultipleValueParser.TryParseValue(input, ref current, out var nameValue))
            {
                return(0);
            }

            if (nameValue != null)
            {
                nameValueList.Add(nameValue);
            }
        }

        // If we get here, we were able to successfully parse the string as list of name/value pairs. Now analyze
        // the name/value pairs.

        // Cache-Control is a header supporting lists of values. However, expose the header as an instance of
        // CacheControlHeaderValue.
        var result = new CacheControlHeaderValue();

        if (!TrySetCacheControlValues(result, nameValueList))
        {
            return(0);
        }

        parsedValue = result;

        // If we get here we successfully parsed the whole string.
        return(input.Length - startIndex);
    }
        public override bool Equals(object?obj)
        {
            CacheControlHeaderValue?other = obj as CacheControlHeaderValue;

            if (other == null)
            {
                return(false);
            }

            if ((_noCache != other._noCache) || (_noStore != other._noStore) || (_maxAge != other._maxAge) ||
                (_sharedMaxAge != other._sharedMaxAge) || (_maxStale != other._maxStale) ||
                (_maxStaleLimit != other._maxStaleLimit) || (_minFresh != other._minFresh) ||
                (_noTransform != other._noTransform) || (_onlyIfCached != other._onlyIfCached) ||
                (_publicField != other._publicField) || (_privateField != other._privateField) ||
                (_mustRevalidate != other._mustRevalidate) || (_proxyRevalidate != other._proxyRevalidate))
            {
                return(false);
            }

            if (!HeaderUtilities.AreEqualCollections(_noCacheHeaders, other._noCacheHeaders,
                                                     StringComparer.OrdinalIgnoreCase))
            {
                return(false);
            }

            if (!HeaderUtilities.AreEqualCollections(_privateHeaders, other._privateHeaders,
                                                     StringComparer.OrdinalIgnoreCase))
            {
                return(false);
            }

            if (!HeaderUtilities.AreEqualCollections(_extensions, other._extensions))
            {
                return(false);
            }

            return(true);
        }