UrlUtils.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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.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. return false;
  82. }
  83. for(int i=1; i < protocol.Length; i++)
  84. {
  85. c = protocol[i];
  86. if(!Char.IsLetterOrDigit(c) && c!='.' && c!='+' && c!='-')
  87. {
  88. return false;
  89. }
  90. }
  91. return true;
  92. }
  93. /*
  94. * MakeRelative("http://www.foo.com/bar1/bar2/file","http://www.foo.com/bar1")
  95. * will return "bar2/file"
  96. * while MakeRelative("http://www.foo.com/bar1/...","http://www.anotherfoo.com")
  97. * return 'null' and so does the call
  98. * MakeRelative("http://www.foo.com/bar1/bar2","http://www.foo.com/bar")
  99. */
  100. public static string MakeRelative(string fullUrl, string relativeTo)
  101. {
  102. //Uri fromUri;
  103. //Uri toUri;
  104. if(fullUrl==relativeTo)
  105. {
  106. return String.Empty;
  107. }
  108. if(fullUrl.IndexOf(relativeTo)!=0)
  109. {
  110. return null;
  111. }
  112. string leftOver = fullUrl.Substring(relativeTo.Length);
  113. if(!fullUrl.EndsWith("/") && !leftOver.StartsWith("/"))
  114. {
  115. return null;
  116. }
  117. if(leftOver.StartsWith("/"))
  118. {
  119. leftOver = leftOver.Substring(1);
  120. }
  121. return leftOver;
  122. }
  123. /*
  124. * Check JavaDocs for java.lang.String#RegionMatches(bool, int, String, int, int)
  125. * Could not find anything similar in the System.String class
  126. */
  127. public static bool RegionMatches(bool ignoreCase, string source, int start, string match, int offset, int len)
  128. {
  129. if(source!=null || match!=null)
  130. {
  131. if(source.Length>0 && match.Length>0)
  132. {
  133. char[] ta = source.ToCharArray();
  134. char[] pa = match.ToCharArray();
  135. if((offset < 0) || (start < 0) || (start > (source.Length - len)) || (offset > (match.Length - len)))
  136. {
  137. return false;
  138. }
  139. while(len-- > 0)
  140. {
  141. char c1 = ta[start++];
  142. char c2 = pa[offset++];
  143. if(c1==c2)
  144. continue;
  145. if(ignoreCase)
  146. {
  147. if(Char.ToUpper(c1)==Char.ToUpper(c2))
  148. continue;
  149. // Check for Gregorian Calendar where the above may not hold good
  150. if(Char.ToLower(c1)==Char.ToLower(c2))
  151. continue;
  152. }
  153. return false;
  154. }
  155. return true;
  156. }
  157. }
  158. return false;
  159. }
  160. public static string GetDirectory(string url)
  161. {
  162. if(url==null)
  163. {
  164. return null;
  165. }
  166. if(url.Length==0)
  167. {
  168. return String.Empty;
  169. }
  170. url.Replace('\\','/');
  171. string baseDir = url.Substring(0, url.LastIndexOf('/'));
  172. if(baseDir.Length==0)
  173. {
  174. baseDir = "/";
  175. }
  176. return baseDir;
  177. }
  178. }
  179. }