2 // System.Web.UrlUtils.cs
5 // Gonzalo Paniagua (gonzalo@ximian.com)
6 // Jackson Harper (jackson@ximian.com)
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:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
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;
\r
36 namespace Mainsoft.Web.Util {
38 internal class UrlUtils {
40 // appRoot + SessionID + vpath
41 internal static string InsertSessionId (string id, string path)
43 string dir = GetDirectory (path);
44 if (!dir.EndsWith ("/"))
47 string appvpath = HttpRuntime.AppDomainAppVirtualPath;
48 if (!appvpath.EndsWith ("/"))
51 if (path.StartsWith (appvpath))
52 path = path.Substring (appvpath.Length);
55 path = path.Length > 1 ? path.Substring (1) : "";
57 return Canonic (appvpath + "(" + id + ")/" + path);
60 internal static string GetSessionId (string path)
62 string appvpath = HttpRuntime.AppDomainAppVirtualPath;
63 if (path.Length <= appvpath.Length)
66 path = path.Substring (appvpath.Length);
67 if (path.Length == 0 || path [0] != '/')
70 int len = path.Length;
71 if ((len < SessionId.IdLength + 3) || (path [1] != '(') ||
72 (path [SessionId.IdLength + 2] != ')'))
75 return path.Substring (2, SessionId.IdLength);
78 internal static string RemoveSessionId (string base_path, string file_path)
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 ("/"))
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 ("/"))
95 return Canonic (dir + GetFile (file_path));
98 public static string Combine (string basePath, string relPath)
101 throw new ArgumentNullException ("relPath");
103 int rlength = relPath.Length;
107 relPath = relPath.Replace ("\\", "/");
108 if (IsRooted (relPath))
109 return Canonic (relPath);
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;
116 string slash = (first == '/') ? "" : "/";
120 } else if (rlength > 1 && relPath [1] == '/') {
121 relPath = relPath.Substring (2);
125 string appvpath = HttpRuntime.AppDomainAppVirtualPath;
126 if (appvpath.EndsWith ("/"))
129 return Canonic (appvpath + slash + relPath);
132 return Canonic (basePath + slash + relPath);
135 if (basePath == null || basePath == "" || basePath [0] == '~')
136 basePath = HttpRuntime.AppDomainAppVirtualPath;
138 if (basePath.Length <= 1)
139 basePath = String.Empty;
141 return Canonic (basePath + "/" + relPath);
144 static char [] path_sep = {'\\', '/'};
146 internal static string Canonic (string path)
148 string [] parts = path.Split (path_sep);
149 int end = parts.Length;
153 for (int i = 0; i < end; i++) {
154 string current = parts [i];
158 if (current == "..") {
160 if (i == 1) // see bug 52599
163 throw new HttpException ("Invalid path.");
170 parts [dest++] = current;
176 return String.Join ("/", parts, 0, dest);
179 internal static string GetDirectory (string url)
181 url = url.Replace('\\','/');
182 int last = url.LastIndexOf ('/');
186 return RemoveDoubleSlashes (url.Substring (0, last));
188 return url.Substring (0, last);
196 internal static string RemoveDoubleSlashes (string input)
198 // MS VirtualPathUtility removes duplicate '/'
201 while ((x = str.Replace ("//", "/")) != str) {
209 internal static string GetFile (string url)
211 url = url.Replace('\\','/');
212 int last = url.LastIndexOf ('/');
214 if (url.Length == 1) // Empty file name instead of ArgumentOutOfRange
216 return url.Substring (last+1);
219 throw new Exception (String.Format ("GetFile: `{0}' does not contain a /", url));
222 internal static bool IsRooted (string path)
224 if (path == null || path == "")
228 if (c == '/' || c == '\\')
234 internal static bool IsRelativeUrl (string path)
236 return (path [0] != '/' && path.IndexOf (':') == -1);
239 public static string ResolveVirtualPathFromAppAbsolute (string path)
241 if (path [0] != '~') return path;
243 if (path.Length == 1)
244 return HttpRuntime.AppDomainAppVirtualPath;
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);
255 public static string ResolvePhysicalPathFromAppAbsolute (string path)
257 if (path [0] != '~') return path;
259 if (path.Length == 1)
260 return HttpRuntime.AppDomainAppPath;
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);