fix UserAgent parsing
authorMarco Rossignoli <marco.rossignoli@gmail.com>
Wed, 27 Feb 2019 11:00:17 +0000 (12:00 +0100)
committerMarco Rossignoli <marco.rossignoli@gmail.com>
Wed, 27 Feb 2019 11:00:17 +0000 (12:00 +0100)
Commit migrated from https://github.com/dotnet/corefx/commit/840d29c05243f5afe31e43e2415d6dce5b3c71fd

src/libraries/System.Net.Http/src/System/Net/Http/HttpRuleParser.cs
src/libraries/System.Net.Http/tests/UnitTests/Headers/HttpRequestHeadersTest.cs

index a10066c..d718be1 100644 (file)
@@ -433,7 +433,7 @@ namespace System.Net.Http
                     }
                 }
 
-                if (input[current] == closeChar)
+                if (current < input.Length &&  input[current] == closeChar)
                 {
                     length = current - startIndex + 1;
                     return HttpParseResult.Parsed;
index 2905ff6..bbded42 100644 (file)
@@ -612,6 +612,17 @@ namespace System.Net.Http.Tests
         }
 
         [Fact]
+        public void UserAgent_TryGetValuesAndGetValues_Malformed()
+        {
+            string malformedUserAgent = "Mozilla/4.0 (compatible (compatible; MSIE 8.0; Windows NT 6.1; Trident/7.0)";
+            headers.TryAddWithoutValidation("User-Agent", malformedUserAgent);
+            Assert.True(headers.TryGetValues("User-Agent", out IEnumerable<string> ua));
+            Assert.Equal(1, ua.Count());
+            Assert.Equal(malformedUserAgent, ua.First());
+            Assert.Equal(malformedUserAgent, headers.GetValues("User-Agent").First());
+        }
+
+        [Fact]
         public void UserAgent_UseAddMethodWithInvalidValue_InvalidValueRecognized()
         {
             headers.TryAddWithoutValidation("User-Agent", "custom\u4F1A");