2 // System.Net.WebProxy.cs
5 // Lawrence Pit (loz@cable.a2000.nl)
6 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Collections;
32 using System.Globalization;
33 using System.Runtime.Serialization;
34 using System.Text.RegularExpressions;
39 public class WebProxy : IWebProxy, ISerializable
44 ICredentials credentials;
45 bool useDefaultCredentials;
50 : this ((Uri) null, false, null, null) {}
52 public WebProxy (string address)
53 : this (ToUri (address), false, null, null) {}
55 public WebProxy (Uri address)
56 : this (address, false, null, null) {}
58 public WebProxy (string address, bool bypassOnLocal)
59 : this (ToUri (address), bypassOnLocal, null, null) {}
61 public WebProxy (string host, int port)
62 : this (new Uri ("http://" + host + ":" + port)) {}
64 public WebProxy (Uri address, bool bypassOnLocal)
65 : this (address, bypassOnLocal, null, null) {}
67 public WebProxy (string address, bool bypassOnLocal, string [] bypassList)
68 : this (ToUri (address), bypassOnLocal, bypassList, null) {}
70 public WebProxy (Uri address, bool bypassOnLocal, string [] bypassList)
71 : this (address, bypassOnLocal, bypassList, null) {}
73 public WebProxy (string address, bool bypassOnLocal, string [] bypassList,
74 ICredentials credentials)
75 : this (ToUri (address), bypassOnLocal, bypassList, credentials) {}
77 public WebProxy (Uri address, bool bypassOnLocal,
78 string[] bypassList, ICredentials credentials)
80 this.address = address;
81 this.bypassOnLocal = bypassOnLocal;
82 if (bypassList != null)
83 this.bypassList = new ArrayList (bypassList);
84 this.credentials = credentials;
88 protected WebProxy (SerializationInfo serializationInfo, StreamingContext streamingContext)
90 this.address = (Uri) serializationInfo.GetValue ("_ProxyAddress", typeof (Uri));
91 this.bypassOnLocal = serializationInfo.GetBoolean ("_BypassOnLocal");
92 this.bypassList = (ArrayList) serializationInfo.GetValue ("_BypassList", typeof (ArrayList));
93 this.useDefaultCredentials = serializationInfo.GetBoolean ("_UseDefaultCredentials");
94 this.credentials = null;
100 get { return address; }
101 set { address = value; }
104 public ArrayList BypassArrayList {
106 if (bypassList == null)
107 bypassList = new ArrayList ();
112 public string [] BypassList {
113 get { return (string []) BypassArrayList.ToArray (typeof (string)); }
116 throw new ArgumentNullException ();
117 bypassList = new ArrayList (value);
122 public bool BypassProxyOnLocal {
123 get { return bypassOnLocal; }
124 set { bypassOnLocal = value; }
127 public ICredentials Credentials {
128 get { return credentials; }
129 set { credentials = value; }
132 [MonoTODO ("Does not affect Credentials, since CredentialCache.DefaultCredentials is not implemented.")]
133 public bool UseDefaultCredentials {
134 get { return useDefaultCredentials; }
135 set { useDefaultCredentials = value; }
139 [Obsolete ("This method has been deprecated", false)]
140 [MonoTODO("Can we get this info under windows from the system?")]
141 public static WebProxy GetDefaultProxy ()
143 // Select gets a WebProxy from config files, if available.
144 IWebProxy p = GlobalProxySelection.Select;
148 return new WebProxy ();
151 public Uri GetProxy (Uri destination)
153 if (IsBypassed (destination))
159 public bool IsBypassed (Uri host)
162 throw new ArgumentNullException ("host");
164 if (host.IsLoopback && bypassOnLocal)
170 string server = host.Host;
171 if (bypassOnLocal && server.IndexOf ('.') == -1)
175 if (!bypassOnLocal) {
176 if (String.Compare (server, "localhost", true, CultureInfo.InvariantCulture) == 0)
178 if (String.Compare (server, "loopback", true, CultureInfo.InvariantCulture) == 0)
181 IPAddress addr = null;
182 if (IPAddress.TryParse (server, out addr) && IPAddress.IsLoopback (addr))
186 if (bypassList == null || bypassList.Count == 0)
190 string hostStr = host.Scheme + "://" + host.Authority;
192 for (; i < bypassList.Count; i++) {
193 Regex regex = new Regex ((string) bypassList [i],
194 // TODO: RegexOptions.Compiled | // not implemented yet by Regex
195 RegexOptions.IgnoreCase |
196 RegexOptions.Singleline);
198 if (regex.IsMatch (hostStr))
202 if (i == bypassList.Count)
205 // continue checking correctness of regular expressions..
206 // will throw expression when an invalid one is found
207 for (; i < bypassList.Count; i++)
208 new Regex ((string) bypassList [i]);
211 } catch (ArgumentException) {
216 protected virtual void GetObjectData (SerializationInfo serializationInfo, StreamingContext streamingContext)
218 serializationInfo.AddValue ("_BypassOnLocal", bypassOnLocal);
219 serializationInfo.AddValue ("_ProxyAddress", address);
220 serializationInfo.AddValue ("_BypassList", bypassList);
221 serializationInfo.AddValue ("_UseDefaultCredentials", UseDefaultCredentials);
224 void ISerializable.GetObjectData (SerializationInfo serializationInfo,
225 StreamingContext streamingContext)
227 GetObjectData (serializationInfo, streamingContext);
231 // this compiles the regular expressions, and will throw
232 // an exception when an invalid one is found.
233 void CheckBypassList ()
235 if (bypassList == null)
237 for (int i = 0; i < bypassList.Count; i++)
238 new Regex ((string) bypassList [i]);
241 static Uri ToUri (string address)
246 if (address.IndexOf ("://", StringComparison.Ordinal) == -1)
247 address = "http://" + address;
249 return new Uri (address);