7993e4428ff3f4c34aacf6fe915e4f1732d276b1
[mono.git] / mcs / class / System.Web.Routing / System.Web.Routing / RouteCollection.cs
1 //
2 // RouteCollection.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.Collections;
32 using System.Collections.Generic;
33 using System.Collections.ObjectModel;
34 using System.IO;
35 using System.Runtime.CompilerServices;
36 using System.Security.Permissions;
37 using System.Web;
38 using System.Web.Hosting;
39
40 namespace System.Web.Routing
41 {
42 #if NET_4_0
43         [TypeForwardedFrom ("System.Web.Routing, Version=3.5.0.0, Culture=Neutral, PublicKeyToken=31bf3856ad364e35")]
44 #endif
45         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
46         [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
47         public class RouteCollection : Collection<RouteBase>
48         {
49                 class Lock : IDisposable
50                 {
51                         //RouteCollection owner;
52                         //bool read;
53
54                         public Lock (RouteCollection owner, bool read)
55                         {
56                                 //this.owner = owner;
57                                 //this.read = read;
58                         }
59
60                         public void Dispose ()
61                         {
62                                 //if (read)
63                                 //      owner.read_lock = null;
64                                 //else
65                                 //      owner_write_lock = null;
66                         }
67                 }
68
69                 public RouteCollection ()
70                         : this (null)
71                 {
72                 }
73
74                 public RouteCollection (VirtualPathProvider virtualPathProvider)
75                 {
76                         // null argument is allowed
77                         //provider = virtualPathProvider;
78
79                         read_lock = new Lock (this, true);
80                         write_lock = new Lock (this, false);
81                 }
82
83                 //VirtualPathProvider provider;
84                 Dictionary<string,RouteBase> d = new Dictionary<string,RouteBase> ();
85
86                 Lock read_lock, write_lock;
87
88                 public RouteBase this [string name] {
89                         get {
90                                 foreach (var p in d)
91                                         if (p.Key == name)
92                                                 return p.Value;
93                                 return null;
94                         }
95                 }
96
97                 public bool RouteExistingFiles { get; set; }
98
99                 public void Add (string name, RouteBase item)
100                 {
101                         lock (GetWriteLock ()) {
102                                 base.Add (item);
103                                 if (!String.IsNullOrEmpty (name))
104                                         d.Add (name, item);
105                         }
106                 }
107
108                 protected override void ClearItems ()
109                 {
110                         lock (GetWriteLock ())
111                                 base.ClearItems ();
112                 }
113
114                 public IDisposable GetReadLock ()
115                 {
116                         return read_lock;
117                 }
118
119                 public RouteData GetRouteData (HttpContextBase httpContext)
120                 {
121                         if (httpContext == null)
122                                 throw new ArgumentNullException ("httpContext");
123
124                         if (httpContext.Request == null)
125                                 throw new ArgumentException ("The context does not contain any request data.", "httpContext");
126 #if NET_4_0
127                         if (Count == 0)
128                                 return null;
129 #endif                  
130                         if (!RouteExistingFiles) {
131                                 var path = httpContext.Request.AppRelativeCurrentExecutionFilePath;
132                                 VirtualPathProvider vpp = HostingEnvironment.VirtualPathProvider;
133                                 if (path != "~/" && vpp != null && (vpp.FileExists (path) || vpp.DirectoryExists (path)))
134                                         return null;
135                         }
136 #if !NET_4_0
137                         if (Count == 0)
138                                 return null;
139 #endif
140                         foreach (RouteBase rb in this) {
141                                 var rd = rb.GetRouteData (httpContext);
142                                 if (rd != null)
143                                         return rd;
144                         }
145
146                         return null;
147                 }
148
149                 public VirtualPathData GetVirtualPath (RequestContext requestContext, RouteValueDictionary values)
150                 {
151                         return GetVirtualPath (requestContext, null, values);
152                 }
153
154                 public VirtualPathData GetVirtualPath (RequestContext requestContext, string name, RouteValueDictionary values)
155                 {
156                         if (requestContext == null)
157                                 throw new ArgumentNullException ("httpContext");
158 #if !NET_4_0
159                         if (Count == 0)
160                                 return null;
161 #endif
162                         VirtualPathData vp = null;
163                         if (!String.IsNullOrEmpty (name)) {
164                                 RouteBase rb = this [name];
165                                 if (rb != null)
166                                         vp = rb.GetVirtualPath (requestContext, values);
167 #if NET_4_0
168                                 else
169                                         throw new ArgumentException ("A route named '" + name + "' could not be found in the route collection.", "name");
170 #endif
171                         } else {
172                                 foreach (RouteBase rb in this) {
173                                         vp = rb.GetVirtualPath (requestContext, values);
174                                         if (vp != null)
175                                                 break;
176                                 }
177                         }
178
179                         if (vp != null) {
180                                 string appPath = requestContext.HttpContext.Request.ApplicationPath;
181                                 if (appPath != null && (appPath.Length == 0 || !appPath.EndsWith ("/", StringComparison.Ordinal)))
182                                         appPath += "/";
183                                 
184                                 string pathWithApp = String.Concat (appPath, vp.VirtualPath);
185                                 vp.VirtualPath = requestContext.HttpContext.Response.ApplyAppPathModifier (pathWithApp);
186                                 return vp;
187                         }
188
189                         return null;
190                 }
191
192                 public IDisposable GetWriteLock ()
193                 {
194                         return write_lock;
195                 }
196 #if NET_4_0
197                 public void Ignore (string url)
198                 {
199                         Ignore (url, null);
200                 }
201
202                 public void Ignore (string url, object constraints)
203                 {
204                         if (url == null)
205                                 throw new ArgumentNullException ("url");
206
207                         Add (new Route (url, null, new RouteValueDictionary (constraints), new StopRoutingHandler ()));
208                 }
209                 
210                 public Route MapPageRoute (string routeName, string routeUrl, string physicalFile)
211                 {
212                         return MapPageRoute (routeName, routeUrl, physicalFile, true, null, null, null);
213                 }
214
215                 public Route MapPageRoute (string routeName, string routeUrl, string physicalFile, bool checkPhysicalUrlAccess)
216                 {
217                         return MapPageRoute (routeName, routeUrl, physicalFile, checkPhysicalUrlAccess, null, null, null);
218                 }
219
220                 public Route MapPageRoute (string routeName, string routeUrl, string physicalFile, bool checkPhysicalUrlAccess,
221                                            RouteValueDictionary defaults)
222                 {
223                         return MapPageRoute (routeName, routeUrl, physicalFile, checkPhysicalUrlAccess, defaults, null, null);
224                 }
225
226                 public Route MapPageRoute (string routeName, string routeUrl, string physicalFile, bool checkPhysicalUrlAccess,
227                                            RouteValueDictionary defaults, RouteValueDictionary constraints)
228                 {
229                         return MapPageRoute (routeName, routeUrl, physicalFile, checkPhysicalUrlAccess, defaults, constraints, null);
230                 }
231
232                 public Route MapPageRoute (string routeName, string routeUrl, string physicalFile, bool checkPhysicalUrlAccess,
233                                            RouteValueDictionary defaults, RouteValueDictionary constraints, RouteValueDictionary dataTokens)
234                 {
235                         if (routeUrl == null)
236                                 throw new ArgumentNullException ("routeUrl");
237                         
238                         var route = new Route (routeUrl, defaults, constraints, dataTokens, new PageRouteHandler (physicalFile, checkPhysicalUrlAccess));
239                         Add (routeName, route);
240
241                         return route;
242                 }
243 #endif
244                 protected override void InsertItem (int index, RouteBase item)
245                 {
246                         // FIXME: what happens wrt its name?
247                         lock (GetWriteLock ())
248                                 base.InsertItem (index, item);
249                 }
250
251                 protected override void RemoveItem (int index)
252                 {
253                         // FIXME: what happens wrt its name?
254                         lock (GetWriteLock ()) {
255                                 string k = GetKey (index);
256                                 base.RemoveItem (index);
257                                 if (k != null)
258                                         d.Remove (k);
259                         }
260                 }
261
262                 protected override void SetItem (int index, RouteBase item)
263                 {
264                         // FIXME: what happens wrt its name?
265                         lock (GetWriteLock ()) {
266                                 string k = GetKey (index);
267                                 base.SetItem (index, item);
268                                 if (k != null)
269                                         d.Remove (k);
270                         }
271                 }
272
273                 string GetKey (int index)
274                 {
275                         var item = this [index];
276                         foreach (var p in d)
277                                 if (p.Value == item)
278                                         return p.Key;
279                         return null;
280                 }
281         }
282 }