forked from unosquare/embedio
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Respect multiple DNS resolutions in EndPointManager
Updates EndPointManager to bind to *all* returned DNS records for hostname-based prefixes, not just the first one. This does diverge from expected Mono behavior, but should better match that of `Http.sys`. Fixes unosquare#576.
- Loading branch information
Showing
2 changed files
with
106 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
using System; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Net.Sockets; | ||
using System.Threading.Tasks; | ||
using NUnit.Framework; | ||
using Swan; | ||
|
||
namespace EmbedIO.Tests.Issues; | ||
|
||
[TestFixture] | ||
public class Issue576_LocalhostDualStack { | ||
[TestCase("127.0.0.1")] | ||
[TestCase("::1")] | ||
public async Task LocalhostAcceptsDualStack(string address) { | ||
if (SwanRuntime.OS != Swan.OperatingSystem.Windows) | ||
Assert.Ignore("Only Windows"); | ||
|
||
using var instance = new WebServer(HttpListenerMode.EmbedIO, "http://localhost:8877"); | ||
instance.OnAny(ctx => ctx.SendDataAsync(DateTime.Now)); | ||
|
||
_ = instance.RunAsync(); | ||
|
||
using var handler = BuildFakeResolver(IPAddress.Parse(address)); | ||
using var client = new HttpClient(handler); | ||
|
||
var uri = new Uri("http://localhost:8877"); | ||
Assert.IsNotEmpty(await client.GetStringAsync(uri).ConfigureAwait(false)); | ||
} | ||
|
||
[TestCase("http://localhost:8877")] | ||
[TestCase("http://127.0.0.1:8877")] | ||
public async Task LocalhostV4AcceptsIpAndHost(string uri) { | ||
if (SwanRuntime.OS != Swan.OperatingSystem.Windows) | ||
Assert.Ignore("Only Windows"); | ||
|
||
using var instance = new WebServer(HttpListenerMode.EmbedIO, "http://localhost:8877", "http://127.0.0.1:8877"); | ||
instance.OnAny(ctx => ctx.SendDataAsync(DateTime.Now)); | ||
|
||
_ = instance.RunAsync(); | ||
|
||
using var handler = BuildFakeResolver(IPAddress.Loopback); // force ipv4 for this test | ||
using var client = new HttpClient(handler); | ||
|
||
Assert.IsNotEmpty(await client.GetStringAsync(new Uri(uri)).ConfigureAwait(false)); | ||
} | ||
|
||
[TestCase("http://localhost:8877")] | ||
[TestCase("http://[::1]:8877")] | ||
public async Task LocalhostV6AcceptsIpAndHost(string uri) { | ||
if (SwanRuntime.OS != Swan.OperatingSystem.Windows) | ||
Assert.Ignore("Only Windows"); | ||
|
||
using var instance = new WebServer(HttpListenerMode.EmbedIO, "http://localhost:8877", "http://[::1]:8877"); | ||
instance.OnAny(ctx => ctx.SendDataAsync(DateTime.Now)); | ||
|
||
_ = instance.RunAsync(); | ||
|
||
using var handler = BuildFakeResolver(IPAddress.IPv6Loopback); // force ipv6 for this test | ||
using var client = new HttpClient(handler); | ||
|
||
Assert.IsNotEmpty(await client.GetStringAsync(new Uri(uri)).ConfigureAwait(false)); | ||
} | ||
|
||
private static SocketsHttpHandler BuildFakeResolver(IPAddress target) { | ||
// Borrowed from https://www.meziantou.net/forcing-httpclient-to-use-ipv4-or-ipv6-addresses.htm and adapted | ||
return new SocketsHttpHandler { | ||
ConnectCallback = async (context, cancellationToken) => { | ||
var socket = new Socket(SocketType.Stream, ProtocolType.Tcp); | ||
socket.NoDelay = true; | ||
try { | ||
await socket.ConnectAsync(target, context.DnsEndPoint.Port, cancellationToken).ConfigureAwait(false); | ||
return new NetworkStream(socket, ownsSocket: true); | ||
} catch { | ||
socket.Dispose(); | ||
throw; | ||
} | ||
}, | ||
}; | ||
} | ||
} |