2009-06-12 Bill Holmes <billholmes54@gmail.com>
[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.Security.Permissions;
32 using System.Text.RegularExpressions;
33 using System.Web;
34
35 namespace System.Web.Routing
36 {
37         [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
38         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
39         public class Route : RouteBase
40         {
41                 PatternParser url;
42
43                 public RouteValueDictionary Constraints { get; set; }
44
45                 public RouteValueDictionary DataTokens { get; set; }
46
47                 public RouteValueDictionary Defaults { get; set; }
48
49                 public IRouteHandler RouteHandler { get; set; }
50
51                 public string Url {
52                         get { return url != null ? url.Url : String.Empty; }
53                         set { url = value != null ? new PatternParser (value) : null; }
54                 }
55
56                 public Route (string url, IRouteHandler routeHandler)
57                         : this (url, null, routeHandler)
58                 {
59                 }
60
61                 public Route (string url, RouteValueDictionary defaults, IRouteHandler routeHandler)
62                         : this (url, defaults, null, routeHandler)
63                 {
64                 }
65
66                 public Route (string url, RouteValueDictionary defaults, RouteValueDictionary constraints, IRouteHandler routeHandler)
67                         : this (url, defaults, constraints, null, routeHandler)
68                 {
69                 }
70
71                 public Route (string url, RouteValueDictionary defaults, RouteValueDictionary constraints, RouteValueDictionary dataTokens, IRouteHandler routeHandler)
72                 {
73                         Url = url;
74                         Defaults = defaults;
75                         Constraints = constraints;
76                         DataTokens = dataTokens;
77                         RouteHandler = routeHandler;
78                 }
79
80                 public override RouteData GetRouteData (HttpContextBase httpContext)
81                 {
82                         var path = httpContext.Request.AppRelativeCurrentExecutionFilePath;
83                         var pathInfo = httpContext.Request.PathInfo;
84
85                         if (pathInfo != String.Empty)
86                                 throw new NotImplementedException ();
87
88                         // probably code like this causes ArgumentOutOfRangeException under .NET.
89                         // It somehow allows such path that is completely equivalent to the Url. Dunno why.
90                         if (Url != path && path.Substring (0, 2) != "~/")
91                                 return null;
92                         path = path.Substring (2);
93
94                         var values = url.Match (path, Defaults);
95                         if (values == null)
96                                 return null;
97
98                         if (Constraints != null)
99                                 foreach (var p in Constraints)
100                                         if (!ProcessConstraint (httpContext, p.Value, p.Key, values, RouteDirection.IncomingRequest))
101                                                 return null;
102
103                         var rd = new RouteData (this, RouteHandler);
104                         RouteValueDictionary rdValues = rd.Values;
105                         
106                         foreach (var p in values)
107                                 rdValues.Add (p.Key, p.Value);
108                         
109                         return rd;
110                 }
111
112                 public override VirtualPathData GetVirtualPath (RequestContext requestContext, RouteValueDictionary values)
113                 {
114                         if (requestContext == null)
115                                 throw new ArgumentNullException ("requestContext");
116                         if (url == null)
117                                 return new VirtualPathData (this, String.Empty);
118
119                         // null values is allowed.
120                         // if (values == null)
121                         //      values = requestContext.RouteData.Values;
122
123                         string s;
124                         if (!url.BuildUrl (this, requestContext, values, out s))
125                                 return null;
126
127                         return new VirtualPathData (this, s);
128                 }
129
130                 internal static bool ProcessConstraintInternal (HttpContextBase httpContext, Route route, object constraint, string parameterName,
131                                                                 RouteValueDictionary values, RouteDirection routeDirection, out bool invalidConstraint)
132                 {
133                         invalidConstraint = false;
134                         IRouteConstraint irc = constraint as IRouteConstraint;
135                         if (irc != null)
136                                 return irc.Match (httpContext, route, parameterName, values, routeDirection);
137
138                         string s = constraint as string;
139                         if (s != null) {
140                                 string v = values [parameterName] as string;
141                                 if (!String.IsNullOrEmpty (v))
142                                         return Regex.Match (v, s).Success;
143                                 return false;
144                         }
145
146                         invalidConstraint = true;
147                         return false;
148                 }
149                 
150                 protected virtual bool ProcessConstraint (HttpContextBase httpContext, object constraint, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
151                 {
152                         if (parameterName == null)
153                                 throw new ArgumentNullException ("parameterName");
154
155                         // .NET "compatibility"
156                         if (values == null)
157                                 throw new NullReferenceException ();
158                         
159                         bool invalidConstraint;
160                         bool ret = ProcessConstraintInternal (httpContext, this, constraint, parameterName, values, routeDirection, out invalidConstraint);
161                         
162                         if (invalidConstraint)
163                                 throw new InvalidOperationException (String.Format ("Constraint parameter '{0}' must be either a string or an IRouteConstraint instance", parameterName));
164
165                         return ret;
166                 }
167         }
168 }