Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uri.IsWellFormedUriString needs to be updated?

Tags:

c#

.net

regex

uri

I think I might have discovered an error in the Uri.IsWellFormedUriString method, it might be because it only conforms to the RFC 2396 and RFC 2732 standards and not the newer RFC 3986 which makes the two aforementioned obsolete.

What I think happens is that any non us-ascii characters makes it fail, so urls with characters like æ, ø, ö or å in it will make it return false. Since these kind of characters are now allowed (wikipedia among others uses them) I think Uri.IsWellFormedUriString should accept them. The regex below is taken from RFC 3986.

What do you think? Should the Uri class be updated?

Anyway here's some sample code which shows the error:

static void Main(string[] args)
        {
            var urls = new []
                           {
                               @"/aaa/bbb/cccd",
                               @"/aaa/bbb/cccæ",
                               @"/aaa/bbb/cccø",
                               @"/aaa/bbb/cccå"
                           };

            var regex = new Regex(@"^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?");

            Debug.WriteLine("");

            foreach (var url in urls)
            {
                if (Uri.IsWellFormedUriString(url, UriKind.Relative))
                    Debug.WriteLine(url + " is a wellformed Uri");

                if (regex.IsMatch(url))
                    Debug.WriteLine(url + " passed the Regex");

                Debug.WriteLine("");
            }
}

Output:

/aaa/bbb/cccd is a wellformed Uri
/aaa/bbb/cccd passed the Regex

/aaa/bbb/cccæ passed the Regex

/aaa/bbb/cccø passed the Regex

/aaa/bbb/cccå passed the Regex
like image 934
Simon Stender Boisen Avatar asked May 24 '11 08:05

Simon Stender Boisen


1 Answers

You have to change you configuration to support the RFC 3986 and RFC 3987. This is the config you have to make:

<configuration>
  <uri>
  <idn enabled="All" />
  <iriParsing enabled="true" />
  </uri>
</configuration>

Taken from here http://msdn.microsoft.com/en-us/library/system.uri.aspx#CodeSnippetContainerCode5

like image 72
Oskar Kjellin Avatar answered Sep 22 '22 13:09

Oskar Kjellin