2005-09-22 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / class / System.Web / System.Web.Util / UrlUtils.cs
1 //
2 // System.Web.UrlUtils.cs 
3 //
4 // Authors:
5 //      Gonzalo Paniagua (gonzalo@ximian.com)
6 //      Jackson Harper   (jackson@ximian.com)
7 //
8
9 //
10 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using System.Web.SessionState;
33 namespace System.Web.Util {
34         
35         internal class UrlUtils {
36
37                 // appRoot + SessionID + vpath
38                 internal static string InsertSessionId (string id, string path)
39                 {
40                         string dir = GetDirectory (path);
41                         if (!dir.EndsWith ("/"))
42                                 dir += "/";
43
44                         string appvpath = HttpRuntime.AppDomainAppVirtualPath;
45                         if (!appvpath.EndsWith ("/"))
46                                 appvpath += "/";
47
48                         if (path.StartsWith (appvpath))
49                                 path = path.Substring (appvpath.Length);
50
51                         if (path [0] == '/')
52                                 path = path.Length > 1 ? path.Substring (1) : "";
53
54                         return Canonic (appvpath + "(" + id + ")/" + path);
55                 }
56
57                 internal static string GetSessionId (string path)
58                 {
59                         string appvpath = HttpRuntime.AppDomainAppVirtualPath;
60                         if (path.Length <= appvpath.Length)
61                                 return null;
62
63                         path = path.Substring (appvpath.Length);
64                         if (path.Length == 0 || path [0] != '/')
65                                 path = '/' + path;
66
67                         int len = path.Length;
68                         if ((len < SessionId.IdLength + 3) || (path [1] != '(') ||
69                             (path [SessionId.IdLength + 2] != ')'))
70                                 return null;
71
72                         return path.Substring (2, SessionId.IdLength);
73                 }
74
75                 internal static string RemoveSessionId (string base_path, string file_path)
76                 {
77                         // Caller did a GetSessionId first
78                         int idx = base_path.IndexOf ("/(");
79                         string dir = base_path.Substring (0, idx + 1);
80                         if (!dir.EndsWith ("/"))
81                                 dir += "/";
82
83                         idx = base_path.IndexOf (")/");
84                         if (idx != -1 && base_path.Length > idx + 2) {
85                                 string dir2 = base_path.Substring (idx + 2);
86                                 if (!dir2.EndsWith ("/"))
87                                         dir2 += "/";
88
89                                 dir += dir2;
90                         }
91
92                         return Canonic (dir + GetFile (file_path));
93                 }
94
95                 public static string Combine (string basePath, string relPath)
96                 {
97                         if (relPath == null)
98                                 throw new ArgumentNullException ("relPath");
99
100                         int rlength = relPath.Length;
101                         if (rlength == 0)
102                                 return "";
103
104                         relPath = relPath.Replace ("\\", "/");
105                         if (IsRooted (relPath))
106                                 return Canonic (relPath);
107
108                         char first = relPath [0];
109                         if (rlength < 3 || first == '~' || first == '/' || first == '\\') {
110                                 if (basePath == null || (basePath.Length == 1 && basePath [0] == '/'))
111                                         basePath = String.Empty;
112
113                                 string slash = (first == '/') ? "" : "/";
114                                 if (first == '~') {
115                                         if (rlength == 1) {
116                                                 relPath = "";
117                                         } else if (rlength > 1 && relPath [1] == '/') {
118                                                 relPath = relPath.Substring (2);
119                                                 slash = "/";
120                                         }
121
122                                         string appvpath = HttpRuntime.AppDomainAppVirtualPath;
123                                         if (appvpath.EndsWith ("/"))
124                                                 slash = "";
125
126                                         return Canonic (appvpath + slash + relPath);
127                                 }
128
129                                 return Canonic (basePath + slash + relPath);
130                         }
131
132                         if (basePath == null || basePath == "" || basePath [0] == '~')
133                                 basePath = HttpRuntime.AppDomainAppVirtualPath;
134
135                         if (basePath.Length <= 1)
136                                 basePath = String.Empty;
137
138                         return Canonic (basePath + "/" + relPath);
139                 }
140
141                 static char [] path_sep = {'\\', '/'};
142                 
143                 internal static string Canonic (string path)
144                 {
145                         string [] parts = path.Split (path_sep);
146                         int end = parts.Length;
147                         
148                         int dest = 0;
149                         
150                         for (int i = 0; i < end; i++) {
151                                 string current = parts [i];
152                                 if (current == "." )
153                                         continue;
154
155                                 if (current == "..") {
156                                         if (dest == 0) {
157                                                 if (i == 1) // see bug 52599
158                                                         continue;
159
160                                                 throw new HttpException ("Invalid path.");
161                                         }
162
163                                         dest --;
164                                         continue;
165                                 }
166
167                                 parts [dest++] = current;
168                         }
169
170                         if (dest == 0)
171                                 return "/";
172
173                         return String.Join ("/", parts, 0, dest);
174                 }
175                 
176                 internal static string GetDirectory (string url)
177                 {
178                         url = url.Replace('\\','/');
179                         int last = url.LastIndexOf ('/');
180                         if (last > 0)
181                                 return url.Substring (0, last);
182                         return "/";
183                 }
184
185                 internal static string GetFile (string url)
186                 {
187                         url = url.Replace('\\','/');
188                         int last = url.LastIndexOf ('/');
189                         if (last >= 0)
190                                 return url.Substring (last+1);
191
192                         throw new Exception (String.Format ("GetFile: `{0}' does not contain a /", url));
193                 }
194                 
195                 internal static bool IsRooted (string path)
196                 {
197                         if (path == null || path == "")
198                                 return true;
199
200                         char c = path [0];
201                         if (c == '/' || c == '\\')
202                                 return true;
203
204                         return false;
205                 }
206
207                 internal static bool IsRelativeUrl (string path)
208                 {
209                         return (path [0] != '/' && path.IndexOf (':') == -1);
210                 }
211
212                 public static string ResolveVirtualPathFromAppAbsolute (string path)
213                 {
214                         if (path [0] != '~') return path;
215                                 
216                         if (path.Length == 1)
217                                 return HttpRuntime.AppDomainAppVirtualPath;
218
219                         if (path [1] == '/' || path [1] == '\\') {
220                                 string appPath = HttpRuntime.AppDomainAppVirtualPath;
221                                 if (appPath.Length > 1) 
222                                         return appPath + "/" + path.Substring (2);
223                                 return "/" + path.Substring (2);
224                         }
225                         return path;    
226                 }
227
228                 public static string ResolvePhysicalPathFromAppAbsolute (string path) 
229                 {
230                         if (path [0] != '~') return path;
231
232                         if (path.Length == 1)
233                                 return HttpRuntime.AppDomainAppPath;
234
235                         if (path [1] == '/' || path [1] == '\\') {
236                                 string appPath = HttpRuntime.AppDomainAppPath;
237                                 if (appPath.Length > 1)
238                                         return appPath + "/" + path.Substring (2);
239                                 return "/" + path.Substring (2);
240                         }
241                         return path;
242                 }
243         }
244 }