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