UrlUtils.cs 6.2 KB

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