private static IList <StringWithQualityHeaderValue> GetAcceptCharsetHeaderValues(HttpRequest request)
 {
     if (StringWithQualityHeaderValue.TryParseList((IList <string>)request.Headers[HeaderNames.AcceptCharset],
                                                   out var result))
     {
         return(result);
     }
     return(Array.Empty <StringWithQualityHeaderValue>());
 }
        public void Ctor_StringOnlyOverload_MatchExpectation()
        {
            var value = new StringWithQualityHeaderValue("token");
            Assert.Equal("token", value.Value);
            Assert.Null(value.Quality);

            Assert.Throws<ArgumentException>(() => new StringWithQualityHeaderValue(null));
            Assert.Throws<ArgumentException>(() => new StringWithQualityHeaderValue(""));
            Assert.Throws<FormatException>(() => new StringWithQualityHeaderValue("in valid"));
        }
        public void Ctor_StringWithQualityOverload_MatchExpectation()
        {
            var value = new StringWithQualityHeaderValue("token", 0.5);
            Assert.Equal("token", value.Value);
            Assert.Equal(0.5, value.Quality);

            Assert.Throws<ArgumentException>(() => new StringWithQualityHeaderValue(null, 0.1));
            Assert.Throws<ArgumentException>(() => new StringWithQualityHeaderValue("", 0.1));
            Assert.Throws<FormatException>(() => new StringWithQualityHeaderValue("in valid", 0.1));

            Assert.Throws<ArgumentOutOfRangeException>(() => new StringWithQualityHeaderValue("t", 1.1));
            Assert.Throws<ArgumentOutOfRangeException>(() => new StringWithQualityHeaderValue("t", -0.1));
        }
        public void ToString_UseDifferentValues_AllSerializedCorrectly()
        {
            var value = new StringWithQualityHeaderValue("token");
            Assert.Equal("token", value.ToString());

            value = new StringWithQualityHeaderValue("token", 0.1);
            Assert.Equal("token; q=0.1", value.ToString());

            value = new StringWithQualityHeaderValue("token", 0);
            Assert.Equal("token; q=0.0", value.ToString());

            value = new StringWithQualityHeaderValue("token", 1);
            Assert.Equal("token; q=1.0", value.ToString());

            // Note that the quality value gets rounded
            value = new StringWithQualityHeaderValue("token", 0.56789);
            Assert.Equal("token; q=0.568", value.ToString());
        }
        public void TryParseStrictList_WithSomeInvlaidValues_ReturnsFalse()
        {
            var inputs = new[]
            {
                "",
                "text1",
                "text 1",
                "text2",
                "\"text 2\",",
                "text3;q=0.5",
                "text4;q=0.5, extra stuff",
                " text5 ; q = 0.50 ",
                "\r\n text6 ; q = 0.05 ",
                "text7,text8;q=0.5",
                " text9 , text10 ; q = 0.5 ",
            };
            IList <StringWithQualityHeaderValue> results;

            Assert.False(StringWithQualityHeaderValue.TryParseStrictList(inputs, out results));
        }
        public void GetHashCode_UseSameAndDifferentValues_SameOrDifferentHashCodes()
        {
            var value1 = new StringWithQualityHeaderValue("t", 0.123);
            var value2 = new StringWithQualityHeaderValue("t", 0.123);
            var value3 = new StringWithQualityHeaderValue("T", 0.123);
            var value4 = new StringWithQualityHeaderValue("t");
            var value5 = new StringWithQualityHeaderValue("x", 0.123);
            var value6 = new StringWithQualityHeaderValue("t", 0.5);
            var value7 = new StringWithQualityHeaderValue("t", 0.1234);
            var value8 = new StringWithQualityHeaderValue("T");
            var value9 = new StringWithQualityHeaderValue("x");

            Assert.Equal(value1.GetHashCode(), value2.GetHashCode());
            Assert.Equal(value1.GetHashCode(), value3.GetHashCode());
            Assert.NotEqual(value1.GetHashCode(), value4.GetHashCode());
            Assert.NotEqual(value1.GetHashCode(), value5.GetHashCode());
            Assert.NotEqual(value1.GetHashCode(), value6.GetHashCode());
            Assert.NotEqual(value1.GetHashCode(), value7.GetHashCode());
            Assert.Equal(value4.GetHashCode(), value8.GetHashCode());
            Assert.NotEqual(value4.GetHashCode(), value9.GetHashCode());
        }
        public void GetHashCode_UseSameAndDifferentValues_SameOrDifferentHashCodes()
        {
            var value1 = new StringWithQualityHeaderValue("t", 0.123);
            var value2 = new StringWithQualityHeaderValue("t", 0.123);
            var value3 = new StringWithQualityHeaderValue("T", 0.123);
            var value4 = new StringWithQualityHeaderValue("t");
            var value5 = new StringWithQualityHeaderValue("x", 0.123);
            var value6 = new StringWithQualityHeaderValue("t", 0.5);
            var value7 = new StringWithQualityHeaderValue("t", 0.1234);
            var value8 = new StringWithQualityHeaderValue("T");
            var value9 = new StringWithQualityHeaderValue("x");

            Assert.Equal(value1.GetHashCode(), value2.GetHashCode());
            Assert.Equal(value1.GetHashCode(), value3.GetHashCode());
            Assert.NotEqual(value1.GetHashCode(), value4.GetHashCode());
            Assert.NotEqual(value1.GetHashCode(), value5.GetHashCode());
            Assert.NotEqual(value1.GetHashCode(), value6.GetHashCode());
            Assert.NotEqual(value1.GetHashCode(), value7.GetHashCode());
            Assert.Equal(value4.GetHashCode(), value8.GetHashCode());
            Assert.NotEqual(value4.GetHashCode(), value9.GetHashCode());
        }
        private static bool TryReadQuality(StringSegment input, StringWithQualityHeaderValue result, ref int index)
        {
            var current = index;

            // See if we have a quality value by looking for "q"
            if ((current == input.Length) || ((input[current] != 'q') && (input[current] != 'Q')))
            {
                return(false);
            }

            current++; // skip 'q' identifier
            current = current + HttpRuleParser.GetWhitespaceLength(input, current);

            // If we found "q" it must be followed by "="
            if ((current == input.Length) || (input[current] != '='))
            {
                return(false);
            }

            current++; // skip '=' separator
            current = current + HttpRuleParser.GetWhitespaceLength(input, current);

            if (current == input.Length)
            {
                return(false);
            }

            if (!HeaderUtilities.TryParseQualityDouble(input, current, out var quality, out var qualityLength))
            {
                return(false);
            }

            result._quality = quality;

            current = current + qualityLength;
            current = current + HttpRuleParser.GetWhitespaceLength(input, current);

            index = current;
            return(true);
        }
