aeb02c319b2e0a68a9215c39e016182769fcf419
[mono.git] / mcs / class / System.Web.Routing / System.Web.Routing / Route.cs
1 //
2 // Route.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // Copyright (C) 2008 Novell Inc. http://novell.com
8 //
9
10 //
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:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
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.
29 //
30 using System;
31 using System.Runtime.CompilerServices;
32 using System.Security.Permissions;
33 using System.Text.RegularExpressions;
34 using System.Web;
35 using System.Globalization;
36
37 namespace System.Web.Routing
38 {
39 #if NET_4_0
40         [TypeForwardedFrom ("System.Web.Routing, Version=3.5.0.0, Culture=Neutral, PublicKeyToken=31bf3856ad364e35")]
41 #endif
42         [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
43         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
44         public class Route : RouteBase
45         {
46 #if NET_4_0
47                 static readonly Type httpRequestBaseType = typeof (HttpRequestBase);
48 #endif
49                 PatternParser url;
50
51                 public RouteValueDictionary Constraints { get; set; }
52
53                 public RouteValueDictionary DataTokens { get; set; }
54
55                 public RouteValueDictionary Defaults { get; set; }
56
57                 public IRouteHandler RouteHandler { get; set; }
58
59                 public string Url {
60                         get { return url != null ? url.Url : String.Empty; }
61                         set { url = value != null ? new PatternParser (value) : new PatternParser (String.Empty); }
62                 }
63
64                 public Route (string url, IRouteHandler routeHandler)
65                         : this (url, null, routeHandler)
66                 {
67                 }
68
69                 public Route (string url, RouteValueDictionary defaults, IRouteHandler routeHandler)
70                         : this (url, defaults, null, routeHandler)
71                 {
72                 }
73
74                 public Route (string url, RouteValueDictionary defaults, RouteValueDictionary constraints, IRouteHandler routeHandler)
75                         : this (url, defaults, constraints, null, routeHandler)
76                 {
77                 }
78
79                 public Route (string url, RouteValueDictionary defaults, RouteValueDictionary constraints, RouteValueDictionary dataTokens, IRouteHandler routeHandler)
80                 {
81                         Url = url;
82                         Defaults = defaults;
83                         Constraints = constraints;
84                         DataTokens = dataTokens;
85                         RouteHandler = routeHandler;
86                 }
87
88                 public override RouteData GetRouteData (HttpContextBase httpContext)
89                 {
90                         var path = httpContext.Request.AppRelativeCurrentExecutionFilePath;
91                         var pathInfo = httpContext.Request.PathInfo;
92
93                         if (!String.IsNullOrEmpty (pathInfo))
94                                 path += pathInfo;
95
96                         // probably code like this causes ArgumentOutOfRangeException under .NET.
97                         // It somehow allows such path that is completely equivalent to the Url. Dunno why.
98                         if (Url != path && path.Substring (0, 2) != "~/")
99                                 return null;
100                         path = path.Substring (2);
101
102                         var values = url.Match (path, Defaults);
103                         if (values == null)
104                                 return null;
105
106                         if (!ProcessConstraints (httpContext, values, RouteDirection.IncomingRequest))
107                                 return null;
108                         
109                         var rd = new RouteData (this, RouteHandler);
110                         RouteValueDictionary rdValues = rd.Values;
111                         
112                         foreach (var p in values)
113                                 rdValues.Add (p.Key, p.Value);
114
115                         RouteValueDictionary dataTokens = DataTokens;
116                         if (dataTokens != null) {
117                                 RouteValueDictionary rdDataTokens = rd.DataTokens;
118                                 foreach (var token in dataTokens)
119                                         rdDataTokens.Add (token.Key, token.Value);
120                         }
121                         
122                         return rd;
123                 }
124
125                 public override VirtualPathData GetVirtualPath (RequestContext requestContext, RouteValueDictionary values)
126                 {
127                         if (requestContext == null)
128                                 throw new ArgumentNullException ("requestContext");
129                         if (url == null)
130                                 return new VirtualPathData (this, String.Empty);
131
132                         // null values is allowed.
133                         // if (values == null)
134                         //      values = requestContext.RouteData.Values;
135
136                         RouteValueDictionary usedValues;
137                         string resultUrl = url.BuildUrl (this, requestContext, values, Constraints, out usedValues);
138
139                         if (resultUrl == null)
140                                 return null;
141
142                         if (!ProcessConstraints (requestContext.HttpContext, usedValues, RouteDirection.UrlGeneration))
143                                 return null;
144
145                         var result = new VirtualPathData (this, resultUrl);
146
147                         RouteValueDictionary dataTokens = DataTokens;
148                         if (dataTokens != null) {
149                                 foreach (var item in dataTokens)
150                                         result.DataTokens[item.Key] = item.Value;
151                         }
152
153                         return result;
154                 }
155
156                 private bool ProcessConstraintInternal (HttpContextBase httpContext, Route route, object constraint, string parameterName,
157                                                                 RouteValueDictionary values, RouteDirection routeDirection, RequestContext reqContext,
158                                                                 out bool invalidConstraint)
159                 {
160                         invalidConstraint = false;
161                         IRouteConstraint irc = constraint as IRouteConstraint;
162                         if (irc != null)
163                                 return irc.Match (httpContext, route, parameterName, values, routeDirection);
164
165                         string s = constraint as string;
166                         if (s != null) {
167                                 string v = null;
168                                 object o;
169
170                                 // NOTE: If constraint was not an IRouteConstraint, is is asumed
171                                 // to be an object 'convertible' to string, or at least this is how
172                                 // ASP.NET seems to work by the tests i've done latelly. (pruiz)
173
174                                 if (values != null && values.TryGetValue (parameterName, out o))
175                                         v = Convert.ToString (o, CultureInfo.InvariantCulture);
176
177                                 if (!String.IsNullOrEmpty (v))
178                                         return MatchConstraintRegex (v, s);
179 #if NET_4_0
180                                 else if (reqContext != null) {
181                                         RouteData rd = reqContext != null ? reqContext.RouteData : null;
182                                         RouteValueDictionary rdValues = rd != null ? rd.Values : null;
183
184                                         if (rdValues == null || rdValues.Count == 0)
185                                                 return false;
186                                         
187                                         if (!rdValues.TryGetValue (parameterName, out o))
188                                                 return false;
189
190                                         v = Convert.ToString (o, CultureInfo.InvariantCulture);
191                                         if (String.IsNullOrEmpty (v))
192                                                 return false;
193
194                                         return MatchConstraintRegex (v, s);
195                                 }
196 #endif
197                                 return false;
198                         }
199
200                         invalidConstraint = true;
201                         return false;
202                 }
203
204                 static bool MatchConstraintRegex (string value, string constraint)
205                 {
206                         int len = constraint.Length;
207                         if (len > 0) {
208                                 // Bug #651966 - regexp constraints must be treated
209                                 // as absolute expressions
210                                 if (constraint [0] != '^') {
211                                         constraint = "^" + constraint;
212                                         len++;
213                                 }
214
215                                 if (constraint [len - 1] != '$')
216                                         constraint += "$";
217                         }
218
219                         return Regex.IsMatch (value, constraint, RegexOptions.CultureInvariant | RegexOptions.IgnoreCase | RegexOptions.Compiled);
220                 }
221                 
222                 protected virtual bool ProcessConstraint (HttpContextBase httpContext, object constraint, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
223                 {
224                         if (parameterName == null)
225                                 throw new ArgumentNullException ("parameterName");
226
227                         // .NET "compatibility"
228                         if (values == null)
229                                 throw new NullReferenceException ();
230
231                         RequestContext reqContext;
232 #if NET_4_0
233                         reqContext = SafeGetContext (httpContext != null ? httpContext.Request : null);
234 #else
235                         reqContext = null;
236 #endif
237                         bool invalidConstraint;
238                         bool ret = ProcessConstraintInternal (httpContext, this, constraint, parameterName, values, routeDirection, reqContext, out invalidConstraint);
239                         
240                         if (invalidConstraint)
241                                 throw new InvalidOperationException (
242                                         String.Format (
243                                                 "Constraint parameter '{0}' on the route with URL '{1}' must have a string value type or be a type which implements IRouteConstraint",
244                                                 parameterName, Url
245                                         )
246                                 );
247
248                         return ret;
249                 }
250
251                 private bool ProcessConstraints (HttpContextBase httpContext, RouteValueDictionary values, RouteDirection routeDirection)
252                 {
253                         var constraints = Constraints;
254
255                         if (Constraints != null) {
256                                 foreach (var p in constraints)
257                                         if (!ProcessConstraint (httpContext, p.Value, p.Key, values, routeDirection))
258                                                 return false;
259                         }
260
261                         return true;
262                 }
263
264 #if NET_4_0
265                 RequestContext SafeGetContext (HttpRequestBase req)
266                 {
267                         if (req == null || req.GetType () != httpRequestBaseType)
268                                 return null;
269                                 
270                         return req.RequestContext;
271                 }
272 #endif
273         }
274 }