WebProxy.cs 4.8 KB

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