示例#9
0
        // https://stackoverflow.com/questions/9927871/need-an-example-on-how-to-get-preferred-language-from-accept-language-request-he
        private static void GetHeader(Microsoft.AspNetCore.Http.HttpContext context)
        {
            // RequestHeaders.AcceptLanguage P
            string header = context.Request.Headers["Accept-Language"];
            // string header = "en-ca,en;q=0.8,en-us;q=0.6,de-de;q=0.4,de;q=0.2";

            string def =
                Microsoft.Net.Http.Headers.StringWithQualityHeaderValue.ParseList(header.Split(';')).Sort(
                    delegate(StringWithQualityHeaderValue a, StringWithQualityHeaderValue b)
            {
                return(a.Quality.GetValueOrDefault(1).CompareTo(b.Quality.GetValueOrDefault(1)));
            }
                    )[0].ToString();


            Microsoft.Net.Http.Headers.StringWithQualityHeaderValue defLang =
                Microsoft.Net.Http.Headers.StringWithQualityHeaderValue.ParseList(header.Split(';'))
                .OrderByDescending(x => x.Quality.GetValueOrDefault(1))
                .FirstOrDefault();

            // Microsoft.Net.Http.Headers.StringWithQualityHeaderValue.Parse(new StringSegment(v));



            // Query syntax
            //System.Linq.IOrderedEnumerable<System.Net.Http.Headers.StringWithQualityHeaderValue> languages1 =
            System.Net.Http.Headers.StringWithQualityHeaderValue defaultLanguage =
                (from headerParts in header.Split(',')
                 select System.Net.Http.Headers.StringWithQualityHeaderValue.Parse(headerParts) into acceptLanguages
                 orderby acceptLanguages.Quality.GetValueOrDefault(1) descending
                 select acceptLanguages
                ).FirstOrDefault();

            // Method syntax
            System.Linq.IOrderedEnumerable <System.Net.Http.Headers.StringWithQualityHeaderValue> languages = header.Split(',')
                                                                                                              .Select(System.Net.Http.Headers.StringWithQualityHeaderValue.Parse)
                                                                                                              .OrderByDescending(s => s.Quality.GetValueOrDefault(1));
        }
        public void TryParseList_WithSomeInvlaidValues_IgnoresInvalidValues()
        {
            var inputs = new[]
            {
                "",
                "text1",
                "text 1",
                "text2",
                "\"text 2\",",
                "text3;q=0.5",
                "text4;q=0.5, extra stuff",
                " text5 ; q = 0.50 ",
                "\r\n text6 ; q = 0.05 ",
                "text7,text8;q=0.5",
                " text9 , text10 ; q = 0.5 ",
            };
            IList <StringWithQualityHeaderValue> results;

            Assert.True(StringWithQualityHeaderValue.TryParseList(inputs, out results));

            var expectedResults = new[]
            {
                new StringWithQualityHeaderValue("text1"),
                new StringWithQualityHeaderValue("1"),
                new StringWithQualityHeaderValue("text2"),
                new StringWithQualityHeaderValue("text3", 0.5),
                new StringWithQualityHeaderValue("text4", 0.5),
                new StringWithQualityHeaderValue("stuff"),
                new StringWithQualityHeaderValue("text5", 0.5),
                new StringWithQualityHeaderValue("text6", 0.05),
                new StringWithQualityHeaderValue("text7"),
                new StringWithQualityHeaderValue("text8", 0.5),
                new StringWithQualityHeaderValue("text9"),
                new StringWithQualityHeaderValue("text10", 0.5),
            }.ToList();

            Assert.Equal(expectedResults, results);
        }
        public void Equals_UseSameAndDifferentRanges_EqualOrNotEqualNoExceptions()
        {
            var value1 = new StringWithQualityHeaderValue("t", 0.123);
            var value2 = new StringWithQualityHeaderValue("t", 0.123);
            var value3 = new StringWithQualityHeaderValue("T", 0.123);
            var value4 = new StringWithQualityHeaderValue("t");
            var value5 = new StringWithQualityHeaderValue("x", 0.123);
            var value6 = new StringWithQualityHeaderValue("t", 0.5);
            var value7 = new StringWithQualityHeaderValue("t", 0.1234);
            var value8 = new StringWithQualityHeaderValue("T");
            var value9 = new StringWithQualityHeaderValue("x");

            Assert.False(value1.Equals(null), "t; q=0.123 vs. <null>");
            Assert.True(value1.Equals(value2), "t; q=0.123 vs. t; q=0.123");
            Assert.True(value1.Equals(value3), "t; q=0.123 vs. T; q=0.123");
            Assert.False(value1.Equals(value4), "t; q=0.123 vs. t");
            Assert.False(value4.Equals(value1), "t vs. t; q=0.123");
            Assert.False(value1.Equals(value5), "t; q=0.123 vs. x; q=0.123");
            Assert.False(value1.Equals(value6), "t; q=0.123 vs. t; q=0.5");
            Assert.False(value1.Equals(value7), "t; q=0.123 vs. t; q=0.1234");
            Assert.True(value4.Equals(value8), "t vs. T");
            Assert.False(value4.Equals(value9), "t vs. T");
        }
        public void Equals_UseSameAndDifferentRanges_EqualOrNotEqualNoExceptions()
        {
            var value1 = new StringWithQualityHeaderValue("t", 0.123);
            var value2 = new StringWithQualityHeaderValue("t", 0.123);
            var value3 = new StringWithQualityHeaderValue("T", 0.123);
            var value4 = new StringWithQualityHeaderValue("t");
            var value5 = new StringWithQualityHeaderValue("x", 0.123);
            var value6 = new StringWithQualityHeaderValue("t", 0.5);
            var value7 = new StringWithQualityHeaderValue("t", 0.1234);
            var value8 = new StringWithQualityHeaderValue("T");
            var value9 = new StringWithQualityHeaderValue("x");

            Assert.False(value1.Equals(null), "t; q=0.123 vs. <null>");
            Assert.True(value1.Equals(value2), "t; q=0.123 vs. t; q=0.123");
            Assert.True(value1.Equals(value3), "t; q=0.123 vs. T; q=0.123");
            Assert.False(value1.Equals(value4), "t; q=0.123 vs. t");
            Assert.False(value4.Equals(value1), "t vs. t; q=0.123");
            Assert.False(value1.Equals(value5), "t; q=0.123 vs. x; q=0.123");
            Assert.False(value1.Equals(value6), "t; q=0.123 vs. t; q=0.5");
            Assert.False(value1.Equals(value7), "t; q=0.123 vs. t; q=0.1234");
            Assert.True(value4.Equals(value8), "t vs. T");
            Assert.False(value4.Equals(value9), "t vs. T");
        }
        public void TryParseStrictList_SetOfValidValueStrings_ParsedCorrectly()
        {
            var inputs = new[]
            {
                "",
                "text1",
                "text2,",
                "textA,textB",
                "text3;q=0.5",
                "text4;q=0.5,",
                " text5 ; q = 0.50 ",
                "\r\n text6 ; q = 0.05 ",
                "text7,text8;q=0.5",
                " text9 , text10 ; q = 0.5 ",
            };
            IList <StringWithQualityHeaderValue> results;

            Assert.True(StringWithQualityHeaderValue.TryParseStrictList(inputs, out results));

            var expectedResults = new[]
            {
                new StringWithQualityHeaderValue("text1"),
                new StringWithQualityHeaderValue("text2"),
                new StringWithQualityHeaderValue("textA"),
                new StringWithQualityHeaderValue("textB"),
                new StringWithQualityHeaderValue("text3", 0.5),
                new StringWithQualityHeaderValue("text4", 0.5),
                new StringWithQualityHeaderValue("text5", 0.5),
                new StringWithQualityHeaderValue("text6", 0.05),
                new StringWithQualityHeaderValue("text7"),
                new StringWithQualityHeaderValue("text8", 0.5),
                new StringWithQualityHeaderValue("text9"),
                new StringWithQualityHeaderValue("text10", 0.5),
            }.ToList();

            Assert.Equal(expectedResults, results);
        }
 public static bool TryParse(string input, out StringWithQualityHeaderValue parsedValue)
 {
     var index = 0;
     return SingleValueParser.TryParseValue(input, ref index, out parsedValue);
 }
        private void CheckValidParse(string input, StringWithQualityHeaderValue expectedResult)
        {
            var result = StringWithQualityHeaderValue.Parse(input);

            Assert.Equal(expectedResult, result);
        }
        private static int GetStringWithQualityLength(string input, int startIndex, out StringWithQualityHeaderValue parsedValue)
        {
            Contract.Requires(startIndex >= 0);

            parsedValue = null;

            if (string.IsNullOrEmpty(input) || (startIndex >= input.Length))
            {
                return 0;
            }

            // Parse the value string: <value> in '<value>; q=<quality>'
            var valueLength = HttpRuleParser.GetTokenLength(input, startIndex);

            if (valueLength == 0)
            {
                return 0;
            }

            StringWithQualityHeaderValue result = new StringWithQualityHeaderValue();
            result._value = input.Substring(startIndex, valueLength);
            var current = startIndex + valueLength;
            current = current + HttpRuleParser.GetWhitespaceLength(input, current);

            if ((current == input.Length) || (input[current] != ';'))
            {
                parsedValue = result;
                return current - startIndex; // we have a valid token, but no quality.
            }

            current++; // skip ';' separator
            current = current + HttpRuleParser.GetWhitespaceLength(input, current);

            // If we found a ';' separator, it must be followed by a quality information
            if (!TryReadQuality(input, result, ref current))
            {
                return 0;
            }

            parsedValue = result;
            return current - startIndex;
        }
        private static bool TryReadQuality(string input, StringWithQualityHeaderValue result, ref int index)
        {
            var current = index;

            // See if we have a quality value by looking for "q"
            if ((current == input.Length) || ((input[current] != 'q') && (input[current] != 'Q')))
            {
                return false;
            }

            current++; // skip 'q' identifier
            current = current + HttpRuleParser.GetWhitespaceLength(input, current);

            // If we found "q" it must be followed by "="
            if ((current == input.Length) || (input[current] != '='))
            {
                return false;
            }

            current++; // skip '=' separator
            current = current + HttpRuleParser.GetWhitespaceLength(input, current);

            if (current == input.Length)
            {
                return false;
            }

            int qualityLength = HttpRuleParser.GetNumberLength(input, current, true);

            if (qualityLength == 0)
            {
                return false;
            }

            double quality;
            if (!double.TryParse(input.Substring(current, qualityLength), NumberStyles.AllowDecimalPoint,
                NumberFormatInfo.InvariantInfo, out quality))
            {
                return false;
            }

            if ((quality < 0) || (quality > 1))
            {
                return false;
            }

            result._quality = quality;

            current = current + qualityLength;
            current = current + HttpRuleParser.GetWhitespaceLength(input, current);

            index = current;
            return true;
        }
 public void Parse_SetOfInvalidValueStrings_Throws(string input)
 {
     Assert.Throws <FormatException>(() => StringWithQualityHeaderValue.Parse(input));
 }
 private void CheckInvalidTryParse(string?input)
 {
     Assert.False(StringWithQualityHeaderValue.TryParse(input, out var result));
     Assert.Null(result);
 }
        public static bool TryParse(StringSegment input, [NotNullWhen(true)] out StringWithQualityHeaderValue parsedValue)
        {
            var index = 0;

            return(SingleValueParser.TryParseValue(input, ref index, out parsedValue !));
        }
 private void CheckValidTryParse(string?input, StringWithQualityHeaderValue expectedResult)
 {
     Assert.True(StringWithQualityHeaderValue.TryParse(input, out var result));
     Assert.Equal(expectedResult, result);
 }
        private static int GetStringWithQualityLength(StringSegment input, int startIndex, out StringWithQualityHeaderValue parsedValue)
        {
            Contract.Requires(startIndex >= 0);

            parsedValue = null;

            if (StringSegment.IsNullOrEmpty(input) || (startIndex >= input.Length))
            {
                return(0);
            }

            // Parse the value string: <value> in '<value>; q=<quality>'
            var valueLength = HttpRuleParser.GetTokenLength(input, startIndex);

            if (valueLength == 0)
            {
                return(0);
            }

            StringWithQualityHeaderValue result = new StringWithQualityHeaderValue();

            result._value = input.Subsegment(startIndex, valueLength);
            var current = startIndex + valueLength;

            current = current + HttpRuleParser.GetWhitespaceLength(input, current);

            if ((current == input.Length) || (input[current] != ';'))
            {
                parsedValue = result;
                return(current - startIndex); // we have a valid token, but no quality.
            }

            current++; // skip ';' separator
            current = current + HttpRuleParser.GetWhitespaceLength(input, current);

            // If we found a ';' separator, it must be followed by a quality information
            if (!TryReadQuality(input, result, ref current))
            {
                return(0);
            }

            parsedValue = result;
            return(current - startIndex);
        }
        public static bool TryParse(string input, out StringWithQualityHeaderValue parsedValue)
        {
            var index = 0;

            return(SingleValueParser.TryParseValue(input, ref index, out parsedValue));
        }
 private void CheckValidParse(string input, StringWithQualityHeaderValue expectedResult)
 {
     var result = StringWithQualityHeaderValue.Parse(input);
     Assert.Equal(expectedResult, result);
 }
 private void CheckValidTryParse(string input, StringWithQualityHeaderValue expectedResult)
 {
     StringWithQualityHeaderValue result = null;
     Assert.True(StringWithQualityHeaderValue.TryParse(input, out result));
     Assert.Equal(expectedResult, result);
 }