2003-04-11 Sebastien Pouliot <spouliot@videotron.ca>
[mono.git] / mcs / class / System / System.Net / WebRequest.cs
1 //\r
2 // System.Net.WebRequest\r
3 //\r
4 // Author:\r
5 //   Lawrence Pit (loz@cable.a2000.nl)\r
6 //\r
7 \r
8 using System;\r
9 using System.Collections;\r
10 using System.Collections.Specialized;\r
11 using System.IO;\r
12 using System.Runtime.Serialization;\r
13 \r
14 namespace System.Net \r
15 {\r
16         [Serializable]\r
17         public abstract class WebRequest : MarshalByRefObject, ISerializable\r
18         {\r
19                 private static HybridDictionary prefixes;\r
20                 \r
21                 static WebRequest () {\r
22                         prefixes = new HybridDictionary (3, true);\r
23                         RegisterPrefix ("file", new FileWebRequestCreator ());\r
24                         RegisterPrefix ("http", new HttpWebRequestCreator ());\r
25                         RegisterPrefix ("https", new HttpWebRequestCreator ());\r
26                 }\r
27                 \r
28                 internal class HttpWebRequestCreator : IWebRequestCreate\r
29                 {\r
30                         internal HttpWebRequestCreator () { }\r
31                         \r
32                         public WebRequest Create (Uri uri) \r
33                         {\r
34                                 return new HttpWebRequest (uri);\r
35                         }\r
36                 }\r
37 \r
38                 internal class FileWebRequestCreator : IWebRequestCreate\r
39                 {\r
40                         internal FileWebRequestCreator () { }\r
41                         \r
42                         public WebRequest Create (Uri uri) \r
43                         {\r
44                                 return new FileWebRequest (uri);\r
45                         }\r
46                 }\r
47 \r
48                 \r
49                 // Constructors\r
50                 \r
51                 protected WebRequest () { }             \r
52                 \r
53                 protected WebRequest (SerializationInfo serializationInfo, StreamingContext streamingContext) \r
54                 {\r
55                         throw new NotSupportedException ();\r
56                 }\r
57                 \r
58                 // Properties\r
59                 \r
60                 public virtual string ConnectionGroupName { \r
61                         get { throw new NotSupportedException (); }\r
62                         set { throw new NotSupportedException (); }\r
63                 }\r
64                 \r
65                 public virtual long ContentLength { \r
66                         get { throw new NotSupportedException (); }\r
67                         set { throw new NotSupportedException (); }\r
68                 }\r
69                 \r
70                 public virtual string ContentType { \r
71                         get { throw new NotSupportedException (); }\r
72                         set { throw new NotSupportedException (); }\r
73                 }\r
74                 \r
75                 public virtual ICredentials Credentials { \r
76                         get { throw new NotSupportedException (); }\r
77                         set { throw new NotSupportedException (); }\r
78                 }\r
79                 \r
80                 public virtual WebHeaderCollection Headers { \r
81                         get { throw new NotSupportedException (); }\r
82                         set { throw new NotSupportedException (); }\r
83                 }\r
84                 \r
85                 public virtual string Method { \r
86                         get { throw new NotSupportedException (); }\r
87                         set { throw new NotSupportedException (); }\r
88                 }\r
89                 \r
90                 public virtual bool PreAuthenticate { \r
91                         get { throw new NotSupportedException (); }\r
92                         set { throw new NotSupportedException (); }\r
93                 }\r
94                 \r
95                 public virtual IWebProxy Proxy { \r
96                         get { throw new NotSupportedException (); }\r
97                         set { throw new NotSupportedException (); }\r
98                 }\r
99                 \r
100                 public virtual Uri RequestUri { \r
101                         get { throw new NotSupportedException (); }\r
102                         set { throw new NotSupportedException (); }\r
103                 }\r
104                 \r
105                 public virtual int Timeout { \r
106                         get { throw new NotSupportedException (); }\r
107                         set { throw new NotSupportedException (); }\r
108                 }\r
109                 \r
110                 // Methods\r
111                 \r
112                 public virtual void Abort()\r
113                 {\r
114                         throw new NotSupportedException ();\r
115                 }\r
116                 \r
117                 public virtual IAsyncResult BeginGetRequestStream (AsyncCallback callback, object state) \r
118                 {\r
119                         throw new NotSupportedException ();\r
120                 }\r
121                 \r
122                 public virtual IAsyncResult BeginGetResponse (AsyncCallback callback, object state)\r
123                 {\r
124                         throw new NotSupportedException ();\r
125                 }\r
126 \r
127                 public static WebRequest Create (string requestUriString) \r
128                 {\r
129                         if (requestUriString == null)\r
130                                 throw new ArgumentNullException ("requestUriString");\r
131                         return Create (new Uri (requestUriString));\r
132                 }\r
133                                 \r
134                 public static WebRequest Create (Uri requestUri) \r
135                 {\r
136                         if (requestUri == null)\r
137                                 throw new ArgumentNullException ("requestUri");\r
138                         return GetCreator (requestUri.AbsoluteUri).Create (requestUri);\r
139                 }\r
140                 \r
141                 public static WebRequest CreateDefault (Uri requestUri)\r
142                 {\r
143                         if (requestUri == null)\r
144                                 throw new ArgumentNullException ("requestUri");\r
145                         return GetCreator (requestUri.Scheme).Create (requestUri);\r
146                 }\r
147 \r
148                 public virtual Stream EndGetRequestStream (IAsyncResult asyncResult)\r
149                 {\r
150                         throw new NotSupportedException ();\r
151                 }\r
152                 \r
153                 public virtual WebResponse EndGetResponse (IAsyncResult asyncResult)\r
154                 {\r
155                         throw new NotSupportedException ();\r
156                 }\r
157                 \r
158                 public virtual Stream GetRequestStream()\r
159                 {\r
160                         throw new NotSupportedException ();\r
161                 }\r
162                 \r
163                 public virtual WebResponse GetResponse()\r
164                 {\r
165                         throw new NotSupportedException ();\r
166                 }\r
167                 \r
168                 void ISerializable.GetObjectData (SerializationInfo serializationInfo,\r
169                                                   StreamingContext streamingContext)\r
170                 {\r
171                         throw new NotSupportedException ();\r
172                 }\r
173 \r
174                 public static bool RegisterPrefix (string prefix, IWebRequestCreate creator)\r
175                 {\r
176                         if (prefix == null)\r
177                                 throw new ArgumentNullException("prefix");\r
178                         if (creator == null)\r
179                                 throw new ArgumentNullException("creator");                     \r
180                         \r
181                         lock (prefixes.SyncRoot) {\r
182                                 if (prefixes.Contains (prefix))\r
183                                         return false;\r
184                                 prefixes.Add (prefix.ToLower (), creator);\r
185                         }\r
186                         return true;\r
187                 }\r
188                 \r
189                 private static IWebRequestCreate GetCreator (string prefix)\r
190                 {\r
191                         int longestPrefix = -1;\r
192                         IWebRequestCreate creator = null;\r
193 \r
194                         prefix = prefix.ToLower ();\r
195 \r
196                         IDictionaryEnumerator e = prefixes.GetEnumerator ();\r
197                         while (e.MoveNext ()) {\r
198                                 string key = e.Key as string;\r
199 \r
200                                 if (key.Length <= longestPrefix) \r
201                                         continue;\r
202                                 \r
203                                 if (!prefix.StartsWith (key))\r
204                                         continue;                                       \r
205                                         \r
206                                 longestPrefix = key.Length;\r
207                                 creator = (IWebRequestCreate) e.Value;\r
208                         }\r
209                         \r
210                         if (creator == null) \r
211                                 throw new NotSupportedException (prefix);\r
212                                 \r
213                         return creator;\r
214                 }\r
215         }\r
216 }