Merge pull request #1500 from criteo-forks/criteo
[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 //      Marek Safar (marek.safar@gmail.com)
7 //
8 // Copyright 2011 Xamarin Inc.
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using System;
31 using System.Collections;
32 using System.Collections.Specialized;
33 using System.Configuration;
34 using System.IO;
35 using System.Reflection;
36 using System.Runtime.Serialization;
37 using System.Globalization;
38 using System.Net.Configuration;
39 using System.Net.Security;
40 using System.Net.Cache;
41 using System.Security.Principal;
42 using System.Threading.Tasks;
43
44 #if NET_2_1
45 using ConfigurationException = System.ArgumentException;
46
47 namespace System.Net.Configuration {
48         class Dummy {}
49 }
50 #endif
51
52 namespace System.Net 
53 {
54         [Serializable]
55         public abstract class WebRequest : MarshalByRefObject, ISerializable {
56                 static HybridDictionary prefixes = new HybridDictionary ();
57                 static bool isDefaultWebProxySet;
58                 static IWebProxy defaultWebProxy;
59                 static RequestCachePolicy defaultCachePolicy;
60
61                 static WebRequest ()
62                 {
63 #if MOBILE
64                         IWebRequestCreate http = new HttpRequestCreator ();
65                         RegisterPrefix ("http", http);
66                         RegisterPrefix ("https", http);
67                         RegisterPrefix ("file", new FileWebRequestCreator ());
68                         RegisterPrefix ("ftp", new FtpRequestCreator ());
69 #else
70         #if CONFIGURATION_DEP
71                         object cfg = ConfigurationManager.GetSection ("system.net/webRequestModules");
72                         WebRequestModulesSection s = cfg as WebRequestModulesSection;
73                         if (s != null) {
74                                 foreach (WebRequestModuleElement el in
75                                          s.WebRequestModules)
76                                         AddPrefix (el.Prefix, el.Type);
77                                 return;
78                         }
79         #endif
80                         ConfigurationSettings.GetConfig ("system.net/webRequestModules");
81 #endif
82                 }
83                 
84                 protected WebRequest () 
85                 {
86                 }
87                 
88                 protected WebRequest (SerializationInfo serializationInfo, StreamingContext streamingContext) 
89                 {
90                 }
91
92                 static Exception GetMustImplement ()
93                 {
94                         return new NotImplementedException ("This method must be implemented in derived classes");
95                 }
96                 
97                 // Properties
98
99                 private AuthenticationLevel authentication_level = AuthenticationLevel.MutualAuthRequested;
100                 
101                 public AuthenticationLevel AuthenticationLevel
102                 {
103                         get {
104                                 return(authentication_level);
105                         }
106                         set {
107                                 authentication_level = value;
108                         }
109                 }
110                 
111                 public virtual string ConnectionGroupName {
112                         get { throw GetMustImplement (); }
113                         set { throw GetMustImplement (); }
114                 }
115                 
116                 public virtual long ContentLength { 
117                         get { throw GetMustImplement (); }
118                         set { throw GetMustImplement (); }
119                 }
120                 
121                 public virtual string ContentType { 
122                         get { throw GetMustImplement (); }
123                         set { throw GetMustImplement (); }
124                 }
125                 
126                 public virtual ICredentials Credentials { 
127                         get { throw GetMustImplement (); }
128                         set { throw GetMustImplement (); }
129                 }
130
131                 [MonoTODO ("Implement the caching system. Currently always returns a policy with the NoCacheNoStore level")]
132                 public virtual RequestCachePolicy CachePolicy
133                 {
134                         get { return DefaultCachePolicy; }
135                         set {
136                         }
137                 }
138                 
139                 public static RequestCachePolicy DefaultCachePolicy {
140                         get {
141                                 return defaultCachePolicy ?? (defaultCachePolicy = new HttpRequestCachePolicy (HttpRequestCacheLevel.NoCacheNoStore));
142                         }
143                         set {
144                                 throw GetMustImplement ();
145                         }
146                 }
147                 
148                 public virtual WebHeaderCollection Headers { 
149                         get { throw GetMustImplement (); }
150                         set { throw GetMustImplement (); }
151                 }
152                 
153
154                 public virtual string Method { 
155                         get { throw GetMustImplement (); }
156                         set { throw GetMustImplement (); }
157                 }
158                 
159                 public virtual bool PreAuthenticate { 
160                         get { throw GetMustImplement (); }
161                         set { throw GetMustImplement (); }
162                 }
163                 
164                 public virtual IWebProxy Proxy { 
165                         get { throw GetMustImplement (); }
166                         set { throw GetMustImplement (); }
167                 }
168                 
169                 public virtual Uri RequestUri { 
170                         get { throw GetMustImplement (); }
171                 }
172                 
173                 public virtual int Timeout { 
174                         get { throw GetMustImplement (); }
175                         set { throw GetMustImplement (); }
176                 }
177                 
178                 public virtual bool UseDefaultCredentials
179                 {
180                         get {
181                                 throw GetMustImplement ();
182                         }
183                         set {
184                                 throw GetMustImplement ();
185                         }
186                 }
187
188                 public TokenImpersonationLevel ImpersonationLevel { get; set; }
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 #if CONFIGURATION_DEP
217                         DefaultProxySection sec = ConfigurationManager.GetSection ("system.net/defaultProxy") as DefaultProxySection;
218                         WebProxy p;
219                         
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                                 IWebProxy proxy = GetSystemWebProxy ();
227                                 
228                                 if (!(proxy is WebProxy))
229                                         return proxy;
230                                 
231                                 p = (WebProxy) proxy;
232                         } else
233                                 p = new WebProxy ();
234                         
235                         if (pe.ProxyAddress != null)
236                                 p.Address = pe.ProxyAddress;
237                         
238                         if (pe.BypassOnLocal != ProxyElement.BypassOnLocalValues.Unspecified)
239                                 p.BypassProxyOnLocal = (pe.BypassOnLocal == ProxyElement.BypassOnLocalValues.True);
240                                 
241                         foreach(BypassElement elem in sec.BypassList)
242                                 p.BypassArrayList.Add(elem.Address);
243                         
244                         return p;
245 #else
246                         return GetSystemWebProxy ();
247 #endif
248                 }
249
250                 // Methods
251                 
252                 public virtual void Abort()
253                 {
254                         throw GetMustImplement ();
255                 }
256                 
257                 public virtual IAsyncResult BeginGetRequestStream (AsyncCallback callback, object state) 
258                 {
259                         throw GetMustImplement ();
260                 }
261                 
262                 public virtual IAsyncResult BeginGetResponse (AsyncCallback callback, object state)
263                 {
264                         throw GetMustImplement ();
265                 }
266
267                 public static WebRequest Create (string requestUriString) 
268                 {
269                         if (requestUriString == null)
270                                 throw new ArgumentNullException ("requestUriString");
271                         return Create (new Uri (requestUriString));
272                 }
273                                 
274                 public static WebRequest Create (Uri requestUri) 
275                 {
276                         if (requestUri == null)
277                                 throw new ArgumentNullException ("requestUri");
278                         return GetCreator (requestUri.AbsoluteUri).Create (requestUri);
279                 }
280                 
281                 public static WebRequest CreateDefault (Uri requestUri)
282                 {
283                         if (requestUri == null)
284                                 throw new ArgumentNullException ("requestUri");
285                         return GetCreator (requestUri.Scheme).Create (requestUri);
286                 }
287                 static HttpWebRequest SharedCreateHttp (Uri uri)
288                 {
289                         if (uri.Scheme != "http" && uri.Scheme != "https")
290                                 throw new NotSupportedException ("The uri should start with http or https");
291
292                         return new HttpWebRequest (uri);
293                 }
294
295                 public static HttpWebRequest CreateHttp (string requestUriString)
296                 {
297                         if (requestUriString == null)
298                                 throw new ArgumentNullException ("requestUriString");
299                         return SharedCreateHttp (new Uri (requestUriString));
300                 }
301                         
302                 public static HttpWebRequest CreateHttp (Uri requestUri)
303                 {
304                         if (requestUri == null)
305                                 throw new ArgumentNullException ("requestUri");
306                         return SharedCreateHttp (requestUri);
307                 }
308                 public virtual Stream EndGetRequestStream (IAsyncResult asyncResult)
309                 {
310                         throw GetMustImplement ();
311                 }
312                 
313                 public virtual WebResponse EndGetResponse (IAsyncResult asyncResult)
314                 {
315                         throw GetMustImplement ();
316                 }
317                 
318                 public virtual Stream GetRequestStream()
319                 {
320                         throw GetMustImplement ();
321                 }
322                 
323                 public virtual WebResponse GetResponse()
324                 {
325                         throw GetMustImplement ();
326                 }
327                 
328                 [MonoTODO("Look in other places for proxy config info")]
329                 public static IWebProxy GetSystemWebProxy ()
330                 {
331 #if MONOTOUCH
332                         return CFNetwork.GetDefaultProxy ();
333 #else
334 #if MONODROID
335                         // Return the system web proxy.  This only works for ICS+.
336                         var androidProxy = AndroidPlatform.GetDefaultProxy ();
337                         if (androidProxy != null)
338                                 return androidProxy;
339 #endif
340 #if !NET_2_1
341                         if (IsWindows ()) {
342                                 int iProxyEnable = (int)Microsoft.Win32.Registry.GetValue ("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", "ProxyEnable", 0);
343
344                                 if (iProxyEnable > 0) {
345                                         string strHttpProxy = "";                                       
346                                         bool bBypassOnLocal = false;
347                                         ArrayList al = new ArrayList ();
348                                         
349                                         string strProxyServer = (string)Microsoft.Win32.Registry.GetValue ("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", "ProxyServer", null);
350                                         string strProxyOverrride = (string)Microsoft.Win32.Registry.GetValue ("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", "ProxyOverride", null);
351                                         
352                                         if (strProxyServer.Contains ("=")) {
353                                                 foreach (string strEntry in strProxyServer.Split (new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries))
354                                                         if (strEntry.StartsWith ("http=")) {
355                                                                 strHttpProxy = strEntry.Substring (5);
356                                                                 break;
357                                                         }
358                                         } else strHttpProxy = strProxyServer;
359                                         
360                                         if (strProxyOverrride != null) {                                                
361                                                 string[] bypassList = strProxyOverrride.Split (new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
362                                         
363                                                 foreach (string str in bypassList) {
364                                                         if (str != "<local>")
365                                                                 al.Add (str);
366                                                         else
367                                                                 bBypassOnLocal = true;
368                                                 }
369                                         }
370                                         
371                                         return new WebProxy (strHttpProxy, bBypassOnLocal, al.ToArray (typeof(string)) as string[]);
372                                 }
373                         } else {
374 #endif
375                                 if (Platform.IsMacOS)
376                                         return CFNetwork.GetDefaultProxy ();
377                                 
378                                 string address = Environment.GetEnvironmentVariable ("http_proxy");
379
380                                 if (address == null)
381                                         address = Environment.GetEnvironmentVariable ("HTTP_PROXY");
382                                 
383                                 if (address != null) {
384                                         try {
385                                                 if (!address.StartsWith ("http://"))
386                                                         address = "http://" + address;
387
388                                                 Uri uri = new Uri (address);
389                                                 IPAddress ip;
390                                                 
391                                                 if (IPAddress.TryParse (uri.Host, out ip)) {
392                                                         if (IPAddress.Any.Equals (ip)) {
393                                                                 UriBuilder builder = new UriBuilder (uri);
394                                                                 builder.Host = "127.0.0.1";
395                                                                 uri = builder.Uri;
396                                                         } else if (IPAddress.IPv6Any.Equals (ip)) {
397                                                                 UriBuilder builder = new UriBuilder (uri);
398                                                                 builder.Host = "[::1]";
399                                                                 uri = builder.Uri;
400                                                         }
401                                                 }
402                                                 
403                                                 bool bBypassOnLocal = false;                                            
404                                                 ArrayList al = new ArrayList ();
405                                                 string bypass = Environment.GetEnvironmentVariable ("no_proxy");
406                                                 
407                                                 if (bypass == null)
408                                                         bypass = Environment.GetEnvironmentVariable ("NO_PROXY");
409                                                 
410                                                 if (bypass != null) {
411                                                         string[] bypassList = bypass.Split (new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
412                                                 
413                                                         foreach (string str in bypassList) {
414                                                                 if (str != "*.local")
415                                                                         al.Add (str);
416                                                                 else
417                                                                         bBypassOnLocal = true;
418                                                         }
419                                                 }
420                                                 
421                                                 return new WebProxy (uri, bBypassOnLocal, al.ToArray (typeof(string)) as string[]);
422                                         } catch (UriFormatException) {
423                                         }
424                                 }
425 #if !NET_2_1
426                         }
427 #endif
428                         
429                         return new WebProxy ();
430 #endif // MONOTOUCH
431                 }
432
433                 void ISerializable.GetObjectData (SerializationInfo serializationInfo, StreamingContext streamingContext)
434                 {
435                         throw new NotSupportedException ();
436                 }
437
438                 protected virtual void GetObjectData (SerializationInfo serializationInfo, StreamingContext streamingContext)
439                 {
440                         throw GetMustImplement ();
441                 }
442
443                 public static bool RegisterPrefix (string prefix, IWebRequestCreate creator)
444                 {
445                         if (prefix == null)
446                                 throw new ArgumentNullException ("prefix");
447                         if (creator == null)
448                                 throw new ArgumentNullException ("creator");
449                         
450                         lock (prefixes.SyncRoot) {
451                                 string lowerCasePrefix = prefix.ToLower (CultureInfo.InvariantCulture);
452                                 if (prefixes.Contains (lowerCasePrefix))
453                                         return false;
454                                 prefixes.Add (lowerCasePrefix, creator);
455                         }
456                         return true;
457                 }
458                 
459                 private static IWebRequestCreate GetCreator (string prefix)
460                 {
461                         int longestPrefix = -1;
462                         IWebRequestCreate creator = null;
463
464                         prefix = prefix.ToLower (CultureInfo.InvariantCulture);
465
466                         IDictionaryEnumerator e = prefixes.GetEnumerator ();
467                         while (e.MoveNext ()) {
468                                 string key = e.Key as string;
469
470                                 if (key.Length <= longestPrefix) 
471                                         continue;
472                                 
473                                 if (!prefix.StartsWith (key))
474                                         continue;
475                                         
476                                 longestPrefix = key.Length;
477                                 creator = (IWebRequestCreate) e.Value;
478                         }
479                         
480                         if (creator == null) 
481                                 throw new NotSupportedException (prefix);
482                                 
483                         return creator;
484                 }
485                 
486                 internal static bool IsWindows ()
487                 {
488                         return (int) Environment.OSVersion.Platform < 4;
489                 }
490
491                 internal static void ClearPrefixes ()
492                 {
493                         prefixes.Clear ();
494                 }
495
496                 internal static void RemovePrefix (string prefix)
497                 {
498                         prefixes.Remove (prefix);
499                 }
500
501                 internal static void AddPrefix (string prefix, string typeName)
502                 {
503                         Type type = Type.GetType (typeName);
504                         if (type == null)
505                                 throw new ConfigurationException (String.Format ("Type {0} not found", typeName));
506                         AddPrefix (prefix, type);
507                 }
508
509                 internal static void AddPrefix (string prefix, Type type)
510                 {
511                         object o = Activator.CreateInstance (type, true);
512                         prefixes [prefix] = o;
513                 }
514
515                 public virtual Task<Stream> GetRequestStreamAsync ()
516                 {
517                         return Task<Stream>.Factory.FromAsync (BeginGetRequestStream, EndGetRequestStream, null);
518                 }
519
520                 public virtual Task<WebResponse> GetResponseAsync ()
521                 {
522                         return Task<WebResponse>.Factory.FromAsync (BeginGetResponse, EndGetResponse, null);
523                 }
524
525         }
526 }