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 {
43 ICredentials credentials;
48 : this ((Uri) null, false, null, null) {}
50 public WebProxy (string address)
51 : this (ToUri (address), false, null, null) {}
53 public WebProxy (Uri address)
54 : this (address, false, null, null) {}
56 public WebProxy (string address, bool bypassOnLocal)
57 : this (ToUri (address), bypassOnLocal, null, null) {}
59 public WebProxy (string host, int port)
60 : this (new Uri ("http://" + host + ":" + port)) {}
62 public WebProxy (Uri address, bool bypassOnLocal)
63 : this (address, bypassOnLocal, null, null) {}
65 public WebProxy (string address, bool bypassOnLocal, string [] bypassList)
66 : this (ToUri (address), bypassOnLocal, bypassList, null) {}
68 public WebProxy (Uri address, bool bypassOnLocal, string [] bypassList)
69 : this (address, bypassOnLocal, bypassList, null) {}
71 public WebProxy (string address, bool bypassOnLocal, string [] bypassList,
72 ICredentials credentials)
73 : this (ToUri (address), bypassOnLocal, bypassList, credentials) {}
75 public WebProxy (Uri address, bool bypassOnLocal,
76 string[] bypassList, ICredentials credentials)
78 this.address = address;
79 this.bypassOnLocal = bypassOnLocal;
80 if (bypassList == null)
81 bypassList = new string [] {};
82 this.bypassList = new ArrayList (bypassList);
83 this.credentials = credentials;
87 protected WebProxy (SerializationInfo serializationInfo, StreamingContext streamingContext)
89 this.address = (Uri) serializationInfo.GetValue ("address", typeof (Uri));
90 this.bypassOnLocal = serializationInfo.GetBoolean ("bypassOnLocal");
91 this.bypassList = (ArrayList) serializationInfo.GetValue ("bypassList", typeof (ArrayList));
92 this.credentials = null;
98 get { return address; }
99 set { address = value; }
102 public ArrayList BypassArrayList {
103 get { return bypassList; }
106 public string [] BypassList {
107 get { return (string []) bypassList.ToArray (typeof (string)); }
110 throw new ArgumentNullException ();
111 bypassList = new ArrayList (value);
116 public bool BypassProxyOnLocal {
117 get { return bypassOnLocal; }
118 set { bypassOnLocal = value; }
121 public ICredentials Credentials {
122 get { return credentials; }
123 set { credentials = value; }
127 [MonoTODO("Can we get this info under windows from the system?")]
128 public static WebProxy GetDefaultProxy ()
130 // Select gets a WebProxy from config files, if available.
131 IWebProxy p = GlobalProxySelection.Select;
135 return new WebProxy ();
138 public Uri GetProxy (Uri destination)
140 if (IsBypassed (destination))
146 public bool IsBypassed (Uri host)
151 if (bypassOnLocal && host.IsLoopback)
154 string server = host.Host;
155 if (bypassOnLocal && server.IndexOf ('.') == -1)
159 if (!bypassOnLocal) {
160 if (String.Compare (server, "localhost", true, CultureInfo.InvariantCulture) == 0)
162 if (String.Compare (server, "loopback", true, CultureInfo.InvariantCulture) == 0)
166 IPAddress addr = IPAddress.Parse (server);
167 if (IPAddress.IsLoopback (addr))
173 string hostStr = host.Scheme + "://" + host.Authority;
175 for (; i < bypassList.Count; i++) {
176 Regex regex = new Regex ((string) bypassList [i],
177 // TODO: RegexOptions.Compiled | // not implemented yet by Regex
178 RegexOptions.IgnoreCase |
179 RegexOptions.Singleline);
181 if (regex.IsMatch (hostStr))
185 if (i == bypassList.Count)
188 // continue checking correctness of regular expressions..
189 // will throw expression when an invalid one is found
190 for (; i < bypassList.Count; i++)
191 new Regex ((string) bypassList [i]);
194 } catch (ArgumentException) {
199 void ISerializable.GetObjectData (SerializationInfo serializationInfo,
200 StreamingContext streamingContext)
202 serializationInfo.AddValue ("bypassOnLocal", bypassOnLocal);
203 serializationInfo.AddValue ("address", address);
204 serializationInfo.AddValue ("bypassList", bypassList);
208 // this compiles the regular expressions, and will throw
209 // an exception when an invalid one is found.
210 void CheckBypassList ()
212 for (int i = 0; i < bypassList.Count; i++)
213 new Regex ((string) bypassList [i]);
216 static Uri ToUri (string address)
221 if (address.IndexOf ("://") == -1)
222 address = "http://" + address;
224 return new Uri (address);