2003-06-11 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[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 \r
8 using System;\r
9 using System.Collections;\r
10 using System.Configuration;\r
11 using System.IO;\r
12 using System.Runtime.Serialization;\r
13 \r
14 namespace System.Net \r
15 {\r
16         public class GlobalProxySelection\r
17         {\r
18                 private static IWebProxy proxy;\r
19                 \r
20                 // Constructors\r
21                 public GlobalProxySelection() { }\r
22                 \r
23                 // Properties\r
24                 \r
25                 static IWebProxy GetProxy ()\r
26                 {\r
27                         if (proxy != null)\r
28                                 return proxy;\r
29 \r
30                         lock (typeof (GlobalProxySelection)) {\r
31                                 if (proxy != null)\r
32                                         return proxy;\r
33 \r
34                                 object p = ConfigurationSettings.GetConfig ("system.net/defaultProxy");\r
35                                 if (p == null)\r
36                                         p = new EmptyWebProxy ();\r
37 \r
38                                 proxy = (IWebProxy) p;\r
39                         }\r
40 \r
41                         return proxy;\r
42                 }\r
43                 \r
44                 public static IWebProxy Select {\r
45                         get { return GetProxy (); }\r
46                         set {\r
47                                 if (value == null)\r
48                                         throw new ArgumentNullException ("GlobalProxySelection.Select",\r
49                                                         "null IWebProxy not allowed. Use GetEmptyWebProxy ()");\r
50 \r
51                                 lock (typeof (GlobalProxySelection))\r
52                                         proxy = value; \r
53                         }\r
54                 }\r
55                 \r
56                 // Methods\r
57                 \r
58                 public static IWebProxy GetEmptyWebProxy()\r
59                 {\r
60                         // must return a new one each time, as the credentials\r
61                         // can be set\r
62                         return new EmptyWebProxy ();    \r
63                 }\r
64                 \r
65                 // Internal Classes\r
66                 \r
67                 internal class EmptyWebProxy : IWebProxy {\r
68                         private ICredentials credentials = null;\r
69                         \r
70                         internal EmptyWebProxy () { }\r
71                         \r
72                         public ICredentials Credentials {\r
73                                 get { return credentials; } \r
74                                 set { credentials = value; }\r
75                         }\r
76 \r
77                         public Uri GetProxy (Uri destination)\r
78                         {\r
79                                 return destination;\r
80                         }\r
81 \r
82                         public bool IsBypassed (Uri host)\r
83                         {\r
84                                 return true; // pass directly to host\r
85                         }\r
86                 }\r
87         }               \r
88 }\r