2 * Namespace: System.Web.UI.WebUtils
\r
5 * Author: Gaurav Vaish
\r
6 * Contact: <gvaish@iitk.ac.in>
\r
9 * (C) Gaurav Vaish (2001)
\r
14 namespace System.Web.UI.WebUtils
\r
16 internal class UrlUtils
\r
19 * I could not find these functions in the class System.Uri
\r
20 * Besides, an instance of Uri will not be formed until and unless the address is of
\r
21 * the form protocol://[user:pass]host[:port]/[fullpath]
\r
22 * ie, a protocol, and that too without any blanks before,
\r
23 * is a must which may not be the case here.
\r
24 * Important: Escaped URL is assumed here. nothing like .aspx?path=/something
\r
25 * It should be .aspx?path=%2Fsomething
\r
27 public static string GetProtocol(string url)
\r
29 //Taking code from Java Class java.net.URL
\r
35 int i, start = 0, limit;
\r
39 while( (limit > 0) && (url[limit-1] <= ' '))
\r
43 while( (start < limit) && (url[start] <= ' '))
\r
47 if(RegionMatches(true, url, start, "url:", 0, 4))
\r
51 if(start < url.Length && url[start]=='#')
\r
55 for(i = start; !aRef && (i < limit) && ((c=url[i]) != '/'); i++)
\r
59 return url.Substring(start, i - start);
\r
64 return String.Empty;
\r
67 public static bool IsRootUrl(string url)
\r
73 return IsValidProtocol(GetProtocol(url).ToLower());
\r
79 public static bool IsValidProtocol(string protocol)
\r
81 if(protocol.Length < 1)
\r
83 char c = protocol[0];
\r
84 if(!Char.IsLetter(c))
\r
86 System.Console.WriteLine("Character {0} is not a letter.", c);
\r
89 for(int i=1; i < protocol.Length; i++)
\r
92 if(!Char.IsLetterOrDigit(c) && c!='.' && c!='+' && c!='-')
\r
94 System.Console.WriteLine("Character \"{0}\" is not a letter or a digit or something.", c);
\r
102 * MakeRelative("http://www.foo.com/bar1/bar2/file","http://www.foo.com/bar1")
\r
103 * will return "bar2/file"
\r
104 * while MakeRelative("http://www.foo.com/bar1/...","http://www.anotherfoo.com")
\r
105 * return 'null' and so does the call
\r
106 * MakeRelative("http://www.foo.com/bar1/bar2","http://www.foo.com/bar")
\r
108 public static string MakeRelative(string fullUrl, string relativeTo)
\r
112 if(fullUrl==relativeTo)
\r
114 return String.Empty;
\r
116 if(fullUrl.IndexOf(relativeTo)!=0)
\r
120 string leftOver = fullUrl.Substring(relativeTo.Length);
\r
121 if(!fullUrl.EndsWith("/") && !leftOver.StartsWith("/"))
\r
125 if(leftOver.StartsWith("/"))
\r
127 leftOver = leftOver.Substring(1);
\r
133 * Check JavaDocs for java.lang.String#RegionMatches(bool, int, String, int, int)
\r
134 * Could not find anything similar in the System.String class
\r
136 public static bool RegionMatches(bool ignoreCase, string source, int start, string match, int offset, int len)
\r
138 if(source!=null || match!=null)
\r
140 if(source.Length>0 && match.Length>0)
\r
142 char[] ta = source.ToCharArray();
\r
143 char[] pa = match.ToCharArray();
\r
144 if((offset < 0) || (start < 0) || (start > (source.Length - len)) || (offset > (match.Length - len)))
\r
150 char c1 = ta[start++];
\r
151 char c2 = pa[offset++];
\r
156 if(Char.ToUpper(c1)==Char.ToUpper(c2))
\r
158 // Check for Gregorian Calendar where the above may not hold good
\r
159 if(Char.ToLower(c1)==Char.ToLower(c2))
\r
170 public static string GetDirectory(string url)
\r
178 return String.Empty;
\r
180 url.Replace('\\','/');
\r
181 string baseDir = url.Substring(0, url.LastIndexOf('/'));
\r
182 if(baseDir.Length==0)
\r