UrlUtils.cs 8.6 KB

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