[System.Net.WebRequest] Implemented new and improved proxy support on Mac/iOS
[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 #if NET_4_5
43 using System.Threading.Tasks;
44 #endif
45
46 #if NET_2_1
47 using ConfigurationException = System.ArgumentException;
48
49 namespace System.Net.Configuration {
50         class Dummy {}
51 }
52 #endif
53
54 namespace System.Net 
55 {
56 #if MOONLIGHT
57         internal abstract class WebRequest : ISerializable {
58 #else
59         [Serializable]
60         public abstract class WebRequest : MarshalByRefObject, ISerializable {
61 #endif
62                 static HybridDictionary prefixes = new HybridDictionary ();
63                 static bool isDefaultWebProxySet;
64                 static IWebProxy defaultWebProxy;
65                 static RequestCachePolicy defaultCachePolicy;
66                 
67                 // Constructors
68                 
69                 static WebRequest ()
70                 {
71 #if NET_2_1
72                         IWebRequestCreate http = new HttpRequestCreator ();
73                         RegisterPrefix ("http", http);
74                         RegisterPrefix ("https", http);
75         #if MOBILE
76                         RegisterPrefix ("file", new FileWebRequestCreator ());
77                         RegisterPrefix ("ftp", new FtpRequestCreator ());
78         #endif
79 #else
80                         defaultCachePolicy = new HttpRequestCachePolicy (HttpRequestCacheLevel.NoCacheNoStore);
81         #if CONFIGURATION_DEP
82                         object cfg = ConfigurationManager.GetSection ("system.net/webRequestModules");
83                         WebRequestModulesSection s = cfg as WebRequestModulesSection;
84                         if (s != null) {
85                                 foreach (WebRequestModuleElement el in
86                                          s.WebRequestModules)
87                                         AddPrefix (el.Prefix, el.Type);
88                                 return;
89                         }
90         #endif
91                         ConfigurationSettings.GetConfig ("system.net/webRequestModules");
92 #endif
93                 }
94                 
95                 protected WebRequest () 
96                 {
97                 }
98                 
99                 protected WebRequest (SerializationInfo serializationInfo, StreamingContext streamingContext) 
100                 {
101                 }
102
103                 static Exception GetMustImplement ()
104                 {
105                         return new NotImplementedException ("This method must be implemented in derived classes");
106                 }
107                 
108                 // Properties
109
110                 private AuthenticationLevel authentication_level = AuthenticationLevel.MutualAuthRequested;
111                 
112                 public AuthenticationLevel AuthenticationLevel
113                 {
114                         get {
115                                 return(authentication_level);
116                         }
117                         set {
118                                 authentication_level = value;
119                         }
120                 }
121
122                 [MonoTODO ("Implement the caching system. Currently always returns a policy with the NoCacheNoStore level")]
123                 public virtual RequestCachePolicy CachePolicy
124                 {
125                         get { return DefaultCachePolicy; }
126                         set {
127                         }
128                 }
129                 
130                 public virtual string ConnectionGroupName {
131                         get { throw GetMustImplement (); }
132                         set { throw GetMustImplement (); }
133                 }
134                 
135                 public virtual long ContentLength { 
136                         get { throw GetMustImplement (); }
137                         set { throw GetMustImplement (); }
138                 }
139                 
140                 public virtual string ContentType { 
141                         get { throw GetMustImplement (); }
142                         set { throw GetMustImplement (); }
143                 }
144                 
145                 public virtual ICredentials Credentials { 
146                         get { throw GetMustImplement (); }
147                         set { throw GetMustImplement (); }
148                 }
149
150                 public static RequestCachePolicy DefaultCachePolicy
151                 {
152                         get { return defaultCachePolicy; }
153                         set {
154                                 throw GetMustImplement ();
155                         }
156                 }
157                 
158                 public virtual WebHeaderCollection Headers { 
159                         get { throw GetMustImplement (); }
160                         set { throw GetMustImplement (); }
161                 }
162                 
163 #if !MOONLIGHT
164                 public TokenImpersonationLevel ImpersonationLevel {
165                         get { throw GetMustImplement (); }
166                         set { throw GetMustImplement (); }
167                 }
168 #endif
169                 public virtual string Method { 
170                         get { throw GetMustImplement (); }
171                         set { throw GetMustImplement (); }
172                 }
173                 
174                 public virtual bool PreAuthenticate { 
175                         get { throw GetMustImplement (); }
176                         set { throw GetMustImplement (); }
177                 }
178                 
179                 public virtual IWebProxy Proxy { 
180                         get { throw GetMustImplement (); }
181                         set { throw GetMustImplement (); }
182                 }
183                 
184                 public virtual Uri RequestUri { 
185                         get { throw GetMustImplement (); }
186                 }
187                 
188                 public virtual int Timeout { 
189                         get { throw GetMustImplement (); }
190                         set { throw GetMustImplement (); }
191                 }
192                 
193                 public virtual bool UseDefaultCredentials
194                 {
195                         get {
196                                 throw GetMustImplement ();
197                         }
198                         set {
199                                 throw GetMustImplement ();
200                         }
201                 }
202                 
203 //              volatile static IWebProxy proxy;
204                 static readonly object lockobj = new object ();
205                 
206                 public static IWebProxy DefaultWebProxy {
207                         get {
208                                 if (!isDefaultWebProxySet) {
209                                         lock (lockobj) {
210                                                 if (defaultWebProxy == null)
211                                                         defaultWebProxy = GetDefaultWebProxy ();
212                                         }
213                                 }
214                                 return defaultWebProxy;
215                         }
216                         set {
217                                 /* MS documentation states that a null value would cause an ArgumentNullException
218                                  * but that's not the way it behaves:
219                                  * https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=304724
220                                  */
221                                 defaultWebProxy = value;
222                                 isDefaultWebProxySet = true;
223                         }
224                 }
225                 
226                 [MonoTODO("Needs to respect Module, Proxy.AutoDetect, and Proxy.ScriptLocation config settings")]
227                 static IWebProxy GetDefaultWebProxy ()
228                 {
229 #if CONFIGURATION_DEP
230                         DefaultProxySection sec = ConfigurationManager.GetSection ("system.net/defaultProxy") as DefaultProxySection;
231                         WebProxy p;
232                         
233                         if (sec == null)
234                                 return GetSystemWebProxy ();
235                         
236                         ProxyElement pe = sec.Proxy;
237                         
238                         if ((pe.UseSystemDefault != ProxyElement.UseSystemDefaultValues.False) && (pe.ProxyAddress == null))
239                                 p = (WebProxy) GetSystemWebProxy ();
240                         else
241                                 p = new WebProxy ();
242                         
243                         if (pe.ProxyAddress != null)
244                                 p.Address = pe.ProxyAddress;
245                         
246                         if (pe.BypassOnLocal != ProxyElement.BypassOnLocalValues.Unspecified)
247                                 p.BypassProxyOnLocal = (pe.BypassOnLocal == ProxyElement.BypassOnLocalValues.True);
248                                 
249                         foreach(BypassElement elem in sec.BypassList)
250                                 p.BypassArrayList.Add(elem.Address);
251                         
252                         return p;
253 #else
254                         return GetSystemWebProxy ();
255 #endif
256                 }
257
258                 // Methods
259                 
260                 public virtual void Abort()
261                 {
262                         throw GetMustImplement ();
263                 }
264                 
265                 public virtual IAsyncResult BeginGetRequestStream (AsyncCallback callback, object state) 
266                 {
267                         throw GetMustImplement ();
268                 }
269                 
270                 public virtual IAsyncResult BeginGetResponse (AsyncCallback callback, object state)
271                 {
272                         throw GetMustImplement ();
273                 }
274
275                 public static WebRequest Create (string requestUriString) 
276                 {
277                         if (requestUriString == null)
278                                 throw new ArgumentNullException ("requestUriString");
279                         return Create (new Uri (requestUriString));
280                 }
281                                 
282                 public static WebRequest Create (Uri requestUri) 
283                 {
284                         if (requestUri == null)
285                                 throw new ArgumentNullException ("requestUri");
286                         return GetCreator (requestUri.AbsoluteUri).Create (requestUri);
287                 }
288                 
289                 public static WebRequest CreateDefault (Uri requestUri)
290                 {
291                         if (requestUri == null)
292                                 throw new ArgumentNullException ("requestUri");
293                         return GetCreator (requestUri.Scheme).Create (requestUri);
294                 }
295
296                 public virtual Stream EndGetRequestStream (IAsyncResult asyncResult)
297                 {
298                         throw GetMustImplement ();
299                 }
300                 
301                 public virtual WebResponse EndGetResponse (IAsyncResult asyncResult)
302                 {
303                         throw GetMustImplement ();
304                 }
305                 
306                 public virtual Stream GetRequestStream()
307                 {
308                         throw GetMustImplement ();
309                 }
310                 
311                 public virtual WebResponse GetResponse()
312                 {
313                         throw GetMustImplement ();
314                 }
315                 
316                 [MonoTODO("Look in other places for proxy config info")]
317                 public static IWebProxy GetSystemWebProxy ()
318                 {
319 #if !NET_2_1
320                         if (IsWindows ()) {
321                                 int iProxyEnable = (int)Microsoft.Win32.Registry.GetValue ("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", "ProxyEnable", 0);
322
323                                 if (iProxyEnable > 0) {
324                                         string strHttpProxy = "";                                       
325                                         bool bBypassOnLocal = false;
326                                         ArrayList al = new ArrayList ();
327                                         
328                                         string strProxyServer = (string)Microsoft.Win32.Registry.GetValue ("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", "ProxyServer", null);
329                                         string strProxyOverrride = (string)Microsoft.Win32.Registry.GetValue ("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", "ProxyOverride", null);
330                                         
331                                         if (strProxyServer.Contains ("=")) {
332                                                 foreach (string strEntry in strProxyServer.Split (new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries))
333                                                         if (strEntry.StartsWith ("http=")) {
334                                                                 strHttpProxy = strEntry.Substring (5);
335                                                                 break;
336                                                         }
337                                         } else strHttpProxy = strProxyServer;
338                                         
339                                         if (strProxyOverrride != null) {                                                
340                                                 string[] bypassList = strProxyOverrride.Split (new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
341                                         
342                                                 foreach (string str in bypassList) {
343                                                         if (str != "<local>")
344                                                                 al.Add (str);
345                                                         else
346                                                                 bBypassOnLocal = true;
347                                                 }
348                                         }
349                                         
350                                         return new WebProxy (strHttpProxy, bBypassOnLocal, al.ToArray (typeof(string)) as string[]);
351                                 }
352                         } else {
353 #endif
354                                 if (Platform.IsMacOS)
355                                         return CFNetwork.GetDefaultProxy ();
356                                 
357                                 string address = Environment.GetEnvironmentVariable ("http_proxy");
358
359                                 if (address == null)
360                                         address = Environment.GetEnvironmentVariable ("HTTP_PROXY");
361                                 
362                                 if (address != null) {
363                                         try {
364                                                 if (!address.StartsWith ("http://"))
365                                                         address = "http://" + address;
366
367                                                 Uri uri = new Uri (address);
368                                                 IPAddress ip;
369                                                 
370                                                 if (IPAddress.TryParse (uri.Host, out ip)) {
371                                                         if (IPAddress.Any.Equals (ip)) {
372                                                                 UriBuilder builder = new UriBuilder (uri);
373                                                                 builder.Host = "127.0.0.1";
374                                                                 uri = builder.Uri;
375                                                         } else if (IPAddress.IPv6Any.Equals (ip)) {
376                                                                 UriBuilder builder = new UriBuilder (uri);
377                                                                 builder.Host = "[::1]";
378                                                                 uri = builder.Uri;
379                                                         }
380                                                 }
381                                                 
382                                                 bool bBypassOnLocal = false;                                            
383                                                 ArrayList al = new ArrayList ();
384                                                 string bypass = Environment.GetEnvironmentVariable ("no_proxy");
385                                                 
386                                                 if (bypass == null)
387                                                         bypass = Environment.GetEnvironmentVariable ("NO_PROXY");
388                                                 
389                                                 if (bypass != null) {
390                                                         string[] bypassList = bypass.Split (new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
391                                                 
392                                                         foreach (string str in bypassList) {
393                                                                 if (str != "*.local")
394                                                                         al.Add (str);
395                                                                 else
396                                                                         bBypassOnLocal = true;
397                                                         }
398                                                 }
399                                                 
400                                                 return new WebProxy (uri, bBypassOnLocal, al.ToArray (typeof(string)) as string[]);
401                                         } catch (UriFormatException) {
402                                         }
403                                 }
404 #if !NET_2_1
405                         }
406 #endif
407                         
408                         return new WebProxy ();
409                 }
410
411                 void ISerializable.GetObjectData (SerializationInfo serializationInfo, StreamingContext streamingContext)
412                 {
413                         throw new NotSupportedException ();
414                 }
415
416                 protected virtual void GetObjectData (SerializationInfo serializationInfo, StreamingContext streamingContext)
417                 {
418                         throw GetMustImplement ();
419                 }
420
421                 public static bool RegisterPrefix (string prefix, IWebRequestCreate creator)
422                 {
423                         if (prefix == null)
424                                 throw new ArgumentNullException ("prefix");
425                         if (creator == null)
426                                 throw new ArgumentNullException ("creator");
427                         
428                         lock (prefixes.SyncRoot) {
429                                 string lowerCasePrefix = prefix.ToLower (CultureInfo.InvariantCulture);
430                                 if (prefixes.Contains (lowerCasePrefix))
431                                         return false;
432                                 prefixes.Add (lowerCasePrefix, creator);
433                         }
434                         return true;
435                 }
436                 
437                 private static IWebRequestCreate GetCreator (string prefix)
438                 {
439                         int longestPrefix = -1;
440                         IWebRequestCreate creator = null;
441
442                         prefix = prefix.ToLower (CultureInfo.InvariantCulture);
443
444                         IDictionaryEnumerator e = prefixes.GetEnumerator ();
445                         while (e.MoveNext ()) {
446                                 string key = e.Key as string;
447
448                                 if (key.Length <= longestPrefix) 
449                                         continue;
450                                 
451                                 if (!prefix.StartsWith (key))
452                                         continue;
453                                         
454                                 longestPrefix = key.Length;
455                                 creator = (IWebRequestCreate) e.Value;
456                         }
457                         
458                         if (creator == null) 
459                                 throw new NotSupportedException (prefix);
460                                 
461                         return creator;
462                 }
463                 
464                 internal static bool IsWindows ()
465                 {
466                         return (int) Environment.OSVersion.Platform < 4;
467                 }
468
469                 internal static void ClearPrefixes ()
470                 {
471                         prefixes.Clear ();
472                 }
473
474                 internal static void RemovePrefix (string prefix)
475                 {
476                         prefixes.Remove (prefix);
477                 }
478
479                 internal static void AddPrefix (string prefix, string typeName)
480                 {
481                         Type type = Type.GetType (typeName);
482                         if (type == null)
483                                 throw new ConfigurationException (String.Format ("Type {0} not found", typeName));
484                         AddPrefix (prefix, type);
485                 }
486
487                 internal static void AddPrefix (string prefix, Type type)
488                 {
489                         object o = Activator.CreateInstance (type, true);
490                         prefixes [prefix] = o;
491                 }
492
493 #if NET_4_5
494                 public virtual Task<Stream> GetRequestStreamAsync ()
495                 {
496                         return Task<Stream>.Factory.FromAsync (BeginGetRequestStream, EndGetRequestStream, null);
497                 }
498
499                 public virtual Task<WebResponse> GetResponseAsync ()
500                 {
501                         return Task<WebResponse>.Factory.FromAsync (BeginGetResponse, EndGetResponse, null);
502                 }
503 #endif
504
505         }
506 }