WebProxy.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. //
  2. // System.Net.WebProxy.cs
  3. //
  4. // Authors:
  5. // Lawrence Pit ([email protected])
  6. // Andreas Nahr ([email protected])
  7. //
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Collections;
  30. using System.Globalization;
  31. using System.Runtime.Serialization;
  32. using System.Text.RegularExpressions;
  33. namespace System.Net
  34. {
  35. [Serializable]
  36. public class WebProxy : IWebProxy, ISerializable
  37. {
  38. Uri address;
  39. bool bypassOnLocal;
  40. ArrayList bypassList;
  41. ICredentials credentials;
  42. #if NET_2_0
  43. bool useDefaultCredentials;
  44. #endif
  45. // Constructors
  46. public WebProxy ()
  47. : this ((Uri) null, false, null, null) {}
  48. public WebProxy (string address)
  49. : this (ToUri (address), false, null, null) {}
  50. public WebProxy (Uri address)
  51. : this (address, false, null, null) {}
  52. public WebProxy (string address, bool bypassOnLocal)
  53. : this (ToUri (address), bypassOnLocal, null, null) {}
  54. public WebProxy (string host, int port)
  55. : this (new Uri ("http://" + host + ":" + port)) {}
  56. public WebProxy (Uri address, bool bypassOnLocal)
  57. : this (address, bypassOnLocal, null, null) {}
  58. public WebProxy (string address, bool bypassOnLocal, string [] bypassList)
  59. : this (ToUri (address), bypassOnLocal, bypassList, null) {}
  60. public WebProxy (Uri address, bool bypassOnLocal, string [] bypassList)
  61. : this (address, bypassOnLocal, bypassList, null) {}
  62. public WebProxy (string address, bool bypassOnLocal, string [] bypassList,
  63. ICredentials credentials)
  64. : this (ToUri (address), bypassOnLocal, bypassList, credentials) {}
  65. public WebProxy (Uri address, bool bypassOnLocal,
  66. string[] bypassList, ICredentials credentials)
  67. {
  68. this.address = address;
  69. this.bypassOnLocal = bypassOnLocal;
  70. if (bypassList != null)
  71. this.bypassList = new ArrayList (bypassList);
  72. this.credentials = credentials;
  73. CheckBypassList ();
  74. }
  75. protected WebProxy (SerializationInfo serializationInfo, StreamingContext streamingContext)
  76. {
  77. this.address = (Uri) serializationInfo.GetValue ("_ProxyAddress", typeof (Uri));
  78. this.bypassOnLocal = serializationInfo.GetBoolean ("_BypassOnLocal");
  79. this.bypassList = (ArrayList) serializationInfo.GetValue ("_BypassList", typeof (ArrayList));
  80. #if NET_2_0
  81. this.useDefaultCredentials = serializationInfo.GetBoolean ("_UseDefaultCredentials");
  82. #endif
  83. this.credentials = null;
  84. CheckBypassList ();
  85. }
  86. // Properties
  87. public Uri Address {
  88. get { return address; }
  89. set { address = value; }
  90. }
  91. public ArrayList BypassArrayList {
  92. get {
  93. if (bypassList == null)
  94. bypassList = new ArrayList ();
  95. return bypassList;
  96. }
  97. }
  98. public string [] BypassList {
  99. get { return (string []) BypassArrayList.ToArray (typeof (string)); }
  100. set {
  101. if (value == null)
  102. throw new ArgumentNullException ();
  103. bypassList = new ArrayList (value);
  104. CheckBypassList ();
  105. }
  106. }
  107. public bool BypassProxyOnLocal {
  108. get { return bypassOnLocal; }
  109. set { bypassOnLocal = value; }
  110. }
  111. public ICredentials Credentials {
  112. get { return credentials; }
  113. set { credentials = value; }
  114. }
  115. #if NET_2_0
  116. [MonoTODO ("Does not affect Credentials, since CredentialCache.DefaultCredentials is not implemented.")]
  117. public bool UseDefaultCredentials {
  118. get { return useDefaultCredentials; }
  119. set { useDefaultCredentials = value; }
  120. }
  121. #endif
  122. // Methods
  123. [MonoTODO("Can we get this info under windows from the system?")]
  124. public static WebProxy GetDefaultProxy ()
  125. {
  126. // Select gets a WebProxy from config files, if available.
  127. IWebProxy p = GlobalProxySelection.Select;
  128. if (p is WebProxy)
  129. return (WebProxy) p;
  130. return new WebProxy ();
  131. }
  132. public Uri GetProxy (Uri destination)
  133. {
  134. if (IsBypassed (destination))
  135. return destination;
  136. return address;
  137. }
  138. public bool IsBypassed (Uri host)
  139. {
  140. #if NET_2_0
  141. if (host == null)
  142. throw new ArgumentNullException ("host");
  143. #endif
  144. if (host.IsLoopback && bypassOnLocal)
  145. return true;
  146. if (address == null)
  147. return true;
  148. string server = host.Host;
  149. if (bypassOnLocal && server.IndexOf ('.') == -1)
  150. return true;
  151. // LAMESPEC
  152. if (!bypassOnLocal) {
  153. if (String.Compare (server, "localhost", true, CultureInfo.InvariantCulture) == 0)
  154. return true;
  155. if (String.Compare (server, "loopback", true, CultureInfo.InvariantCulture) == 0)
  156. return true;
  157. try {
  158. IPAddress addr = IPAddress.Parse (server);
  159. if (IPAddress.IsLoopback (addr))
  160. return true;
  161. } catch {}
  162. }
  163. if (bypassList == null)
  164. return false;
  165. try {
  166. string hostStr = host.Scheme + "://" + host.Authority;
  167. int i = 0;
  168. for (; i < bypassList.Count; i++) {
  169. Regex regex = new Regex ((string) bypassList [i],
  170. // TODO: RegexOptions.Compiled | // not implemented yet by Regex
  171. RegexOptions.IgnoreCase |
  172. RegexOptions.Singleline);
  173. if (regex.IsMatch (hostStr))
  174. break;
  175. }
  176. if (i == bypassList.Count)
  177. return false;
  178. // continue checking correctness of regular expressions..
  179. // will throw expression when an invalid one is found
  180. for (; i < bypassList.Count; i++)
  181. new Regex ((string) bypassList [i]);
  182. return true;
  183. } catch (ArgumentException) {
  184. return false;
  185. }
  186. }
  187. #if NET_2_0
  188. protected virtual
  189. #endif
  190. void GetObjectData (SerializationInfo serializationInfo, StreamingContext streamingContext)
  191. {
  192. serializationInfo.AddValue ("_BypassOnLocal", bypassOnLocal);
  193. serializationInfo.AddValue ("_ProxyAddress", address);
  194. serializationInfo.AddValue ("_BypassList", bypassList);
  195. #if NET_2_0
  196. serializationInfo.AddValue ("_UseDefaultCredentials", UseDefaultCredentials);
  197. #endif
  198. }
  199. void ISerializable.GetObjectData (SerializationInfo serializationInfo,
  200. StreamingContext streamingContext)
  201. {
  202. GetObjectData (serializationInfo, streamingContext);
  203. }
  204. // Private Methods
  205. // this compiles the regular expressions, and will throw
  206. // an exception when an invalid one is found.
  207. void CheckBypassList ()
  208. {
  209. if (bypassList == null)
  210. return;
  211. for (int i = 0; i < bypassList.Count; i++)
  212. new Regex ((string) bypassList [i]);
  213. }
  214. static Uri ToUri (string address)
  215. {
  216. if (address == null)
  217. return null;
  218. if (address.IndexOf ("://") == -1)
  219. address = "http://" + address;
  220. return new Uri (address);
  221. }
  222. }
  223. }