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);
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 {
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
+ }
+ }
}
}