5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2008 Novell Inc. http://novell.com
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Runtime.CompilerServices;
32 using System.Security.Permissions;
33 using System.Text.RegularExpressions;
36 namespace System.Web.Routing
39 [TypeForwardedFrom ("System.Web.Routing, Version=3.5.0.0, Culture=Neutral, PublicKeyToken=31bf3856ad364e35")]
41 [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
42 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
43 public class Route : RouteBase
46 static readonly Type httpRequestBaseType = typeof (HttpRequestBase);
50 public RouteValueDictionary Constraints { get; set; }
52 public RouteValueDictionary DataTokens { get; set; }
54 public RouteValueDictionary Defaults { get; set; }
56 public IRouteHandler RouteHandler { get; set; }
59 get { return url != null ? url.Url : String.Empty; }
60 set { url = value != null ? new PatternParser (value) : new PatternParser (String.Empty); }
63 public Route (string url, IRouteHandler routeHandler)
64 : this (url, null, routeHandler)
68 public Route (string url, RouteValueDictionary defaults, IRouteHandler routeHandler)
69 : this (url, defaults, null, routeHandler)
73 public Route (string url, RouteValueDictionary defaults, RouteValueDictionary constraints, IRouteHandler routeHandler)
74 : this (url, defaults, constraints, null, routeHandler)
78 public Route (string url, RouteValueDictionary defaults, RouteValueDictionary constraints, RouteValueDictionary dataTokens, IRouteHandler routeHandler)
82 Constraints = constraints;
83 DataTokens = dataTokens;
84 RouteHandler = routeHandler;
87 public override RouteData GetRouteData (HttpContextBase httpContext)
89 var path = httpContext.Request.AppRelativeCurrentExecutionFilePath;
90 var pathInfo = httpContext.Request.PathInfo;
92 if (!String.IsNullOrEmpty (pathInfo))
95 // probably code like this causes ArgumentOutOfRangeException under .NET.
96 // It somehow allows such path that is completely equivalent to the Url. Dunno why.
97 if (Url != path && path.Substring (0, 2) != "~/")
99 path = path.Substring (2);
101 var values = url.Match (path, Defaults);
105 RouteValueDictionary constraints = Constraints;
106 if (constraints != null)
107 foreach (var p in constraints)
108 if (!ProcessConstraint (httpContext, p.Value, p.Key, values, RouteDirection.IncomingRequest))
111 var rd = new RouteData (this, RouteHandler);
112 RouteValueDictionary rdValues = rd.Values;
114 foreach (var p in values)
115 rdValues.Add (p.Key, p.Value);
117 RouteValueDictionary dataTokens = DataTokens;
118 if (dataTokens != null) {
119 RouteValueDictionary rdDataTokens = rd.DataTokens;
120 foreach (var token in dataTokens)
121 rdDataTokens.Add (token.Key, token.Value);
127 public override VirtualPathData GetVirtualPath (RequestContext requestContext, RouteValueDictionary values)
129 if (requestContext == null)
130 throw new ArgumentNullException ("requestContext");
132 return new VirtualPathData (this, String.Empty);
134 // null values is allowed.
135 // if (values == null)
136 // values = requestContext.RouteData.Values;
139 if (!url.BuildUrl (this, requestContext, values, out s))
142 return new VirtualPathData (this, s);
145 internal static bool ProcessConstraintInternal (HttpContextBase httpContext, Route route, object constraint, string parameterName,
146 RouteValueDictionary values, RouteDirection routeDirection, RequestContext reqContext,
147 out bool invalidConstraint)
149 invalidConstraint = false;
150 IRouteConstraint irc = constraint as IRouteConstraint;
152 return irc.Match (httpContext, route, parameterName, values, routeDirection);
154 string s = constraint as string;
159 if (values != null && values.TryGetValue (parameterName, out o))
164 if (!String.IsNullOrEmpty (v))
165 return MatchConstraintRegex (v, s);
167 else if (reqContext != null) {
168 RouteData rd = reqContext != null ? reqContext.RouteData : null;
169 RouteValueDictionary rdValues = rd != null ? rd.Values : null;
171 if (rdValues == null || rdValues.Count == 0)
174 if (!rdValues.TryGetValue (parameterName, out o))
178 if (String.IsNullOrEmpty (v))
181 return MatchConstraintRegex (v, s);
187 invalidConstraint = true;
191 static bool MatchConstraintRegex (string value, string constraint)
193 int len = constraint.Length;
195 // Bug #651966 - regexp constraints must be treated
196 // as absolute expressions
197 if (constraint [0] != '^') {
198 constraint = "^" + constraint;
202 if (constraint [len - 1] != '$')
206 return Regex.Match (value, constraint).Success;
209 protected virtual bool ProcessConstraint (HttpContextBase httpContext, object constraint, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
211 if (parameterName == null)
212 throw new ArgumentNullException ("parameterName");
214 // .NET "compatibility"
216 throw new NullReferenceException ();
218 RequestContext reqContext;
220 reqContext = SafeGetContext (httpContext != null ? httpContext.Request : null);
224 bool invalidConstraint;
225 bool ret = ProcessConstraintInternal (httpContext, this, constraint, parameterName, values, routeDirection, reqContext, out invalidConstraint);
227 if (invalidConstraint)
228 throw new InvalidOperationException (
230 "Constraint parameter '{0}' on the route with URL '{1}' must have a string value type or be a type which implements IRouteConstraint",
238 RequestContext SafeGetContext (HttpRequestBase req)
240 if (req == null || req.GetType () != httpRequestBaseType)
243 return req.RequestContext;