[asp.net] Fix for bug #671753. Route.GetVirtualPath must take ambient values into account.

This commit is contained in:
Marek Habersack committed 2011-02-25 13:06:51 +01:00
1 parent 48d34c69fb
commit 62b047ae5b
3 files changed
+89 -19

No files matched your search

@@ -445,7 +445,7 @@ namespace System.Web.Routing
bool invalidConstraint;
foreach (var de in constraints) {
if (!Route.ProcessConstraintInternal (context, route, de.Value, de.Key, userValues, RouteDirection.UrlGeneration, out invalidConstraint) ||
if (!Route.ProcessConstraintInternal (context, route, de.Value, de.Key, userValues, RouteDirection.UrlGeneration, requestContext, out invalidConstraint) ||
invalidConstraint)
return false; // constraint not met => no match
}
@@ -42,6 +42,9 @@ namespace System.Web.Routing
[AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public class Route : RouteBase
{
#if NET_4_0
static readonly Type httpRequestBaseType = typeof (HttpRequestBase);
#endif
PatternParser url;
public RouteValueDictionary Constraints { get; set; }
@@ -140,7 +143,8 @@ namespace System.Web.Routing
}
internal static bool ProcessConstraintInternal (HttpContextBase httpContext, Route route, object constraint, string parameterName,
RouteValueDictionary values, RouteDirection routeDirection, out bool invalidConstraint)
RouteValueDictionary values, RouteDirection routeDirection, RequestContext reqContext,
out bool invalidConstraint)
{
invalidConstraint = false;
IRouteConstraint irc = constraint as IRouteConstraint;
@@ -149,29 +153,58 @@ namespace System.Web.Routing
string s = constraint as string;
if (s != null) {
string v = values [parameterName] as string;
if (!String.IsNullOrEmpty (v)) {
int slen = s.Length;
if (slen > 0) {
// Bug #651966 - regexp constraints must be treated
// as absolute expressions
if (s [0] != '^') {
s = "^" + s;
slen++;
}
if (s [slen - 1] != '$')
s += "$";
}
string v;
object o;
return Regex.Match (v, s).Success;
if (values != null && values.TryGetValue (parameterName, out o))
v = o as string;
else
v = null;
if (!String.IsNullOrEmpty (v))
return MatchConstraintRegex (v, s);
#if NET_4_0
else if (reqContext != null) {
RouteData rd = reqContext != null ? reqContext.RouteData : null;
RouteValueDictionary rdValues = rd != null ? rd.Values : null;
if (rdValues == null || rdValues.Count == 0)
return false;
if (!rdValues.TryGetValue (parameterName, out o))
return false;
v = o as string;
if (String.IsNullOrEmpty (v))
return false;
return MatchConstraintRegex (v, s);
}
#endif
return false;
}
invalidConstraint = true;
return false;
}
static bool MatchConstraintRegex (string rx, string constraint)
{
int rxlen = rx.Length;
if (rxlen > 0) {
// Bug #651966 - regexp constraints must be treated
// as absolute expressions
if (rx [0] != '^') {
rx = "^" + rx;
rxlen++;
}
if (rx [rxlen - 1] != '$')
rx += "$";
}
return Regex.Match (rx, constraint).Success;
}
protected virtual bool ProcessConstraint (HttpContextBase httpContext, object constraint, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
@@ -181,9 +214,15 @@ namespace System.Web.Routing
// .NET "compatibility"
if (values == null)
throw new NullReferenceException ();
RequestContext reqContext;
#if NET_4_0
reqContext = SafeGetContext (httpContext != null ? httpContext.Request : null);
#else
reqContext = null;
#endif
bool invalidConstraint;
bool ret = ProcessConstraintInternal (httpContext, this, constraint, parameterName, values, routeDirection, out invalidConstraint);
bool ret = ProcessConstraintInternal (httpContext, this, constraint, parameterName, values, routeDirection, reqContext, out invalidConstraint);
if (invalidConstraint)
throw new InvalidOperationException (
@@ -195,5 +234,14 @@ namespace System.Web.Routing
return ret;
}
#if NET_4_0
RequestContext SafeGetContext (HttpRequestBase req)
{
if (req == null || req.GetType () != httpRequestBaseType)
return null;
return req.RequestContext;
}
#endif
}
}
@@ -28,6 +28,7 @@
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.IO;
using System.Web;
using System.Web.Routing;
using NUnit.Framework;
@@ -1271,6 +1272,27 @@ namespace MonoTests.System.Web.Routing
}
[Test (Description="Bug #671753")]
public void GetVirtualPath15 ()
{
var context = new HttpContextWrapper (
new HttpContext (new HttpRequest ("filename", "http://localhost/filename", String.Empty),
new HttpResponse (new StringWriter())
)
);
var rc = new RequestContext (context, new RouteData ());
Assert.IsNotNull (RouteTable.Routes, "#A1");
RouteTable.Routes.MapPageRoute ("TestRoute", "{language}/testroute", "~/TestRoute.aspx", true, null,
new RouteValueDictionary {{"language", "(ru|en)"}});
Assert.IsNotNull(RouteTable.Routes.GetVirtualPath (rc, "TestRoute", new RouteValueDictionary {{"language", "en"}}), "#A2");
rc.RouteData.Values["language"] = "ru";
Assert.IsNotNull (RouteTable.Routes.GetVirtualPath (rc, "TestRoute", new RouteValueDictionary ()), "#A3");
Assert.IsNotNull (RouteTable.Routes.GetVirtualPath (rc, "TestRoute", null), "#A4");
}
// Bug #500739
[Test]
public void RouteGetRequiredStringWithDefaults ()