WebProxy.cs 6.2 KB

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