HttpServer.HttpHelper.ParseHeaderAttribute C# (CSharp) Method

ParseHeaderAttribute() public static method

Parses an attribute from a header string. For example, the charset attribute can be parsed from the header Content-Type: text/html; charset=utf-8
public static ParseHeaderAttribute ( string header, string attribute ) : string
header string The raw header string containing the desired attribute
attribute string The name of the attribute to parse
return string
        public static string ParseHeaderAttribute(string header, string attribute)
        {
            if (header == null)
                return null;

            int i;
            int headerLength = header.Length;
            int attrLength = attribute.Length;
            int start = 1;

            while (start < headerLength)
            {
                start = CultureInfo.InvariantCulture.CompareInfo.IndexOf(header, attribute, start, CompareOptions.IgnoreCase);

                if ((start < 0) || ((start + attrLength) >= headerLength))
                    break;

                char c = header[start - 1];
                char c2 = header[start + attrLength];

                if ((((c == ';') || (c == ',')) || char.IsWhiteSpace(c)) && ((c2 == '=') || char.IsWhiteSpace(c2)))
                    break;

                start += attrLength;
            }

            if ((start < 0) || (start >= headerLength))
                return null;

            start += attrLength;

            while ((start < headerLength) && char.IsWhiteSpace(header[start]))
                start++;

            if ((start >= headerLength) || (header[start] != '='))
                return null;

            start++;

            while ((start < headerLength) && char.IsWhiteSpace(header[start]))
                start++;

            if (start >= headerLength)
                return null;

            if ((start < headerLength) && (header[start] == '"'))
            {
                if (start == (headerLength - 1))
                    return null;

                i = header.IndexOf('"', start + 1);

                if ((i < 0) || (i == (start + 1)))
                    return null;

                return header.Substring(start + 1, (i - start) - 1).Trim();
            }

            i = start;

            while (i < headerLength)
            {
                if ((header[i] == ' ') || (header[i] == ','))
                    break;

                i++;
            }

            if (i == start)
                return null;

            return header.Substring(start, i - start).Trim();
        }