forgot this
[mono.git] / mcs / class / System / System.Net / WebProxy.cs
1 //\r
2 // System.Net.WebProxy\r
3 //\r
4 // Author:\r
5 //   Lawrence Pit (loz@cable.a2000.nl)\r
6 //\r
7 \r
8 using System;\r
9 using System.Collections;\r
10 using System.Runtime.Serialization;\r
11 using System.Text.RegularExpressions;\r
12 \r
13 namespace System.Net \r
14 {\r
15         [Serializable]\r
16         public class WebProxy : IWebProxy, ISerializable\r
17         {               \r
18                 private Uri address;\r
19                 private bool bypassOnLocal;\r
20                 private ArrayList bypassList;\r
21                 private ICredentials credentials;\r
22         \r
23                 // Constructors\r
24         \r
25                 public WebProxy () \r
26                         : this ((Uri) null, false, null, null) {}\r
27                 \r
28                 public WebProxy (string address) \r
29                         : this (ToUri (address), false, null, null) {}\r
30                 \r
31                 public WebProxy (Uri address) \r
32                         : this (address, false, null, null) {}\r
33                 \r
34                 public WebProxy (string address, bool bypassOnLocal) \r
35                         : this (ToUri (address), bypassOnLocal, null, null) {}\r
36                 \r
37                 public WebProxy (string host, int port)\r
38                         : this (new Uri ("http://" + host + ":" + port)) {}\r
39                 \r
40                 public WebProxy (Uri address, bool bypassOnLocal)\r
41                         : this (address, bypassOnLocal, null, null) {}\r
42 \r
43                 public WebProxy (string address, bool bypassOnLocal, string [] bypassList)\r
44                         : this (ToUri (address), bypassOnLocal, bypassList, null) {}\r
45 \r
46                 public WebProxy (Uri address, bool bypassOnLocal, string [] bypassList)\r
47                         : this (address, bypassOnLocal, bypassList, null) {}\r
48                 \r
49                 public WebProxy (string address, bool bypassOnLocal,\r
50                                  string[] bypassList, ICredentials credentials)\r
51                         : this (ToUri (address), bypassOnLocal, bypassList, null) {}\r
52 \r
53                 public WebProxy (Uri address, bool bypassOnLocal, \r
54                                  string[] bypassList, ICredentials credentials)\r
55                 {\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
62                         CheckBypassList ();\r
63                 }\r
64                 \r
65                 [MonoTODO]\r
66                 protected WebProxy (SerializationInfo serializationInfo, StreamingContext streamingContext) \r
67                 {\r
68                         throw new NotImplementedException ();\r
69                 }\r
70                 \r
71                 // Properties\r
72                 \r
73                 public Uri Address {\r
74                         get { return address; }\r
75                         set { address = value; }\r
76                 }\r
77                 \r
78                 public ArrayList BypassArrayList {\r
79                         get { \r
80                                 return bypassList;\r
81                         }\r
82                 }\r
83                 \r
84                 public string [] BypassList {\r
85                         get { return (string []) bypassList.ToArray (typeof (string)); }\r
86                         set { \r
87                                 if (value == null)\r
88                                         throw new ArgumentNullException ();\r
89                                 bypassList = new ArrayList (value); \r
90                                 CheckBypassList ();\r
91                         }\r
92                 }\r
93                 \r
94                 public bool BypassProxyOnLocal {\r
95                         get { return bypassOnLocal; }\r
96                         set { bypassOnLocal = value; }\r
97                 }\r
98                 \r
99                 public ICredentials Credentials {\r
100                         get { return credentials; }\r
101                         set { credentials = value; }\r
102                 }\r
103                 \r
104                 // Methods\r
105                 \r
106                 [MonoTODO]\r
107                 public static WebProxy GetDefaultProxy ()\r
108                 {\r
109                         // for Mono we should probably read in these settings\r
110                         // from the global application configuration file\r
111                         \r
112                         // for now, return the empty WebProxy to indicate\r
113                         // no proxy is used\r
114                         // return GlobalProxySelection.GetEmptyWebProxy ();\r
115                         // ??\r
116                         \r
117                         throw new NotImplementedException ();\r
118                 }\r
119                 \r
120                 public Uri GetProxy (Uri destination)\r
121                 {\r
122                         if (IsBypassed (destination))\r
123                                 return destination;\r
124                                 \r
125                         return address;\r
126                 }\r
127                 \r
128                 public bool IsBypassed (Uri host)\r
129                 {\r
130                         if (address == null)\r
131                                 return true;\r
132                         \r
133                         if (host.IsLoopback)\r
134                                 return true;\r
135                                 \r
136                         if (bypassOnLocal && host.Host.IndexOf ('.') == -1)\r
137                                 return true;\r
138                                 \r
139                         try {                           \r
140                                 string hostStr = host.Scheme + "://" + host.Authority;                          \r
141                                 int i = 0;\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
147 \r
148                                         if (regex.IsMatch (hostStr))\r
149                                                 break;\r
150                                 }\r
151 \r
152                                 if (i == bypassList.Count)\r
153                                         return false;\r
154 \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
159 \r
160                                 return true;\r
161                         } catch (ArgumentException) {\r
162                                 return false;\r
163                         }\r
164                 }\r
165 \r
166                 [MonoTODO]              \r
167                 void ISerializable.GetObjectData (SerializationInfo serializationInfo,\r
168                                                   StreamingContext streamingContext)\r
169                 {\r
170                         throw new NotImplementedException ();\r
171                 }\r
172                 \r
173                 // Private Methods\r
174                 \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
178                 {                       \r
179                         for (int i = 0; i < bypassList.Count; i++)\r
180                                 new Regex ((string) bypassList [i]);\r
181                 }\r
182                 \r
183                 private static Uri ToUri (string address)\r
184                 {\r
185                         if (address == null)\r
186                                 return null;\r
187                                 \r
188                         if (address.IndexOf (':') == -1) \r
189                                 address = "http://" + address;\r
190                         \r
191                         return new Uri (address);\r
192                 }\r
193         }\r
194 }