UrlUtils.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. if (relPath == null)
  101. throw new ArgumentNullException ("relPath");
  102. int rlength = relPath.Length;
  103. if (rlength == 0)
  104. return "";
  105. FailIfPhysicalPath (relPath);
  106. if (IsRooted (relPath))
  107. return Reduce (relPath);
  108. char first = relPath [0];
  109. if (rlength < 3 || first == '~' || first == '/' || first == '\\') {
  110. if (basePath == null || (basePath.Length == 1 && basePath [0] == '/'))
  111. basePath = String.Empty;
  112. if (first == '~') {
  113. if (rlength == 1) {
  114. relPath = "";
  115. } else if (rlength > 1 && relPath [1] == '/') {
  116. relPath = relPath.Substring (2);
  117. }
  118. }
  119. string slash = (first == '/') ? "" : "/";
  120. return Reduce (basePath + slash + relPath);
  121. }
  122. if (basePath == null || basePath == "") {
  123. basePath = HttpRuntime.AppDomainAppVirtualPath;
  124. if (basePath.Length <= 1)
  125. basePath = String.Empty;
  126. }
  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. throw new HttpException ("Invalid path.");
  226. result.RemoveAt (result.Count - 1);
  227. continue;
  228. }
  229. result.Add (current);
  230. }
  231. if (result.Count == 0)
  232. return "/";
  233. result.Insert (0, "");
  234. return String.Join ("/", (string []) result.ToArray (typeof (string)));
  235. }
  236. public static string GetDirectory(string url)
  237. {
  238. if(url==null)
  239. {
  240. return null;
  241. }
  242. if(url.Length==0)
  243. {
  244. return String.Empty;
  245. }
  246. url.Replace('\\','/');
  247. string baseDir = "";
  248. int last = url.LastIndexOf ('/');
  249. if (last > 0)
  250. baseDir = url.Substring(0, url.LastIndexOf('/'));
  251. if(baseDir.Length==0)
  252. {
  253. baseDir = "/";
  254. }
  255. return baseDir;
  256. }
  257. public static string ResolveVirtualPathFromAppAbsolute (string path)
  258. {
  259. if (path [0] != '~') return path;
  260. if (path.Length == 1)
  261. return HttpRuntime.AppDomainAppVirtualPath;
  262. if (path [1] == '/' || path [1] == '\\') {
  263. string appPath = HttpRuntime.AppDomainAppVirtualPath;
  264. if (appPath.Length > 1)
  265. return appPath + "/" + path.Substring (2);
  266. return "/" + path.Substring (2);
  267. }
  268. return path;
  269. }
  270. public static string ResolvePhysicalPathFromAppAbsolute (string path)
  271. {
  272. if (path [0] != '~') return path;
  273. if (path.Length == 1)
  274. return HttpRuntime.AppDomainAppPath;
  275. if (path [1] == '/' || path [1] == '\\') {
  276. string appPath = HttpRuntime.AppDomainAppPath;
  277. if (appPath.Length > 1)
  278. return appPath + "/" + path.Substring (2);
  279. return "/" + path.Substring (2);
  280. }
  281. return path;
  282. }
  283. }
  284. }