Skip to content

Commit

Permalink
Consistently use CreateProxyUri in WebProxy ctors (#62338)
Browse files Browse the repository at this point in the history
  • Loading branch information
MihaZupan authored Dec 3, 2021
1 parent c9f9697 commit f2c8f7c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
27 changes: 22 additions & 5 deletions src/libraries/System.Net.WebProxy/src/System/Net/WebProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public WebProxy(Uri? Address, bool BypassOnLocal, string[]? BypassList, ICredent
}

public WebProxy(string Host, int Port)
: this(new Uri(string.Create(CultureInfo.InvariantCulture, $"http://{Host}:{Port}")), false, null, null)
: this(CreateProxyUri(Host, Port), false, null, null)
{
}

Expand Down Expand Up @@ -102,10 +102,27 @@ public bool UseDefaultCredentials
return IsBypassed(destination) ? destination : Address;
}

private static Uri? CreateProxyUri(string? address) =>
address == null ? null :
!address.Contains("://") ? new Uri("http://" + address) :
new Uri(address);
private static Uri? CreateProxyUri(string? address, int? port = null)
{
if (address is null)
{
return null;
}

if (!address.Contains("://", StringComparison.Ordinal))
{
address = "http://" + address;
}

var proxyUri = new Uri(address);

if (port.HasValue && proxyUri.IsAbsoluteUri)
{
proxyUri = new UriBuilder(proxyUri) { Port = port.Value }.Uri;
}

return proxyUri;
}

private void UpdateRegexList(bool canThrow)
{
Expand Down
5 changes: 5 additions & 0 deletions src/libraries/System.Net.WebProxy/tests/WebProxyTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ public static IEnumerable<object[]> Ctor_ExpectedPropertyValues_MemberData()
yield return new object[] { new WebProxy(), null, false, false, Array.Empty<string>(), null };

yield return new object[] { new WebProxy("http://anything"), new Uri("http://anything"), false, false, Array.Empty<string>(), null };
yield return new object[] { new WebProxy("http://anything:42"), new Uri("http://anything:42"), false, false, Array.Empty<string>(), null };
yield return new object[] { new WebProxy("anything:42"), new Uri("http://anything:42"), false, false, Array.Empty<string>(), null };
yield return new object[] { new WebProxy("anything", 42), new Uri("http://anything:42"), false, false, Array.Empty<string>(), null };
yield return new object[] { new WebProxy("http://anything", 42), new Uri("http://anything:42"), false, false, Array.Empty<string>(), null };
yield return new object[] { new WebProxy("http://anything:123", 42), new Uri("http://anything:42"), false, false, Array.Empty<string>(), null };
yield return new object[] { new WebProxy("socks5://anything", 42), new Uri("socks5://anything:42"), false, false, Array.Empty<string>(), null };
yield return new object[] { new WebProxy(new Uri("http://anything")), new Uri("http://anything"), false, false, Array.Empty<string>(), null };

yield return new object[] { new WebProxy("http://anything", true), new Uri("http://anything"), false, true, Array.Empty<string>(), null };
Expand Down

0 comments on commit f2c8f7c

Please sign in to comment.