Skip to content

Commit 52df362

Browse files
committed
Improve exception messages 2
1 parent c08e717 commit 52df362

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/SocksException.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ namespace System.Net.Http
77
{
88
internal class SocksException : IOException
99
{
10-
public SocksException(string message) : base(message)
11-
{
12-
}
10+
public SocksException(string message) : base(message) { }
11+
12+
public SocksException(string message, Exception innerException) : base(message, innerException) { }
1313
}
1414
}

src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/SocksHelper.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ private static async ValueTask EstablishSocks4TunnelAsync(Stream stream, bool is
227227
BinaryPrimitives.WriteUInt16BigEndian(buffer.AsSpan(2), (ushort)port);
228228

229229
IPAddress? ipv4Address = null;
230-
if (IPAddress.TryParse(host, out var hostIP))
230+
if (IPAddress.TryParse(host, out IPAddress? hostIP))
231231
{
232232
if (hostIP.AddressFamily == AddressFamily.InterNetwork)
233233
{
@@ -245,9 +245,17 @@ private static async ValueTask EstablishSocks4TunnelAsync(Stream stream, bool is
245245
else if (!isVersion4a)
246246
{
247247
// Socks4 does not support domain names - try to resolve it here
248-
var addresses = async
249-
? await Dns.GetHostAddressesAsync(host, AddressFamily.InterNetwork, cancellationToken).ConfigureAwait(false)
250-
: Dns.GetHostAddresses(host, AddressFamily.InterNetwork);
248+
IPAddress[] addresses;
249+
try
250+
{
251+
addresses = async
252+
? await Dns.GetHostAddressesAsync(host, AddressFamily.InterNetwork, cancellationToken).ConfigureAwait(false)
253+
: Dns.GetHostAddresses(host, AddressFamily.InterNetwork);
254+
}
255+
catch (Exception ex)
256+
{
257+
throw new SocksException(SR.net_socks_no_ipv4_address, ex);
258+
}
251259

252260
if (addresses.Length == 0)
253261
{

src/libraries/System.Net.Http/tests/FunctionalTests/Socks/SocksProxyTest.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ public static IEnumerable<object[]> TestExceptionalAsync_MemberData()
7676
yield return new object[] { scheme, "localhost", true, new NetworkCredential(new string('a', 256), "foo"), "Encoding the UserName took more than the maximum of 255 bytes." };
7777
}
7878

79+
yield return new object[] { "socks4", new string('a', 256), false, null, "Failed to resolve the destination host to an IPv4 address." };
80+
7981
foreach (string scheme in new[] { "socks4a", "socks5" })
8082
{
8183
yield return new object[] { scheme, new string('a', 256), false, null, "Encoding the host took more than the maximum of 255 bytes." };

0 commit comments

Comments
 (0)