Fix problems with overlong directory names: phase #1
[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
181                         if (last > 0) {
182 #if NET_2_0
183                                 return RemoveDoubleSlashes (url.Substring (0, last));
184 #else
185                                 return url.Substring (0, last);
186 #endif
187                         }
188
189                         return "/";
190                 }
191
192 #if NET_2_0
193                 internal static string RemoveDoubleSlashes (string input)
194                 {
195                         // MS VirtualPathUtility removes duplicate '/'
196                         string str = input;
197                         string x;
198                         while ((x = str.Replace ("//", "/")) != str) {
199                                 str = x;
200                         }
201
202                         return str;
203                 }
204 #endif
205
206                 internal static string GetFile (string url)
207                 {
208                         url = url.Replace('\\','/');
209                         int last = url.LastIndexOf ('/');
210                         if (last >= 0) {
211                                 if (url.Length == 1) // Empty file name instead of ArgumentOutOfRange
212                                         return "";
213                                 return url.Substring (last+1);
214                         }
215
216                         throw new Exception (String.Format ("GetFile: `{0}' does not contain a /", url));
217                 }
218                 
219                 internal static bool IsRooted (string path)
220                 {
221                         if (path == null || path == "")
222                                 return true;
223
224                         char c = path [0];
225                         if (c == '/' || c == '\\')
226                                 return true;
227
228                         return false;
229                 }
230
231                 internal static bool IsRelativeUrl (string path)
232                 {
233                         return (path [0] != '/' && path.IndexOf (':') == -1);
234                 }
235
236                 public static string ResolveVirtualPathFromAppAbsolute (string path)
237                 {
238                         if (path [0] != '~') return path;
239                                 
240                         if (path.Length == 1)
241                                 return HttpRuntime.AppDomainAppVirtualPath;
242
243                         if (path [1] == '/' || path [1] == '\\') {
244                                 string appPath = HttpRuntime.AppDomainAppVirtualPath;
245                                 if (appPath.Length > 1) 
246                                         return appPath + "/" + path.Substring (2);
247                                 return "/" + path.Substring (2);
248                         }
249                         return path;    
250                 }
251
252                 public static string ResolvePhysicalPathFromAppAbsolute (string path) 
253                 {
254                         if (path [0] != '~') return path;
255
256                         if (path.Length == 1)
257                                 return HttpRuntime.AppDomainAppPath;
258
259                         if (path [1] == '/' || path [1] == '\\') {
260                                 string appPath = HttpRuntime.AppDomainAppPath;
261                                 if (appPath.Length > 1)
262                                         return appPath + "/" + path.Substring (2);
263                                 return "/" + path.Substring (2);
264                         }
265                         return path;
266                 }
267         }
268 }