[HTTP/3] Fix #53632 by using original host when omitted (#53648)
authorTimTim <i@timtim.hk>
Fri, 11 Jun 2021 14:08:57 +0000 (22:08 +0800)
committerGitHub <noreply@github.com>
Fri, 11 Jun 2021 14:08:57 +0000 (16:08 +0200)
* Fix #53632

* Annotate non-nullable _originAuthority

* Test inheriting original host for HTTP/3 Alt-Svc

src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnectionPool.cs
src/libraries/System.Net.Http/tests/FunctionalTests/HttpClientHandlerTest.AltSvc.cs

index 250aba3..00a1a37 100644 (file)
@@ -999,7 +999,7 @@ namespace System.Net.Http
 
                     if (nextAuthority == null && value != null && value.AlpnProtocolName == "h3")
                     {
-                        var authority = new HttpAuthority(value.Host!, value.Port);
+                        var authority = new HttpAuthority(value.Host ?? _originAuthority!.IdnHost, value.Port);
 
                         if (IsAltSvcBlocked(authority))
                         {
index ff6ec71..b56398b 100644 (file)
@@ -28,7 +28,7 @@ namespace System.Net.Http.Functional.Tests
 
         [Theory]
         [MemberData(nameof(AltSvcHeaderUpgradeVersions))]
-        public async Task AltSvc_Header_Upgrade_Success(Version fromVersion)
+        public async Task AltSvc_Header_Upgrade_Success(Version fromVersion, bool overrideHost)
         {
             // The test makes a request to a HTTP/1 or HTTP/2 server first, which supplies an Alt-Svc header pointing to the second server.
             using GenericLoopbackServer firstServer =
@@ -41,13 +41,16 @@ namespace System.Net.Http.Functional.Tests
 
             // The second request is expected to come in on this HTTP/3 server.
             using Http3LoopbackServer secondServer = CreateHttp3LoopbackServer();
+            
+            if (!overrideHost)
+                Assert.Equal(firstServer.Address.IdnHost, secondServer.Address.IdnHost);
 
             using HttpClient client = CreateHttpClient(fromVersion);
 
             Task<HttpResponseMessage> firstResponseTask = client.GetAsync(firstServer.Address);
             Task serverTask = firstServer.AcceptConnectionSendResponseAndCloseAsync(additionalHeaders: new[]
             {
-                new HttpHeaderData("Alt-Svc", $"h3=\"{secondServer.Address.IdnHost}:{secondServer.Address.Port}\"")
+                new HttpHeaderData("Alt-Svc", $"h3=\"{(overrideHost ? secondServer.Address.IdnHost : null)}:{secondServer.Address.Port}\"")
             });
 
             await new[] { firstResponseTask, serverTask }.WhenAllOrAnyFailed(30_000);
@@ -58,11 +61,13 @@ namespace System.Net.Http.Functional.Tests
             await AltSvc_Upgrade_Success(firstServer, secondServer, client);
         }
 
-        public static TheoryData<Version> AltSvcHeaderUpgradeVersions =>
-            new TheoryData<Version>
+        public static TheoryData<Version, bool> AltSvcHeaderUpgradeVersions =>
+            new TheoryData<Version, bool>
             {
-                { HttpVersion.Version11 },
-                { HttpVersion.Version20 }
+                { HttpVersion.Version11, true },
+                { HttpVersion.Version11, false },
+                { HttpVersion.Version20, true },
+                { HttpVersion.Version20, false }
             };
 
         [Fact]