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