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