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