Skip to content

Commit

Permalink
use custom scheme Uri to check registry contains port or not
Browse files Browse the repository at this point in the history
  • Loading branch information
dameng324 authored and baronfel committed Oct 18, 2024
1 parent ccceafd commit e35b936
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,17 @@ internal static bool ShouldAttemptFallbackToHttp(HttpRequestException exception)
return exception.HttpRequestError == HttpRequestError.SecureConnectionError;
}

private static bool RegistryNameContainsPort(string registryName)
{
// use `container` scheme which does not have a default port.
return new Uri($"container://{registryName}").Port != -1;
}

private static void FallbackToHttp(string registryName, HttpRequestMessage request)
{
var uriBuilder = new UriBuilder(request.RequestUri!);
uriBuilder.Scheme = "http";
if (registryName.IndexOf(':') < 0)
if (RegistryNameContainsPort(registryName) == false)
{
// registeryName does not contains port number, so reset the port number to -1, otherwise it will be https default port 443
uriBuilder.Port = -1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@ public class FallbackToHttpMessageHandlerTests
[InlineData("mcr.microsoft.com:443", 443)]
[InlineData("mcr.microsoft.com:80", 80)]
[InlineData("mcr.microsoft.com:5555", 5555)]
[InlineData("[2408:8120:245:49a0:f041:d7bb:bb13:5b64]", 80)]
[InlineData("[2408:8120:245:49a0:f041:d7bb:bb13:5b64]:443", 443)]
[InlineData("[2408:8120:245:49a0:f041:d7bb:bb13:5b64]:80", 80)]
[InlineData("[2408:8120:245:49a0:f041:d7bb:bb13:5b64]:5555", 5555)]
public async Task FallBackToHttpPortShouldAsExpected(string registry, int expectedPort)
{
var uri = new UriBuilder($"https://{registry}").Uri;
var uri = new Uri($"https://{registry}");
var handler = new FallbackToHttpMessageHandler(
registry,
uri.Host,
Expand Down

0 comments on commit e35b936

Please sign in to comment.