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;
46 bool useDefaultCredentials;
52 : this ((Uri) null, false, null, null) {}
54 public WebProxy (string address)
55 : this (ToUri (address), false, null, null) {}
57 public WebProxy (Uri address)
58 : this (address, false, null, null) {}
60 public WebProxy (string address, bool bypassOnLocal)
61 : this (ToUri (address), bypassOnLocal, null, null) {}
63 public WebProxy (string host, int port)
64 : this (new Uri ("http://" + host + ":" + port)) {}
66 public WebProxy (Uri address, bool bypassOnLocal)
67 : this (address, bypassOnLocal, null, null) {}
69 public WebProxy (string address, bool bypassOnLocal, string [] bypassList)
70 : this (ToUri (address), bypassOnLocal, bypassList, null) {}
72 public WebProxy (Uri address, bool bypassOnLocal, string [] bypassList)
73 : this (address, bypassOnLocal, bypassList, null) {}
75 public WebProxy (string address, bool bypassOnLocal, string [] bypassList,
76 ICredentials credentials)
77 : this (ToUri (address), bypassOnLocal, bypassList, credentials) {}
79 public WebProxy (Uri address, bool bypassOnLocal,
80 string[] bypassList, ICredentials credentials)
82 this.address = address;
83 this.bypassOnLocal = bypassOnLocal;
84 if (bypassList != null)
85 this.bypassList = new ArrayList (bypassList);
86 this.credentials = credentials;
90 protected WebProxy (SerializationInfo serializationInfo, StreamingContext streamingContext)
92 this.address = (Uri) serializationInfo.GetValue ("_ProxyAddress", typeof (Uri));
93 this.bypassOnLocal = serializationInfo.GetBoolean ("_BypassOnLocal");
94 this.bypassList = (ArrayList) serializationInfo.GetValue ("_BypassList", typeof (ArrayList));
96 this.useDefaultCredentials = serializationInfo.GetBoolean ("_UseDefaultCredentials");
98 this.credentials = null;
104 get { return address; }
105 set { address = value; }
108 public ArrayList BypassArrayList {
110 if (bypassList == null)
111 bypassList = new ArrayList ();
116 public string [] BypassList {
117 get { return (string []) BypassArrayList.ToArray (typeof (string)); }
120 throw new ArgumentNullException ();
121 bypassList = new ArrayList (value);
126 public bool BypassProxyOnLocal {
127 get { return bypassOnLocal; }
128 set { bypassOnLocal = value; }
131 public ICredentials Credentials {
132 get { return credentials; }
133 set { credentials = value; }
137 [MonoTODO ("Does not affect Credentials, since CredentialCache.DefaultCredentials is not implemented.")]
138 public bool UseDefaultCredentials {
139 get { return useDefaultCredentials; }
140 set { useDefaultCredentials = value; }
146 [Obsolete ("This method has been deprecated", false)]
148 [MonoTODO("Can we get this info under windows from the system?")]
149 public static WebProxy GetDefaultProxy ()
151 // Select gets a WebProxy from config files, if available.
152 IWebProxy p = GlobalProxySelection.Select;
156 return new WebProxy ();
159 public Uri GetProxy (Uri destination)
161 if (IsBypassed (destination))
167 public bool IsBypassed (Uri host)
171 throw new ArgumentNullException ("host");
174 if (host.IsLoopback && bypassOnLocal)
180 string server = host.Host;
181 if (bypassOnLocal && server.IndexOf ('.') == -1)
185 if (!bypassOnLocal) {
186 if (String.Compare (server, "localhost", true, CultureInfo.InvariantCulture) == 0)
188 if (String.Compare (server, "loopback", true, CultureInfo.InvariantCulture) == 0)
191 IPAddress addr = null;
192 if (IPAddress.TryParse (server, out addr) && IPAddress.IsLoopback (addr))
196 if (bypassList == null || bypassList.Count == 0)
200 string hostStr = host.Scheme + "://" + host.Authority;
202 for (; i < bypassList.Count; i++) {
203 Regex regex = new Regex ((string) bypassList [i],
204 // TODO: RegexOptions.Compiled | // not implemented yet by Regex
205 RegexOptions.IgnoreCase |
206 RegexOptions.Singleline);
208 if (regex.IsMatch (hostStr))
212 if (i == bypassList.Count)
215 // continue checking correctness of regular expressions..
216 // will throw expression when an invalid one is found
217 for (; i < bypassList.Count; i++)
218 new Regex ((string) bypassList [i]);
221 } catch (ArgumentException) {
229 void GetObjectData (SerializationInfo serializationInfo, StreamingContext streamingContext)
231 serializationInfo.AddValue ("_BypassOnLocal", bypassOnLocal);
232 serializationInfo.AddValue ("_ProxyAddress", address);
233 serializationInfo.AddValue ("_BypassList", bypassList);
235 serializationInfo.AddValue ("_UseDefaultCredentials", UseDefaultCredentials);
239 void ISerializable.GetObjectData (SerializationInfo serializationInfo,
240 StreamingContext streamingContext)
242 GetObjectData (serializationInfo, streamingContext);
246 // this compiles the regular expressions, and will throw
247 // an exception when an invalid one is found.
248 void CheckBypassList ()
250 if (bypassList == null)
252 for (int i = 0; i < bypassList.Count; i++)
253 new Regex ((string) bypassList [i]);
256 static Uri ToUri (string address)
261 if (address.IndexOf ("://", StringComparison.Ordinal) == -1)
262 address = "http://" + address;
264 return new Uri (address);