VirtualPath.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. //
  2. // System.Web.VirtualPath.cs
  3. //
  4. // Authors:
  5. // Marek Habersack ([email protected])
  6. //
  7. // (C) 2008-2009 Novell, Inc
  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.IO;
  30. using System.Web.Compilation;
  31. using System.Web.Util;
  32. namespace System.Web
  33. {
  34. internal class VirtualPath : IDisposable
  35. {
  36. string _absolute;
  37. string _appRelative;
  38. string _appRelativeNotRooted;
  39. string _extension;
  40. string _directory;
  41. string _directoryNoNormalize;
  42. string _currentRequestDirectory;
  43. string _physicalPath;
  44. public bool IsAbsolute {
  45. get;
  46. private set;
  47. }
  48. public bool IsFake {
  49. get;
  50. private set;
  51. }
  52. public bool IsRooted {
  53. get;
  54. private set;
  55. }
  56. public bool IsAppRelative {
  57. get;
  58. private set;
  59. }
  60. public string Original {
  61. get;
  62. private set;
  63. }
  64. public string Absolute {
  65. get {
  66. if (IsAbsolute)
  67. return Original;
  68. if (_absolute == null) {
  69. string original = Original;
  70. if (!VirtualPathUtility.IsRooted (original))
  71. _absolute = MakeRooted (original);
  72. else
  73. _absolute = original;
  74. if (VirtualPathUtility.IsAppRelative (_absolute))
  75. _absolute = VirtualPathUtility.ToAbsolute (_absolute);
  76. }
  77. return _absolute;
  78. }
  79. }
  80. public string AppRelative {
  81. get {
  82. if (IsAppRelative)
  83. return Original;
  84. if (_appRelative == null) {
  85. string original = Original;
  86. if (!VirtualPathUtility.IsRooted (original))
  87. _appRelative = MakeRooted (original);
  88. else
  89. _appRelative = original;
  90. if (VirtualPathUtility.IsAbsolute (_appRelative))
  91. _appRelative = VirtualPathUtility.ToAppRelative (_appRelative);
  92. }
  93. return _appRelative;
  94. }
  95. }
  96. public string AppRelativeNotRooted {
  97. get {
  98. if (_appRelativeNotRooted == null)
  99. _appRelativeNotRooted = AppRelative.Substring (2);
  100. return _appRelativeNotRooted;
  101. }
  102. }
  103. public string Extension {
  104. get {
  105. if (_extension == null)
  106. _extension = VirtualPathUtility.GetExtension (Original);
  107. return _extension;
  108. }
  109. }
  110. public string Directory {
  111. get {
  112. if (_directory == null)
  113. _directory = VirtualPathUtility.GetDirectory (Absolute);
  114. return _directory;
  115. }
  116. }
  117. public string DirectoryNoNormalize {
  118. get {
  119. if (_directoryNoNormalize == null)
  120. _directoryNoNormalize = VirtualPathUtility.GetDirectory (Absolute, false);
  121. return _directoryNoNormalize;
  122. }
  123. }
  124. public string CurrentRequestDirectory {
  125. get {
  126. if (_currentRequestDirectory != null)
  127. return _currentRequestDirectory;
  128. HttpContext ctx = HttpContext.Current;
  129. HttpRequest req = ctx != null ? ctx.Request : null;
  130. if (req != null)
  131. return VirtualPathUtility.GetDirectory (req.CurrentExecutionFilePath);
  132. return null;
  133. }
  134. set { _currentRequestDirectory = value; }
  135. }
  136. public string PhysicalPath {
  137. get {
  138. if (_physicalPath != null)
  139. return _physicalPath;
  140. HttpContext ctx = HttpContext.Current;
  141. HttpRequest req = ctx != null ? ctx.Request : null;
  142. if (req != null)
  143. _physicalPath = req.MapPath (Absolute);
  144. else
  145. return null;
  146. return _physicalPath;
  147. }
  148. }
  149. public VirtualPath (string vpath)
  150. : this (vpath, null, false)
  151. {
  152. }
  153. public VirtualPath (string vpath, string baseVirtualDir)
  154. : this (vpath, null, false)
  155. {
  156. CurrentRequestDirectory = baseVirtualDir;
  157. }
  158. public VirtualPath (string vpath, string physicalPath, bool isFake)
  159. {
  160. IsRooted = VirtualPathUtility.IsRooted (vpath);
  161. IsAbsolute = VirtualPathUtility.IsAbsolute (vpath);
  162. IsAppRelative = VirtualPathUtility.IsAppRelative (vpath);
  163. if (isFake) {
  164. if (String.IsNullOrEmpty (physicalPath))
  165. throw new ArgumentException ("physicalPath");
  166. _physicalPath = physicalPath;
  167. Original = "~/" + Path.GetFileName (_physicalPath);
  168. IsFake = true;
  169. } else {
  170. Original = vpath;
  171. IsFake = false;
  172. }
  173. }
  174. public bool StartsWith (string s)
  175. {
  176. return StrUtils.StartsWith (Original, s);
  177. }
  178. // Assumes 'original' is NOT rooted
  179. string MakeRooted (string original)
  180. {
  181. string reqdir = CurrentRequestDirectory;
  182. if (!String.IsNullOrEmpty (reqdir))
  183. return VirtualPathUtility.Combine (reqdir, original);
  184. else
  185. return VirtualPathUtility.Combine (HttpRuntime.AppDomainAppVirtualPath, original);
  186. }
  187. public void Dispose ()
  188. {
  189. _absolute = null;
  190. _appRelative = null;
  191. _appRelativeNotRooted = null;
  192. _extension = null;
  193. _directory = null;
  194. }
  195. public override string ToString ()
  196. {
  197. string ret = Original;
  198. if (String.IsNullOrEmpty (ret))
  199. return GetType ().ToString ();
  200. if (IsFake)
  201. ret += " [fake: " + PhysicalPath + "]";
  202. return ret;
  203. }
  204. public static VirtualPath PhysicalToVirtual (string physical_path)
  205. {
  206. if (String.IsNullOrEmpty (physical_path))
  207. return null;
  208. string appPhysicalPath = HttpRuntime.AppDomainAppPath;
  209. if (!StrUtils.StartsWith (physical_path, appPhysicalPath))
  210. return null;
  211. string vp = physical_path.Substring (appPhysicalPath.Length - 1);
  212. if (vp [0] != '/')
  213. return null;
  214. return new VirtualPath (vp);
  215. }
  216. }
  217. }