public static StringWithQualityHeaderValue Parse(string input)
        {
            var inputArray = input.Split(new[] { ';' }, 2);
            var value = inputArray[0].Trim();

            // Unspecified q factor value is equal to a match.
            var quality = FormattingUtilities.Match;
            if (inputArray.Length > 1)
            {
                var parameter = inputArray[1].Trim();
                var nameValuePair = parameter.Split(new[] { '=' }, 2);
                if (nameValuePair.Length > 1 && nameValuePair[0].Trim().Equals("q"))
                {
                    // TODO: all extraneous parameters are ignored. Throw/return null if that is the case.
                    if (!Double.TryParse(nameValuePair[1].Trim(), out quality))
                    {
                        return null;
                    }
                }
            }

            var stringWithQualityHeader = new StringWithQualityHeaderValue()
            {
                Quality = quality,
                Value = value,
                RawValue = input
            };

            return stringWithQualityHeader;
        }
        public static bool TryParse(string input, out StringWithQualityHeaderValue headerValue)
        {
            var inputArray = input.Split(new[] { ';' }, 2);
            var value      = inputArray[0].Trim();

            // Unspecified q factor value is equal to a match.
            var quality = HttpHeaderUtilitites.Match;

            if (inputArray.Length > 1)
            {
                var parameter     = inputArray[1].Trim();
                var nameValuePair = parameter.Split(new[] { '=' }, 2);
                if (nameValuePair.Length > 1 && nameValuePair[0].Trim().Equals("q"))
                {
                    // TODO: all extraneous parameters are ignored. Throw/return null if that is the case.
                    if (!double.TryParse(
                            nameValuePair[1],
                            NumberStyles.AllowLeadingWhite | NumberStyles.AllowDecimalPoint |
                            NumberStyles.AllowTrailingWhite,
                            NumberFormatInfo.InvariantInfo,
                            out quality))
                    {
                        headerValue = null;
                        return(false);
                    }
                }
            }

            var stringWithQualityHeader = new StringWithQualityHeaderValue()
            {
                Quality  = quality,
                Value    = value,
                RawValue = input
            };

            headerValue = stringWithQualityHeader;
            return(true);
        }
Example #3
0
        public static IList <StringWithQualityHeaderValue> GetAcceptCharsetHeaders(string acceptCharsetHeader)
        {
            if (string.IsNullOrEmpty(acceptCharsetHeader))
            {
                return(null);
            }

            var acceptCharsetHeaderCollection = new List <StringWithQualityHeaderValue>();

            foreach (var item in acceptCharsetHeader.Split(','))
            {
                StringWithQualityHeaderValue parsedAcceptCharsetHeader;
                // If we are unable to parse any of the Accept-Charset Headers, we ignore them completely.
                if (!StringWithQualityHeaderValue.TryParse(item, out parsedAcceptCharsetHeader))
                {
                    return(null);
                }

                acceptCharsetHeaderCollection.Add(parsedAcceptCharsetHeader);
            }

            return(acceptCharsetHeaderCollection);
        }