Skip to content

Commit e35b936

Browse files
dameng324baronfel
authored andcommitted
use custom scheme Uri to check registry contains port or not
1 parent ccceafd commit e35b936

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

src/Containers/Microsoft.NET.Build.Containers/FallbackToHttpMessageHandler.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,17 @@ internal static bool ShouldAttemptFallbackToHttp(HttpRequestException exception)
7979
return exception.HttpRequestError == HttpRequestError.SecureConnectionError;
8080
}
8181

82+
private static bool RegistryNameContainsPort(string registryName)
83+
{
84+
// use `container` scheme which does not have a default port.
85+
return new Uri($"container://{registryName}").Port != -1;
86+
}
87+
8288
private static void FallbackToHttp(string registryName, HttpRequestMessage request)
8389
{
8490
var uriBuilder = new UriBuilder(request.RequestUri!);
8591
uriBuilder.Scheme = "http";
86-
if (registryName.IndexOf(':') < 0)
92+
if (RegistryNameContainsPort(registryName) == false)
8793
{
8894
// registeryName does not contains port number, so reset the port number to -1, otherwise it will be https default port 443
8995
uriBuilder.Port = -1;

test/Microsoft.NET.Build.Containers.UnitTests/FallbackToHttpMessageHandlerTests.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,13 @@ public class FallbackToHttpMessageHandlerTests
1818
[InlineData("mcr.microsoft.com:443", 443)]
1919
[InlineData("mcr.microsoft.com:80", 80)]
2020
[InlineData("mcr.microsoft.com:5555", 5555)]
21+
[InlineData("[2408:8120:245:49a0:f041:d7bb:bb13:5b64]", 80)]
22+
[InlineData("[2408:8120:245:49a0:f041:d7bb:bb13:5b64]:443", 443)]
23+
[InlineData("[2408:8120:245:49a0:f041:d7bb:bb13:5b64]:80", 80)]
24+
[InlineData("[2408:8120:245:49a0:f041:d7bb:bb13:5b64]:5555", 5555)]
2125
public async Task FallBackToHttpPortShouldAsExpected(string registry, int expectedPort)
2226
{
23-
var uri = new UriBuilder($"https://{registry}").Uri;
27+
var uri = new Uri($"https://{registry}");
2428
var handler = new FallbackToHttpMessageHandler(
2529
registry,
2630
uri.Host,

0 commit comments

Comments
 (0)