UrlUtils.cs 8.6 KB

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