UrlUtils.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /**
  2. * Namespace: System.Web.UI.Utils
  3. * Class: UrlUtils
  4. *
  5. * Author: Gaurav Vaish
  6. * Contact: <[email protected]>
  7. * Status: 10??%
  8. *
  9. * (C) Gaurav Vaish (2001)
  10. */
  11. using System;
  12. namespace System.Web.UI.Utils
  13. {
  14. internal class UrlUtils
  15. {
  16. /*
  17. * I could not find these functions in the class System.Uri
  18. * Besides, an instance of Uri will not be formed until and unless the address is of
  19. * the form protocol://[user:pass]host[:port]/[fullpath]
  20. * ie, a protocol, and that too without any blanks before,
  21. * is a must which may not be the case here.
  22. * Important: Escaped URL is assumed here. nothing like .aspx?path=/something
  23. * It should be .aspx?path=%2Fsomething
  24. */
  25. public static string GetProtocol(string url)
  26. {
  27. //Taking code from Java Class java.net.URL
  28. if(url!=null)
  29. {
  30. if(url.Length>0)
  31. {
  32. int i, start = 0, limit;
  33. limit = url.Length;
  34. char c;
  35. bool aRef = false;
  36. while( (limit > 0) && (url[limit-1] <= ' '))
  37. {
  38. limit --;
  39. }
  40. while( (start < limit) && (url[start] <= ' '))
  41. {
  42. start++;
  43. }
  44. if(RegionMatches(true, url, start, "url:", 0, 4))
  45. {
  46. start += 4;
  47. }
  48. if(start < url.Length && url[start]=='#')
  49. {
  50. aRef = true;
  51. }
  52. for(i = start; !aRef && (i < limit) && ((c=url[i]) != '/'); i++)
  53. {
  54. if(c==':')
  55. {
  56. return url.Substring(start, i - start);
  57. }
  58. }
  59. }
  60. }
  61. return String.Empty;
  62. }
  63. public static bool IsRootUrl(string url)
  64. {
  65. if(url!=null)
  66. {
  67. if(url.Length>0)
  68. {
  69. return IsValidProtocol(GetProtocol(url).ToLower());
  70. }
  71. }
  72. return false;
  73. }
  74. public static bool IsValidProtocol(string protocol)
  75. {
  76. if(protocol.Length < 1)
  77. return false;
  78. char c = protocol[0];
  79. if(!Char.IsLetter(c))
  80. {
  81. System.Console.WriteLine("Character {0} is not a letter.", c);
  82. return false;
  83. }
  84. for(int i=1; i < protocol.Length; i++)
  85. {
  86. c = protocol[i];
  87. if(!Char.IsLetterOrDigit(c) && c!='.' && c!='+' && c!='-')
  88. {
  89. System.Console.WriteLine("Character \"{0}\" is not a letter or a digit or something.", c);
  90. return false;
  91. }
  92. }
  93. return true;
  94. }
  95. /*
  96. * MakeRelative("http://www.foo.com/bar1/bar2/file","http://www.foo.com/bar1")
  97. * will return "bar2/file"
  98. * while MakeRelative("http://www.foo.com/bar1/...","http://www.anotherfoo.com")
  99. * return 'null' and so does the call
  100. * MakeRelative("http://www.foo.com/bar1/bar2","http://www.foo.com/bar")
  101. */
  102. public static string MakeRelative(string fullUrl, string relativeTo)
  103. {
  104. //Uri fromUri;
  105. //Uri toUri;
  106. if(fullUrl==relativeTo)
  107. {
  108. return String.Empty;
  109. }
  110. if(fullUrl.IndexOf(relativeTo)!=0)
  111. {
  112. return null;
  113. }
  114. string leftOver = fullUrl.Substring(relativeTo.Length);
  115. if(!fullUrl.EndsWith("/") && !leftOver.StartsWith("/"))
  116. {
  117. return null;
  118. }
  119. if(leftOver.StartsWith("/"))
  120. {
  121. leftOver = leftOver.Substring(1);
  122. }
  123. return leftOver;
  124. }
  125. /*
  126. * Check JavaDocs for java.lang.String#RegionMatches(bool, int, String, int, int)
  127. * Could not find anything similar in the System.String class
  128. */
  129. public static bool RegionMatches(bool ignoreCase, string source, int start, string match, int offset, int len)
  130. {
  131. if(source!=null || match!=null)
  132. {
  133. if(source.Length>0 && match.Length>0)
  134. {
  135. char[] ta = source.ToCharArray();
  136. char[] pa = match.ToCharArray();
  137. if((offset < 0) || (start < 0) || (start > (source.Length - len)) || (offset > (match.Length - len)))
  138. {
  139. return false;
  140. }
  141. while(len-- > 0)
  142. {
  143. char c1 = ta[start++];
  144. char c2 = pa[offset++];
  145. if(c1==c2)
  146. continue;
  147. if(ignoreCase)
  148. {
  149. if(Char.ToUpper(c1)==Char.ToUpper(c2))
  150. continue;
  151. // Check for Gregorian Calendar where the above may not hold good
  152. if(Char.ToLower(c1)==Char.ToLower(c2))
  153. continue;
  154. }
  155. return false;
  156. }
  157. return true;
  158. }
  159. }
  160. return false;
  161. }
  162. public static string GetDirectory(string url)
  163. {
  164. if(url==null)
  165. {
  166. return null;
  167. }
  168. if(url.Length==0)
  169. {
  170. return String.Empty;
  171. }
  172. url.Replace('\\','/');
  173. string baseDir = url.Substring(0, url.LastIndexOf('/'));
  174. if(baseDir.Length==0)
  175. {
  176. baseDir = "/";
  177. }
  178. return baseDir;
  179. }
  180. }
  181. }