5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2008 Novell Inc. http://novell.com
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
31 using System.Collections;
32 using System.Collections.Generic;
33 using System.Collections.ObjectModel;
35 using System.Security.Permissions;
37 using System.Web.Hosting;
39 namespace System.Web.Routing
41 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
42 [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
43 public class RouteCollection : Collection<RouteBase>
45 class Lock : IDisposable
47 RouteCollection owner;
50 public Lock (RouteCollection owner, bool read)
56 public void Dispose ()
59 // owner.read_lock = null;
61 // owner_write_lock = null;
65 public RouteCollection ()
70 public RouteCollection (VirtualPathProvider virtualPathProvider)
72 // null argument is allowed
73 provider = virtualPathProvider;
75 read_lock = new Lock (this, true);
76 write_lock = new Lock (this, false);
79 VirtualPathProvider provider;
80 Dictionary<string,RouteBase> d = new Dictionary<string,RouteBase> ();
82 Lock read_lock, write_lock;
84 public RouteBase this [string name] {
93 public bool RouteExistingFiles { get; set; }
95 public void Add (string name, RouteBase item)
97 lock (GetWriteLock ()) {
99 if (!String.IsNullOrEmpty (name))
104 protected override void ClearItems ()
106 lock (GetWriteLock ())
110 public IDisposable GetReadLock ()
115 public RouteData GetRouteData (HttpContextBase httpContext)
117 if (httpContext == null)
118 throw new ArgumentNullException ("httpContext");
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)))
130 foreach (RouteBase rb in this) {
131 var rd = rb.GetRouteData (httpContext);
139 public VirtualPathData GetVirtualPath (RequestContext requestContext, RouteValueDictionary values)
141 return GetVirtualPath (requestContext, null, values);
144 public VirtualPathData GetVirtualPath (RequestContext requestContext, string name, RouteValueDictionary values)
146 if (requestContext == null)
147 throw new ArgumentNullException ("httpContext");
152 VirtualPathData vp = null;
153 if (!String.IsNullOrEmpty (name)) {
154 RouteBase rb = this [name];
156 vp = rb.GetVirtualPath (requestContext, values);
158 foreach (RouteBase rb in this) {
159 vp = rb.GetVirtualPath (requestContext, values);
166 var pathWithApp = String.Concat (requestContext.HttpContext.Request.ApplicationPath, "/", vp.VirtualPath);
167 vp.VirtualPath = requestContext.HttpContext.Response.ApplyAppPathModifier (pathWithApp);
174 public IDisposable GetWriteLock ()
179 protected override void InsertItem (int index, RouteBase item)
181 // FIXME: what happens wrt its name?
182 lock (GetWriteLock ())
183 base.InsertItem (index, item);
186 protected override void RemoveItem (int index)
188 // FIXME: what happens wrt its name?
189 lock (GetWriteLock ()) {
190 string k = GetKey (index);
191 base.RemoveItem (index);
197 protected override void SetItem (int index, RouteBase item)
199 // FIXME: what happens wrt its name?
200 lock (GetWriteLock ()) {
201 string k = GetKey (index);
202 base.SetItem (index, item);
208 string GetKey (int index)
210 var item = this [index];