2001:11:12 Gaurav Vaish <gvaish@iitk.ac.in>
[mono.git] / mcs / class / System.Web / System.Web.WebUtils / UrlUtils.cs
1 /**\r
2  * Namespace: System.Web.UI.WebUtils\r
3  * Class:     UrlUtils\r
4  * \r
5  * Author:  Gaurav Vaish\r
6  * Contact: <gvaish@iitk.ac.in>\r
7  * Status:  10??%\r
8  * \r
9  * (C) Gaurav Vaish (2001)\r
10  */\r
11 \r
12 using System;\r
13 \r
14 namespace System.Web.UI.WebUtils\r
15 {\r
16         internal class UrlUtils\r
17         {\r
18                 /*\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
26                  */\r
27                 public static string GetProtocol(string url)\r
28                 {\r
29                         //Taking code from Java Class java.net.URL\r
30                         if(url!=null)\r
31                         {\r
32                                 if(url.Length>0)\r
33                                 {\r
34                                         \r
35                                         int i, start = 0, limit;\r
36                                         limit = url.Length;\r
37                                         char c;\r
38                                         bool aRef = false;\r
39                                         while( (limit > 0) && (url[limit-1] <= ' '))\r
40                                         {\r
41                                                 limit --;\r
42                                         }\r
43                                         while( (start < limit) && (url[start] <= ' '))\r
44                                         {\r
45                                                 start++;\r
46                                         }\r
47                                         if(RegionMatches(true, url, start, "url:", 0, 4))\r
48                                         {\r
49                                                 start += 4;\r
50                                         }\r
51                                         if(start < url.Length && url[start]=='#')\r
52                                         {\r
53                                                 aRef = true;\r
54                                         }\r
55                                         for(i = start; !aRef && (i < limit) && ((c=url[i]) != '/'); i++)\r
56                                         {\r
57                                                 if(c==':')\r
58                                                 {\r
59                                                         return url.Substring(start, i - start);\r
60                                                 }\r
61                                         }\r
62                                 }\r
63                         }\r
64                         return String.Empty;\r
65                 }\r
66                 \r
67                 public static bool IsRootUrl(string url)\r
68                 {\r
69                         if(url!=null)\r
70                         {\r
71                                 if(url.Length>0)\r
72                                 {\r
73                                         return IsValidProtocol(GetProtocol(url).ToLower());\r
74                                 }\r
75                         }\r
76                         return false;\r
77                 }\r
78                 \r
79                 public static bool IsValidProtocol(string protocol)\r
80                 {\r
81                         if(protocol.Length < 1)\r
82                                 return false;\r
83                         char c = protocol[0];\r
84                         if(!Char.IsLetter(c))\r
85                         {\r
86                                 System.Console.WriteLine("Character {0} is not a letter.", c);\r
87                                 return false;\r
88                         }\r
89                         for(int i=1; i < protocol.Length; i++)\r
90                         {\r
91                                 c = protocol[i];\r
92                                 if(!Char.IsLetterOrDigit(c) && c!='.' && c!='+' && c!='-')\r
93                                 {\r
94                                         System.Console.WriteLine("Character \"{0}\" is not a letter or a digit or something.", c);\r
95                                         return false;\r
96                                 }\r
97                         }\r
98                         return true;\r
99                 }\r
100                 \r
101                 /*\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
107                  */\r
108                 public static string MakeRelative(string fullUrl, string relativeTo)\r
109                 {\r
110                         //Uri fromUri;\r
111                         //Uri toUri;\r
112                         if(fullUrl==relativeTo)\r
113                         {\r
114                                 return String.Empty;\r
115                         }\r
116                         if(fullUrl.IndexOf(relativeTo)!=0)\r
117                         {\r
118                                 return null;\r
119                         }\r
120                         string leftOver = fullUrl.Substring(relativeTo.Length);\r
121                         if(!fullUrl.EndsWith("/") && !leftOver.StartsWith("/"))\r
122                         {\r
123                                 return null;\r
124                         }\r
125                         if(leftOver.StartsWith("/"))\r
126                         {\r
127                                 leftOver = leftOver.Substring(1);\r
128                         }\r
129                         return leftOver;\r
130                 }\r
131                 \r
132                 /*\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
135                  */\r
136                 public static bool RegionMatches(bool ignoreCase, string source, int start, string match, int offset, int len)\r
137                 {\r
138                         if(source!=null || match!=null)\r
139                         {\r
140                                 if(source.Length>0 && match.Length>0)\r
141                                 {\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
145                                         {\r
146                                                 return false;\r
147                                         }\r
148                                         while(len-- > 0)\r
149                                         {\r
150                                                 char c1 = ta[start++];\r
151                                                 char c2 = pa[offset++];\r
152                                                 if(c1==c2)\r
153                                                         continue;\r
154                                                 if(ignoreCase)\r
155                                                 {\r
156                                                         if(Char.ToUpper(c1)==Char.ToUpper(c2))\r
157                                                                 continue;\r
158                                                         // Check for Gregorian Calendar where the above may not hold good\r
159                                                         if(Char.ToLower(c1)==Char.ToLower(c2))\r
160                                                                 continue;\r
161                                                 }\r
162                                                 return false;\r
163                                         }\r
164                                         return true;\r
165                                 }\r
166                         }\r
167                         return false;\r
168                 }\r
169                 \r
170                 public static string GetDirectory(string url)\r
171                 {\r
172                         if(url==null)\r
173                         {\r
174                                 return null;\r
175                         }\r
176                         if(url.Length==0)\r
177                         {\r
178                                 return String.Empty;\r
179                         }\r
180                         url.Replace('\\','/');\r
181                         string baseDir = url.Substring(0, url.LastIndexOf('/'));\r
182                         if(baseDir.Length==0)\r
183                         {\r
184                                 baseDir = "/";\r
185                         }\r
186                         return baseDir;\r
187                 }\r
188         }\r
189 }\r