| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 |
- //
- // RouteCollection.cs
- //
- // Author:
- // Atsushi Enomoto <[email protected]>
- //
- // Copyright (C) 2008 Novell Inc. http://novell.com
- //
- //
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- //
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.IO;
- using System.Runtime.CompilerServices;
- using System.Security.Permissions;
- using System.Web;
- using System.Web.Hosting;
- namespace System.Web.Routing
- {
- [TypeForwardedFrom ("System.Web.Routing, Version=3.5.0.0, Culture=Neutral, PublicKeyToken=31bf3856ad364e35")]
- [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
- [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
- public class RouteCollection : Collection<RouteBase>
- {
- class Lock : IDisposable
- {
- //RouteCollection owner;
- //bool read;
- public Lock (RouteCollection owner, bool read)
- {
- //this.owner = owner;
- //this.read = read;
- }
- public void Dispose ()
- {
- //if (read)
- // owner.read_lock = null;
- //else
- // owner_write_lock = null;
- }
- }
- public RouteCollection ()
- : this (null)
- {
- }
- public RouteCollection (VirtualPathProvider virtualPathProvider)
- {
- // null argument is allowed
- //provider = virtualPathProvider;
- read_lock = new Lock (this, true);
- write_lock = new Lock (this, false);
- }
- //VirtualPathProvider provider;
- Dictionary<string,RouteBase> d = new Dictionary<string,RouteBase> ();
- Lock read_lock, write_lock;
- public RouteBase this [string name] {
- get {
- foreach (var p in d)
- if (p.Key == name)
- return p.Value;
- return null;
- }
- }
- public bool LowercaseUrls { get; set; }
- public bool AppendTrailingSlash { get; set; }
- public bool RouteExistingFiles { get; set; }
- public void Add (string name, RouteBase item)
- {
- lock (GetWriteLock ()) {
- base.Add (item);
- if (!String.IsNullOrEmpty (name))
- d.Add (name, item);
- }
- }
- protected override void ClearItems ()
- {
- lock (GetWriteLock ())
- base.ClearItems ();
- }
- public IDisposable GetReadLock ()
- {
- return read_lock;
- }
- public RouteData GetRouteData (HttpContextBase httpContext)
- {
- if (httpContext == null)
- throw new ArgumentNullException ("httpContext");
- if (httpContext.Request == null)
- throw new ArgumentException ("The context does not contain any request data.", "httpContext");
- if (Count == 0)
- return null;
- if (!RouteExistingFiles) {
- var path = httpContext.Request.AppRelativeCurrentExecutionFilePath;
- VirtualPathProvider vpp = HostingEnvironment.VirtualPathProvider;
- if (path != "~/" && vpp != null && (vpp.FileExists (path) || vpp.DirectoryExists (path)))
- return null;
- }
- foreach (RouteBase rb in this) {
- var rd = rb.GetRouteData (httpContext);
- if (rd != null)
- return rd;
- }
- return null;
- }
- public VirtualPathData GetVirtualPath (RequestContext requestContext, RouteValueDictionary values)
- {
- return GetVirtualPath (requestContext, null, values);
- }
- public VirtualPathData GetVirtualPath (RequestContext requestContext, string name, RouteValueDictionary values)
- {
- if (requestContext == null)
- throw new ArgumentNullException ("httpContext");
- VirtualPathData vp = null;
- if (!String.IsNullOrEmpty (name)) {
- RouteBase rb = this [name];
- if (rb != null)
- vp = rb.GetVirtualPath (requestContext, values);
- else
- throw new ArgumentException ("A route named '" + name + "' could not be found in the route collection.", "name");
- } else {
- foreach (RouteBase rb in this) {
- vp = rb.GetVirtualPath (requestContext, values);
- if (vp != null)
- break;
- }
- }
- if (vp != null) {
- string appPath = requestContext.HttpContext.Request.ApplicationPath;
- if (appPath != null && (appPath.Length == 0 || !appPath.EndsWith ("/", StringComparison.Ordinal)))
- appPath += "/";
-
- string pathWithApp = String.Concat (appPath, vp.VirtualPath);
- vp.VirtualPath = requestContext.HttpContext.Response.ApplyAppPathModifier (pathWithApp);
- return vp;
- }
- return null;
- }
- public IDisposable GetWriteLock ()
- {
- return write_lock;
- }
- public void Ignore (string url)
- {
- Ignore (url, null);
- }
- public void Ignore (string url, object constraints)
- {
- if (url == null)
- throw new ArgumentNullException ("url");
- Add (new Route (url, null, new RouteValueDictionary (constraints), new StopRoutingHandler ()));
- }
-
- public Route MapPageRoute (string routeName, string routeUrl, string physicalFile)
- {
- return MapPageRoute (routeName, routeUrl, physicalFile, true, null, null, null);
- }
- public Route MapPageRoute (string routeName, string routeUrl, string physicalFile, bool checkPhysicalUrlAccess)
- {
- return MapPageRoute (routeName, routeUrl, physicalFile, checkPhysicalUrlAccess, null, null, null);
- }
- public Route MapPageRoute (string routeName, string routeUrl, string physicalFile, bool checkPhysicalUrlAccess,
- RouteValueDictionary defaults)
- {
- return MapPageRoute (routeName, routeUrl, physicalFile, checkPhysicalUrlAccess, defaults, null, null);
- }
- public Route MapPageRoute (string routeName, string routeUrl, string physicalFile, bool checkPhysicalUrlAccess,
- RouteValueDictionary defaults, RouteValueDictionary constraints)
- {
- return MapPageRoute (routeName, routeUrl, physicalFile, checkPhysicalUrlAccess, defaults, constraints, null);
- }
- public Route MapPageRoute (string routeName, string routeUrl, string physicalFile, bool checkPhysicalUrlAccess,
- RouteValueDictionary defaults, RouteValueDictionary constraints, RouteValueDictionary dataTokens)
- {
- if (routeUrl == null)
- throw new ArgumentNullException ("routeUrl");
-
- var route = new Route (routeUrl, defaults, constraints, dataTokens, new PageRouteHandler (physicalFile, checkPhysicalUrlAccess));
- Add (routeName, route);
- return route;
- }
- protected override void InsertItem (int index, RouteBase item)
- {
- // FIXME: what happens wrt its name?
- lock (GetWriteLock ())
- base.InsertItem (index, item);
- }
- protected override void RemoveItem (int index)
- {
- // FIXME: what happens wrt its name?
- lock (GetWriteLock ()) {
- string k = GetKey (index);
- base.RemoveItem (index);
- if (k != null)
- d.Remove (k);
- }
- }
- protected override void SetItem (int index, RouteBase item)
- {
- // FIXME: what happens wrt its name?
- lock (GetWriteLock ()) {
- string k = GetKey (index);
- base.SetItem (index, item);
- if (k != null)
- d.Remove (k);
- }
- }
- string GetKey (int index)
- {
- var item = this [index];
- foreach (var p in d)
- if (p.Value == item)
- return p.Key;
- return null;
- }
- }
- }
|