UrlUtils.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. //
  2. // Permission is hereby granted, free of charge, to any person obtaining
  3. // a copy of this software and associated documentation files (the
  4. // "Software"), to deal in the Software without restriction, including
  5. // without limitation the rights to use, copy, modify, merge, publish,
  6. // distribute, sublicense, and/or sell copies of the Software, and to
  7. // permit persons to whom the Software is furnished to do so, subject to
  8. // the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be
  11. // included in all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  17. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  18. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  19. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. //
  21. /**
  22. * Namespace: System.Web.UI.Util
  23. * Class: UrlUtils
  24. *
  25. * Author: Gaurav Vaish
  26. * Maintainer: [email protected]
  27. * Status: ??%
  28. *
  29. * (C) Gaurav Vaish (2001)
  30. */
  31. using System;
  32. using System.Collections;
  33. using System.Text;
  34. using System.Web.SessionState;
  35. namespace System.Web.Util
  36. {
  37. internal class UrlUtils
  38. {
  39. /*
  40. * I could not find these functions in the class System.Uri
  41. * Besides, an instance of Uri will not be formed until and unless the address is of
  42. * the form protocol://[user:pass]host[:port]/[fullpath]
  43. * ie, a protocol, and that too without any blanks before,
  44. * is a must which may not be the case here.
  45. * Important: Escaped URL is assumed here. nothing like .aspx?path=/something
  46. * It should be .aspx?path=%2Fsomething
  47. */
  48. public static string GetProtocol(string url)
  49. {
  50. //Taking code from Java Class java.net.URL
  51. if(url!=null)
  52. {
  53. if(url.Length>0)
  54. {
  55. int i, start = 0, limit;
  56. limit = url.Length;
  57. char c;
  58. bool aRef = false;
  59. while( (limit > 0) && (url[limit-1] <= ' '))
  60. {
  61. limit --;
  62. }
  63. while( (start < limit) && (url[start] <= ' '))
  64. {
  65. start++;
  66. }
  67. if(RegionMatches(true, url, start, "url:", 0, 4))
  68. {
  69. start += 4;
  70. }
  71. if(start < url.Length && url[start]=='#')
  72. {
  73. aRef = true;
  74. }
  75. for(i = start; !aRef && (i < limit) && ((c=url[i]) != '/'); i++)
  76. {
  77. if(c==':')
  78. {
  79. return url.Substring(start, i - start);
  80. }
  81. }
  82. }
  83. }
  84. return String.Empty;
  85. }
  86. public static bool IsRelativeUrl(string url)
  87. {
  88. if (url.IndexOf(':') == -1)
  89. return !IsRooted(url);
  90. return false;
  91. }
  92. public static bool IsRootUrl(string url)
  93. {
  94. if(url!=null)
  95. {
  96. if(url.Length>0)
  97. {
  98. return IsValidProtocol(GetProtocol(url).ToLower());
  99. }
  100. }
  101. return true;
  102. }
  103. public static bool IsRooted(string path)
  104. {
  105. if(path!=null && path.Length > 0)
  106. {
  107. return (path[0]=='/' || path[0]=='\\');
  108. }
  109. return true;
  110. }
  111. public static void FailIfPhysicalPath(string path)
  112. {
  113. if(path!= null && path.Length > 1)
  114. {
  115. if(path[1]==':' || path.StartsWith(@"\\"))
  116. throw new HttpException(HttpRuntime.FormatResourceString("Physical_path_not_allowed", path));
  117. }
  118. }
  119. public static string Combine (string basePath, string relPath)
  120. {
  121. if (relPath == null)
  122. throw new ArgumentNullException ("relPath");
  123. int rlength = relPath.Length;
  124. if (rlength == 0)
  125. return "";
  126. FailIfPhysicalPath (relPath);
  127. relPath = relPath.Replace ("\\", "/");
  128. if (IsRooted (relPath))
  129. return Reduce (relPath);
  130. char first = relPath [0];
  131. if (rlength < 3 || first == '~' || first == '/' || first == '\\') {
  132. if (basePath == null || (basePath.Length == 1 && basePath [0] == '/'))
  133. basePath = String.Empty;
  134. string slash = (first == '/') ? "" : "/";
  135. if (first == '~') {
  136. if (rlength == 1) {
  137. relPath = "";
  138. } else if (rlength > 1 && relPath [1] == '/') {
  139. relPath = relPath.Substring (2);
  140. slash = "/";
  141. }
  142. return Reduce (HttpRuntime.AppDomainAppVirtualPath + slash + relPath);
  143. }
  144. return Reduce (basePath + slash + relPath);
  145. }
  146. if (basePath == null || basePath == "")
  147. basePath = HttpRuntime.AppDomainAppVirtualPath;
  148. if (basePath.Length <= 1)
  149. basePath = String.Empty;
  150. return Reduce (basePath + "/" + relPath);
  151. }
  152. public static bool IsValidProtocol(string protocol)
  153. {
  154. if(protocol.Length < 1)
  155. return false;
  156. char c = protocol[0];
  157. if(!Char.IsLetter(c))
  158. {
  159. return false;
  160. }
  161. for(int i=1; i < protocol.Length; i++)
  162. {
  163. c = protocol[i];
  164. if(!Char.IsLetterOrDigit(c) && c!='.' && c!='+' && c!='-')
  165. {
  166. return false;
  167. }
  168. }
  169. return true;
  170. }
  171. /*
  172. * MakeRelative("http://www.foo.com/bar1/bar2/file","http://www.foo.com/bar1")
  173. * will return "bar2/file"
  174. * while MakeRelative("http://www.foo.com/bar1/...","http://www.anotherfoo.com")
  175. * return 'null' and so does the call
  176. * MakeRelative("http://www.foo.com/bar1/bar2","http://www.foo.com/bar")
  177. */
  178. public static string MakeRelative(string fullUrl, string relativeTo)
  179. {
  180. if (fullUrl == relativeTo)
  181. return String.Empty;
  182. if (fullUrl.IndexOf (relativeTo) != 0)
  183. return null;
  184. string leftOver = fullUrl.Substring (relativeTo.Length);
  185. if (leftOver.Length > 0 && leftOver [0] == '/')
  186. leftOver = leftOver.Substring (1);
  187. leftOver = Reduce (leftOver);
  188. if (leftOver.Length > 0 && leftOver [0] == '/')
  189. leftOver = leftOver.Substring (1);
  190. return leftOver;
  191. }
  192. /*
  193. * Check JavaDocs for java.lang.String#RegionMatches(bool, int, String, int, int)
  194. * Could not find anything similar in the System.String class
  195. */
  196. public static bool RegionMatches(bool ignoreCase, string source, int start, string match, int offset, int len)
  197. {
  198. if(source!=null || match!=null)
  199. {
  200. if(source.Length>0 && match.Length>0)
  201. {
  202. char[] ta = source.ToCharArray();
  203. char[] pa = match.ToCharArray();
  204. if((offset < 0) || (start < 0) || (start > (source.Length - len)) || (offset > (match.Length - len)))
  205. {
  206. return false;
  207. }
  208. while(len-- > 0)
  209. {
  210. char c1 = ta[start++];
  211. char c2 = pa[offset++];
  212. if(c1==c2)
  213. continue;
  214. if(ignoreCase)
  215. {
  216. if(Char.ToUpper(c1)==Char.ToUpper(c2))
  217. continue;
  218. // Check for Gregorian Calendar where the above may not hold good
  219. if(Char.ToLower(c1)==Char.ToLower(c2))
  220. continue;
  221. }
  222. return false;
  223. }
  224. return true;
  225. }
  226. }
  227. return false;
  228. }
  229. public static string Reduce (string path)
  230. {
  231. path = path.Replace ('\\','/');
  232. string [] parts = path.Split ('/');
  233. ArrayList result = new ArrayList ();
  234. int end = parts.Length;
  235. for (int i = 0; i < end; i++) {
  236. string current = parts [i];
  237. if (current == "." )
  238. continue;
  239. if (current == "..") {
  240. if (result.Count == 0) {
  241. if (i == 1) // see bug 52599
  242. continue;
  243. throw new HttpException ("Invalid path.");
  244. }
  245. result.RemoveAt (result.Count - 1);
  246. continue;
  247. }
  248. result.Add (current);
  249. }
  250. if (result.Count == 0)
  251. return "/";
  252. return String.Join ("/", (string []) result.ToArray (typeof (string)));
  253. }
  254. public static string GetDirectory(string url)
  255. {
  256. if(url==null)
  257. {
  258. return null;
  259. }
  260. if(url.Length==0)
  261. {
  262. return String.Empty;
  263. }
  264. url = url.Replace('\\','/');
  265. string baseDir = "";
  266. int last = url.LastIndexOf ('/');
  267. if (last > 0)
  268. baseDir = url.Substring(0, url.LastIndexOf('/'));
  269. if(baseDir.Length==0)
  270. {
  271. baseDir = "/";
  272. }
  273. return baseDir;
  274. }
  275. static string GetFile (string url)
  276. {
  277. if (url == null)
  278. return null;
  279. if (url.Length == 0)
  280. return String.Empty;
  281. url = url.Replace ('\\', '/');
  282. int last = url.LastIndexOf ('/') + 1;
  283. if (last != 0) {
  284. url = url.Substring (last);
  285. }
  286. return url;
  287. }
  288. public static string InsertSessionId (string id, string path)
  289. {
  290. string dir = GetDirectory (path);
  291. if (!dir.EndsWith ("/"))
  292. dir += "/";
  293. return Reduce (dir + "(" + id + ")/" + GetFile (path));
  294. }
  295. public static string GetSessionId (string path)
  296. {
  297. int len = path.Length;
  298. if ((len < SessionId.IdLength + 2) || (path [len - 1] != ')') ||
  299. (path [len - SessionId.IdLength - 2] != '('))
  300. return null;
  301. return path.Substring (len - SessionId.IdLength - 1, SessionId.IdLength);
  302. }
  303. public static string RemoveSessionId (string base_path, string file_path)
  304. {
  305. int len = base_path.Length;
  306. string dir = base_path.Substring (0, len - SessionId.IdLength - 2);
  307. if (!dir.EndsWith ("/"))
  308. dir += "/";
  309. return Reduce (dir + GetFile (file_path));
  310. }
  311. public static string ResolveVirtualPathFromAppAbsolute (string path)
  312. {
  313. if (path [0] != '~') return path;
  314. if (path.Length == 1)
  315. return HttpRuntime.AppDomainAppVirtualPath;
  316. if (path [1] == '/' || path [1] == '\\') {
  317. string appPath = HttpRuntime.AppDomainAppVirtualPath;
  318. if (appPath.Length > 1)
  319. return appPath + "/" + path.Substring (2);
  320. return "/" + path.Substring (2);
  321. }
  322. return path;
  323. }
  324. public static string ResolvePhysicalPathFromAppAbsolute (string path)
  325. {
  326. if (path [0] != '~') return path;
  327. if (path.Length == 1)
  328. return HttpRuntime.AppDomainAppPath;
  329. if (path [1] == '/' || path [1] == '\\') {
  330. string appPath = HttpRuntime.AppDomainAppPath;
  331. if (appPath.Length > 1)
  332. return appPath + "/" + path.Substring (2);
  333. return "/" + path.Substring (2);
  334. }
  335. return path;
  336. }
  337. }
  338. }