UrlUtils.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 !IsRooted(url);
  69. return false;
  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 true;
  89. }
  90. public static void FailIfPhysicalPath(string path)
  91. {
  92. if(path!= null && path.Length > 1)
  93. {
  94. if(path[1]==':' || 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. if (relPath != null && relPath.Length > 0)
  103. return Reduce (relPath);
  104. return String.Empty;
  105. }
  106. if (relPath.Length < 3 || relPath [0] != '~' || relPath [0] == '/' || relPath [0] == '\\') {
  107. if (basePath == null || (basePath.Length == 1 && basePath [0] == '/'))
  108. basePath = String.Empty;
  109. string slash = relPath [0] == '/' ? "" : "/";
  110. return Reduce (basePath + slash + relPath);
  111. }
  112. string vPath = HttpRuntime.AppDomainAppVirtualPath;
  113. if (vPath.Length <= 1)
  114. vPath = String.Empty;
  115. return Reduce (vPath + "/" + relPath.Substring (2));
  116. }
  117. public static bool IsValidProtocol(string protocol)
  118. {
  119. if(protocol.Length < 1)
  120. return false;
  121. char c = protocol[0];
  122. if(!Char.IsLetter(c))
  123. {
  124. return false;
  125. }
  126. for(int i=1; i < protocol.Length; i++)
  127. {
  128. c = protocol[i];
  129. if(!Char.IsLetterOrDigit(c) && c!='.' && c!='+' && c!='-')
  130. {
  131. return false;
  132. }
  133. }
  134. return true;
  135. }
  136. /*
  137. * MakeRelative("http://www.foo.com/bar1/bar2/file","http://www.foo.com/bar1")
  138. * will return "bar2/file"
  139. * while MakeRelative("http://www.foo.com/bar1/...","http://www.anotherfoo.com")
  140. * return 'null' and so does the call
  141. * MakeRelative("http://www.foo.com/bar1/bar2","http://www.foo.com/bar")
  142. */
  143. public static string MakeRelative(string fullUrl, string relativeTo)
  144. {
  145. if(fullUrl==relativeTo)
  146. {
  147. return String.Empty;
  148. }
  149. if(fullUrl.IndexOf(relativeTo)!=0)
  150. {
  151. return null;
  152. }
  153. string leftOver = fullUrl.Substring(relativeTo.Length);
  154. if(!fullUrl.EndsWith("/") && !leftOver.StartsWith("/"))
  155. {
  156. return null;
  157. }
  158. if(leftOver.StartsWith("/"))
  159. {
  160. leftOver = leftOver.Substring(1);
  161. }
  162. return leftOver;
  163. }
  164. /*
  165. * Check JavaDocs for java.lang.String#RegionMatches(bool, int, String, int, int)
  166. * Could not find anything similar in the System.String class
  167. */
  168. public static bool RegionMatches(bool ignoreCase, string source, int start, string match, int offset, int len)
  169. {
  170. if(source!=null || match!=null)
  171. {
  172. if(source.Length>0 && match.Length>0)
  173. {
  174. char[] ta = source.ToCharArray();
  175. char[] pa = match.ToCharArray();
  176. if((offset < 0) || (start < 0) || (start > (source.Length - len)) || (offset > (match.Length - len)))
  177. {
  178. return false;
  179. }
  180. while(len-- > 0)
  181. {
  182. char c1 = ta[start++];
  183. char c2 = pa[offset++];
  184. if(c1==c2)
  185. continue;
  186. if(ignoreCase)
  187. {
  188. if(Char.ToUpper(c1)==Char.ToUpper(c2))
  189. continue;
  190. // Check for Gregorian Calendar where the above may not hold good
  191. if(Char.ToLower(c1)==Char.ToLower(c2))
  192. continue;
  193. }
  194. return false;
  195. }
  196. return true;
  197. }
  198. }
  199. return false;
  200. }
  201. public static string Reduce(string path)
  202. {
  203. int len = path.Length;
  204. int dotIndex = -1;
  205. path = path.Replace('\\','/');
  206. while(true)
  207. {
  208. dotIndex++;
  209. dotIndex = path.IndexOf('.', dotIndex);
  210. if(dotIndex < 0)
  211. {
  212. return path;
  213. }
  214. if(dotIndex != 0 && path[dotIndex -1]=='/')
  215. continue;
  216. if(dotIndex+1 == len || path[dotIndex+1]=='/')
  217. break;
  218. if(path[dotIndex+1]=='.')
  219. continue;
  220. if(dotIndex+2 == len || path[dotIndex+2]=='/')
  221. break;
  222. }
  223. ArrayList list = new ArrayList();
  224. StringBuilder sb = new StringBuilder();
  225. dotIndex = 0;
  226. int temp;
  227. do
  228. {
  229. temp = dotIndex;
  230. dotIndex = path.IndexOf('/', temp + 1);
  231. if(dotIndex < 0)
  232. dotIndex = len;
  233. if( (dotIndex - temp) <= 3 && (dotIndex < 1 || path[dotIndex - 1]== '.') && ( (temp+1) >= len || path[temp+1]=='.') )
  234. {
  235. if(dotIndex - temp == 3)
  236. continue;
  237. if(list.Count == 0)
  238. throw new System.Web.HttpException(System.Web.HttpRuntime.FormatResourceString("Cannot_exit_up_top_directory"));
  239. sb.Length = (int) list[list.Count - 1];
  240. list.RemoveRange(list.Count - 1, 1);
  241. continue;
  242. }
  243. list.Add(sb.Length);
  244. sb.Append(path, temp, dotIndex - temp);
  245. } while(dotIndex != len);
  246. return sb.ToString();
  247. }
  248. public static string GetDirectory(string url)
  249. {
  250. if(url==null)
  251. {
  252. return null;
  253. }
  254. if(url.Length==0)
  255. {
  256. return String.Empty;
  257. }
  258. url.Replace('\\','/');
  259. string baseDir = "";
  260. int last = url.LastIndexOf ('/');
  261. if (last > 0)
  262. baseDir = url.Substring(0, url.LastIndexOf('/'));
  263. if(baseDir.Length==0)
  264. {
  265. baseDir = "/";
  266. }
  267. return baseDir;
  268. }
  269. }
  270. }