Skip to content

Commit f2c8f7c

Browse files
authored
Consistently use CreateProxyUri in WebProxy ctors (#62338)
1 parent c9f9697 commit f2c8f7c

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

src/libraries/System.Net.WebProxy/src/System/Net/WebProxy.cs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public WebProxy(Uri? Address, bool BypassOnLocal, string[]? BypassList, ICredent
3535
}
3636

3737
public WebProxy(string Host, int Port)
38-
: this(new Uri(string.Create(CultureInfo.InvariantCulture, $"http://{Host}:{Port}")), false, null, null)
38+
: this(CreateProxyUri(Host, Port), false, null, null)
3939
{
4040
}
4141

@@ -102,10 +102,27 @@ public bool UseDefaultCredentials
102102
return IsBypassed(destination) ? destination : Address;
103103
}
104104

105-
private static Uri? CreateProxyUri(string? address) =>
106-
address == null ? null :
107-
!address.Contains("://") ? new Uri("http://" + address) :
108-
new Uri(address);
105+
private static Uri? CreateProxyUri(string? address, int? port = null)
106+
{
107+
if (address is null)
108+
{
109+
return null;
110+
}
111+
112+
if (!address.Contains("://", StringComparison.Ordinal))
113+
{
114+
address = "http://" + address;
115+
}
116+
117+
var proxyUri = new Uri(address);
118+
119+
if (port.HasValue && proxyUri.IsAbsoluteUri)
120+
{
121+
proxyUri = new UriBuilder(proxyUri) { Port = port.Value }.Uri;
122+
}
123+
124+
return proxyUri;
125+
}
109126

110127
private void UpdateRegexList(bool canThrow)
111128
{

src/libraries/System.Net.WebProxy/tests/WebProxyTest.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@ public static IEnumerable<object[]> Ctor_ExpectedPropertyValues_MemberData()
1616
yield return new object[] { new WebProxy(), null, false, false, Array.Empty<string>(), null };
1717

1818
yield return new object[] { new WebProxy("http://anything"), new Uri("http://anything"), false, false, Array.Empty<string>(), null };
19+
yield return new object[] { new WebProxy("http://anything:42"), new Uri("http://anything:42"), false, false, Array.Empty<string>(), null };
20+
yield return new object[] { new WebProxy("anything:42"), new Uri("http://anything:42"), false, false, Array.Empty<string>(), null };
1921
yield return new object[] { new WebProxy("anything", 42), new Uri("http://anything:42"), false, false, Array.Empty<string>(), null };
22+
yield return new object[] { new WebProxy("http://anything", 42), new Uri("http://anything:42"), false, false, Array.Empty<string>(), null };
23+
yield return new object[] { new WebProxy("http://anything:123", 42), new Uri("http://anything:42"), false, false, Array.Empty<string>(), null };
24+
yield return new object[] { new WebProxy("socks5://anything", 42), new Uri("socks5://anything:42"), false, false, Array.Empty<string>(), null };
2025
yield return new object[] { new WebProxy(new Uri("http://anything")), new Uri("http://anything"), false, false, Array.Empty<string>(), null };
2126

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

0 commit comments

Comments
 (0)