2009-11-16 Marek Habersack <mhabersack@novell.com>
authorMarek Habersack <grendel@twistedcode.net>
Mon, 16 Nov 2009 11:53:56 +0000 (11:53 -0000)
committerMarek Habersack <grendel@twistedcode.net>
Mon, 16 Nov 2009 11:53:56 +0000 (11:53 -0000)
* HttpContext.cs: RewritePath now treats non-rooted and
app-relative paths correctly - the former are formed by
concatenating base virtual path with the passed file path and the
latter by calling VirtualPathUtility.ToAbsolute. Rooted and
absolute paths are left unmodified. Fixes bug #550103

2009-11-16  Marek Habersack  <mhabersack@novell.com>

* HttpContext.cs: added some tests for RewritePath

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

mcs/class/System.Web/System.Web/ChangeLog
mcs/class/System.Web/System.Web/HttpContext.cs
mcs/class/System.Web/Test/System.Web/ChangeLog
mcs/class/System.Web/Test/System.Web/HttpContext.cs

index 64d6c1bf5829c241844d6e6215d28919761de024..dde7b6c2694ca7b92d39fdef8bebae7a4211a921 100644 (file)
@@ -1,3 +1,11 @@
+2009-11-16  Marek Habersack  <mhabersack@novell.com>
+
+       * HttpContext.cs: RewritePath now treats non-rooted and
+       app-relative paths correctly - the former are formed by
+       concatenating base virtual path with the passed file path and the
+       latter by calling VirtualPathUtility.ToAbsolute. Rooted and
+       absolute paths are left unmodified. Fixes bug #550103
+
 2009-11-13  Marek Habersack  <mhabersack@novell.com>
 
        * HttpResponse.cs: added parameter checks to Redirect ().
index 8302571dfaa8940263ec341f463c8581a791f739..1b7b0e307b30d3a7ed903b4b64f9c1c23e91c088 100644 (file)
@@ -618,25 +618,20 @@ namespace System.Web
                        bool pathRelative = VirtualPathUtility.IsAppRelative (filePath);
                        bool pathAbsolute = pathRelative ? false : VirtualPathUtility.IsAbsolute (filePath);
                        HttpRequest req = Request;
+                       if (req == null)
+                               return;
                        
                        if (pathRelative || pathAbsolute) {
-                               bool needSubstring = false;
-
-                               if (pathRelative && filePath.Length > 1)
-                                       needSubstring = true;
-
-                               string bvd = req.BaseVirtualDir;
-                               if (bvd.Length > 1)
-                                       bvd += "/";
-
-                               string canonizedFilePath = VirtualPathUtility.Canonize (filePath);
-                               filePath = VirtualPathUtility.Combine (bvd, needSubstring ? canonizedFilePath.Substring (2) : canonizedFilePath);
-                       } else 
-                               filePath = VirtualPathUtility.Combine (VirtualPathUtility.GetDirectory (req.FilePath), filePath);
+                               if (pathRelative)
+                                       filePath = VirtualPathUtility.ToAbsolute (filePath);
+                               else
+                                       filePath = filePath;
+                       } else
+                               filePath = VirtualPathUtility.AppendTrailingSlash (req.BaseVirtualDir) + filePath;
                        
                        if (!StrUtils.StartsWith (filePath, HttpRuntime.AppDomainAppVirtualPath))
                                throw new HttpException (404, "The virtual path '" + HttpUtility.HtmlEncode (filePath) + "' maps to another application.", filePath);
-                       
+
                        req.SetCurrentExePath (filePath);
                        req.SetFilePath (filePath);
 
index d35c4f3f9224d58e2dfd0b33877d370b560370a3..2f640577a411e9e31ceac5eae41d5fab17700a30 100644 (file)
@@ -1,3 +1,7 @@
+2009-11-16  Marek Habersack  <mhabersack@novell.com>
+
+       * HttpContext.cs: added some tests for RewritePath
+
 2009-11-09  Marek Habersack  <mhabersack@novell.com>
 
        * HttpCookieTest.cs: added test for bug #553063
index a5e2ccd390c49cc95c4edcce3b42d961bb1d5632..640c540aa8f789924b2e3a136bd9c22d9a673db0 100644 (file)
 using System;
 using System.Text;
 using System.Web;
+using System.Web.UI;
 using System.Collections.Specialized;
 using NUnit.Framework;
+using MonoTests.SystemWeb.Framework;
+using MonoTests.stand_alone.WebHarness;
 
 namespace MonoTests.System.Web {
 
@@ -86,5 +89,61 @@ namespace MonoTests.System.Web {
                        Assert.IsNotNull (ctx.Request, "Request");
                        Assert.IsNotNull (ctx.Response, "Response");
                }
+
+               [Test]
+               public void RewritePath ()
+               {
+                       WebTest t = new WebTest (PageInvoker.CreateOnInit (RewritePath_OnInit));
+                       string html = t.Run ();
+               }
+
+               protected static void RewritePath_OnInit (Page p)
+               {
+                       HttpContext ctx = HttpContext.Current;
+                       HttpRequest req = p.Request;
+                       string origPath = req.FilePath;
+
+                       ctx.RewritePath ("/NunitWeb/file.html", null, null, true);
+                       Assert.AreEqual ("/NunitWeb/file.html", req.FilePath, "#A1");
+                       ctx.RewritePath (origPath, null, null, true);
+
+                       ctx.RewritePath ("~/file.html", null, null, true);
+                       Assert.AreEqual ("/NunitWeb/file.html", req.FilePath, "#A2");
+                       ctx.RewritePath (origPath, null, null, true);
+
+                       ctx.RewritePath ("file.html", null, null, true);
+                       Assert.AreEqual ("/NunitWeb/file.html", req.FilePath, "#A3");
+                       ctx.RewritePath (origPath, null, null, true);
+
+                       try {
+                               ctx.RewritePath ("/file.html", null, null, true);
+                               Assert.Fail ("#A4");
+                       } catch (HttpException ex) {
+                               // The virtual path '/file.html' maps to another application.
+                               //
+                               // success
+                       }
+                       
+                       ctx.RewritePath ("/NunitWeb/sub/file.html", null, null, true);
+                       Assert.AreEqual ("/NunitWeb/sub/file.html", req.FilePath, "#B1");
+                       ctx.RewritePath (origPath, null, null, true);
+
+                       ctx.RewritePath ("~/sub/file.html", null, null, true);
+                       Assert.AreEqual ("/NunitWeb/sub/file.html", req.FilePath, "#B2");
+                       ctx.RewritePath (origPath, null, null, true);
+
+                       ctx.RewritePath ("sub/file.html", null, null, true);
+                       Assert.AreEqual ("/NunitWeb/sub/file.html", req.FilePath, "#B3");
+                       ctx.RewritePath (origPath, null, null, true);
+
+                       try {
+                               ctx.RewritePath ("/sub/file.html", null, null, true);
+                               Assert.Fail ("#B4");
+                       } catch (HttpException ex) {
+                               // The virtual path '/file.html' maps to another application.
+                               //
+                               // success
+                       }
+               }
        }
 }