2 // System.Net.WebProxy
\r
5 // Lawrence Pit (loz@cable.a2000.nl)
\r
9 using System.Collections;
\r
10 using System.Runtime.Serialization;
\r
11 using System.Text.RegularExpressions;
\r
13 namespace System.Net
\r
16 public class WebProxy : IWebProxy, ISerializable
\r
18 private Uri address;
\r
19 private bool bypassOnLocal;
\r
20 private ArrayList bypassList;
\r
21 private ICredentials credentials;
\r
26 : this ((Uri) null, false, null, null) {}
\r
28 public WebProxy (string address)
\r
29 : this (ToUri (address), false, null, null) {}
\r
31 public WebProxy (Uri address)
\r
32 : this (address, false, null, null) {}
\r
34 public WebProxy (string address, bool bypassOnLocal)
\r
35 : this (ToUri (address), bypassOnLocal, null, null) {}
\r
37 public WebProxy (string host, int port)
\r
38 : this (new Uri ("http://" + host + ":" + port)) {}
\r
40 public WebProxy (Uri address, bool bypassOnLocal)
\r
41 : this (address, bypassOnLocal, null, null) {}
\r
43 public WebProxy (string address, bool bypassOnLocal, string [] bypassList)
\r
44 : this (ToUri (address), bypassOnLocal, bypassList, null) {}
\r
46 public WebProxy (Uri address, bool bypassOnLocal, string [] bypassList)
\r
47 : this (address, bypassOnLocal, bypassList, null) {}
\r
49 public WebProxy (string address, bool bypassOnLocal,
\r
50 string[] bypassList, ICredentials credentials)
\r
51 : this (ToUri (address), bypassOnLocal, bypassList, null) {}
\r
53 public WebProxy (Uri address, bool bypassOnLocal,
\r
54 string[] bypassList, ICredentials credentials)
\r
56 this.address = address;
\r
57 this.bypassOnLocal = bypassOnLocal;
\r
58 if (bypassList == null)
\r
59 bypassList = new string [] {};
\r
60 this.bypassList = new ArrayList (bypassList);
\r
61 this.credentials = credentials;
\r
66 protected WebProxy (SerializationInfo serializationInfo, StreamingContext streamingContext)
\r
68 throw new NotImplementedException ();
\r
73 public Uri Address {
\r
74 get { return address; }
\r
75 set { address = value; }
\r
78 public ArrayList BypassArrayList {
\r
84 public string [] BypassList {
\r
85 get { return (string []) bypassList.ToArray (typeof (string)); }
\r
88 throw new ArgumentNullException ();
\r
89 bypassList = new ArrayList (value);
\r
94 public bool BypassProxyOnLocal {
\r
95 get { return bypassOnLocal; }
\r
96 set { bypassOnLocal = value; }
\r
99 public ICredentials Credentials {
\r
100 get { return credentials; }
\r
101 set { credentials = value; }
\r
107 public static WebProxy GetDefaultProxy ()
\r
109 // for Mono we should probably read in these settings
\r
110 // from the global application configuration file
\r
112 // for now, return the empty WebProxy to indicate
\r
113 // no proxy is used
\r
114 // return GlobalProxySelection.GetEmptyWebProxy ();
\r
117 throw new NotImplementedException ();
\r
120 public Uri GetProxy (Uri destination)
\r
122 if (IsBypassed (destination))
\r
123 return destination;
\r
128 public bool IsBypassed (Uri host)
\r
130 if (address == null)
\r
133 if (host.IsLoopback)
\r
136 if (bypassOnLocal && host.Host.IndexOf ('.') == -1)
\r
140 string hostStr = host.Scheme + "://" + host.Authority;
\r
142 for (; i < bypassList.Count; i++) {
\r
143 Regex regex = new Regex ((string) bypassList [i],
\r
144 // TODO: RegexOptions.Compiled | // not implemented yet by Regex
\r
145 RegexOptions.IgnoreCase |
\r
146 RegexOptions.Singleline);
\r
148 if (regex.IsMatch (hostStr))
\r
152 if (i == bypassList.Count)
\r
155 // continue checking correctness of regular expressions..
\r
156 // will throw expression when an invalid one is found
\r
157 for (; i < bypassList.Count; i++)
\r
158 new Regex ((string) bypassList [i]);
\r
161 } catch (ArgumentException) {
\r
167 void ISerializable.GetObjectData (SerializationInfo serializationInfo,
\r
168 StreamingContext streamingContext)
\r
170 throw new NotImplementedException ();
\r
175 // this compiles the regular expressions, and will throw
\r
176 // an exception when an invalid one is found.
\r
177 private void CheckBypassList ()
\r
179 for (int i = 0; i < bypassList.Count; i++)
\r
180 new Regex ((string) bypassList [i]);
\r
183 private static Uri ToUri (string address)
\r
185 if (address == null)
\r
188 if (address.IndexOf (':') == -1)
\r
189 address = "http://" + address;
\r
191 return new Uri (address);
\r