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
;
34 namespace System
.Web
.Util
{
43 // appRoot + SessionID + vpath
44 public static string InsertSessionId (string id
, string path
)
46 string dir
= GetDirectory (path
);
47 if (!dir
.EndsWith ("/"))
50 string appvpath
= HttpRuntime
.AppDomainAppVirtualPath
;
51 if (!appvpath
.EndsWith ("/"))
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
)
71 string appvpath
= HttpRuntime
.AppDomainAppVirtualPath
;
72 int appvpathlen
= appvpath
.Length
;
74 if (path
.Length
<= appvpathlen
)
77 path
= path
.Substring (appvpathlen
);
79 int len
= path
.Length
;
80 if (len
== 0 || path
[0] != '/') {
85 if ((len
< SessionId
.IdLength
+ 3) || (path
[1] != '(') ||
86 (path
[SessionId
.IdLength
+ 2] != ')'))
89 return path
.Substring (2, SessionId
.IdLength
);
93 public static bool HasSessionId (string path
)
95 if (path
== null || path
.Length
< 5)
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 ("/"))
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 ("/"))
118 return Canonic (dir
+ GetFile (file_path
));
121 public static string Combine (string basePath
, string relPath
)
124 throw new ArgumentNullException ("relPath");
126 int rlength
= relPath
.Length
;
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
== '/') ? "" : "/";
143 } else if (rlength
> 1 && relPath
[1] == '/') {
144 relPath
= relPath
.Substring (2);
148 string appvpath
= HttpRuntime
.AppDomainAppVirtualPath
;
149 if (appvpath
.EndsWith ("/"))
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
;
178 for (int i
= 0; i
< end
; i
++) {
179 string current
= parts
[i
];
181 if (current
.Length
== 0)
187 if (current
== "..") {
193 throw new HttpException ("Invalid path.");
197 parts
[dest
++] = current
;
200 throw new HttpException ("Invalid path.");
205 string str
= String
.Join ("/", parts
, 0, dest
);
206 str
= RemoveDoubleSlashes (str
);
215 public static string GetDirectory (string url
)
217 url
= url
.Replace('\\','/');
218 int last
= url
.LastIndexOf ('/');
221 if (last
< url
.Length
)
223 return RemoveDoubleSlashes (url
.Substring (0, last
));
229 public static string RemoveDoubleSlashes (string input
)
231 // MS VirtualPathUtility removes duplicate '/'
234 for (int i
= 1; i
< input
.Length
; i
++)
235 if (input
[i
] == '/' && input
[i
- 1] == '/') {
240 if (index
== -1) // common case optimization
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
] == '/') {
249 if (next
< input
.Length
&& input
[next
] == '/')
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 ('/');
266 if (url
.Length
== 1) // Empty file name instead of ArgumentOutOfRange
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)
280 if (c
== '/' || c
== '\\')
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);
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);