2009-06-12 Bill Holmes <billholmes54@gmail.com>
[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.Security.Permissions;
36 using System.Web;
37 using System.Web.Hosting;
38
39 namespace System.Web.Routing
40 {
41         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
42         [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
43         public class RouteCollection : Collection<RouteBase>
44         {
45                 class Lock : IDisposable
46                 {
47                         RouteCollection owner;
48                         bool read;
49
50                         public Lock (RouteCollection owner, bool read)
51                         {
52                                 this.owner = owner;
53                                 this.read = read;
54                         }
55
56                         public void Dispose ()
57                         {
58                                 //if (read)
59                                 //      owner.read_lock = null;
60                                 //else
61                                 //      owner_write_lock = null;
62                         }
63                 }
64
65                 public RouteCollection ()
66                         : this (null)
67                 {
68                 }
69
70                 public RouteCollection (VirtualPathProvider virtualPathProvider)
71                 {
72                         // null argument is allowed
73                         provider = virtualPathProvider;
74
75                         read_lock = new Lock (this, true);
76                         write_lock = new Lock (this, false);
77                 }
78
79                 VirtualPathProvider provider;
80                 Dictionary<string,RouteBase> d = new Dictionary<string,RouteBase> ();
81
82                 Lock read_lock, write_lock;
83
84                 public RouteBase this [string name] {
85                         get {
86                                 foreach (var p in d)
87                                         if (p.Key == name)
88                                                 return p.Value;
89                                 return null;
90                         }
91                 }
92
93                 public bool RouteExistingFiles { get; set; }
94
95                 public void Add (string name, RouteBase item)
96                 {
97                         lock (GetWriteLock ()) {
98                                 base.Add (item);
99                                 if (!String.IsNullOrEmpty (name))
100                                         d.Add (name, item);
101                         }
102                 }
103
104                 protected override void ClearItems ()
105                 {
106                         lock (GetWriteLock ())
107                                 base.ClearItems ();
108                 }
109
110                 public IDisposable GetReadLock ()
111                 {
112                         return read_lock;
113                 }
114
115                 public RouteData GetRouteData (HttpContextBase httpContext)
116                 {
117                         if (httpContext == null)
118                                 throw new ArgumentNullException ("httpContext");
119
120                         if (!RouteExistingFiles) {
121                                 var path = httpContext.Request.AppRelativeCurrentExecutionFilePath;
122                                 VirtualPathProvider vpp = HostingEnvironment.VirtualPathProvider;
123                                 if (path != "~/" && vpp != null && (vpp.FileExists (path) || vpp.DirectoryExists (path)))
124                                         return null;
125                         }
126                         
127                         if (Count == 0)
128                                 return null;
129
130                         foreach (RouteBase rb in this) {
131                                 var rd = rb.GetRouteData (httpContext);
132                                 if (rd != null)
133                                         return rd;
134                         }
135
136                         return null;
137                 }
138
139                 public VirtualPathData GetVirtualPath (RequestContext requestContext, RouteValueDictionary values)
140                 {
141                         return GetVirtualPath (requestContext, null, values);
142                 }
143
144                 public VirtualPathData GetVirtualPath (RequestContext requestContext, string name, RouteValueDictionary values)
145                 {
146                         if (requestContext == null)
147                                 throw new ArgumentNullException ("httpContext");
148
149                         if (Count == 0)
150                                 return null;
151
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                                 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                                 var pathWithApp = String.Concat (requestContext.HttpContext.Request.ApplicationPath, "/", vp.VirtualPath);
167                                 vp.VirtualPath = requestContext.HttpContext.Response.ApplyAppPathModifier (pathWithApp);
168                                 return vp;
169                         }
170
171                         return null;
172                 }
173
174                 public IDisposable GetWriteLock ()
175                 {
176                         return write_lock;
177                 }
178
179                 protected override void InsertItem (int index, RouteBase item)
180                 {
181                         // FIXME: what happens wrt its name?
182                         lock (GetWriteLock ())
183                                 base.InsertItem (index, item);
184                 }
185
186                 protected override void RemoveItem (int index)
187                 {
188                         // FIXME: what happens wrt its name?
189                         lock (GetWriteLock ()) {
190                                 string k = GetKey (index);
191                                 base.RemoveItem (index);
192                                 if (k != null)
193                                         d.Remove (k);
194                         }
195                 }
196
197                 protected override void SetItem (int index, RouteBase item)
198                 {
199                         // FIXME: what happens wrt its name?
200                         lock (GetWriteLock ()) {
201                                 string k = GetKey (index);
202                                 base.SetItem (index, item);
203                                 if (k != null)
204                                         d.Remove (k);
205                         }
206                 }
207
208                 string GetKey (int index)
209                 {
210                         var item = this [index];
211                         foreach (var p in d)
212                                 if (p.Value == item)
213                                         return p.Key;
214                         return null;
215                 }
216         }
217 }