Bug 12393 - HttpWebRequest.Host does not understand IPv6 addresses
Summary: HttpWebRequest.Host does not understand IPv6 addresses
Alias: None
Product: Class Libraries
Classification: Mono
Component: System ()
Version: 2.10.x
Hardware: PC Linux
: Normal normal
Target Milestone: Untriaged
Assignee: Martin Baulig
Depends on:
Reported: 2013-05-24 18:41 UTC by gregs
Modified: 2013-06-26 06:31 UTC (History)
4 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description gregs 2013-05-24 18:41:11 UTC
Trying to set an HttpWebRequest's Host property with an IPv6 address fails.

Looking at the code, this is because it does a CheckValidHost(), which in turn does a Uri.IsWellFormedUriString.

This furthermore calls uri.IsWellFormedOriginalString() which fails because it escapes the opening and closing brackets "[", "]", which are required as part of IPv6 addresses.  It also fails when setting it to something without the brackets.

Here's some example code that fails:

HttpWebRequest hwr = (HttpWebRequest)WebRequest.Create (new Uri("http://[2001:0000:0000:0001:0001:0001:0157:0000]/test.css"));

hwr.Host = "[2001:0000:0000:0001:0001:0001:0157:0000]";
// also fails
// hwr.Host = "2001:0000:0000:0001:0001:0001:0157:0000";

This works fine in Windows .NET.
Comment 1 Martin Baulig 2013-05-28 20:15:53 UTC
I removed the dependency on #5887 because these are really two completely different issues.

URI already supports IPv6 literals, at least it did last time I checked.  It's possible though that the opening and closing brackets are not recognized.  However, that's a high-priority bug that must be fixed asap.  I'll look at that this week.

#5887 is about rewriting/extending the URI class to fully support RFC 3986 - which is a lot more than just IPv6 support.  That's something that's likely going to take a while to implement.  So let's keep the two bugs separate.
Comment 2 Shawn 2013-06-05 18:01:45 UTC
I am also experiencing this exact same issue. Our application sets a different host from the one in the url and when setting an IPv6 address (with or without brackets) the mono version throws an exception where as the .NET version does not.

System.ArgumentException: Invalid host: [2607:f8b0:4002:802::1012]
  at System.Net.HttpWebRequest.set_Host (System.String value) [0x00027] in /private/tmp/source/bockbuild/profiles/mono-2-10/build-root/mono-2.10.12/_build/mono-2.10.12.git/mcs/class/System/System.Net/HttpWebRequest.cs:392
  at at (wrapper remoting-invoke-with-check) System.Net.HttpWebRequest:set_Host (string)
  at ipv6test.MainClass.Main (System.String[] args) [0x00010] in /Users/shawn/Projects/ipv6test/ipv6test/Program.cs:11

Here is the example code

var webRequest = (HttpWebRequest)WebRequest.Create("http://www.google.com/");
webRequest.Host = "[2607:f8b0:4002:802::1012]";
Comment 3 Martin Baulig 2013-06-26 06:22:02 UTC
First part of this is fixed; HttpWebRequest.Host now accepts IPv6 addresses.

Note that
// hwr.Host = "2001:0000:0000:0001:0001:0001:0157:0000";
is not allowed - but we currently don't report an error.
Comment 4 Martin Baulig 2013-06-26 06:31:21 UTC
The other problem is tracked in bug #8981.