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