Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consistently use CreateProxyUri in WebProxy ctors #62338

Merged
merged 1 commit into from
Dec 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 };
karelz marked this conversation as resolved.
Show resolved Hide resolved
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