UrlUtils.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. if (IsRooted (relPath))
  108. return Reduce (relPath);
  109. char first = relPath [0];
  110. if (rlength < 3 || first == '~' || first == '/' || first == '\\') {
  111. if (basePath == null || (basePath.Length == 1 && basePath [0] == '/'))
  112. basePath = String.Empty;
  113. if (first == '~') {
  114. if (rlength == 1) {
  115. relPath = "";
  116. } else if (rlength > 1 && relPath [1] == '/') {
  117. relPath = relPath.Substring (2);
  118. }
  119. }
  120. string slash = (first == '/') ? "" : "/";
  121. return Reduce (basePath + slash + relPath);
  122. }
  123. if (basePath == null || basePath == "")
  124. basePath = HttpRuntime.AppDomainAppVirtualPath;
  125. if (basePath.Length <= 1)
  126. basePath = String.Empty;
  127. return Reduce (basePath + "/" + relPath);
  128. }
  129. public static bool IsValidProtocol(string protocol)
  130. {
  131. if(protocol.Length < 1)
  132. return false;
  133. char c = protocol[0];
  134. if(!Char.IsLetter(c))
  135. {
  136. return false;
  137. }
  138. for(int i=1; i < protocol.Length; i++)
  139. {
  140. c = protocol[i];
  141. if(!Char.IsLetterOrDigit(c) && c!='.' && c!='+' && c!='-')
  142. {
  143. return false;
  144. }
  145. }
  146. return true;
  147. }
  148. /*
  149. * MakeRelative("http://www.foo.com/bar1/bar2/file","http://www.foo.com/bar1")
  150. * will return "bar2/file"
  151. * while MakeRelative("http://www.foo.com/bar1/...","http://www.anotherfoo.com")
  152. * return 'null' and so does the call
  153. * MakeRelative("http://www.foo.com/bar1/bar2","http://www.foo.com/bar")
  154. */
  155. public static string MakeRelative(string fullUrl, string relativeTo)
  156. {
  157. if(fullUrl==relativeTo)
  158. {
  159. return String.Empty;
  160. }
  161. if(fullUrl.IndexOf(relativeTo)!=0)
  162. {
  163. return null;
  164. }
  165. string leftOver = fullUrl.Substring(relativeTo.Length);
  166. if(!fullUrl.EndsWith("/") && !leftOver.StartsWith("/"))
  167. {
  168. return null;
  169. }
  170. if(leftOver.StartsWith("/"))
  171. {
  172. leftOver = leftOver.Substring(1);
  173. }
  174. return leftOver;
  175. }
  176. /*
  177. * Check JavaDocs for java.lang.String#RegionMatches(bool, int, String, int, int)
  178. * Could not find anything similar in the System.String class
  179. */
  180. public static bool RegionMatches(bool ignoreCase, string source, int start, string match, int offset, int len)
  181. {
  182. if(source!=null || match!=null)
  183. {
  184. if(source.Length>0 && match.Length>0)
  185. {
  186. char[] ta = source.ToCharArray();
  187. char[] pa = match.ToCharArray();
  188. if((offset < 0) || (start < 0) || (start > (source.Length - len)) || (offset > (match.Length - len)))
  189. {
  190. return false;
  191. }
  192. while(len-- > 0)
  193. {
  194. char c1 = ta[start++];
  195. char c2 = pa[offset++];
  196. if(c1==c2)
  197. continue;
  198. if(ignoreCase)
  199. {
  200. if(Char.ToUpper(c1)==Char.ToUpper(c2))
  201. continue;
  202. // Check for Gregorian Calendar where the above may not hold good
  203. if(Char.ToLower(c1)==Char.ToLower(c2))
  204. continue;
  205. }
  206. return false;
  207. }
  208. return true;
  209. }
  210. }
  211. return false;
  212. }
  213. public static string Reduce (string path)
  214. {
  215. path = path.Replace ('\\','/');
  216. string [] parts = path.Split ('/');
  217. ArrayList result = new ArrayList ();
  218. int end = parts.Length;
  219. for (int i = 0; i < end; i++) {
  220. string current = parts [i];
  221. if (current == "" || current == "." )
  222. continue;
  223. if (current == "..") {
  224. if (result.Count == 0) {
  225. if (i == 1) // see bug 52599
  226. continue;
  227. throw new HttpException ("Invalid path.");
  228. }
  229. result.RemoveAt (result.Count - 1);
  230. continue;
  231. }
  232. result.Add (current);
  233. }
  234. if (result.Count == 0)
  235. return "/";
  236. result.Insert (0, "");
  237. return String.Join ("/", (string []) result.ToArray (typeof (string)));
  238. }
  239. public static string GetDirectory(string url)
  240. {
  241. if(url==null)
  242. {
  243. return null;
  244. }
  245. if(url.Length==0)
  246. {
  247. return String.Empty;
  248. }
  249. url.Replace('\\','/');
  250. string baseDir = "";
  251. int last = url.LastIndexOf ('/');
  252. if (last > 0)
  253. baseDir = url.Substring(0, url.LastIndexOf('/'));
  254. if(baseDir.Length==0)
  255. {
  256. baseDir = "/";
  257. }
  258. return baseDir;
  259. }
  260. public static string GetFile (string url)
  261. {
  262. if(url == null)
  263. return null;
  264. if(url.Length == 0)
  265. return String.Empty;
  266. url.Replace ('\\', '/');
  267. string file = url;
  268. int last = url.LastIndexOf ('/');
  269. if (last > 0)
  270. file = url.Substring (last);
  271. return file;
  272. }
  273. public static string InsertSessionId (string id, string path)
  274. {
  275. return Reduce (GetDirectory (path) + "/(" + id + ")/" + GetFile (path));
  276. }
  277. public static string GetSessionId (string path)
  278. {
  279. int len = path.Length;
  280. if ((len < SessionId.IdLength + 2) || (path [len - 1] != ')') ||
  281. (path [len - SessionId.IdLength - 2] != '('))
  282. return null;
  283. return path.Substring (len - SessionId.IdLength - 1, SessionId.IdLength);
  284. }
  285. public static string RemoveSessionId (string base_path, string file_path)
  286. {
  287. int len = base_path.Length;
  288. return Reduce (base_path.Substring (0, len - SessionId.IdLength - 2) + "/" +
  289. GetFile (file_path));
  290. }
  291. public static string ResolveVirtualPathFromAppAbsolute (string path)
  292. {
  293. if (path [0] != '~') return path;
  294. if (path.Length == 1)
  295. return HttpRuntime.AppDomainAppVirtualPath;
  296. if (path [1] == '/' || path [1] == '\\') {
  297. string appPath = HttpRuntime.AppDomainAppVirtualPath;
  298. if (appPath.Length > 1)
  299. return appPath + "/" + path.Substring (2);
  300. return "/" + path.Substring (2);
  301. }
  302. return path;
  303. }
  304. public static string ResolvePhysicalPathFromAppAbsolute (string path)
  305. {
  306. if (path [0] != '~') return path;
  307. if (path.Length == 1)
  308. return HttpRuntime.AppDomainAppPath;
  309. if (path [1] == '/' || path [1] == '\\') {
  310. string appPath = HttpRuntime.AppDomainAppPath;
  311. if (appPath.Length > 1)
  312. return appPath + "/" + path.Substring (2);
  313. return "/" + path.Substring (2);
  314. }
  315. return path;
  316. }
  317. }
  318. }