2 // System.Net.WebRequest
5 // Lawrence Pit (loz@cable.a2000.nl)
6 // Marek Safar (marek.safar@gmail.com)
8 // Copyright 2011 Xamarin Inc.
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
31 using System.Collections;
32 using System.Collections.Specialized;
33 using System.Configuration;
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;
45 using ConfigurationException = System.ArgumentException;
47 namespace System.Net.Configuration {
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;
64 IWebRequestCreate http = new HttpRequestCreator ();
65 RegisterPrefix ("http", http);
66 RegisterPrefix ("https", http);
67 RegisterPrefix ("file", new FileWebRequestCreator ());
68 RegisterPrefix ("ftp", new FtpRequestCreator ());
71 object cfg = ConfigurationManager.GetSection ("system.net/webRequestModules");
72 WebRequestModulesSection s = cfg as WebRequestModulesSection;
74 foreach (WebRequestModuleElement el in
76 AddPrefix (el.Prefix, el.Type);
80 ConfigurationSettings.GetConfig ("system.net/webRequestModules");
84 protected WebRequest ()
88 protected WebRequest (SerializationInfo serializationInfo, StreamingContext streamingContext)
92 static Exception GetMustImplement ()
94 return new NotImplementedException ("This method must be implemented in derived classes");
99 private AuthenticationLevel authentication_level = AuthenticationLevel.MutualAuthRequested;
101 public AuthenticationLevel AuthenticationLevel
104 return(authentication_level);
107 authentication_level = value;
111 public virtual string ConnectionGroupName {
112 get { throw GetMustImplement (); }
113 set { throw GetMustImplement (); }
116 public virtual long ContentLength {
117 get { throw GetMustImplement (); }
118 set { throw GetMustImplement (); }
121 public virtual string ContentType {
122 get { throw GetMustImplement (); }
123 set { throw GetMustImplement (); }
126 public virtual ICredentials Credentials {
127 get { throw GetMustImplement (); }
128 set { throw GetMustImplement (); }
131 [MonoTODO ("Implement the caching system. Currently always returns a policy with the NoCacheNoStore level")]
132 public virtual RequestCachePolicy CachePolicy
134 get { return DefaultCachePolicy; }
139 public static RequestCachePolicy DefaultCachePolicy {
141 return defaultCachePolicy ?? (defaultCachePolicy = new HttpRequestCachePolicy (HttpRequestCacheLevel.NoCacheNoStore));
144 throw GetMustImplement ();
148 public virtual WebHeaderCollection Headers {
149 get { throw GetMustImplement (); }
150 set { throw GetMustImplement (); }
154 public virtual string Method {
155 get { throw GetMustImplement (); }
156 set { throw GetMustImplement (); }
159 public virtual bool PreAuthenticate {
160 get { throw GetMustImplement (); }
161 set { throw GetMustImplement (); }
164 public virtual IWebProxy Proxy {
165 get { throw GetMustImplement (); }
166 set { throw GetMustImplement (); }
169 public virtual Uri RequestUri {
170 get { throw GetMustImplement (); }
173 public virtual int Timeout {
174 get { throw GetMustImplement (); }
175 set { throw GetMustImplement (); }
178 public virtual bool UseDefaultCredentials
181 throw GetMustImplement ();
184 throw GetMustImplement ();
188 public TokenImpersonationLevel ImpersonationLevel { get; set; }
190 // volatile static IWebProxy proxy;
191 static readonly object lockobj = new object ();
193 public static IWebProxy DefaultWebProxy {
195 if (!isDefaultWebProxySet) {
197 if (defaultWebProxy == null)
198 defaultWebProxy = GetDefaultWebProxy ();
201 return defaultWebProxy;
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
208 defaultWebProxy = value;
209 isDefaultWebProxySet = true;
213 [MonoTODO("Needs to respect Module, Proxy.AutoDetect, and Proxy.ScriptLocation config settings")]
214 static IWebProxy GetDefaultWebProxy ()
216 #if CONFIGURATION_DEP
217 DefaultProxySection sec = ConfigurationManager.GetSection ("system.net/defaultProxy") as DefaultProxySection;
221 return GetSystemWebProxy ();
223 ProxyElement pe = sec.Proxy;
225 if ((pe.UseSystemDefault != ProxyElement.UseSystemDefaultValues.False) && (pe.ProxyAddress == null)) {
226 IWebProxy proxy = GetSystemWebProxy ();
228 if (!(proxy is WebProxy))
231 p = (WebProxy) proxy;
235 if (pe.ProxyAddress != null)
236 p.Address = pe.ProxyAddress;
238 if (pe.BypassOnLocal != ProxyElement.BypassOnLocalValues.Unspecified)
239 p.BypassProxyOnLocal = (pe.BypassOnLocal == ProxyElement.BypassOnLocalValues.True);
241 foreach(BypassElement elem in sec.BypassList)
242 p.BypassArrayList.Add(elem.Address);
246 return GetSystemWebProxy ();
252 public virtual void Abort()
254 throw GetMustImplement ();
257 public virtual IAsyncResult BeginGetRequestStream (AsyncCallback callback, object state)
259 throw GetMustImplement ();
262 public virtual IAsyncResult BeginGetResponse (AsyncCallback callback, object state)
264 throw GetMustImplement ();
267 public static WebRequest Create (string requestUriString)
269 if (requestUriString == null)
270 throw new ArgumentNullException ("requestUriString");
271 return Create (new Uri (requestUriString));
274 public static WebRequest Create (Uri requestUri)
276 if (requestUri == null)
277 throw new ArgumentNullException ("requestUri");
278 return GetCreator (requestUri.AbsoluteUri).Create (requestUri);
281 public static WebRequest CreateDefault (Uri requestUri)
283 if (requestUri == null)
284 throw new ArgumentNullException ("requestUri");
285 return GetCreator (requestUri.Scheme).Create (requestUri);
287 static HttpWebRequest SharedCreateHttp (Uri uri)
289 if (uri.Scheme != "http" && uri.Scheme != "https")
290 throw new NotSupportedException ("The uri should start with http or https");
292 return new HttpWebRequest (uri);
295 public static HttpWebRequest CreateHttp (string requestUriString)
297 if (requestUriString == null)
298 throw new ArgumentNullException ("requestUriString");
299 return SharedCreateHttp (new Uri (requestUriString));
302 public static HttpWebRequest CreateHttp (Uri requestUri)
304 if (requestUri == null)
305 throw new ArgumentNullException ("requestUri");
306 return SharedCreateHttp (requestUri);
308 public virtual Stream EndGetRequestStream (IAsyncResult asyncResult)
310 throw GetMustImplement ();
313 public virtual WebResponse EndGetResponse (IAsyncResult asyncResult)
315 throw GetMustImplement ();
318 public virtual Stream GetRequestStream()
320 throw GetMustImplement ();
323 public virtual WebResponse GetResponse()
325 throw GetMustImplement ();
328 [MonoTODO("Look in other places for proxy config info")]
329 public static IWebProxy GetSystemWebProxy ()
332 return CFNetwork.GetDefaultProxy ();
335 // Return the system web proxy. This only works for ICS+.
336 var androidProxy = AndroidPlatform.GetDefaultProxy ();
337 if (androidProxy != null)
342 int iProxyEnable = (int)Microsoft.Win32.Registry.GetValue ("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", "ProxyEnable", 0);
344 if (iProxyEnable > 0) {
345 string strHttpProxy = "";
346 bool bBypassOnLocal = false;
347 ArrayList al = new ArrayList ();
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);
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);
358 } else strHttpProxy = strProxyServer;
360 if (strProxyOverrride != null) {
361 string[] bypassList = strProxyOverrride.Split (new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
363 foreach (string str in bypassList) {
364 if (str != "<local>")
367 bBypassOnLocal = true;
371 return new WebProxy (strHttpProxy, bBypassOnLocal, al.ToArray (typeof(string)) as string[]);
375 if (Platform.IsMacOS)
376 return CFNetwork.GetDefaultProxy ();
378 string address = Environment.GetEnvironmentVariable ("http_proxy");
381 address = Environment.GetEnvironmentVariable ("HTTP_PROXY");
383 if (address != null) {
385 if (!address.StartsWith ("http://"))
386 address = "http://" + address;
388 Uri uri = new Uri (address);
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";
396 } else if (IPAddress.IPv6Any.Equals (ip)) {
397 UriBuilder builder = new UriBuilder (uri);
398 builder.Host = "[::1]";
403 bool bBypassOnLocal = false;
404 ArrayList al = new ArrayList ();
405 string bypass = Environment.GetEnvironmentVariable ("no_proxy");
408 bypass = Environment.GetEnvironmentVariable ("NO_PROXY");
410 if (bypass != null) {
411 string[] bypassList = bypass.Split (new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
413 foreach (string str in bypassList) {
414 if (str != "*.local")
417 bBypassOnLocal = true;
421 return new WebProxy (uri, bBypassOnLocal, al.ToArray (typeof(string)) as string[]);
422 } catch (UriFormatException) {
429 return new WebProxy ();
433 void ISerializable.GetObjectData (SerializationInfo serializationInfo, StreamingContext streamingContext)
435 throw new NotSupportedException ();
438 protected virtual void GetObjectData (SerializationInfo serializationInfo, StreamingContext streamingContext)
440 throw GetMustImplement ();
443 public static bool RegisterPrefix (string prefix, IWebRequestCreate creator)
446 throw new ArgumentNullException ("prefix");
448 throw new ArgumentNullException ("creator");
450 lock (prefixes.SyncRoot) {
451 string lowerCasePrefix = prefix.ToLower (CultureInfo.InvariantCulture);
452 if (prefixes.Contains (lowerCasePrefix))
454 prefixes.Add (lowerCasePrefix, creator);
459 private static IWebRequestCreate GetCreator (string prefix)
461 int longestPrefix = -1;
462 IWebRequestCreate creator = null;
464 prefix = prefix.ToLower (CultureInfo.InvariantCulture);
466 IDictionaryEnumerator e = prefixes.GetEnumerator ();
467 while (e.MoveNext ()) {
468 string key = e.Key as string;
470 if (key.Length <= longestPrefix)
473 if (!prefix.StartsWith (key))
476 longestPrefix = key.Length;
477 creator = (IWebRequestCreate) e.Value;
481 throw new NotSupportedException (prefix);
486 internal static bool IsWindows ()
488 return (int) Environment.OSVersion.Platform < 4;
491 internal static void ClearPrefixes ()
496 internal static void RemovePrefix (string prefix)
498 prefixes.Remove (prefix);
501 internal static void AddPrefix (string prefix, string typeName)
503 Type type = Type.GetType (typeName);
505 throw new ConfigurationException (String.Format ("Type {0} not found", typeName));
506 AddPrefix (prefix, type);
509 internal static void AddPrefix (string prefix, Type type)
511 object o = Activator.CreateInstance (type, true);
512 prefixes [prefix] = o;
515 public virtual Task<Stream> GetRequestStreamAsync ()
517 return Task<Stream>.Factory.FromAsync (BeginGetRequestStream, EndGetRequestStream, null);
520 public virtual Task<WebResponse> GetResponseAsync ()
522 return Task<WebResponse>.Factory.FromAsync (BeginGetResponse, EndGetResponse, null);