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;
33 namespace System.Web.Util {
35 internal class UrlUtils {
37 // appRoot + SessionID + vpath
38 internal static string InsertSessionId (string id, string path)
40 string dir = GetDirectory (path);
41 if (!dir.EndsWith ("/"))
44 string appvpath = HttpRuntime.AppDomainAppVirtualPath;
45 if (!appvpath.EndsWith ("/"))
48 if (path.StartsWith (appvpath))
49 path = path.Substring (appvpath.Length);
52 path = path.Length > 1 ? path.Substring (1) : "";
54 return Canonic (appvpath + "(" + id + ")/" + path);
57 internal static string GetSessionId (string path)
59 string appvpath = HttpRuntime.AppDomainAppVirtualPath;
60 if (path.Length <= appvpath.Length)
63 path = path.Substring (appvpath.Length);
64 if (path.Length == 0 || path [0] != '/')
67 int len = path.Length;
68 if ((len < SessionId.IdLength + 3) || (path [1] != '(') ||
69 (path [SessionId.IdLength + 2] != ')'))
72 return path.Substring (2, SessionId.IdLength);
75 internal static string RemoveSessionId (string base_path, string file_path)
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 ("/"))
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 ("/"))
92 return Canonic (dir + GetFile (file_path));
95 public static string Combine (string basePath, string relPath)
98 throw new ArgumentNullException ("relPath");
100 int rlength = relPath.Length;
104 relPath = relPath.Replace ("\\", "/");
105 if (IsRooted (relPath))
106 return Canonic (relPath);
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;
113 string slash = (first == '/') ? "" : "/";
117 } else if (rlength > 1 && relPath [1] == '/') {
118 relPath = relPath.Substring (2);
122 string appvpath = HttpRuntime.AppDomainAppVirtualPath;
123 if (appvpath.EndsWith ("/"))
126 return Canonic (appvpath + slash + relPath);
129 return Canonic (basePath + slash + relPath);
132 if (basePath == null || basePath == "" || basePath [0] == '~')
133 basePath = HttpRuntime.AppDomainAppVirtualPath;
135 if (basePath.Length <= 1)
136 basePath = String.Empty;
138 return Canonic (basePath + "/" + relPath);
141 static char [] path_sep = {'\\', '/'};
143 internal static string Canonic (string path)
145 string [] parts = path.Split (path_sep);
146 int end = parts.Length;
150 for (int i = 0; i < end; i++) {
151 string current = parts [i];
155 if (current == "..") {
157 if (i == 1) // see bug 52599
160 throw new HttpException ("Invalid path.");
167 parts [dest++] = current;
173 return String.Join ("/", parts, 0, dest);
176 internal static string GetDirectory (string url)
178 url = url.Replace('\\','/');
179 int last = url.LastIndexOf ('/');
181 return url.Substring (0, last);
185 internal static string GetFile (string url)
187 url = url.Replace('\\','/');
188 int last = url.LastIndexOf ('/');
190 return url.Substring (last+1);
192 throw new Exception (String.Format ("GetFile: `{0}' does not contain a /", url));
195 internal static bool IsRooted (string path)
197 if (path == null || path == "")
201 if (c == '/' || c == '\\')
207 internal static bool IsRelativeUrl (string path)
209 return (path [0] != '/' && path.IndexOf (':') == -1);
212 public static string ResolveVirtualPathFromAppAbsolute (string path)
214 if (path [0] != '~') return path;
216 if (path.Length == 1)
217 return HttpRuntime.AppDomainAppVirtualPath;
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);
228 public static string ResolvePhysicalPathFromAppAbsolute (string path)
230 if (path [0] != '~') return path;
232 if (path.Length == 1)
233 return HttpRuntime.AppDomainAppPath;
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);