RouteCollection.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. //
  2. // RouteCollection.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2008 Novell Inc. http://novell.com
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Collections;
  31. using System.Collections.Generic;
  32. using System.Collections.ObjectModel;
  33. using System.IO;
  34. using System.Runtime.CompilerServices;
  35. using System.Security.Permissions;
  36. using System.Web;
  37. using System.Web.Hosting;
  38. namespace System.Web.Routing
  39. {
  40. [TypeForwardedFrom ("System.Web.Routing, Version=3.5.0.0, Culture=Neutral, PublicKeyToken=31bf3856ad364e35")]
  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. public Lock (RouteCollection owner, bool read)
  50. {
  51. //this.owner = owner;
  52. //this.read = read;
  53. }
  54. public void Dispose ()
  55. {
  56. //if (read)
  57. // owner.read_lock = null;
  58. //else
  59. // owner_write_lock = null;
  60. }
  61. }
  62. public RouteCollection ()
  63. : this (null)
  64. {
  65. }
  66. public RouteCollection (VirtualPathProvider virtualPathProvider)
  67. {
  68. // null argument is allowed
  69. //provider = virtualPathProvider;
  70. read_lock = new Lock (this, true);
  71. write_lock = new Lock (this, false);
  72. }
  73. //VirtualPathProvider provider;
  74. Dictionary<string,RouteBase> d = new Dictionary<string,RouteBase> ();
  75. Lock read_lock, write_lock;
  76. public RouteBase this [string name] {
  77. get {
  78. foreach (var p in d)
  79. if (p.Key == name)
  80. return p.Value;
  81. return null;
  82. }
  83. }
  84. public bool LowercaseUrls { get; set; }
  85. public bool AppendTrailingSlash { get; set; }
  86. public bool RouteExistingFiles { get; set; }
  87. public void Add (string name, RouteBase item)
  88. {
  89. lock (GetWriteLock ()) {
  90. base.Add (item);
  91. if (!String.IsNullOrEmpty (name))
  92. d.Add (name, item);
  93. }
  94. }
  95. protected override void ClearItems ()
  96. {
  97. lock (GetWriteLock ())
  98. base.ClearItems ();
  99. }
  100. public IDisposable GetReadLock ()
  101. {
  102. return read_lock;
  103. }
  104. public RouteData GetRouteData (HttpContextBase httpContext)
  105. {
  106. if (httpContext == null)
  107. throw new ArgumentNullException ("httpContext");
  108. if (httpContext.Request == null)
  109. throw new ArgumentException ("The context does not contain any request data.", "httpContext");
  110. if (Count == 0)
  111. return null;
  112. if (!RouteExistingFiles) {
  113. var path = httpContext.Request.AppRelativeCurrentExecutionFilePath;
  114. VirtualPathProvider vpp = HostingEnvironment.VirtualPathProvider;
  115. if (path != "~/" && vpp != null && (vpp.FileExists (path) || vpp.DirectoryExists (path)))
  116. return null;
  117. }
  118. foreach (RouteBase rb in this) {
  119. var rd = rb.GetRouteData (httpContext);
  120. if (rd != null)
  121. return rd;
  122. }
  123. return null;
  124. }
  125. public VirtualPathData GetVirtualPath (RequestContext requestContext, RouteValueDictionary values)
  126. {
  127. return GetVirtualPath (requestContext, null, values);
  128. }
  129. public VirtualPathData GetVirtualPath (RequestContext requestContext, string name, RouteValueDictionary values)
  130. {
  131. if (requestContext == null)
  132. throw new ArgumentNullException ("httpContext");
  133. VirtualPathData vp = null;
  134. if (!String.IsNullOrEmpty (name)) {
  135. RouteBase rb = this [name];
  136. if (rb != null)
  137. vp = rb.GetVirtualPath (requestContext, values);
  138. else
  139. throw new ArgumentException ("A route named '" + name + "' could not be found in the route collection.", "name");
  140. } else {
  141. foreach (RouteBase rb in this) {
  142. vp = rb.GetVirtualPath (requestContext, values);
  143. if (vp != null)
  144. break;
  145. }
  146. }
  147. if (vp != null) {
  148. string appPath = requestContext.HttpContext.Request.ApplicationPath;
  149. if (appPath != null && (appPath.Length == 0 || !appPath.EndsWith ("/", StringComparison.Ordinal)))
  150. appPath += "/";
  151. string pathWithApp = String.Concat (appPath, vp.VirtualPath);
  152. vp.VirtualPath = requestContext.HttpContext.Response.ApplyAppPathModifier (pathWithApp);
  153. return vp;
  154. }
  155. return null;
  156. }
  157. public IDisposable GetWriteLock ()
  158. {
  159. return write_lock;
  160. }
  161. public void Ignore (string url)
  162. {
  163. Ignore (url, null);
  164. }
  165. public void Ignore (string url, object constraints)
  166. {
  167. if (url == null)
  168. throw new ArgumentNullException ("url");
  169. Add (new Route (url, null, new RouteValueDictionary (constraints), new StopRoutingHandler ()));
  170. }
  171. public Route MapPageRoute (string routeName, string routeUrl, string physicalFile)
  172. {
  173. return MapPageRoute (routeName, routeUrl, physicalFile, true, null, null, null);
  174. }
  175. public Route MapPageRoute (string routeName, string routeUrl, string physicalFile, bool checkPhysicalUrlAccess)
  176. {
  177. return MapPageRoute (routeName, routeUrl, physicalFile, checkPhysicalUrlAccess, null, null, null);
  178. }
  179. public Route MapPageRoute (string routeName, string routeUrl, string physicalFile, bool checkPhysicalUrlAccess,
  180. RouteValueDictionary defaults)
  181. {
  182. return MapPageRoute (routeName, routeUrl, physicalFile, checkPhysicalUrlAccess, defaults, null, null);
  183. }
  184. public Route MapPageRoute (string routeName, string routeUrl, string physicalFile, bool checkPhysicalUrlAccess,
  185. RouteValueDictionary defaults, RouteValueDictionary constraints)
  186. {
  187. return MapPageRoute (routeName, routeUrl, physicalFile, checkPhysicalUrlAccess, defaults, constraints, null);
  188. }
  189. public Route MapPageRoute (string routeName, string routeUrl, string physicalFile, bool checkPhysicalUrlAccess,
  190. RouteValueDictionary defaults, RouteValueDictionary constraints, RouteValueDictionary dataTokens)
  191. {
  192. if (routeUrl == null)
  193. throw new ArgumentNullException ("routeUrl");
  194. var route = new Route (routeUrl, defaults, constraints, dataTokens, new PageRouteHandler (physicalFile, checkPhysicalUrlAccess));
  195. Add (routeName, route);
  196. return route;
  197. }
  198. protected override void InsertItem (int index, RouteBase item)
  199. {
  200. // FIXME: what happens wrt its name?
  201. lock (GetWriteLock ())
  202. base.InsertItem (index, item);
  203. }
  204. protected override void RemoveItem (int index)
  205. {
  206. // FIXME: what happens wrt its name?
  207. lock (GetWriteLock ()) {
  208. string k = GetKey (index);
  209. base.RemoveItem (index);
  210. if (k != null)
  211. d.Remove (k);
  212. }
  213. }
  214. protected override void SetItem (int index, RouteBase item)
  215. {
  216. // FIXME: what happens wrt its name?
  217. lock (GetWriteLock ()) {
  218. string k = GetKey (index);
  219. base.SetItem (index, item);
  220. if (k != null)
  221. d.Remove (k);
  222. }
  223. }
  224. string GetKey (int index)
  225. {
  226. var item = this [index];
  227. foreach (var p in d)
  228. if (p.Value == item)
  229. return p.Key;
  230. return null;
  231. }
  232. }
  233. }