2010-06-21 Atsushi Enomoto <atsushi@ximian.com>
[mcs.git] / class / System / System.Net.Configuration / DefaultProxyHandler.cs
blob99514ebddb88c325ff7a32c05819375d5f28d5a2
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 //
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.
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
39 namespace System.Net.Configuration
41 class DefaultProxyHandler : IConfigurationSectionHandler
43 public virtual object Create (object parent, object configContext, XmlNode section)
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);
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;
56 if (ntype != XmlNodeType.Element)
57 HandlersUtil.ThrowException ("Only elements allowed", child);
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);
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);
75 if (!(result is WebProxy))
76 continue;
78 ((WebProxy) result).BypassProxyOnLocal = bp;
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
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");
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;
107 ((WebProxy) result).Address = uri;
108 } catch (UriFormatException) { }
112 continue;
115 if (name == "bypasslist") {
116 if (!(result is WebProxy))
117 continue;
119 FillByPassList (child, (WebProxy) result);
120 continue;
123 if (name == "module") {
124 HandlersUtil.ThrowException ("WARNING: module not implemented yet", child);
127 HandlersUtil.ThrowException ("Unexpected element", child);
129 #endif
130 return result;
133 #if (XML_DEP)
134 static void FillByPassList (XmlNode node, WebProxy proxy)
136 ArrayList bypass = new ArrayList (proxy.BypassArrayList);
137 if (node.Attributes != null && node.Attributes.Count != 0)
138 HandlersUtil.ThrowException ("Unrecognized attribute", node);
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;
146 if (ntype != XmlNodeType.Element)
147 HandlersUtil.ThrowException ("Only elements allowed", child);
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);
155 continue;
158 if (name == "remove") {
159 string address = HandlersUtil.ExtractAttributeValue ("address", child);
160 bypass.Remove (address);
161 continue;
164 if (name == "clear") {
165 if (node.Attributes != null && node.Attributes.Count != 0)
166 HandlersUtil.ThrowException ("Unrecognized attribute", node);
168 bypass.Clear ();
169 continue;
172 HandlersUtil.ThrowException ("Unexpected element", child);
175 proxy.BypassList = (string []) bypass.ToArray (typeof (string));
177 #endif