System.Net.Http.Headers.CacheControlHeaderValue.GetCacheControlLength C# (CSharp) Method

GetCacheControlLength() static private method

static private GetCacheControlLength ( string input, int startIndex, CacheControlHeaderValue storeValue, CacheControlHeaderValue &parsedValue ) : int
input string
startIndex int
storeValue CacheControlHeaderValue
parsedValue CacheControlHeaderValue
return int
        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 = null;
            List<NameValueHeaderValue> nameValueList = new List<NameValueHeaderValue>();
            while (current < input.Length)
            {
                if (!s_nameValueListParser.TryParseValue(input, null, ref current, out nameValue))
                {
                    return 0;
                }

                nameValueList.Add(nameValue as NameValueHeaderValue);
            }

            // 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;
        }

Usage Example

Esempio 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);
        }
All Usage Examples Of System.Net.Http.Headers.CacheControlHeaderValue::GetCacheControlLength