Load custom attribute data using Cecil if possible in TypeMirror/FieldInfoMirror...
[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
36 namespace System.Web.Routing
37 {
38 #if NET_4_0
39         [TypeForwardedFrom ("System.Web.Routing, Version=3.5.0.0, Culture=Neutral, PublicKeyToken=31bf3856ad364e35")]
40 #endif
41         [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
42         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
43         public class Route : RouteBase
44         {
45 #if NET_4_0
46                 static readonly Type httpRequestBaseType = typeof (HttpRequestBase);
47 #endif
48                 PatternParser url;
49
50                 public RouteValueDictionary Constraints { get; set; }
51
52                 public RouteValueDictionary DataTokens { get; set; }
53
54                 public RouteValueDictionary Defaults { get; set; }
55
56                 public IRouteHandler RouteHandler { get; set; }
57
58                 public string Url {
59                         get { return url != null ? url.Url : String.Empty; }
60                         set { url = value != null ? new PatternParser (value) : new PatternParser (String.Empty); }
61                 }
62
63                 public Route (string url, IRouteHandler routeHandler)
64                         : this (url, null, routeHandler)
65                 {
66                 }
67
68                 public Route (string url, RouteValueDictionary defaults, IRouteHandler routeHandler)
69                         : this (url, defaults, null, routeHandler)
70                 {
71                 }
72
73                 public Route (string url, RouteValueDictionary defaults, RouteValueDictionary constraints, IRouteHandler routeHandler)
74                         : this (url, defaults, constraints, null, routeHandler)
75                 {
76                 }
77
78                 public Route (string url, RouteValueDictionary defaults, RouteValueDictionary constraints, RouteValueDictionary dataTokens, IRouteHandler routeHandler)
79                 {
80                         Url = url;
81                         Defaults = defaults;
82                         Constraints = constraints;
83                         DataTokens = dataTokens;
84                         RouteHandler = routeHandler;
85                 }
86
87                 public override RouteData GetRouteData (HttpContextBase httpContext)
88                 {
89                         var path = httpContext.Request.AppRelativeCurrentExecutionFilePath;
90                         var pathInfo = httpContext.Request.PathInfo;
91
92                         if (!String.IsNullOrEmpty (pathInfo))
93                                 path += pathInfo;
94
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) != "~/")
98                                 return null;
99                         path = path.Substring (2);
100
101                         var values = url.Match (path, Defaults);
102                         if (values == null)
103                                 return null;
104
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))
109                                                 return null;
110                         
111                         var rd = new RouteData (this, RouteHandler);
112                         RouteValueDictionary rdValues = rd.Values;
113                         
114                         foreach (var p in values)
115                                 rdValues.Add (p.Key, p.Value);
116
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);
122                         }
123                         
124                         return rd;
125                 }
126
127                 public override VirtualPathData GetVirtualPath (RequestContext requestContext, RouteValueDictionary values)
128                 {
129                         if (requestContext == null)
130                                 throw new ArgumentNullException ("requestContext");
131                         if (url == null)
132                                 return new VirtualPathData (this, String.Empty);
133
134                         // null values is allowed.
135                         // if (values == null)
136                         //      values = requestContext.RouteData.Values;
137
138                         string s;
139                         if (!url.BuildUrl (this, requestContext, values, out s))
140                                 return null;
141
142                         return new VirtualPathData (this, s);
143                 }
144
145                 internal static bool ProcessConstraintInternal (HttpContextBase httpContext, Route route, object constraint, string parameterName,
146                                                                 RouteValueDictionary values, RouteDirection routeDirection, RequestContext reqContext,
147                                                                 out bool invalidConstraint)
148                 {
149                         invalidConstraint = false;
150                         IRouteConstraint irc = constraint as IRouteConstraint;
151                         if (irc != null)
152                                 return irc.Match (httpContext, route, parameterName, values, routeDirection);
153
154                         string s = constraint as string;
155                         if (s != null) {
156                                 string v;
157                                 object o;
158
159                                 if (values != null && values.TryGetValue (parameterName, out o))
160                                         v = o as string;
161                                 else
162                                         v = null;
163
164                                 if (!String.IsNullOrEmpty (v))
165                                         return MatchConstraintRegex (v, s);
166 #if NET_4_0
167                                 else if (reqContext != null) {
168                                         RouteData rd = reqContext != null ? reqContext.RouteData : null;
169                                         RouteValueDictionary rdValues = rd != null ? rd.Values : null;
170
171                                         if (rdValues == null || rdValues.Count == 0)
172                                                 return false;
173                                         
174                                         if (!rdValues.TryGetValue (parameterName, out o))
175                                                 return false;
176
177                                         v = o as string;
178                                         if (String.IsNullOrEmpty (v))
179                                                 return false;
180
181                                         return MatchConstraintRegex (v, s);
182                                 }
183 #endif
184                                 return false;
185                         }
186
187                         invalidConstraint = true;
188                         return false;
189                 }
190
191                 static bool MatchConstraintRegex (string value, string constraint)
192                 {
193                         int len = constraint.Length;
194                         if (len > 0) {
195                                 // Bug #651966 - regexp constraints must be treated
196                                 // as absolute expressions
197                                 if (constraint [0] != '^') {
198                                         constraint = "^" + constraint;
199                                         len++;
200                                 }
201
202                                 if (constraint [len - 1] != '$')
203                                         constraint += "$";
204                         }
205
206                         return Regex.Match (value, constraint).Success;
207                 }
208                 
209                 protected virtual bool ProcessConstraint (HttpContextBase httpContext, object constraint, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
210                 {
211                         if (parameterName == null)
212                                 throw new ArgumentNullException ("parameterName");
213
214                         // .NET "compatibility"
215                         if (values == null)
216                                 throw new NullReferenceException ();
217
218                         RequestContext reqContext;
219 #if NET_4_0
220                         reqContext = SafeGetContext (httpContext != null ? httpContext.Request : null);
221 #else
222                         reqContext = null;
223 #endif
224                         bool invalidConstraint;
225                         bool ret = ProcessConstraintInternal (httpContext, this, constraint, parameterName, values, routeDirection, reqContext, out invalidConstraint);
226                         
227                         if (invalidConstraint)
228                                 throw new InvalidOperationException (
229                                         String.Format (
230                                                 "Constraint parameter '{0}' on the route with URL '{1}' must have a string value type or be a type which implements IRouteConstraint",
231                                                 parameterName, Url
232                                         )
233                                 );
234
235                         return ret;
236                 }
237 #if NET_4_0
238                 RequestContext SafeGetContext (HttpRequestBase req)
239                 {
240                         if (req == null || req.GetType () != httpRequestBaseType)
241                                 return null;
242                                 
243                         return req.RequestContext;
244                 }
245 #endif
246         }
247 }