Add a more functional (i.e. fewer-stubs) implementation of System.Data.Linq.
[mono.git] / mcs / class / System / System.Net / WebRequest.cs
1 //
2 // System.Net.WebRequest
3 //
4 // Authors:
5 //   Lawrence Pit (loz@cable.a2000.nl)
6 //
7
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using System;
30 using System.Collections;
31 using System.Collections.Specialized;
32 using System.Configuration;
33 using System.IO;
34 using System.Runtime.Serialization;
35 using System.Globalization;
36 #if NET_2_0
37 using System.Net.Configuration;
38 using System.Net.Security;
39 using System.Net.Cache;
40 using System.Security.Principal;
41 #endif
42
43 namespace System.Net 
44 {
45         [Serializable]
46         public abstract class WebRequest : MarshalByRefObject, ISerializable
47         {
48                 static HybridDictionary prefixes = new HybridDictionary ();
49 #if NET_2_0
50                 static bool isDefaultWebProxySet;
51                 static IWebProxy defaultWebProxy;
52 #endif
53                 
54                 // Constructors
55                 
56                 static WebRequest ()
57                 {
58 #if NET_2_0 && CONFIGURATION_DEP
59                         object cfg = ConfigurationManager.GetSection ("system.net/webRequestModules");
60                         WebRequestModulesSection s = cfg as WebRequestModulesSection;
61                         if (s != null) {
62                                 foreach (WebRequestModuleElement el in
63                                          s.WebRequestModules)
64                                         AddPrefix (el.Prefix, el.Type);
65                                 return;
66                         }
67 #endif
68                         ConfigurationSettings.GetConfig ("system.net/webRequestModules");
69                 }
70                 
71                 protected WebRequest () 
72                 {
73                 }
74                 
75                 protected WebRequest (SerializationInfo serializationInfo, StreamingContext streamingContext) 
76                 {
77 #if ONLY_1_1
78                         throw GetMustImplement ();
79 #endif
80                 }
81
82                 static Exception GetMustImplement ()
83                 {
84                         return new NotImplementedException ("This method must be implemented in derived classes");
85                 }
86                 
87                 // Properties
88
89 #if NET_2_0
90                 private AuthenticationLevel authentication_level = AuthenticationLevel.MutualAuthRequested;
91                 
92                 public AuthenticationLevel AuthenticationLevel
93                 {
94                         get {
95                                 return(authentication_level);
96                         }
97                         set {
98                                 authentication_level = value;
99                         }
100                 }
101
102                 public virtual RequestCachePolicy CachePolicy
103                 {
104                         get {
105                                 throw GetMustImplement ();
106                         }
107                         set {
108                         }
109                 }
110 #endif
111                 
112                 public virtual string ConnectionGroupName {
113                         get { throw GetMustImplement (); }
114                         set { throw GetMustImplement (); }
115                 }
116                 
117                 public virtual long ContentLength { 
118                         get { throw GetMustImplement (); }
119                         set { throw GetMustImplement (); }
120                 }
121                 
122                 public virtual string ContentType { 
123                         get { throw GetMustImplement (); }
124                         set { throw GetMustImplement (); }
125                 }
126                 
127                 public virtual ICredentials Credentials { 
128                         get { throw GetMustImplement (); }
129                         set { throw GetMustImplement (); }
130                 }
131
132 #if NET_2_0
133                 public static RequestCachePolicy DefaultCachePolicy
134                 {
135                         get {
136                                 throw GetMustImplement ();
137                         }
138                         set {
139                                 throw GetMustImplement ();
140                         }
141                 }
142 #endif
143                 
144                 public virtual WebHeaderCollection Headers { 
145                         get { throw GetMustImplement (); }
146                         set { throw GetMustImplement (); }
147                 }
148                 
149 #if NET_2_0
150                 public TokenImpersonationLevel ImpersonationLevel {
151                         get { throw GetMustImplement (); }
152                         set { throw GetMustImplement (); }
153                 }
154 #endif
155                 public virtual string Method { 
156                         get { throw GetMustImplement (); }
157                         set { throw GetMustImplement (); }
158                 }
159                 
160                 public virtual bool PreAuthenticate { 
161                         get { throw GetMustImplement (); }
162                         set { throw GetMustImplement (); }
163                 }
164                 
165                 public virtual IWebProxy Proxy { 
166                         get { throw GetMustImplement (); }
167                         set { throw GetMustImplement (); }
168                 }
169                 
170                 public virtual Uri RequestUri { 
171                         get { throw GetMustImplement (); }
172                 }
173                 
174                 public virtual int Timeout { 
175                         get { throw GetMustImplement (); }
176                         set { throw GetMustImplement (); }
177                 }
178                 
179 #if NET_2_0
180                 public virtual bool UseDefaultCredentials
181                 {
182                         get {
183                                 throw GetMustImplement ();
184                         }
185                         set {
186                                 throw GetMustImplement ();
187                         }
188                 }
189                 
190 //              volatile static IWebProxy proxy;
191                 static readonly object lockobj = new object ();
192                 
193                 public static IWebProxy DefaultWebProxy {
194                         get {
195                                 if (!isDefaultWebProxySet) {
196                                         lock (lockobj) {
197                                                 if (defaultWebProxy == null)
198                                                         defaultWebProxy = GetDefaultWebProxy ();
199                                         }
200                                 }
201                                 return defaultWebProxy;
202                         }
203                         set {
204                                 /* MS documentation states that a null value would cause an ArgumentNullException
205                                  * but that's not the way it behaves:
206                                  * https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=304724
207                                  */
208                                 defaultWebProxy = value;
209                                 isDefaultWebProxySet = true;
210                         }
211                 }
212                 
213                 [MonoTODO("Needs to respect Module, Proxy.AutoDetect, and Proxy.ScriptLocation config settings")]
214                 static IWebProxy GetDefaultWebProxy ()
215                 {
216                         WebProxy p = null;
217                         
218 #if CONFIGURATION_DEP
219                         DefaultProxySection sec = ConfigurationManager.GetSection ("system.net/defaultProxy") as DefaultProxySection;
220                         if (sec == null)
221                                 return GetSystemWebProxy ();
222                         
223                         ProxyElement pe = sec.Proxy;
224                         
225                         if ((pe.UseSystemDefault != ProxyElement.UseSystemDefaultValues.False) && (pe.ProxyAddress == null))
226                                 p = (WebProxy) GetSystemWebProxy ();
227                         else
228                                 p = new WebProxy ();
229                         
230                         if (pe.ProxyAddress != null)
231                                 p.Address = pe.ProxyAddress;
232                         
233                         if (pe.BypassOnLocal != ProxyElement.BypassOnLocalValues.Unspecified)
234                                 p.BypassProxyOnLocal = (pe.BypassOnLocal == ProxyElement.BypassOnLocalValues.True);
235 #endif
236                         return p;
237                 }
238 #endif
239
240                 // Methods
241                 
242                 public virtual void Abort()
243                 {
244                         throw GetMustImplement ();
245                 }
246                 
247                 public virtual IAsyncResult BeginGetRequestStream (AsyncCallback callback, object state) 
248                 {
249                         throw GetMustImplement ();
250                 }
251                 
252                 public virtual IAsyncResult BeginGetResponse (AsyncCallback callback, object state)
253                 {
254                         throw GetMustImplement ();
255                 }
256
257                 public static WebRequest Create (string requestUriString) 
258                 {
259                         if (requestUriString == null)
260                                 throw new ArgumentNullException ("requestUriString");
261                         return Create (new Uri (requestUriString));
262                 }
263                                 
264                 public static WebRequest Create (Uri requestUri) 
265                 {
266                         if (requestUri == null)
267                                 throw new ArgumentNullException ("requestUri");
268                         return GetCreator (requestUri.AbsoluteUri).Create (requestUri);
269                 }
270                 
271                 public static WebRequest CreateDefault (Uri requestUri)
272                 {
273                         if (requestUri == null)
274                                 throw new ArgumentNullException ("requestUri");
275                         return GetCreator (requestUri.Scheme).Create (requestUri);
276                 }
277
278                 public virtual Stream EndGetRequestStream (IAsyncResult asyncResult)
279                 {
280                         throw GetMustImplement ();
281                 }
282                 
283                 public virtual WebResponse EndGetResponse (IAsyncResult asyncResult)
284                 {
285                         throw GetMustImplement ();
286                 }
287                 
288                 public virtual Stream GetRequestStream()
289                 {
290                         throw GetMustImplement ();
291                 }
292                 
293                 public virtual WebResponse GetResponse()
294                 {
295                         throw GetMustImplement ();
296                 }
297                 
298 #if NET_2_0
299                 [MonoTODO("Look in other places for proxy config info")]
300                 public static IWebProxy GetSystemWebProxy ()
301                 {
302                         string address = Environment.GetEnvironmentVariable ("http_proxy");
303                         if (address != null) {
304                                 try {
305                                         WebProxy p = new WebProxy (address);
306                                         return p;
307                                 } catch (UriFormatException) {}
308                         }
309                         return new WebProxy ();
310                 }
311 #endif
312
313                 void ISerializable.GetObjectData
314                 (SerializationInfo serializationInfo,
315                                                   StreamingContext streamingContext)
316                 {
317                         throw new NotSupportedException ();
318                 }
319
320
321 #if NET_2_0
322                 protected virtual void GetObjectData (SerializationInfo serializationInfo, StreamingContext streamingContext)
323                 {
324                         throw GetMustImplement ();
325                 }
326 #endif
327
328                 public static bool RegisterPrefix (string prefix, IWebRequestCreate creator)
329                 {
330                         if (prefix == null)
331                                 throw new ArgumentNullException ("prefix");
332                         if (creator == null)
333                                 throw new ArgumentNullException ("creator");
334                         
335                         lock (prefixes.SyncRoot) {
336                                 string lowerCasePrefix = prefix.ToLower (CultureInfo.InvariantCulture);
337                                 if (prefixes.Contains (lowerCasePrefix))
338                                         return false;
339                                 prefixes.Add (lowerCasePrefix, creator);
340                         }
341                         return true;
342                 }
343                 
344                 private static IWebRequestCreate GetCreator (string prefix)
345                 {
346                         int longestPrefix = -1;
347                         IWebRequestCreate creator = null;
348
349                         prefix = prefix.ToLower (CultureInfo.InvariantCulture);
350
351                         IDictionaryEnumerator e = prefixes.GetEnumerator ();
352                         while (e.MoveNext ()) {
353                                 string key = e.Key as string;
354
355                                 if (key.Length <= longestPrefix) 
356                                         continue;
357                                 
358                                 if (!prefix.StartsWith (key))
359                                         continue;
360                                         
361                                 longestPrefix = key.Length;
362                                 creator = (IWebRequestCreate) e.Value;
363                         }
364                         
365                         if (creator == null) 
366                                 throw new NotSupportedException (prefix);
367                                 
368                         return creator;
369                 }
370
371                 internal static void ClearPrefixes ()
372                 {
373                         prefixes.Clear ();
374                 }
375
376                 internal static void RemovePrefix (string prefix)
377                 {
378                         prefixes.Remove (prefix);
379                 }
380
381                 internal static void AddPrefix (string prefix, string typeName)
382                 {
383                         Type type = Type.GetType (typeName);
384                         if (type == null)
385                                 throw new ConfigurationException (String.Format ("Type {0} not found", typeName));
386                         AddPrefix (prefix, type);
387                 }
388
389                 internal static void AddPrefix (string prefix, Type type)
390                 {
391                         object o = Activator.CreateInstance (type, true);
392                         prefixes [prefix] = o;
393                 }
394         }
395 }
396