update readme (#21797)
[mono-project.git] / mcs / class / System.Web / System.Web.Util / UrlUtils.cs
blobe229a913f8f984008c46e2fbb770d21e29bd1188
1 //
2 // System.Web.UrlUtils.cs
3 //
4 // Authors:
5 // Gonzalo Paniagua (gonzalo@ximian.com)
6 // Jackson Harper (jackson@ximian.com)
7 //
9 //
10 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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.
32 using System.Web.SessionState;
33 using System.Text;
34 namespace System.Web.Util {
36 #if VISUAL_STUDIO
37 public
38 #else
39 internal
40 #endif
41 class UrlUtils {
43 // appRoot + SessionID + vpath
44 public static string InsertSessionId (string id, string path)
46 string dir = GetDirectory (path);
47 if (!dir.EndsWith ("/"))
48 dir += "/";
50 string appvpath = HttpRuntime.AppDomainAppVirtualPath;
51 if (!appvpath.EndsWith ("/"))
52 appvpath += "/";
54 if (path.StartsWith (appvpath))
55 path = path.Substring (appvpath.Length);
57 if (path.StartsWith("/"))
58 path = path.Length > 1 ? path.Substring (1) : "";
60 return Canonic (appvpath + "(" + id + ")/" + path);
63 public static string GetSessionId (string path)
65 #if TARGET_DOTNET
66 return null;
67 #else
68 if (path == null)
69 return null;
71 string appvpath = HttpRuntime.AppDomainAppVirtualPath;
72 int appvpathlen = appvpath.Length;
74 if (path.Length <= appvpathlen)
75 return null;
77 path = path.Substring (appvpathlen);
79 int len = path.Length;
80 if (len == 0 || path [0] != '/') {
81 path = '/' + path;
82 len++;
85 if ((len < SessionId.IdLength + 3) || (path [1] != '(') ||
86 (path [SessionId.IdLength + 2] != ')'))
87 return null;
89 return path.Substring (2, SessionId.IdLength);
90 #endif
93 public static bool HasSessionId (string path)
95 if (path == null || path.Length < 5)
96 return false;
98 return (StrUtils.StartsWith (path, "/(") && path.IndexOf (")/") > 2);
101 public static string RemoveSessionId (string base_path, string file_path)
103 // Caller did a GetSessionId first
104 int idx = base_path.IndexOf ("/(");
105 string dir = base_path.Substring (0, idx + 1);
106 if (!dir.EndsWith ("/"))
107 dir += "/";
109 idx = base_path.IndexOf (")/");
110 if (idx != -1 && base_path.Length > idx + 2) {
111 string dir2 = base_path.Substring (idx + 2);
112 if (!dir2.EndsWith ("/"))
113 dir2 += "/";
115 dir += dir2;
118 return Canonic (dir + GetFile (file_path));
121 public static string Combine (string basePath, string relPath)
123 if (relPath == null)
124 throw new ArgumentNullException ("relPath");
126 int rlength = relPath.Length;
127 if (rlength == 0)
128 return "";
130 relPath = relPath.Replace ('\\', '/');
131 if (IsRooted (relPath))
132 return Canonic (relPath);
134 char first = relPath [0];
135 if (rlength < 3 || first == '~' || first == '/' || first == '\\') {
136 if (basePath == null || (basePath.Length == 1 && basePath [0] == '/'))
137 basePath = String.Empty;
139 string slash = (first == '/') ? "" : "/";
140 if (first == '~') {
141 if (rlength == 1) {
142 relPath = "";
143 } else if (rlength > 1 && relPath [1] == '/') {
144 relPath = relPath.Substring (2);
145 slash = "/";
148 string appvpath = HttpRuntime.AppDomainAppVirtualPath;
149 if (appvpath.EndsWith ("/"))
150 slash = "";
152 return Canonic (appvpath + slash + relPath);
155 return Canonic (basePath + slash + relPath);
158 if (basePath == null || basePath.Length == 0 || basePath [0] == '~')
159 basePath = HttpRuntime.AppDomainAppVirtualPath;
161 if (basePath.Length <= 1)
162 basePath = String.Empty;
164 return Canonic (basePath + "/" + relPath);
167 static char [] path_sep = {'\\', '/'};
169 public static string Canonic (string path)
171 bool isRooted = IsRooted(path);
172 bool endsWithSlash = path.EndsWith("/");
173 string [] parts = path.Split (path_sep);
174 int end = parts.Length;
176 int dest = 0;
178 for (int i = 0; i < end; i++) {
179 string current = parts [i];
181 if (current.Length == 0)
182 continue;
184 if (current == "." )
185 continue;
187 if (current == "..") {
188 dest --;
189 continue;
191 if (dest < 0)
192 if (!isRooted)
193 throw new HttpException ("Invalid path.");
194 else
195 dest = 0;
197 parts [dest++] = current;
199 if (dest < 0)
200 throw new HttpException ("Invalid path.");
202 if (dest == 0)
203 return "/";
205 string str = String.Join ("/", parts, 0, dest);
206 str = RemoveDoubleSlashes (str);
207 if (isRooted)
208 str = "/" + str;
209 if (endsWithSlash)
210 str = str + "/";
212 return str;
215 public static string GetDirectory (string url)
217 url = url.Replace('\\','/');
218 int last = url.LastIndexOf ('/');
220 if (last > 0) {
221 if (last < url.Length)
222 last++;
223 return RemoveDoubleSlashes (url.Substring (0, last));
226 return "/";
229 public static string RemoveDoubleSlashes (string input)
231 // MS VirtualPathUtility removes duplicate '/'
233 int index = -1;
234 for (int i = 1; i < input.Length; i++)
235 if (input [i] == '/' && input [i - 1] == '/') {
236 index = i - 1;
237 break;
240 if (index == -1) // common case optimization
241 return input;
243 StringBuilder sb = new StringBuilder (input.Length);
244 sb.Append (input, 0, index);
246 for (int i = index; i < input.Length; i++) {
247 if (input [i] == '/') {
248 int next = i + 1;
249 if (next < input.Length && input [next] == '/')
250 continue;
251 sb.Append ('/');
253 else {
254 sb.Append (input [i]);
258 return sb.ToString ();
261 public static string GetFile (string url)
263 url = url.Replace('\\','/');
264 int last = url.LastIndexOf ('/');
265 if (last >= 0) {
266 if (url.Length == 1) // Empty file name instead of ArgumentOutOfRange
267 return "";
268 return url.Substring (last+1);
271 throw new ArgumentException (String.Format ("GetFile: `{0}' does not contain a /", url));
274 public static bool IsRooted (string path)
276 if (path == null || path.Length == 0)
277 return true;
279 char c = path [0];
280 if (c == '/' || c == '\\')
281 return true;
283 return false;
286 public static bool IsRelativeUrl (string path)
288 return (path [0] != '/' && path.IndexOf (':') == -1);
291 public static string ResolveVirtualPathFromAppAbsolute (string path)
293 if (path [0] != '~') return path;
295 if (path.Length == 1)
296 return HttpRuntime.AppDomainAppVirtualPath;
298 if (path [1] == '/' || path [1] == '\\') {
299 string appPath = HttpRuntime.AppDomainAppVirtualPath;
300 if (appPath.Length > 1)
301 return appPath + "/" + path.Substring (2);
302 return "/" + path.Substring (2);
304 return path;
307 public static string ResolvePhysicalPathFromAppAbsolute (string path)
309 if (path [0] != '~') return path;
311 if (path.Length == 1)
312 return HttpRuntime.AppDomainAppPath;
314 if (path [1] == '/' || path [1] == '\\') {
315 string appPath = HttpRuntime.AppDomainAppPath;
316 if (appPath.Length > 1)
317 return appPath + "/" + path.Substring (2);
318 return "/" + path.Substring (2);
320 return path;