2009-07-11 Michael Barker <mike@middlesoft.co.uk>
[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 (!RouteExistingFiles) {
125                                 var path = httpContext.Request.AppRelativeCurrentExecutionFilePath;
126                                 VirtualPathProvider vpp = HostingEnvironment.VirtualPathProvider;
127                                 if (path != "~/" && vpp != null && (vpp.FileExists (path) || vpp.DirectoryExists (path)))
128                                         return null;
129                         }
130                         
131                         if (Count == 0)
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
153                         if (Count == 0)
154                                 return null;
155
156                         VirtualPathData vp = null;
157                         if (!String.IsNullOrEmpty (name)) {
158                                 RouteBase rb = this [name];
159                                 if (rb != null)
160                                         vp = rb.GetVirtualPath (requestContext, values);
161                         } else {
162                                 foreach (RouteBase rb in this) {
163                                         vp = rb.GetVirtualPath (requestContext, values);
164                                         if (vp != null)
165                                                 break;
166                                 }
167                         }
168
169                         if (vp != null) {
170                                 var pathWithApp = String.Concat (requestContext.HttpContext.Request.ApplicationPath, "/", 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
183                 protected override void InsertItem (int index, RouteBase item)
184                 {
185                         // FIXME: what happens wrt its name?
186                         lock (GetWriteLock ())
187                                 base.InsertItem (index, item);
188                 }
189
190                 protected override void RemoveItem (int index)
191                 {
192                         // FIXME: what happens wrt its name?
193                         lock (GetWriteLock ()) {
194                                 string k = GetKey (index);
195                                 base.RemoveItem (index);
196                                 if (k != null)
197                                         d.Remove (k);
198                         }
199                 }
200
201                 protected override void SetItem (int index, RouteBase item)
202                 {
203                         // FIXME: what happens wrt its name?
204                         lock (GetWriteLock ()) {
205                                 string k = GetKey (index);
206                                 base.SetItem (index, item);
207                                 if (k != null)
208                                         d.Remove (k);
209                         }
210                 }
211
212                 string GetKey (int index)
213                 {
214                         var item = this [index];
215                         foreach (var p in d)
216                                 if (p.Value == item)
217                                         return p.Key;
218                         return null;
219                 }
220         }
221 }