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