2
0

UrlUtils.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. namespace System.Web.Util
  15. {
  16. internal class UrlUtils
  17. {
  18. /*
  19. * I could not find these functions in the class System.Uri
  20. * Besides, an instance of Uri will not be formed until and unless the address is of
  21. * the form protocol://[user:pass]host[:port]/[fullpath]
  22. * ie, a protocol, and that too without any blanks before,
  23. * is a must which may not be the case here.
  24. * Important: Escaped URL is assumed here. nothing like .aspx?path=/something
  25. * It should be .aspx?path=%2Fsomething
  26. */
  27. public static string GetProtocol(string url)
  28. {
  29. //Taking code from Java Class java.net.URL
  30. if(url!=null)
  31. {
  32. if(url.Length>0)
  33. {
  34. int i, start = 0, limit;
  35. limit = url.Length;
  36. char c;
  37. bool aRef = false;
  38. while( (limit > 0) && (url[limit-1] <= ' '))
  39. {
  40. limit --;
  41. }
  42. while( (start < limit) && (url[start] <= ' '))
  43. {
  44. start++;
  45. }
  46. if(RegionMatches(true, url, start, "url:", 0, 4))
  47. {
  48. start += 4;
  49. }
  50. if(start < url.Length && url[start]=='#')
  51. {
  52. aRef = true;
  53. }
  54. for(i = start; !aRef && (i < limit) && ((c=url[i]) != '/'); i++)
  55. {
  56. if(c==':')
  57. {
  58. return url.Substring(start, i - start);
  59. }
  60. }
  61. }
  62. }
  63. return String.Empty;
  64. }
  65. public static bool IsRelativeUrl(string url)
  66. {
  67. if(url.IndexOf(':') != -1)
  68. return !IsRootUrl(url);
  69. return true;
  70. }
  71. public static bool IsRootUrl(string url)
  72. {
  73. if(url!=null)
  74. {
  75. if(url.Length>0)
  76. {
  77. return IsValidProtocol(GetProtocol(url).ToLower());
  78. }
  79. }
  80. return true;
  81. }
  82. public static bool IsRooted(string path)
  83. {
  84. if(path!=null && path.Length > 0)
  85. {
  86. return (path[0]=='/' || path[0]=='\\');
  87. }
  88. return false;
  89. }
  90. public static void FailIfPhysicalPath(string path)
  91. {
  92. if(path!= null && path.Length > 0)
  93. {
  94. if(path[0]==':' || path.StartsWith(@"\\"))
  95. throw new HttpException(HttpRuntime.FormatResourceString("Physical_path_not_allowed", path));
  96. }
  97. }
  98. public static string Combine(string basePath, string relPath)
  99. {
  100. FailIfPhysicalPath(relPath);
  101. if(IsRootUrl(relPath))
  102. {
  103. if(relPath != null && relPath.Length > 0)
  104. {
  105. return Reduce(relPath);
  106. }
  107. return String.Empty;
  108. }
  109. if(relPath.Length < 3 || relPath[0]!='~' || (relPath[0]!='/' && relPath[0]!='\\'))
  110. {
  111. if(basePath==null || basePath.Length==1 || basePath[0]=='/')
  112. basePath = String.Empty;
  113. return Reduce(basePath + "/" + relPath);
  114. }
  115. string vPath = HttpRuntime.AppDomainAppVirtualPath;
  116. if(vPath.Length <= 1)
  117. vPath = String.Empty;
  118. return Reduce(vPath + "/" + relPath.Substring(2));
  119. }
  120. public static bool IsValidProtocol(string protocol)
  121. {
  122. if(protocol.Length < 1)
  123. return false;
  124. char c = protocol[0];
  125. if(!Char.IsLetter(c))
  126. {
  127. return false;
  128. }
  129. for(int i=1; i < protocol.Length; i++)
  130. {
  131. c = protocol[i];
  132. if(!Char.IsLetterOrDigit(c) && c!='.' && c!='+' && c!='-')
  133. {
  134. return false;
  135. }
  136. }
  137. return true;
  138. }
  139. /*
  140. * MakeRelative("http://www.foo.com/bar1/bar2/file","http://www.foo.com/bar1")
  141. * will return "bar2/file"
  142. * while MakeRelative("http://www.foo.com/bar1/...","http://www.anotherfoo.com")
  143. * return 'null' and so does the call
  144. * MakeRelative("http://www.foo.com/bar1/bar2","http://www.foo.com/bar")
  145. */
  146. public static string MakeRelative(string fullUrl, string relativeTo)
  147. {
  148. if(fullUrl==relativeTo)
  149. {
  150. return String.Empty;
  151. }
  152. if(fullUrl.IndexOf(relativeTo)!=0)
  153. {
  154. return null;
  155. }
  156. string leftOver = fullUrl.Substring(relativeTo.Length);
  157. if(!fullUrl.EndsWith("/") && !leftOver.StartsWith("/"))
  158. {
  159. return null;
  160. }
  161. if(leftOver.StartsWith("/"))
  162. {
  163. leftOver = leftOver.Substring(1);
  164. }
  165. return leftOver;
  166. }
  167. /*
  168. * Check JavaDocs for java.lang.String#RegionMatches(bool, int, String, int, int)
  169. * Could not find anything similar in the System.String class
  170. */
  171. public static bool RegionMatches(bool ignoreCase, string source, int start, string match, int offset, int len)
  172. {
  173. if(source!=null || match!=null)
  174. {
  175. if(source.Length>0 && match.Length>0)
  176. {
  177. char[] ta = source.ToCharArray();
  178. char[] pa = match.ToCharArray();
  179. if((offset < 0) || (start < 0) || (start > (source.Length - len)) || (offset > (match.Length - len)))
  180. {
  181. return false;
  182. }
  183. while(len-- > 0)
  184. {
  185. char c1 = ta[start++];
  186. char c2 = pa[offset++];
  187. if(c1==c2)
  188. continue;
  189. if(ignoreCase)
  190. {
  191. if(Char.ToUpper(c1)==Char.ToUpper(c2))
  192. continue;
  193. // Check for Gregorian Calendar where the above may not hold good
  194. if(Char.ToLower(c1)==Char.ToLower(c2))
  195. continue;
  196. }
  197. return false;
  198. }
  199. return true;
  200. }
  201. }
  202. return false;
  203. }
  204. public static string Reduce(string path)
  205. {
  206. int len = path.Length;
  207. int dotIndex = -1;
  208. path = path.Replace('\\','/');
  209. while(true)
  210. {
  211. dotIndex++;
  212. dotIndex = path.IndexOf('.', dotIndex);
  213. if(dotIndex < 0)
  214. {
  215. return path;
  216. }
  217. if(dotIndex != 0 && path[dotIndex -1]=='/')
  218. continue;
  219. if(dotIndex+1 == len || path[dotIndex+1]=='/')
  220. break;
  221. if(path[dotIndex+1]=='.')
  222. continue;
  223. if(dotIndex+2 == len || path[dotIndex+2]=='/')
  224. break;
  225. }
  226. ArrayList list = new ArrayList();
  227. StringBuilder sb = new StringBuilder();
  228. dotIndex = 0;
  229. int temp;
  230. do
  231. {
  232. temp = dotIndex;
  233. dotIndex = path.IndexOf('/', temp + 1);
  234. if(dotIndex < 0)
  235. dotIndex = len;
  236. if( (dotIndex - temp) <= 3 && (dotIndex < 1 || path[dotIndex - 1]== '.') && ( (temp+1) >= len || path[temp+1]=='.') )
  237. {
  238. if(dotIndex - temp == 3)
  239. continue;
  240. if(list.Count == 0)
  241. throw new System.Web.HttpException(System.Web.HttpRuntime.FormatResourceString("Cannot_exit_up_top_directory"));
  242. sb.Length = (int) list[list.Count - 1];
  243. list.RemoveRange(list.Count - 1, 1);
  244. continue;
  245. }
  246. list.Add(sb.Length);
  247. sb.Append(path, temp, dotIndex - temp);
  248. } while(dotIndex != len);
  249. return sb.ToString();
  250. }
  251. public static string GetDirectory(string url)
  252. {
  253. if(url==null)
  254. {
  255. return null;
  256. }
  257. if(url.Length==0)
  258. {
  259. return String.Empty;
  260. }
  261. url.Replace('\\','/');
  262. string baseDir = url.Substring(0, url.LastIndexOf('/'));
  263. if(baseDir.Length==0)
  264. {
  265. baseDir = "/";
  266. }
  267. return baseDir;
  268. }
  269. }
  270. }