WebProxy.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. //
  2. // System.Net.WebProxy.cs
  3. //
  4. // Authors:
  5. // Lawrence Pit ([email protected])
  6. // Andreas Nahr ([email protected])
  7. //
  8. using System;
  9. using System.Collections;
  10. using System.Runtime.Serialization;
  11. using System.Text.RegularExpressions;
  12. namespace System.Net
  13. {
  14. [Serializable]
  15. public class WebProxy : IWebProxy, ISerializable
  16. {
  17. private Uri address;
  18. private bool bypassOnLocal;
  19. private ArrayList bypassList;
  20. private ICredentials credentials;
  21. // Constructors
  22. public WebProxy ()
  23. : this ((Uri) null, false, null, null) {}
  24. public WebProxy (string address)
  25. : this (ToUri (address), false, null, null) {}
  26. public WebProxy (Uri address)
  27. : this (address, false, null, null) {}
  28. public WebProxy (string address, bool bypassOnLocal)
  29. : this (ToUri (address), bypassOnLocal, null, null) {}
  30. public WebProxy (string host, int port)
  31. : this (new Uri ("http://" + host + ":" + port)) {}
  32. public WebProxy (Uri address, bool bypassOnLocal)
  33. : this (address, bypassOnLocal, null, null) {}
  34. public WebProxy (string address, bool bypassOnLocal, string [] bypassList)
  35. : this (ToUri (address), bypassOnLocal, bypassList, null) {}
  36. public WebProxy (Uri address, bool bypassOnLocal, string [] bypassList)
  37. : this (address, bypassOnLocal, bypassList, null) {}
  38. public WebProxy (string address, bool bypassOnLocal,
  39. string[] bypassList, ICredentials credentials)
  40. : this (ToUri (address), bypassOnLocal, bypassList, null) {}
  41. public WebProxy (Uri address, bool bypassOnLocal,
  42. string[] bypassList, ICredentials credentials)
  43. {
  44. this.address = address;
  45. this.bypassOnLocal = bypassOnLocal;
  46. if (bypassList == null)
  47. bypassList = new string [] {};
  48. this.bypassList = new ArrayList (bypassList);
  49. this.credentials = credentials;
  50. CheckBypassList ();
  51. }
  52. protected WebProxy (SerializationInfo serializationInfo, StreamingContext streamingContext)
  53. {
  54. this.address = (Uri) serializationInfo.GetValue ("address", typeof(Uri));
  55. this.bypassOnLocal = serializationInfo.GetBoolean ("bypassOnLocal");
  56. this.bypassList = (ArrayList) serializationInfo.GetValue ("bypassList", typeof(ArrayList));
  57. this.credentials = null;
  58. CheckBypassList ();
  59. }
  60. // Properties
  61. public Uri Address {
  62. get { return address; }
  63. set { address = value; }
  64. }
  65. public ArrayList BypassArrayList {
  66. get {
  67. return bypassList;
  68. }
  69. }
  70. public string [] BypassList {
  71. get { return (string []) bypassList.ToArray (typeof (string)); }
  72. set {
  73. if (value == null)
  74. throw new ArgumentNullException ();
  75. bypassList = new ArrayList (value);
  76. CheckBypassList ();
  77. }
  78. }
  79. public bool BypassProxyOnLocal {
  80. get { return bypassOnLocal; }
  81. set { bypassOnLocal = value; }
  82. }
  83. public ICredentials Credentials {
  84. get { return credentials; }
  85. set { credentials = value; }
  86. }
  87. // Methods
  88. [MonoTODO("Can we get this info under windows from the system?")]
  89. public static WebProxy GetDefaultProxy ()
  90. {
  91. // Select gets a WebProxy from config files, if available.
  92. IWebProxy p = GlobalProxySelection.Select;
  93. if (p is WebProxy)
  94. return (WebProxy) p;
  95. return new WebProxy ();
  96. }
  97. public Uri GetProxy (Uri destination)
  98. {
  99. if (IsBypassed (destination))
  100. return destination;
  101. return address;
  102. }
  103. public bool IsBypassed (Uri host)
  104. {
  105. if (address == null)
  106. return true;
  107. if (host.IsLoopback)
  108. return true;
  109. if (bypassOnLocal && host.Host.IndexOf ('.') == -1)
  110. return true;
  111. try {
  112. string hostStr = host.Scheme + "://" + host.Authority;
  113. int i = 0;
  114. for (; i < bypassList.Count; i++) {
  115. Regex regex = new Regex ((string) bypassList [i],
  116. // TODO: RegexOptions.Compiled | // not implemented yet by Regex
  117. RegexOptions.IgnoreCase |
  118. RegexOptions.Singleline);
  119. if (regex.IsMatch (hostStr))
  120. break;
  121. }
  122. if (i == bypassList.Count)
  123. return false;
  124. // continue checking correctness of regular expressions..
  125. // will throw expression when an invalid one is found
  126. for (; i < bypassList.Count; i++)
  127. new Regex ((string) bypassList [i]);
  128. return true;
  129. } catch (ArgumentException) {
  130. return false;
  131. }
  132. }
  133. void ISerializable.GetObjectData (SerializationInfo serializationInfo,
  134. StreamingContext streamingContext)
  135. {
  136. serializationInfo.AddValue ("bypassOnLocal", bypassOnLocal);
  137. serializationInfo.AddValue ("address", address);
  138. serializationInfo.AddValue ("bypassList", bypassList);
  139. }
  140. // Private Methods
  141. // this compiles the regular expressions, and will throw
  142. // an exception when an invalid one is found.
  143. private void CheckBypassList ()
  144. {
  145. for (int i = 0; i < bypassList.Count; i++)
  146. new Regex ((string) bypassList [i]);
  147. }
  148. private static Uri ToUri (string address)
  149. {
  150. if (address == null)
  151. return null;
  152. if (address.IndexOf (':') == -1)
  153. address = "http://" + address;
  154. return new Uri (address);
  155. }
  156. }
  157. }