mscorlib(4.0.0.0) API with additions
WebProxy.cs
1 using System.Collections;
8 
9 namespace System.Net
10 {
12  [Serializable]
13  public class WebProxy : IAutoWebProxy, IWebProxy, ISerializable
14  {
15  private bool _UseRegistry;
16 
17  private bool _BypassOnLocal;
18 
19  private bool m_EnableAutoproxy;
20 
21  private Uri _ProxyAddress;
22 
23  private ArrayList _BypassList;
24 
25  private ICredentials _Credentials;
26 
27  private Regex[] _RegExBypassList;
28 
29  private Hashtable _ProxyHostAddresses;
30 
31  private AutoWebProxyScriptEngine m_ScriptEngine;
32 
35  public Uri Address
36  {
37  get
38  {
39  CheckForChanges();
40  return _ProxyAddress;
41  }
42  set
43  {
44  _UseRegistry = false;
45  DeleteScriptEngine();
46  _ProxyHostAddresses = null;
47  _ProxyAddress = value;
48  }
49  }
50 
51  internal bool AutoDetect
52  {
53  set
54  {
55  if (ScriptEngine == null)
56  {
57  ScriptEngine = new AutoWebProxyScriptEngine(this, useRegistry: false);
58  }
59  ScriptEngine.AutomaticallyDetectSettings = value;
60  }
61  }
62 
63  internal Uri ScriptLocation
64  {
65  set
66  {
67  if (ScriptEngine == null)
68  {
69  ScriptEngine = new AutoWebProxyScriptEngine(this, useRegistry: false);
70  }
71  ScriptEngine.AutomaticConfigurationScript = value;
72  }
73  }
74 
78  public bool BypassProxyOnLocal
79  {
80  get
81  {
82  CheckForChanges();
83  return _BypassOnLocal;
84  }
85  set
86  {
87  _UseRegistry = false;
88  DeleteScriptEngine();
89  _BypassOnLocal = value;
90  }
91  }
92 
95  public string[] BypassList
96  {
97  get
98  {
99  CheckForChanges();
100  if (_BypassList == null)
101  {
102  _BypassList = new ArrayList();
103  }
104  return (string[])_BypassList.ToArray(typeof(string));
105  }
106  set
107  {
108  _UseRegistry = false;
109  DeleteScriptEngine();
110  _BypassList = new ArrayList(value);
111  UpdateRegExList(canThrow: true);
112  }
113  }
114 
119  {
120  get
121  {
122  return _Credentials;
123  }
124  set
125  {
126  _Credentials = value;
127  }
128  }
129 
134  public bool UseDefaultCredentials
135  {
136  get
137  {
138  if (!(Credentials is SystemNetworkCredential))
139  {
140  return false;
141  }
142  return true;
143  }
144  set
145  {
146  _Credentials = (value ? CredentialCache.DefaultCredentials : null);
147  }
148  }
149 
153  {
154  get
155  {
156  CheckForChanges();
157  if (_BypassList == null)
158  {
159  _BypassList = new ArrayList();
160  }
161  return _BypassList;
162  }
163  }
164 
165  internal AutoWebProxyScriptEngine ScriptEngine
166  {
167  get
168  {
169  return m_ScriptEngine;
170  }
171  set
172  {
173  m_ScriptEngine = value;
174  }
175  }
176 
178  public WebProxy()
179  : this((Uri)null, BypassOnLocal: false, (string[])null, (ICredentials)null)
180  {
181  }
182 
186  : this(Address, BypassOnLocal: false, null, null)
187  {
188  }
189 
194  public WebProxy(Uri Address, bool BypassOnLocal)
195  : this(Address, BypassOnLocal, null, null)
196  {
197  }
198 
204  public WebProxy(Uri Address, bool BypassOnLocal, string[] BypassList)
205  : this(Address, BypassOnLocal, BypassList, null)
206  {
207  }
208 
215  public WebProxy(Uri Address, bool BypassOnLocal, string[] BypassList, ICredentials Credentials)
216  {
217  _ProxyAddress = Address;
218  _BypassOnLocal = BypassOnLocal;
219  if (BypassList != null)
220  {
221  _BypassList = new ArrayList(BypassList);
222  UpdateRegExList(canThrow: true);
223  }
224  _Credentials = Credentials;
225  m_EnableAutoproxy = true;
226  }
227 
232  public WebProxy(string Host, int Port)
233  : this(new Uri("http://" + Host + ":" + Port.ToString(CultureInfo.InvariantCulture)), BypassOnLocal: false, null, null)
234  {
235  }
236 
241  public WebProxy(string Address)
242  : this(CreateProxyUri(Address), BypassOnLocal: false, null, null)
243  {
244  }
245 
252  public WebProxy(string Address, bool BypassOnLocal)
253  : this(CreateProxyUri(Address), BypassOnLocal, null, null)
254  {
255  }
256 
264  public WebProxy(string Address, bool BypassOnLocal, string[] BypassList)
265  : this(CreateProxyUri(Address), BypassOnLocal, BypassList, null)
266  {
267  }
268 
277  public WebProxy(string Address, bool BypassOnLocal, string[] BypassList, ICredentials Credentials)
278  : this(CreateProxyUri(Address), BypassOnLocal, BypassList, Credentials)
279  {
280  }
281 
282  internal void CheckForChanges()
283  {
284  if (ScriptEngine != null)
285  {
286  ScriptEngine.CheckForChanges();
287  }
288  }
289 
294  public Uri GetProxy(Uri destination)
295  {
296  if (destination == null)
297  {
298  throw new ArgumentNullException("destination");
299  }
300  if (GetProxyAuto(destination, out Uri proxyUri))
301  {
302  return proxyUri;
303  }
304  if (IsBypassedManual(destination))
305  {
306  return destination;
307  }
308  Hashtable proxyHostAddresses = _ProxyHostAddresses;
309  Uri uri = (proxyHostAddresses != null) ? (proxyHostAddresses[destination.Scheme] as Uri) : _ProxyAddress;
310  if (!(uri != null))
311  {
312  return destination;
313  }
314  return uri;
315  }
316 
317  private static Uri CreateProxyUri(string address)
318  {
319  if (address == null)
320  {
321  return null;
322  }
323  if (address.IndexOf("://") == -1)
324  {
325  address = "http://" + address;
326  }
327  return new Uri(address);
328  }
329 
330  private void UpdateRegExList(bool canThrow)
331  {
332  Regex[] array = null;
333  ArrayList bypassList = _BypassList;
334  try
335  {
336  if (bypassList != null && bypassList.Count > 0)
337  {
338  array = new Regex[bypassList.Count];
339  for (int i = 0; i < bypassList.Count; i++)
340  {
341  array[i] = new Regex((string)bypassList[i], RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
342  }
343  }
344  }
345  catch
346  {
347  if (!canThrow)
348  {
349  _RegExBypassList = null;
350  return;
351  }
352  throw;
353  }
354  _RegExBypassList = array;
355  }
356 
357  private bool IsMatchInBypassList(Uri input)
358  {
359  UpdateRegExList(canThrow: false);
360  if (_RegExBypassList == null)
361  {
362  return false;
363  }
364  string input2 = input.Scheme + "://" + input.Host + ((!input.IsDefaultPort) ? (":" + input.Port) : "");
365  for (int i = 0; i < _BypassList.Count; i++)
366  {
367  if (_RegExBypassList[i].IsMatch(input2))
368  {
369  return true;
370  }
371  }
372  return false;
373  }
374 
375  private bool IsLocal(Uri host)
376  {
377  string host2 = host.Host;
378  if (IPAddress.TryParse(host2, out IPAddress address))
379  {
380  if (!IPAddress.IsLoopback(address))
381  {
382  return NclUtilities.IsAddressLocal(address);
383  }
384  return true;
385  }
386  int num = host2.IndexOf('.');
387  if (num == -1)
388  {
389  return true;
390  }
391  string text = "." + IPGlobalProperties.InternalGetIPGlobalProperties().DomainName;
392  if (text != null && text.Length == host2.Length - num && string.Compare(text, 0, host2, num, text.Length, StringComparison.OrdinalIgnoreCase) == 0)
393  {
394  return true;
395  }
396  return false;
397  }
398 
399  private bool IsLocalInProxyHash(Uri host)
400  {
401  Hashtable proxyHostAddresses = _ProxyHostAddresses;
402  if (proxyHostAddresses != null)
403  {
404  Uri uri = (Uri)proxyHostAddresses[host.Scheme];
405  if (uri == null)
406  {
407  return true;
408  }
409  }
410  return false;
411  }
412 
418  public bool IsBypassed(Uri host)
419  {
420  if (host == null)
421  {
422  throw new ArgumentNullException("host");
423  }
424  if (IsBypassedAuto(host, out bool isBypassed))
425  {
426  return isBypassed;
427  }
428  return IsBypassedManual(host);
429  }
430 
431  private bool IsBypassedManual(Uri host)
432  {
433  if (host.IsLoopback)
434  {
435  return true;
436  }
437  if ((!(_ProxyAddress == null) || _ProxyHostAddresses != null) && (!_BypassOnLocal || !IsLocal(host)) && !IsMatchInBypassList(host))
438  {
439  return IsLocalInProxyHash(host);
440  }
441  return true;
442  }
443 
446  [Obsolete("This method has been deprecated. Please use the proxy selected for you by default. http://go.microsoft.com/fwlink/?linkid=14202")]
447  public static WebProxy GetDefaultProxy()
448  {
449  ExceptionHelper.WebPermissionUnrestricted.Demand();
450  return new WebProxy(enableAutoproxy: true);
451  }
452 
456  protected WebProxy(SerializationInfo serializationInfo, StreamingContext streamingContext)
457  {
458  bool flag = false;
459  try
460  {
461  flag = serializationInfo.GetBoolean("_UseRegistry");
462  }
463  catch
464  {
465  }
466  if (flag)
467  {
468  ExceptionHelper.WebPermissionUnrestricted.Demand();
469  UnsafeUpdateFromRegistry();
470  return;
471  }
472  _ProxyAddress = (Uri)serializationInfo.GetValue("_ProxyAddress", typeof(Uri));
473  _BypassOnLocal = serializationInfo.GetBoolean("_BypassOnLocal");
474  _BypassList = (ArrayList)serializationInfo.GetValue("_BypassList", typeof(ArrayList));
475  try
476  {
477  UseDefaultCredentials = serializationInfo.GetBoolean("_UseDefaultCredentials");
478  }
479  catch
480  {
481  }
482  }
483 
487  [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter, SerializationFormatter = true)]
488  void ISerializable.GetObjectData(SerializationInfo serializationInfo, StreamingContext streamingContext)
489  {
490  GetObjectData(serializationInfo, streamingContext);
491  }
492 
496  [SecurityPermission(SecurityAction.LinkDemand, SerializationFormatter = true)]
497  protected virtual void GetObjectData(SerializationInfo serializationInfo, StreamingContext streamingContext)
498  {
499  serializationInfo.AddValue("_BypassOnLocal", _BypassOnLocal);
500  serializationInfo.AddValue("_ProxyAddress", _ProxyAddress);
501  serializationInfo.AddValue("_BypassList", _BypassList);
502  serializationInfo.AddValue("_UseDefaultCredentials", UseDefaultCredentials);
503  if (_UseRegistry)
504  {
505  serializationInfo.AddValue("_UseRegistry", value: true);
506  }
507  }
508 
509  internal WebProxy(bool enableAutoproxy)
510  {
511  m_EnableAutoproxy = enableAutoproxy;
512  UnsafeUpdateFromRegistry();
513  }
514 
515  internal void DeleteScriptEngine()
516  {
517  if (ScriptEngine != null)
518  {
519  ScriptEngine.Close();
520  ScriptEngine = null;
521  }
522  }
523 
524  internal void UnsafeUpdateFromRegistry()
525  {
526  _UseRegistry = true;
527  ScriptEngine = new AutoWebProxyScriptEngine(this, useRegistry: true);
528  WebProxyData webProxyData = ScriptEngine.GetWebProxyData();
529  Update(webProxyData);
530  }
531 
532  internal void Update(WebProxyData webProxyData)
533  {
534  lock (this)
535  {
536  _BypassOnLocal = webProxyData.bypassOnLocal;
537  _ProxyAddress = webProxyData.proxyAddress;
538  _ProxyHostAddresses = webProxyData.proxyHostAddresses;
539  _BypassList = webProxyData.bypassList;
540  ScriptEngine.AutomaticallyDetectSettings = (m_EnableAutoproxy && webProxyData.automaticallyDetectSettings);
541  ScriptEngine.AutomaticConfigurationScript = (m_EnableAutoproxy ? webProxyData.scriptLocation : null);
542  }
543  }
544 
545  ProxyChain IAutoWebProxy.GetProxies(Uri destination)
546  {
547  if (destination == null)
548  {
549  throw new ArgumentNullException("destination");
550  }
551  return new ProxyScriptChain(this, destination);
552  }
553 
554  private bool GetProxyAuto(Uri destination, out Uri proxyUri)
555  {
556  proxyUri = null;
557  if (ScriptEngine == null)
558  {
559  return false;
560  }
561  IList<string> proxyList = null;
562  if (!ScriptEngine.GetProxies(destination, out proxyList))
563  {
564  return false;
565  }
566  if (proxyList.Count > 0)
567  {
568  if (AreAllBypassed(proxyList, checkFirstOnly: true))
569  {
570  proxyUri = destination;
571  }
572  else
573  {
574  proxyUri = ProxyUri(proxyList[0]);
575  }
576  }
577  return true;
578  }
579 
580  private bool IsBypassedAuto(Uri destination, out bool isBypassed)
581  {
582  isBypassed = true;
583  if (ScriptEngine == null)
584  {
585  return false;
586  }
587  if (!ScriptEngine.GetProxies(destination, out IList<string> proxyList))
588  {
589  return false;
590  }
591  if (proxyList.Count == 0)
592  {
593  isBypassed = false;
594  }
595  else
596  {
597  isBypassed = AreAllBypassed(proxyList, checkFirstOnly: true);
598  }
599  return true;
600  }
601 
602  internal Uri[] GetProxiesAuto(Uri destination, ref int syncStatus)
603  {
604  if (ScriptEngine == null)
605  {
606  return null;
607  }
608  IList<string> proxyList = null;
609  if (!ScriptEngine.GetProxies(destination, out proxyList, ref syncStatus))
610  {
611  return null;
612  }
613  Uri[] array = null;
614  if (proxyList.Count == 0)
615  {
616  array = new Uri[0];
617  }
618  else if (AreAllBypassed(proxyList, checkFirstOnly: false))
619  {
620  array = new Uri[1];
621  }
622  else
623  {
624  array = new Uri[proxyList.Count];
625  for (int i = 0; i < proxyList.Count; i++)
626  {
627  array[i] = ProxyUri(proxyList[i]);
628  }
629  }
630  return array;
631  }
632 
633  internal void AbortGetProxiesAuto(ref int syncStatus)
634  {
635  if (ScriptEngine != null)
636  {
637  ScriptEngine.Abort(ref syncStatus);
638  }
639  }
640 
641  internal Uri GetProxyAutoFailover(Uri destination)
642  {
643  if (IsBypassedManual(destination))
644  {
645  return null;
646  }
647  Uri result = _ProxyAddress;
648  Hashtable proxyHostAddresses = _ProxyHostAddresses;
649  if (proxyHostAddresses != null)
650  {
651  result = (proxyHostAddresses[destination.Scheme] as Uri);
652  }
653  return result;
654  }
655 
656  private static bool AreAllBypassed(IEnumerable<string> proxies, bool checkFirstOnly)
657  {
658  bool flag = true;
659  foreach (string proxy in proxies)
660  {
661  flag = string.IsNullOrEmpty(proxy);
662  if (checkFirstOnly)
663  {
664  return flag;
665  }
666  if (!flag)
667  {
668  return flag;
669  }
670  }
671  return flag;
672  }
673 
674  private static Uri ProxyUri(string proxyName)
675  {
676  if (proxyName != null && proxyName.Length != 0)
677  {
678  return new Uri("http://" + proxyName);
679  }
680  return null;
681  }
682  }
683 }
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
Describes a set of security permissions applied to code. This class cannot be inherited.
StringComparison
Specifies the culture, case, and sort rules to be used by certain overloads of the M:System....
bool? UseDefaultCredentials
Gets or sets a T:System.Boolean value that controls whether the P:System.Net.CredentialCache....
Definition: WebProxy.cs:135
bool GetBoolean(string name)
Retrieves a Boolean value from the T:System.Runtime.Serialization.SerializationInfo store.
virtual int Count
Gets the number of elements actually contained in the T:System.Collections.ArrayList.
Definition: ArrayList.cs:2255
Definition: __Canon.cs:3
The Host header, which specifies the host name and port number of the resource being requested.
abstract string DomainName
Gets the domain in which the local computer is registered.
string Scheme
Gets the scheme name for this URI.
Definition: Uri.cs:702
WebProxy(SerializationInfo serializationInfo, StreamingContext streamingContext)
Initializes an instance of the T:System.Net.WebProxy class using previously serialized content.
Definition: WebProxy.cs:456
WebProxy(string Address, bool BypassOnLocal, string[] BypassList, ICredentials Credentials)
Initializes a new instance of the T:System.Net.WebProxy class with the specified URI,...
Definition: WebProxy.cs:277
Compare strings using culture-sensitive sort rules and the invariant culture.
bool IsLoopback
Gets whether the specified T:System.Uri references the local host.
Definition: Uri.cs:489
WebProxy(string Address, bool BypassOnLocal)
Initializes a new instance of the T:System.Net.WebProxy class with the specified URI and bypass setti...
Definition: WebProxy.cs:252
Describes the source and destination of a given serialized stream, and provides an additional caller-...
WebProxy()
Initializes an empty instance of the T:System.Net.WebProxy class.
Definition: WebProxy.cs:178
Provides information about the network connectivity of the local computer.
SecurityAction
Specifies the security actions that can be performed using declarative security.
void AddValue(string name, object value, Type type)
Adds a value into the T:System.Runtime.Serialization.SerializationInfo store, where value is associa...
WebProxy(Uri Address)
Initializes a new instance of the T:System.Net.WebProxy class from the specified T:System....
Definition: WebProxy.cs:185
WebProxy(string Host, int Port)
Initializes a new instance of the T:System.Net.WebProxy class with the specified host and port number...
Definition: WebProxy.cs:232
WebProxy(Uri Address, bool BypassOnLocal, string[] BypassList)
Initializes a new instance of the T:System.Net.WebProxy class with the specified T:System....
Definition: WebProxy.cs:204
Uri Address
Gets or sets the address of the proxy server.
Definition: WebProxy.cs:36
Provides storage for multiple credentials.
Represents a collection of key/value pairs that are organized based on the hash code of the key....
Definition: Hashtable.cs:17
static ICredentials DefaultCredentials
Gets the system credentials of the application.
Uri GetProxy(Uri destination)
Returns the proxied URI for a request.
Definition: WebProxy.cs:294
virtual void GetObjectData(SerializationInfo serializationInfo, StreamingContext streamingContext)
Populates a T:System.Runtime.Serialization.SerializationInfo with the data that is needed to serializ...
Definition: WebProxy.cs:497
Provides the base authentication interface for retrieving credentials for Web client authentication.
Definition: ICredentials.cs:5
ArrayList BypassArrayList
Gets a list of addresses that do not use the proxy server.
Definition: WebProxy.cs:153
Stores all the data needed to serialize or deserialize an object. This class cannot be inherited.
ICredentials Credentials
Gets or sets the credentials to submit to the proxy server for authentication.
Definition: WebProxy.cs:119
RegexOptions
Provides enumerated values to use to set regular expression options.
Definition: RegexOptions.cs:6
Allows an object to control its own serialization and deserialization.
Definition: ISerializable.cs:8
string [] BypassList
Gets or sets an array of addresses that do not use the proxy server.
Definition: WebProxy.cs:96
The P:System.Uri.Port data.
bool BypassProxyOnLocal
Gets or sets a value that indicates whether to bypass the proxy server for local addresses.
Definition: WebProxy.cs:79
static WebProxy GetDefaultProxy()
Reads the Internet Explorer nondynamic proxy settings.
Definition: WebProxy.cs:447
bool IsBypassed(Uri host)
Indicates whether to use the proxy server for the specified host.
Definition: WebProxy.cs:418
object GetValue(string name, Type type)
Retrieves a value from the T:System.Runtime.Serialization.SerializationInfo store.
Specifies that the class can be serialized.
WebProxy(Uri Address, bool BypassOnLocal, string[] BypassList, ICredentials Credentials)
Initializes a new instance of the T:System.Net.WebProxy class with the specified T:System....
Definition: WebProxy.cs:215
Provides the base interface for implementation of proxy access for the T:System.Net....
Definition: IWebProxy.cs:5
Provides information about a specific culture (called a locale for unmanaged code development)....
Definition: CultureInfo.cs:16
SecurityPermissionFlag
Specifies access flags for the security permission object.
Provides an object representation of a uniform resource identifier (URI) and easy access to the parts...
Definition: Uri.cs:19
WebProxy(string Address, bool BypassOnLocal, string[] BypassList)
Initializes a new instance of the T:System.Net.WebProxy class with the specified URI,...
Definition: WebProxy.cs:264
WebProxy(string Address)
Initializes a new instance of the T:System.Net.WebProxy class with the specified URI.
Definition: WebProxy.cs:241
void GetObjectData(SerializationInfo info, StreamingContext context)
Populates a T:System.Runtime.Serialization.SerializationInfo with the data needed to serialize the ta...
Represents an immutable regular expression.To browse the .NET Framework source code for this type,...
Definition: Regex.cs:16
WebProxy(Uri Address, bool BypassOnLocal)
Initializes a new instance of the T:System.Net.WebProxy class with the T:System.Uri instance and bypa...
Definition: WebProxy.cs:194
Contains HTTP proxy settings for the T:System.Net.WebRequest class.
Definition: WebProxy.cs:13
virtual object [] ToArray()
Copies the elements of the T:System.Collections.ArrayList to a new T:System.Object array.
Definition: ArrayList.cs:3083
Implements the T:System.Collections.IList interface using an array whose size is dynamically increase...
Definition: ArrayList.cs:14