internal static int GetNameValueLength(string input, int startIndex,
Func<NameValueHeaderValue> nameValueCreator, out NameValueHeaderValue parsedValue)
{
Debug.Assert(input != null);
Debug.Assert(startIndex >= 0);
Debug.Assert(nameValueCreator != null);
parsedValue = null;
if (string.IsNullOrEmpty(input) || (startIndex >= input.Length))
{
return 0;
}
// Parse the name, i.e. <name> in name/value string "<name>=<value>". Caller must remove
// leading whitespace.
int nameLength = HttpRuleParser.GetTokenLength(input, startIndex);
if (nameLength == 0)
{
return 0;
}
string name = input.Substring(startIndex, nameLength);
int current = startIndex + nameLength;
current = current + HttpRuleParser.GetWhitespaceLength(input, current);
// Parse the separator between name and value
if ((current == input.Length) || (input[current] != '='))
{
// We only have a name and that's OK. Return.
parsedValue = nameValueCreator();
parsedValue._name = name;
current = current + HttpRuleParser.GetWhitespaceLength(input, current); // skip whitespace
return current - startIndex;
}
current++; // skip delimiter.
current = current + HttpRuleParser.GetWhitespaceLength(input, current);
// Parse the value, i.e. <value> in name/value string "<name>=<value>"
int valueLength = GetValueLength(input, current);
if (valueLength == 0)
{
return 0; // We have an invalid value.
}
// Use parameterless ctor to avoid double-parsing of name and value, i.e. skip public ctor validation.
parsedValue = nameValueCreator();
parsedValue._name = name;
parsedValue._value = input.Substring(current, valueLength);
current = current + valueLength;
current = current + HttpRuleParser.GetWhitespaceLength(input, current); // skip whitespace
return current - startIndex;
}