2
0

UrlUtils.cs 6.9 KB

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