Fixes: #341443
authorMiguel de Icaza <miguel@gnome.org>
Mon, 8 Sep 2008 21:59:21 +0000 (21:59 -0000)
committerMiguel de Icaza <miguel@gnome.org>
Mon, 8 Sep 2008 21:59:21 +0000 (21:59 -0000)
2008-09-08  Miguel de Icaza  <miguel@novell.com>

* EndPointListener.cs (SearchListener): Cope with the raw_url not
being merely a path, but potentially a full URI (see the proxy
case).

* HttpListenerRequest.cs: Cope with raw_url not being merely a
path, but potentially a full Uri.   Also uses TryCreate, hoping
that one day it will be faster.

2008-09-08  Miguel de Icaza  <miguel@novell.com>

* Uri.cs (MaybeUri): A helper routine for methods in other classes
to quickly determine if something might be a Uri, before calling
the more expensive Uri.TryCreate (see bug 424192).

svn path=/trunk/mcs/; revision=112551

mcs/class/System/System.Net/ChangeLog
mcs/class/System/System.Net/EndPointListener.cs
mcs/class/System/System.Net/HttpListenerRequest.cs
mcs/class/System/System/ChangeLog
mcs/class/System/System/Uri.cs
mcs/class/System/Test/System.Net/HttpListener2Test.cs

index 2f0d20a7638796a23b024c5f17d1871cbad4effd..4433d572a030e479d14c5d5e98204a313974be75 100644 (file)
@@ -1,3 +1,13 @@
+2008-09-08  Miguel de Icaza  <miguel@novell.com>
+
+       * EndPointListener.cs (SearchListener): Cope with the raw_url not
+       being merely a path, but potentially a full URI (see the proxy
+       case). 
+
+       * HttpListenerRequest.cs: Cope with raw_url not being merely a
+       path, but potentially a full Uri.   Also uses TryCreate, hoping
+       that one day it will be faster.
+
 2008-08-23  Zoltan Varga  <vargaz@gmail.com>
 
        * WebConnectionStream.cs (WriteRequest): Fix the copying in the
index 54a655e1dc49a2a7e63b3b0ccb07ef45c44ea747..fb5c55daab87111ca8311e0ffb0229a87c054a3b 100644 (file)
@@ -149,7 +149,13 @@ namespace System.Net {
                                        host = host.Substring (0, colon);
                        }
 
-                       string path = HttpUtility.UrlDecode (raw_url);
+                       string path;
+                       Uri raw_uri;
+                       if (Uri.MaybeUri (raw_url) && Uri.TryCreate (raw_url, UriKind.Absolute, out raw_uri))
+                               path = raw_uri.PathAndQuery;
+                       else
+                               path = HttpUtility.UrlDecode (raw_url);
+                       
                        string path_slash = path [path.Length - 1] == '/' ? path : path + "/";
                        
                        HttpListener best_match = null;
index 3d598b5e11132c5421ba46cbec883f30c2076e8b..4df1e4623992531959c4c6bfa25e910b7e84f0da 100644 (file)
@@ -138,9 +138,19 @@ namespace System.Net {
                                return;
                        }
 
-                       if (host == null || host.Length == 0)
+                       string path;
+                       Uri raw_uri;
+                       if (Uri.MaybeUri (raw_url) && Uri.TryCreate (raw_url, UriKind.Absolute, out raw_uri))
+                               path = raw_uri.PathAndQuery;
+                       else
+                               path = raw_url;
+
+                       if ((host == null || host.Length == 0))
                                host = UserHostAddress;
 
+                       if (raw_uri != null)
+                               host = raw_uri.Host;
+       
                        int colon = host.IndexOf (':');
                        if (colon >= 0)
                                host = host.Substring (0, colon);
@@ -149,10 +159,9 @@ namespace System.Net {
                                                                (IsSecureConnection) ? "https" : "http",
                                                                host,
                                                                LocalEndPoint.Port);
-                       try {
-                               url = new Uri (base_uri + raw_url);
-                       } catch {
-                               context.ErrorMessage = "Invalid url";
+
+                       if (!Uri.TryCreate (base_uri + path, UriKind.Absolute, out url)){
+                               context.ErrorMessage = "Invalid url: " + base_uri + path;
                                return;
                        }
 
index 8dac8f39d532cf7b183c522838ec594059ac7ab6..fff899506beb700ecb320f8dd14bfe69ebbede78 100644 (file)
@@ -1,3 +1,9 @@
+2008-09-08  Miguel de Icaza  <miguel@novell.com>
+
+       * Uri.cs (MaybeUri): A helper routine for methods in other classes
+       to quickly determine if something might be a Uri, before calling
+       the more expensive Uri.TryCreate (see bug 424192).
+
 2008-08-08  Sebastien Pouliot  <sebastien@ximian.com>
 
        * UriTypeConverter.cs: Fix cp typo found when cp (of course ;-) 
index 2a9a0acc01882c682559470b3239506f997a0e91..9db1072aca1c309b1fde5bf802c1b07773368e8f 100644 (file)
@@ -1733,6 +1733,18 @@ namespace System {
                        return false;
                }
 
+               internal static bool MaybeUri (string s)
+               {
+                       int p = s.IndexOf (':');
+                       if (p == -1)
+                               return false;
+
+                       if (p >= 10)
+                               return false;
+
+                       return IsPredefinedScheme (s.Substring (0, p));
+               }
+               
                private static bool IsPredefinedScheme (string scheme)
                {
                        switch (scheme) {
index ddcc8314594ee9894c38b69979bc53558aff8ce0..1fab5173ec7870b254ee60a7bd7fbc0e97530d7f 100644 (file)
@@ -682,7 +682,38 @@ namespace MonoTests.System.Net {
                        Console.WriteLine ("Done");
                        listener.Close ();
                }
-       
+
+               //
+               // Test case for bug 341443, an pretty old bug, filed on November of 2007.
+               //
+               [Test]
+               public void Test_HostInUri ()
+               {
+                       var wait = new ManualResetEvent (false);
+                       var wait2 = new ManualResetEvent (false);
+                       
+                       Thread t = new Thread (delegate (object a) {
+                               wait.WaitOne ();
+
+                               NetworkStream ns = HttpListener2Test.CreateNS (9145);
+                               HttpListener2Test.Send (ns, "GET http://www.google.com/ HTTP/1.1\r\nHost: www.google.com\r\nContent-Length: 3\r\n\r\n123456");
+
+                               wait2.WaitOne ();
+                               ns.Close ();
+                       });
+                       t.Start ();
+                               
+                       HttpListener listener = HttpListener2Test.CreateAndStartListener ("http://*:9145/");
+                       wait.Set ();
+                       HttpListenerContext ctx = listener.GetContext ();
+                       
+                       Assert.AreEqual ("http://www.google.com:9145/", ctx.Request.Url.ToString ());
+                       Assert.AreEqual ("http://www.google.com/", ctx.Request.RawUrl);
+                       wait2.Set ();
+
+                       listener.Close ();
+               }
+               
        }
 }
 #endif