From abcb513ddece01bb67dc11f8d331af5199201212 Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Thu, 6 Jul 2017 16:37:32 -0400 Subject: [PATCH] Fix ManagedHandler to support connecting to IPv6 servers (dotnet/corefx#21951) Also use IdnHost instead of Host, in case the Host isn't DNS-safe. Commit migrated from https://github.com/dotnet/corefx/commit/45ae10c713f9799f2f47a2c390b30529bd222bac --- .../System/Net/Http/Managed/ConnectHelper.cs | 47 ++++++------------- .../System/Net/Http/Managed/HttpConnection.cs | 2 +- .../Net/Http/Managed/HttpConnectionHandler.cs | 4 +- .../Managed/HttpProxyConnectionHandler.cs | 2 +- 4 files changed, 19 insertions(+), 36 deletions(-) diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/Managed/ConnectHelper.cs b/src/libraries/System.Net.Http/src/System/Net/Http/Managed/ConnectHelper.cs index fc0a58c844e..aca4b488af0 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/Managed/ConnectHelper.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/Managed/ConnectHelper.cs @@ -11,48 +11,31 @@ namespace System.Net.Http { public static async ValueTask ConnectAsync(string host, int port) { - TcpClient client; + var socket = new Socket(SocketType.Stream, ProtocolType.Tcp) { NoDelay = true }; try { - // You would think TcpClient.Connect would just do this, but apparently not. - // It works for IPv4 addresses but seems to barf on IPv6. - // I need to explicitly invoke the constructor with AddressFamily = IPv6. - // TODO #21452: Does this mean that connecting by name will only work with IPv4 - // (since that's the default)? If so, need to rework this logic - // to resolve the IPAddress ourselves. Yuck. // TODO #21452: No cancellationToken on ConnectAsync? - IPAddress ipAddress; - if (IPAddress.TryParse(host, out ipAddress)) - { - client = new TcpClient(ipAddress.AddressFamily); - await client.ConnectAsync(ipAddress, port).ConfigureAwait(false); - } - else - { - client = new TcpClient(); - await client.ConnectAsync(host, port).ConfigureAwait(false); - } + await (IPAddress.TryParse(host, out IPAddress address) ? + socket.ConnectAsync(address, port) : + socket.ConnectAsync(host, port)).ConfigureAwait(false); } catch (SocketException se) { - throw new HttpRequestException("could not connect", se); + socket.Dispose(); + throw new HttpRequestException(se.Message, se); } - client.NoDelay = true; - - NetworkStream networkStream = client.GetStream(); - - // TODO #21452: Timeouts? - // Default timeout should be something less than infinity (the Socket default) - // Timeouts probably need to be configurable - // However, timeouts are also a huge pain when debugging, so consider that too. + return new NetworkStream(socket, ownsSocket: true) + { #if false - // Set default read/write timeouts of 5 seconds. - networkStream.ReadTimeout = 5000; - networkStream.WriteTimeout = 5000; + // TODO #21452: Timeouts? + // Default timeout should be something less than infinity (the Socket default) + // Timeouts probably need to be configurable + // However, timeouts are also a huge pain when debugging, so consider that too. + ReadTimeout = 5000, + WriteTimeout = 5000 #endif - - return networkStream; + }; } } } diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/Managed/HttpConnection.cs b/src/libraries/System.Net.Http/src/System/Net/Http/Managed/HttpConnection.cs index 921b1faee15..a7f580ed809 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/Managed/HttpConnection.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/Managed/HttpConnection.cs @@ -599,7 +599,7 @@ namespace System.Net.Http { await WriteBytesAsync(s_hostKeyAndSeparator, cancellationToken).ConfigureAwait(false); - await WriteStringAsync(uri.Host, cancellationToken).ConfigureAwait(false); + await WriteStringAsync(uri.IdnHost, cancellationToken).ConfigureAwait(false); if (!uri.IsDefaultPort) { await WriteByteAsync((byte)':', cancellationToken).ConfigureAwait(false); diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/Managed/HttpConnectionHandler.cs b/src/libraries/System.Net.Http/src/System/Net/Http/Managed/HttpConnectionHandler.cs index 3b4d6b54e0b..f225f57a4c6 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/Managed/HttpConnectionHandler.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/Managed/HttpConnectionHandler.cs @@ -96,13 +96,13 @@ namespace System.Net.Http { Uri uri = request.RequestUri; - Stream stream = await ConnectHelper.ConnectAsync(uri.Host, uri.Port).ConfigureAwait(false); + Stream stream = await ConnectHelper.ConnectAsync(uri.IdnHost, uri.Port).ConfigureAwait(false); TransportContext transportContext = null; if (uri.Scheme == UriScheme.Https) { - SslStream sslStream = await EstablishSslConnection(uri.Host, request, stream).ConfigureAwait(false); + SslStream sslStream = await EstablishSslConnection(uri.IdnHost, request, stream).ConfigureAwait(false); stream = sslStream; transportContext = sslStream.TransportContext; diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/Managed/HttpProxyConnectionHandler.cs b/src/libraries/System.Net.Http/src/System/Net/Http/Managed/HttpProxyConnectionHandler.cs index 1a119232e3d..0e7c1225da5 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/Managed/HttpProxyConnectionHandler.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/Managed/HttpProxyConnectionHandler.cs @@ -116,7 +116,7 @@ namespace System.Net.Http } } - Stream stream = await ConnectHelper.ConnectAsync(proxyUri.Host, proxyUri.Port).ConfigureAwait(false); + Stream stream = await ConnectHelper.ConnectAsync(proxyUri.IdnHost, proxyUri.Port).ConfigureAwait(false); if (pool == null) {