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