2010-07-25 Carlos Alberto Cortez <calberto.cortez@gmail.com>
[mono.git] / mcs / class / Mainsoft.Web / Mainsoft.Web.AspnetConfig / aspnetconfig / 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;\r
33 using System.Web;\r
34 using System;
35
36 namespace Mainsoft.Web.Util {
37         
38         internal class UrlUtils {
39
40                 // appRoot + SessionID + vpath
41                 internal static string InsertSessionId (string id, string path)
42                 {
43                         string dir = GetDirectory (path);
44                         if (!dir.EndsWith ("/"))
45                                 dir += "/";
46
47                         string appvpath = HttpRuntime.AppDomainAppVirtualPath;
48                         if (!appvpath.EndsWith ("/"))
49                                 appvpath += "/";
50
51                         if (path.StartsWith (appvpath))
52                                 path = path.Substring (appvpath.Length);
53
54                         if (path [0] == '/')
55                                 path = path.Length > 1 ? path.Substring (1) : "";
56
57                         return Canonic (appvpath + "(" + id + ")/" + path);
58                 }
59 #if false
60                 internal static string GetSessionId (string path)
61                 {
62                         string appvpath = HttpRuntime.AppDomainAppVirtualPath;
63                         if (path.Length <= appvpath.Length)
64                                 return null;
65
66                         path = path.Substring (appvpath.Length);
67                         if (path.Length == 0 || path [0] != '/')
68                                 path = '/' + path;
69
70                         int len = path.Length;
71                         if ((len < SessionId.IdLength + 3) || (path [1] != '(') ||
72                             (path [SessionId.IdLength + 2] != ')'))
73                                 return null;
74
75                         return path.Substring (2, SessionId.IdLength);
76                 }
77 #endif
78                 internal static string RemoveSessionId (string base_path, string file_path)
79                 {
80                         // Caller did a GetSessionId first
81                         int idx = base_path.IndexOf ("/(");
82                         string dir = base_path.Substring (0, idx + 1);
83                         if (!dir.EndsWith ("/"))
84                                 dir += "/";
85
86                         idx = base_path.IndexOf (")/");
87                         if (idx != -1 && base_path.Length > idx + 2) {
88                                 string dir2 = base_path.Substring (idx + 2);
89                                 if (!dir2.EndsWith ("/"))
90                                         dir2 += "/";
91
92                                 dir += dir2;
93                         }
94
95                         return Canonic (dir + GetFile (file_path));
96                 }
97
98                 public static string Combine (string basePath, string relPath)
99                 {
100                         if (relPath == null)
101                                 throw new ArgumentNullException ("relPath");
102
103                         int rlength = relPath.Length;
104                         if (rlength == 0)
105                                 return "";
106
107                         relPath = relPath.Replace ("\\", "/");
108                         if (IsRooted (relPath))
109                                 return Canonic (relPath);
110
111                         char first = relPath [0];
112                         if (rlength < 3 || first == '~' || first == '/' || first == '\\') {
113                                 if (basePath == null || (basePath.Length == 1 && basePath [0] == '/'))
114                                         basePath = String.Empty;
115
116                                 string slash = (first == '/') ? "" : "/";
117                                 if (first == '~') {
118                                         if (rlength == 1) {
119                                                 relPath = "";
120                                         } else if (rlength > 1 && relPath [1] == '/') {
121                                                 relPath = relPath.Substring (2);
122                                                 slash = "/";
123                                         }
124
125                                         string appvpath = HttpRuntime.AppDomainAppVirtualPath;
126                                         if (appvpath.EndsWith ("/"))
127                                                 slash = "";
128
129                                         return Canonic (appvpath + slash + relPath);
130                                 }
131
132                                 return Canonic (basePath + slash + relPath);
133                         }
134
135                         if (basePath == null || basePath == "" || basePath [0] == '~')
136                                 basePath = HttpRuntime.AppDomainAppVirtualPath;
137
138                         if (basePath.Length <= 1)
139                                 basePath = String.Empty;
140
141                         return Canonic (basePath + "/" + relPath);
142                 }
143
144                 static char [] path_sep = {'\\', '/'};
145                 
146                 internal static string Canonic (string path)
147                 {
148                         string [] parts = path.Split (path_sep);
149                         int end = parts.Length;
150                         
151                         int dest = 0;
152                         
153                         for (int i = 0; i < end; i++) {
154                                 string current = parts [i];
155                                 if (current == "." )
156                                         continue;
157
158                                 if (current == "..") {
159                                         if (dest == 0) {
160                                                 if (i == 1) // see bug 52599
161                                                         continue;
162
163                                                 throw new HttpException ("Invalid path.");
164                                         }
165
166                                         dest --;
167                                         continue;
168                                 }
169
170                                 parts [dest++] = current;
171                         }
172
173                         if (dest == 0)
174                                 return "/";
175
176                         return String.Join ("/", parts, 0, dest);
177                 }
178                 
179                 internal static string GetDirectory (string url)
180                 {
181                         url = url.Replace('\\','/');
182                         int last = url.LastIndexOf ('/');
183
184                         if (last > 0) {
185 #if NET_2_0
186                                 return RemoveDoubleSlashes (url.Substring (0, last));
187 #else
188                                 return url.Substring (0, last);
189 #endif
190                         }
191
192                         return "/";
193                 }
194
195 #if NET_2_0
196                 internal static string RemoveDoubleSlashes (string input)
197                 {
198                         // MS VirtualPathUtility removes duplicate '/'
199                         string str = input;
200                         string x;
201                         while ((x = str.Replace ("//", "/")) != str) {
202                                 str = x;
203                         }
204
205                         return str;
206                 }
207 #endif
208
209                 internal static string GetFile (string url)
210                 {
211                         url = url.Replace('\\','/');
212                         int last = url.LastIndexOf ('/');
213                         if (last >= 0) {
214                                 if (url.Length == 1) // Empty file name instead of ArgumentOutOfRange
215                                         return "";
216                                 return url.Substring (last+1);
217                         }
218
219                         throw new Exception (String.Format ("GetFile: `{0}' does not contain a /", url));
220                 }
221                 
222                 internal static bool IsRooted (string path)
223                 {
224                         if (path == null || path == "")
225                                 return true;
226
227                         char c = path [0];
228                         if (c == '/' || c == '\\')
229                                 return true;
230
231                         return false;
232                 }
233
234                 internal static bool IsRelativeUrl (string path)
235                 {
236                         return (path [0] != '/' && path.IndexOf (':') == -1);
237                 }
238
239                 public static string ResolveVirtualPathFromAppAbsolute (string path)
240                 {
241                         if (path [0] != '~') return path;
242                                 
243                         if (path.Length == 1)
244                                 return HttpRuntime.AppDomainAppVirtualPath;
245
246                         if (path [1] == '/' || path [1] == '\\') {
247                                 string appPath = HttpRuntime.AppDomainAppVirtualPath;
248                                 if (appPath.Length > 1) 
249                                         return appPath + "/" + path.Substring (2);
250                                 return "/" + path.Substring (2);
251                         }
252                         return path;    
253                 }
254
255                 public static string ResolvePhysicalPathFromAppAbsolute (string path) 
256                 {
257                         if (path [0] != '~') return path;
258
259                         if (path.Length == 1)
260                                 return HttpRuntime.AppDomainAppPath;
261
262                         if (path [1] == '/' || path [1] == '\\') {
263                                 string appPath = HttpRuntime.AppDomainAppPath;
264                                 if (appPath.Length > 1)
265                                         return appPath + "/" + path.Substring (2);
266                                 return "/" + path.Substring (2);
267                         }
268                         return path;
269                 }
270         }
271 }