[runtime] Fixed get_process_module module name.
[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         [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 RouteExistingFiles { get; set; }
96
97                 public void Add (string name, RouteBase item)
98                 {
99                         lock (GetWriteLock ()) {
100                                 base.Add (item);
101                                 if (!String.IsNullOrEmpty (name))
102                                         d.Add (name, item);
103                         }
104                 }
105
106                 protected override void ClearItems ()
107                 {
108                         lock (GetWriteLock ())
109                                 base.ClearItems ();
110                 }
111
112                 public IDisposable GetReadLock ()
113                 {
114                         return read_lock;
115                 }
116
117                 public RouteData GetRouteData (HttpContextBase httpContext)
118                 {
119                         if (httpContext == null)
120                                 throw new ArgumentNullException ("httpContext");
121
122                         if (httpContext.Request == null)
123                                 throw new ArgumentException ("The context does not contain any request data.", "httpContext");
124                         if (Count == 0)
125                                 return null;
126                         if (!RouteExistingFiles) {
127                                 var path = httpContext.Request.AppRelativeCurrentExecutionFilePath;
128                                 VirtualPathProvider vpp = HostingEnvironment.VirtualPathProvider;
129                                 if (path != "~/" && vpp != null && (vpp.FileExists (path) || vpp.DirectoryExists (path)))
130                                         return null;
131                         }
132                         foreach (RouteBase rb in this) {
133                                 var rd = rb.GetRouteData (httpContext);
134                                 if (rd != null)
135                                         return rd;
136                         }
137
138                         return null;
139                 }
140
141                 public VirtualPathData GetVirtualPath (RequestContext requestContext, RouteValueDictionary values)
142                 {
143                         return GetVirtualPath (requestContext, null, values);
144                 }
145
146                 public VirtualPathData GetVirtualPath (RequestContext requestContext, string name, RouteValueDictionary values)
147                 {
148                         if (requestContext == null)
149                                 throw new ArgumentNullException ("httpContext");
150                         VirtualPathData vp = null;
151                         if (!String.IsNullOrEmpty (name)) {
152                                 RouteBase rb = this [name];
153                                 if (rb != null)
154                                         vp = rb.GetVirtualPath (requestContext, values);
155                                 else
156                                         throw new ArgumentException ("A route named '" + name + "' could not be found in the route collection.", "name");
157                         } else {
158                                 foreach (RouteBase rb in this) {
159                                         vp = rb.GetVirtualPath (requestContext, values);
160                                         if (vp != null)
161                                                 break;
162                                 }
163                         }
164
165                         if (vp != null) {
166                                 string appPath = requestContext.HttpContext.Request.ApplicationPath;
167                                 if (appPath != null && (appPath.Length == 0 || !appPath.EndsWith ("/", StringComparison.Ordinal)))
168                                         appPath += "/";
169                                 
170                                 string pathWithApp = String.Concat (appPath, vp.VirtualPath);
171                                 vp.VirtualPath = requestContext.HttpContext.Response.ApplyAppPathModifier (pathWithApp);
172                                 return vp;
173                         }
174
175                         return null;
176                 }
177
178                 public IDisposable GetWriteLock ()
179                 {
180                         return write_lock;
181                 }
182                 public void Ignore (string url)
183                 {
184                         Ignore (url, null);
185                 }
186
187                 public void Ignore (string url, object constraints)
188                 {
189                         if (url == null)
190                                 throw new ArgumentNullException ("url");
191
192                         Add (new Route (url, null, new RouteValueDictionary (constraints), new StopRoutingHandler ()));
193                 }
194                 
195                 public Route MapPageRoute (string routeName, string routeUrl, string physicalFile)
196                 {
197                         return MapPageRoute (routeName, routeUrl, physicalFile, true, null, null, null);
198                 }
199
200                 public Route MapPageRoute (string routeName, string routeUrl, string physicalFile, bool checkPhysicalUrlAccess)
201                 {
202                         return MapPageRoute (routeName, routeUrl, physicalFile, checkPhysicalUrlAccess, null, null, null);
203                 }
204
205                 public Route MapPageRoute (string routeName, string routeUrl, string physicalFile, bool checkPhysicalUrlAccess,
206                                            RouteValueDictionary defaults)
207                 {
208                         return MapPageRoute (routeName, routeUrl, physicalFile, checkPhysicalUrlAccess, defaults, null, null);
209                 }
210
211                 public Route MapPageRoute (string routeName, string routeUrl, string physicalFile, bool checkPhysicalUrlAccess,
212                                            RouteValueDictionary defaults, RouteValueDictionary constraints)
213                 {
214                         return MapPageRoute (routeName, routeUrl, physicalFile, checkPhysicalUrlAccess, defaults, constraints, null);
215                 }
216
217                 public Route MapPageRoute (string routeName, string routeUrl, string physicalFile, bool checkPhysicalUrlAccess,
218                                            RouteValueDictionary defaults, RouteValueDictionary constraints, RouteValueDictionary dataTokens)
219                 {
220                         if (routeUrl == null)
221                                 throw new ArgumentNullException ("routeUrl");
222                         
223                         var route = new Route (routeUrl, defaults, constraints, dataTokens, new PageRouteHandler (physicalFile, checkPhysicalUrlAccess));
224                         Add (routeName, route);
225
226                         return route;
227                 }
228                 protected override void InsertItem (int index, RouteBase item)
229                 {
230                         // FIXME: what happens wrt its name?
231                         lock (GetWriteLock ())
232                                 base.InsertItem (index, item);
233                 }
234
235                 protected override void RemoveItem (int index)
236                 {
237                         // FIXME: what happens wrt its name?
238                         lock (GetWriteLock ()) {
239                                 string k = GetKey (index);
240                                 base.RemoveItem (index);
241                                 if (k != null)
242                                         d.Remove (k);
243                         }
244                 }
245
246                 protected override void SetItem (int index, RouteBase item)
247                 {
248                         // FIXME: what happens wrt its name?
249                         lock (GetWriteLock ()) {
250                                 string k = GetKey (index);
251                                 base.SetItem (index, item);
252                                 if (k != null)
253                                         d.Remove (k);
254                         }
255                 }
256
257                 string GetKey (int index)
258                 {
259                         var item = this [index];
260                         foreach (var p in d)
261                                 if (p.Value == item)
262                                         return p.Key;
263                         return null;
264                 }
265         }
266 }