Another Random.cs compile fix.
[mono.git] / mcs / class / corlib / System / Uri.cs
1 //
2 // System.Uri
3 //
4 // Author:
5 //    Garrett Rooney (rooneg@electricjellyfish.net)
6 //
7 // (C) 2001 Garrett Rooney
8 //
9
10 using System.Runtime.Serialization;
11
12 namespace System {
13
14         public class Uri : MarshalByRefObject, ISerializable {
15
16                 private string path = "";
17                 private string host = "";
18                 private string fragment = "";
19                 private string scheme = "";
20                 private string port = "";
21                 private string query = "";
22                 
23                 // FIXME: is this correct?
24                 private bool userEscaped = false;
25
26                 public static readonly string SchemeDelimiter = "://";
27                 public static readonly string UriSchemeFile = "file";
28                 public static readonly string UriSchemeFtp = "ftp";
29                 public static readonly string UriSchemeGopher = "gopher";
30                 public static readonly string UriSchemeHttp = "http";
31                 public static readonly string UriSchemeHttps = "https";
32                 public static readonly string UriSchemeMailto = "mailto";
33                 public static readonly string UriSchemeNntp = "nntp";
34
35                 // the details table holds random info about the types of uri's
36                 private struct detail {
37                         public string scheme;
38                         public string port;
39                         public string delimiter;
40
41                         public detail(string s, string p, string d) {
42                                 scheme = s;
43                                 port = p;
44                                 delimiter = d;
45                         }
46                 };
47
48                 static detail[] details = new detail[] {
49                         new detail(UriSchemeFile, "-1", SchemeDelimiter),
50                         new detail(UriSchemeFtp, "23", SchemeDelimiter),
51                         new detail(UriSchemeGopher, "70", SchemeDelimiter),
52                         new detail(UriSchemeHttp, "80", SchemeDelimiter),
53                         new detail(UriSchemeHttps, "223", SchemeDelimiter),
54                         new detail(UriSchemeMailto, "25", ":"),
55                         new detail(UriSchemeNntp, "119", SchemeDelimiter)
56                 };
57
58                 public static UriHostNameType CheckHostName(string name) {
59                         throw new NotImplementedException();
60                 }
61
62                 public static bool CheckSchemeName(string schemeName) {
63                         throw new NotImplementedException();
64                 }
65
66                 public static int FromHex(char digit) {
67                         throw new NotImplementedException();
68                 }
69
70                 public static string HexEscape(char character) {
71                         throw new NotImplementedException();
72                 }
73
74                 public static char HexUnescape(string pattern, ref int index) {
75                         throw new NotImplementedException();
76                 }
77
78                 public static bool IsHexDigit(char character) {
79                         throw new NotImplementedException();
80                 }
81
82                 public static bool IsHexEncoding(string pattern, int index) {
83                         throw new NotImplementedException();
84                 }
85
86                 private void Parse(string uri) {
87                         int i;
88
89                         // figure out the scheme
90                         int colon = uri.IndexOf(':');
91                         if (colon == -1 || colon == uri.Length) {
92                                 throw new UriFormatException();
93                         } else {
94                                 string s = uri.Substring(0, colon).ToLower();
95                                 uri = uri.Remove(0, colon);
96                                 for (i = 0; i < details.Length; i++) {
97                                         if (details[i].scheme == s) {
98                                                 scheme = details[i].scheme;
99
100                                                 // assume default port.  if 
101                                                 // they specify one it'll get 
102                                                 // set later on.
103                                                 port = details[i].port;
104                                                 break;
105                                         }
106                                 }
107                                 if (i == details.Length) {
108                                         throw new UriFormatException();
109                                 }
110                         }
111
112                         // get rid of the delimiter
113                         uri = uri.Remove(0, details[i].delimiter.Length);
114
115                         parseHost(uri);
116                 }
117
118                 private void parseHost(string uri) {
119
120                         // FIXME: this doesn't handle IPv6 addresses correctly
121                         for (int i = 0; i < uri.Length; i++) {
122                                 switch (uri[i]) {
123                                         case ':':
124                                                 host = uri.Substring(0, i);
125                                                 parsePort(uri.Remove(0, i + 1));
126                                                 return;
127                                         case '/':
128                                                 host = uri.Substring(0, i);
129                                                 parsePath(uri.Remove(0, i + 1));
130                                                 return;
131                                         case '?':
132                                         case '#':
133                                                 throw new UriFormatException();
134                                         default:
135                                                 break;
136                                 }
137                         }
138
139                         host = uri;
140                 }
141
142                 private void parsePort(string uri) {
143
144                         for (int i = 0; i < uri.Length; i++) {
145                                 switch (uri[i]) {
146                                         case '/':
147                                                 port = uri.Substring(0, i);
148                                                 parsePath(uri.Remove(0, i + 1));
149                                                 return;
150                                         case '?':
151                                         case '#':
152                                                 throw new UriFormatException();
153                                         default:
154                                                 // FIXME: should this check if 
155                                                 // uri[i] is a number?
156                                                 break;
157                                 }
158                         }
159
160                         port = uri;
161                 }
162
163                 private void parsePath(string uri) {
164                         
165                         for (int i = 0; i < uri.Length; i++) {
166                                 switch (uri[i]) {
167                                         case '#':
168                                                 path = uri.Substring(0, i);
169                                                 fragment = uri.Remove(0, i + 1);
170                                                 return;
171                                         case '?':
172                                                 path = uri.Substring(0, i);
173                                                 query = uri.Remove(0, i + 1);
174                                                 return;
175                                         default:
176                                                 break;
177                                 }
178                         }
179
180                         path = uri;
181                 }
182
183                 public Uri(string uri) {
184                         Parse(uri);
185                 }
186
187                 protected Uri(SerializationInfo serializationInfo, 
188                               StreamingContext streamingContext) {
189                         throw new NotImplementedException();
190                 }
191
192                 public Uri(string uri, bool dontEscape) {
193                         userEscaped = dontEscape;
194                         Parse(uri);
195                 }
196
197                 public Uri(Uri baseUri, string relativeUri) {
198                         throw new NotImplementedException();
199                 }
200
201                 public Uri(Uri baseUri, string relativeUri, bool dontEscape) {
202                         userEscaped = dontEscape;
203
204                         throw new NotImplementedException();
205                 }
206
207                 public string AbsolutePath { get { return path; } }
208
209                 public string AbsoluteUri { 
210                         get { throw new NotImplementedException(); } 
211                 }
212
213                 public string Authority { get { return host + ":" + port; } }
214
215                 public string Fragment { get { return fragment; } }
216
217                 public string Host { get { return host; } }
218
219                 public UriHostNameType HostNameType { 
220                         get { throw new NotImplementedException(); } 
221                 }
222
223                 public bool IsDefaultPort { 
224                         get { 
225                                 for (int i = 0; i < details.Length; i++) {
226                                         if (details[i].scheme == scheme) {
227                                                 if (details[i].port == port) {
228                                                         return true;
229                                                 }
230                                         }
231                                 }
232                                 
233                                 return false;                   
234                         } 
235                 }
236
237                 public bool IsFile { 
238                         get {
239                                 if (scheme == UriSchemeFile)
240                                         return true;
241                                 else
242                                         return false;
243                         }
244                 }
245
246                 // FIXME: should check IPv6
247                 public bool IsLoopback { 
248                         get { 
249                                 if (host == "localhost" || host == "127.0.0.1")
250                                         return true;
251                                 else
252                                         return false;
253                         } 
254                 }
255
256                 public bool IsUnc { 
257                         get { throw new NotImplementedException(); } 
258                 }
259
260                 public string LocalPath { 
261                         get { throw new NotImplementedException(); } 
262                 }
263
264                 public string PathAndQuery { 
265                         get { return path + "?" + query; } 
266                 }
267
268                 public string Port { get { return port; } }
269
270                 public string Query { get { return query; } }
271
272                 public string Scheme { get { return scheme; } }
273
274                 // FIXME: what the hell are segments?
275                 public string[] Segments { 
276                         get { throw new NotImplementedException(); } 
277                 }
278
279                 public bool UserEscaped { get { return userEscaped; } }
280
281                 public string UserInfo { 
282                         get { throw new NotImplementedException(); } 
283                 }
284
285                 public override bool Equals(object compared) {
286                         throw new NotImplementedException();    
287                 }
288
289                 public override int GetHashCode() {
290                         throw new NotImplementedException();    
291                 }
292
293                 public string GetLeftPart(UriPartial part) {
294                         throw new NotImplementedException();    
295                 }
296
297                 public string MakeRelative(Uri toUri) {
298                         throw new NotImplementedException();    
299                 }
300
301                 public override string ToString() {
302                         throw new NotImplementedException();    
303                 }
304
305                 public void GetObjectData(SerializationInfo info, 
306                                           StreamingContext context)
307                 {
308                         // FIXME: Implement me.  yes, it is public because it implements ISerializable
309                 }
310
311                 protected static string EscapeString(string str) {
312                         throw new NotImplementedException();    
313                 }
314         }
315 }