Merge pull request #1222 from LogosBible/uri-trycreate
authorMarcos Henrich <marcoshenrich@gmail.com>
Mon, 20 Apr 2015 13:06:22 +0000 (14:06 +0100)
committerMarcos Henrich <marcoshenrich@gmail.com>
Mon, 20 Apr 2015 13:06:22 +0000 (14:06 +0100)
Lower-case the host in Uri.TryCreate to match behavior of constructors

1  2 
mcs/class/System/System/Uri.cs
mcs/class/System/Test/System/UriTest3.cs

index eab294b3f8f00fb84b3caf15d1acf4b4c1246a71,5a6b930d40201ba539cdf7809e3bb523b38786a1..a1417e99527c0bd59f0befff9246110310ab4080
@@@ -140,7 -140,9 +140,7 @@@ namespace System 
  
                static Uri ()
                {
 -#if NET_4_5
                        IriParsing = true;
 -#endif
  
                        var iriparsingVar = Environment.GetEnvironmentVariable ("MONO_URI_IRIPARSING");
                        if (iriparsingVar == "true")
                                        success = false;
                                        break;
                                }
+                               if (success && host.Length > 1 && host [0] != '[' && host [host.Length - 1] != ']') {
+                                       // host name present (but not an IPv6 address)
+                                       host = host.ToLower (CultureInfo.InvariantCulture);
+                               }
                        }
                }
  
                                }
                        } else {
                                path = baseEl.path;
 -#if !NET_4_0
 -                              if (relativeEl.query != null) {
 -                                      var pathEnd = path.LastIndexOf ('/');
 -                                      path = (pathEnd > 0)? path.Substring (0, pathEnd+1) : "";
 -                              }
 -#endif
                        }
  
                        if ((path.Length == 0 || path [0] != '/') && baseEl.delimiter == SchemeDelimiter)
                //
                public Uri MakeRelativeUri (Uri uri)
                {
 -#if NET_4_0
                        if (uri == null)
                                throw new ArgumentNullException ("uri");
 -#endif
                        if (Host != uri.Host || Scheme != uri.Scheme)
                                return uri;
  
                {
                        Parse (kind, source);
  
-                       if (userEscaped)
-                               return;
                        if (host.Length > 1 && host [0] != '[' && host [host.Length - 1] != ']') {
                                // host name present (but not an IPv6 address)
                                host = host.ToLower (CultureInfo.InvariantCulture);
                private UriParser Parser {
                        get {
                                if (parser == null) {
 -                                      parser = UriParser.GetParser (Scheme);
 +                                      parser = UriParser.GetParser (scheme);
                                        // no specific parser ? then use a default one
                                        if (parser == null)
                                                parser = new DefaultUriParser ("*");
  
                public string GetComponents (UriComponents components, UriFormat format)
                {
 +                      if ((components & UriComponents.SerializationInfoString) == 0)
 +                              EnsureAbsoluteUri ();
 +
                        return Parser.GetComponents (this, components, format);
                }
  
                public bool IsBaseOf (Uri uri)
                {
 -#if NET_4_0
                        if (uri == null)
                                throw new ArgumentNullException ("uri");
 -#endif
                        return Parser.IsBaseOf (this, uri);
                }
  
  
                        if ((uri1 == null) && (uri2 == null))
                                return 0;
 +                      if (uri1 == null)
 +                              return -1;
 +                      if (uri2 == null)
 +                              return 1;
  
                        string s1 = uri1.GetComponents (partsToCompare, compareFormat);
                        string s2 = uri2.GetComponents (partsToCompare, compareFormat);
                                return false;
                        }
  
 -#if !NET_4_5
 -                      switch (b) {
 -                      case '!':
 -                      case '\'':
 -                      case '(':
 -                      case ')':
 -                      case '*':
 -                      case '-':
 -                      case '.':
 -                              return false;
 -                      }
 -#endif
  
                        return true;
                }
                        case '_':
                        case '~':
                                return false;
 -#if NET_4_5
                        case '[':
                        case ']':
                                return false;
 -#endif
                        }
  
                        return true;
                        result = null;
                        if ((baseUri == null) || !baseUri.IsAbsoluteUri)
                                return false;
 -#if NET_4_0
                        if (relativeUri == null)
                                return false;
 -#endif
                        try {
                                // FIXME: this should call UriParser.Resolve
                                result = new Uri (baseUri, relativeUri.OriginalString);
index 0300aad9d579b981a08d5b90585e0249b3a3f296,db22913e3a760586a824230d855eed384a2d9dbf..7ff8924d0daaed4c9091dbdcbba456fa6b3a0323
@@@ -184,6 -184,12 +184,12 @@@ namespace MonoTests.Syste
  
                        Assert.IsTrue (Uri.TryCreate ("http://mono-project.com/☕", UriKind.Absolute, out uri), "highunicode-Absolute");
                        Assert.AreEqual("http://mono-project.com/%E2%98%95", uri.AbsoluteUri, "highunicode-Absolute-AbsoluteUri");
+                       string mixedCaseUri = "http://mOnO-proJECT.com";
+                       uri = new Uri (mixedCaseUri);
+                       Uri uri2;
+                       Assert.IsTrue (Uri.TryCreate (mixedCaseUri, UriKind.Absolute, out uri2), "mixedcasehost-absolute");
+                       Assert.AreEqual (uri.AbsoluteUri, uri2.AbsoluteUri, "mixedcasehost-absoluteuri-absoluteuri");
                }
  
                [Test] // TryCreate (String, UriKind, Uri)
                        Uri u2 = null;
                        Assert.AreEqual (0, Uri.Compare (u1, u2, UriComponents.AbsoluteUri, UriFormat.UriEscaped, StringComparison.CurrentCulture), "null-null");
  
 +                      u1 = new Uri ("http://www.go-mono.com");
 +                      Assert.AreEqual (1, Uri.Compare (u1, u2, UriComponents.AbsoluteUri, UriFormat.UriEscaped, StringComparison.CurrentCulture), "non-null - null");
 +
 +                      u1 = null;
 +                      u2 = new Uri ("http://www.go-mono.com");
 +                      Assert.AreEqual (-1, Uri.Compare (u1, u2, UriComponents.AbsoluteUri, UriFormat.UriEscaped, StringComparison.CurrentCulture), "null - non-null");
 +
                        u1 = new Uri ("http://www.go-mono.com/Main Page");
                        u2 = new Uri ("http://www.go-mono.com/Main%20Page");
                        Assert.AreEqual (0, Uri.Compare (u1, u2, UriComponents.AbsoluteUri, UriFormat.Unescaped, StringComparison.CurrentCulture), "http/space-http/%20-unescaped");
                                http.IsBaseOf (null);
                                Assert.Fail ();
                        }
 -#if NET_4_0
                        catch (ArgumentNullException) {
                        }
 -#else
 -                      catch (NullReferenceException) {
 -                      }
 -#endif
                }
  
                [Test]