ServicePoint: Use DateTime.UtcNow internally, which avoids looking up the timezone...
[mono.git] / mcs / class / System / System.Net.Configuration / DefaultProxyHandler.cs
1 //
2 // System.Net.Configuration.DefaultProxyHandler
3 //
4 // Authors:
5 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //
7 // (C) 2003 Ximian, Inc (http://www.ximian.com)
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System.Collections;
32 using System.Configuration;
33 #if (XML_DEP)
34 using System.Xml;
35 #else
36 using XmlNode = System.Object;
37 #endif
38
39 namespace System.Net.Configuration
40 {
41         class DefaultProxyHandler : IConfigurationSectionHandler
42         {
43                 public virtual object Create (object parent, object configContext, XmlNode section)
44                 {
45                         IWebProxy result = parent as IWebProxy;
46 #if (XML_DEP)
47                         if (section.Attributes != null && section.Attributes.Count != 0)
48                                 HandlersUtil.ThrowException ("Unrecognized attribute", section);
49
50                         XmlNodeList nodes = section.ChildNodes;
51                         foreach (XmlNode child in nodes) {
52                                 XmlNodeType ntype = child.NodeType;
53                                 if (ntype == XmlNodeType.Whitespace || ntype == XmlNodeType.Comment)
54                                         continue;
55
56                                 if (ntype != XmlNodeType.Element)
57                                         HandlersUtil.ThrowException ("Only elements allowed", child);
58                                 
59                                 string name = child.Name;
60                                 if (name == "proxy") {
61                                         string sysdefault = HandlersUtil.ExtractAttributeValue ("usesystemdefault", child, true);
62                                         string bypass = HandlersUtil.ExtractAttributeValue ("bypassonlocal", child, true);
63                                         string address = HandlersUtil.ExtractAttributeValue ("proxyaddress", child, true);
64                                         if (child.Attributes != null && child.Attributes.Count != 0) {
65                                                 HandlersUtil.ThrowException ("Unrecognized attribute", child);
66                                         }
67
68                                         result = new WebProxy ();
69                                         bool bp = (bypass != null && String.Compare (bypass, "true", true) == 0);
70                                         if (bp == false) {
71                                                 if (bypass != null && String.Compare (bypass, "false", true) != 0)
72                                                         HandlersUtil.ThrowException ("Invalid boolean value", child);
73                                         }
74
75                                         if (!(result is WebProxy))
76                                                 continue;
77
78                                         ((WebProxy) result).BypassProxyOnLocal = bp;
79                                         
80                                         if (address != null)
81                                                 try {
82                                                         ((WebProxy) result).Address = new Uri (address);
83                                                         continue;
84                                                 } catch (UriFormatException) {} //MS: ignore bad URIs, fall through to default
85                                         
86                                         //MS: presence of valid address URI takes precedence over usesystemdefault
87                                         if (sysdefault != null && String.Compare (sysdefault, "true", true) == 0) {
88                                                 address = Environment.GetEnvironmentVariable ("http_proxy");
89                                                 if (address == null)
90                                                         address = Environment.GetEnvironmentVariable ("HTTP_PROXY");
91
92                                                 if (address != null) {
93                                                         try {
94                                                                 Uri uri = new Uri (address);
95                                                                 IPAddress ip;
96                                                                 if (IPAddress.TryParse (uri.Host, out ip)) {
97                                                                         if (IPAddress.Any.Equals (ip)) {
98                                                                                 UriBuilder builder = new UriBuilder (uri);
99                                                                                 builder.Host = "127.0.0.1";
100                                                                                 uri = builder.Uri;
101                                                                         } else if (IPAddress.IPv6Any.Equals (ip)) {
102                                                                                 UriBuilder builder = new UriBuilder (uri);
103                                                                                 builder.Host = "[::1]";
104                                                                                 uri = builder.Uri;
105                                                                         }
106                                                                 }
107                                                                 ((WebProxy) result).Address = uri;
108                                                         } catch (UriFormatException) { }
109                                                 }
110                                         }
111                                         
112                                         continue;
113                                 }
114
115                                 if (name == "bypasslist") {
116                                         if (!(result is WebProxy))
117                                                 continue;
118
119                                         FillByPassList (child, (WebProxy) result);
120                                         continue;
121                                 }
122
123                                 if (name == "module") {
124                                         HandlersUtil.ThrowException ("WARNING: module not implemented yet", child);
125                                 }
126
127                                 HandlersUtil.ThrowException ("Unexpected element", child);
128                         }
129 #endif
130                         return result;
131                 }
132
133 #if (XML_DEP)
134                 static void FillByPassList (XmlNode node, WebProxy proxy)
135                 {
136                         ArrayList bypass = new ArrayList (proxy.BypassArrayList);
137                         if (node.Attributes != null && node.Attributes.Count != 0)
138                                 HandlersUtil.ThrowException ("Unrecognized attribute", node);
139
140                         XmlNodeList nodes = node.ChildNodes;
141                         foreach (XmlNode child in nodes) {
142                                 XmlNodeType ntype = child.NodeType;
143                                 if (ntype == XmlNodeType.Whitespace || ntype == XmlNodeType.Comment)
144                                         continue;
145
146                                 if (ntype != XmlNodeType.Element)
147                                         HandlersUtil.ThrowException ("Only elements allowed", child);
148                                 
149                                 string name = child.Name;
150                                 if (name == "add") {
151                                         string address = HandlersUtil.ExtractAttributeValue ("address", child);
152                                         if (!bypass.Contains (address)) {
153                                                 bypass.Add (address);
154                                         }
155                                         continue;
156                                 }
157
158                                 if (name == "remove") {
159                                         string address = HandlersUtil.ExtractAttributeValue ("address", child);
160                                         bypass.Remove (address);
161                                         continue;
162                                 }
163
164                                 if (name == "clear") {
165                                         if (node.Attributes != null && node.Attributes.Count != 0)
166                                                 HandlersUtil.ThrowException ("Unrecognized attribute", node);
167                                         
168                                         bypass.Clear ();
169                                         continue;
170                                 }
171
172                                 HandlersUtil.ThrowException ("Unexpected element", child);
173                         }
174
175                         proxy.BypassList = (string []) bypass.ToArray (typeof (string));
176                 }
177 #endif
178         }
179 }
180