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