New test.
[mono.git] / mcs / class / System / System.Net / GlobalProxySelection.cs
1 //\r
2 // System.Net.GlobalProxySelection\r
3 //\r
4 // Author:\r
5 //   Lawrence Pit (loz@cable.a2000.nl)\r
6 //\r
7
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28 \r
29 using System;\r
30 using System.Collections;\r
31 using System.Configuration;\r
32 using System.IO;\r
33 using System.Runtime.Serialization;\r
34 #if NET_2_0\r
35 using System.Net.Configuration;\r
36 #endif\r
37 \r
38 namespace System.Net \r
39 {\r
40         public class GlobalProxySelection\r
41         {\r
42                 volatile static IWebProxy proxy;
43                 static readonly object lockobj = new object ();\r
44                 \r
45                 // Constructors\r
46                 public GlobalProxySelection() { }\r
47                 \r
48                 // Properties\r
49                 \r
50                 static IWebProxy GetProxy ()\r
51                 {\r
52                         lock (lockobj) {\r
53                                 if (proxy != null)\r
54                                         return proxy;\r
55 \r
56                                 object p = ConfigurationSettings.GetConfig ("system.net/defaultProxy");\r
57                                 if (p == null)\r
58                                         p = new EmptyWebProxy ();\r
59 #if NET_2_0 && CONFIGURATION_DEP\r
60                                 DefaultProxySection s = p as DefaultProxySection;\r
61                                 if (s != null) {\r
62                                         // FIXME: handle Module\r
63                                         ProxyElement e = s.Proxy;\r
64                                         // FIXME: handle AutoDetect, ScriptLocation, UseSystemDefault\r
65                                         if (e.BypassOnLocal == ProxyElement.BypassOnLocalValues.Unspecified)\r
66                                                 p = new WebProxy (e.ProxyAddress);\r
67                                         else\r
68                                                 p = new WebProxy (e.ProxyAddress, e.BypassOnLocal == ProxyElement.BypassOnLocalValues.True);\r
69                                 }\r
70 #endif\r
71                                 proxy = (IWebProxy) p;\r
72                         }\r
73 \r
74                         return proxy;\r
75                 }\r
76                 \r
77                 public static IWebProxy Select {\r
78                         get { return GetProxy (); }\r
79                         set {\r
80                                 if (value == null)\r
81                                         throw new ArgumentNullException ("GlobalProxySelection.Select",\r
82                                                         "null IWebProxy not allowed. Use GetEmptyWebProxy ()");\r
83                                 proxy = value; \r
84                         }\r
85                 }\r
86                 \r
87                 // Methods\r
88                 \r
89                 public static IWebProxy GetEmptyWebProxy()\r
90                 {\r
91                         // must return a new one each time, as the credentials\r
92                         // can be set\r
93                         return new EmptyWebProxy ();    \r
94                 }\r
95                 \r
96                 // Internal Classes\r
97                 \r
98                 internal class EmptyWebProxy : IWebProxy {\r
99                         private ICredentials credentials = null;\r
100                         \r
101                         internal EmptyWebProxy () { }\r
102                         \r
103                         public ICredentials Credentials {\r
104                                 get { return credentials; } \r
105                                 set { credentials = value; }\r
106                         }\r
107 \r
108                         public Uri GetProxy (Uri destination)\r
109                         {\r
110                                 return destination;\r
111                         }\r
112 \r
113                         public bool IsBypassed (Uri host)\r
114                         {\r
115                                 return true; // pass directly to host\r
116                         }\r
117                 }\r
118         }               \r
119 }\r