Making sure mono_marshal_free is used instead of g_free in mono_string_builder_to_utf8
[mono.git] / mcs / class / System / System.Net / WebProxy.cs
1 //
2 // System.Net.WebProxy.cs
3 //
4 // Authors:
5 //   Lawrence Pit (loz@cable.a2000.nl)
6 //   Andreas Nahr (ClassDevelopment@A-SoftTech.com)
7 //
8
9 //
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:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
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.
28 //
29
30 using System;
31 using System.Collections;
32 using System.Globalization;
33 using System.Runtime.Serialization;
34 using System.Text.RegularExpressions;
35
36 namespace System.Net 
37 {
38         [Serializable]
39         public class WebProxy : IWebProxy, ISerializable
40         {
41                 Uri address;
42                 bool bypassOnLocal;
43                 ArrayList bypassList;
44                 ICredentials credentials;
45                 bool useDefaultCredentials;
46
47                 // Constructors
48
49                 public WebProxy ()
50                         : this ((Uri) null, false, null, null) {}
51
52                 public WebProxy (string address)
53                         : this (ToUri (address), false, null, null) {}
54
55                 public WebProxy (Uri address) 
56                         : this (address, false, null, null) {}
57
58                 public WebProxy (string address, bool bypassOnLocal) 
59                         : this (ToUri (address), bypassOnLocal, null, null) {}
60
61                 public WebProxy (string host, int port)
62                         : this (new Uri ("http://" + host + ":" + port)) {}
63
64                 public WebProxy (Uri address, bool bypassOnLocal)
65                         : this (address, bypassOnLocal, null, null) {}
66
67                 public WebProxy (string address, bool bypassOnLocal, string [] bypassList)
68                         : this (ToUri (address), bypassOnLocal, bypassList, null) {}
69
70                 public WebProxy (Uri address, bool bypassOnLocal, string [] bypassList)
71                         : this (address, bypassOnLocal, bypassList, null) {}
72
73                 public WebProxy (string address, bool bypassOnLocal, string [] bypassList,
74                                 ICredentials credentials)
75                         : this (ToUri (address), bypassOnLocal, bypassList, credentials) {}
76
77                 public WebProxy (Uri address, bool bypassOnLocal, 
78                                  string[] bypassList, ICredentials credentials)
79                 {
80                         this.address = address;
81                         this.bypassOnLocal = bypassOnLocal;
82                         if (bypassList != null)
83                                 this.bypassList = new ArrayList (bypassList);
84                         this.credentials = credentials;
85                         CheckBypassList ();
86                 }
87
88                 protected WebProxy (SerializationInfo serializationInfo, StreamingContext streamingContext) 
89                 {
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;
95                         CheckBypassList ();
96                 }
97
98                 // Properties
99                 public Uri Address {
100                         get { return address; }
101                         set { address = value; }
102                 }
103
104                 public ArrayList BypassArrayList {
105                         get {
106                                 if (bypassList == null)
107                                         bypassList = new ArrayList ();
108                                 return bypassList;
109                         }
110                 }
111
112                 public string [] BypassList {
113                         get { return (string []) BypassArrayList.ToArray (typeof (string)); }
114                         set { 
115                                 if (value == null)
116                                         throw new ArgumentNullException ();
117                                 bypassList = new ArrayList (value); 
118                                 CheckBypassList ();
119                         }
120                 }
121
122                 public bool BypassProxyOnLocal {
123                         get { return bypassOnLocal; }
124                         set { bypassOnLocal = value; }
125                 }
126
127                 public ICredentials Credentials {
128                         get { return credentials; }
129                         set { credentials = value; }
130                 }
131
132                 [MonoTODO ("Does not affect Credentials, since CredentialCache.DefaultCredentials is not implemented.")]
133                 public bool UseDefaultCredentials {
134                         get { return useDefaultCredentials; }
135                         set { useDefaultCredentials = value; }
136                 }
137
138                 // Methods
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 ()
142                 {
143                         // Select gets a WebProxy from config files, if available.
144                         IWebProxy p = GlobalProxySelection.Select;
145                         if (p is WebProxy)
146                                 return (WebProxy) p;
147
148                         return new WebProxy ();
149                 }
150
151                 public Uri GetProxy (Uri destination)
152                 {
153                         if (IsBypassed (destination))
154                                 return destination;
155
156                         return address;
157                 }
158
159                 public bool IsBypassed (Uri host)
160                 {
161                         if (host == null)
162                                 throw new ArgumentNullException ("host");
163
164                         if (host.IsLoopback && bypassOnLocal)
165                                 return true;
166
167                         if (address == null)
168                                 return true;
169
170                         string server = host.Host;
171                         if (bypassOnLocal && server.IndexOf ('.') == -1)
172                                 return true;
173
174                         // LAMESPEC
175                         if (!bypassOnLocal) {
176                                 if (String.Compare (server, "localhost", true, CultureInfo.InvariantCulture) == 0)
177                                         return true;
178                                 if (String.Compare (server, "loopback", true, CultureInfo.InvariantCulture) == 0)
179                                         return true;
180
181                                 IPAddress addr = null;
182                                 if (IPAddress.TryParse (server, out addr) && IPAddress.IsLoopback (addr))
183                                         return true;
184                         }
185
186                         if (bypassList == null || bypassList.Count == 0)
187                                 return false;
188
189                         try {
190                                 string hostStr = host.Scheme + "://" + host.Authority;
191                                 int i = 0;
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);
197
198                                         if (regex.IsMatch (hostStr))
199                                                 break;
200                                 }
201
202                                 if (i == bypassList.Count)
203                                         return false;
204
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]);
209
210                                 return true;
211                         } catch (ArgumentException) {
212                                 return false;
213                         }
214                 }
215
216                 protected virtual void GetObjectData (SerializationInfo serializationInfo, StreamingContext streamingContext)
217                 {
218                         serializationInfo.AddValue ("_BypassOnLocal", bypassOnLocal);
219                         serializationInfo.AddValue ("_ProxyAddress", address);
220                         serializationInfo.AddValue ("_BypassList", bypassList);
221                         serializationInfo.AddValue ("_UseDefaultCredentials", UseDefaultCredentials);
222                 }
223
224                 void ISerializable.GetObjectData (SerializationInfo serializationInfo,
225                                                   StreamingContext streamingContext)
226                 {
227                         GetObjectData (serializationInfo, streamingContext);
228                 }
229
230                 // Private Methods
231                 // this compiles the regular expressions, and will throw
232                 // an exception when an invalid one is found.
233                 void CheckBypassList ()
234                 {
235                         if (bypassList == null)
236                                 return;
237                         for (int i = 0; i < bypassList.Count; i++)
238                                 new Regex ((string) bypassList [i]);
239                 }
240
241                 static Uri ToUri (string address)
242                 {
243                         if (address == null)
244                                 return null;
245                                 
246                         if (address.IndexOf ("://", StringComparison.Ordinal) == -1) 
247                                 address = "http://" + address;
248
249                         return new Uri (address);
250                 }
251         }
252 }