2 // System.Net.Configuration.DefaultProxyHandler
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // (C) 2003 Ximian, Inc (http://www.ximian.com)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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;
36 using XmlNode = System.Object;
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;
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)
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);
71 if (bypass != null && String.Compare (bypass, "false", true) != 0)
72 HandlersUtil.ThrowException ("Invalid boolean value", child);
75 if (!(result is WebProxy))
78 ((WebProxy) result).BypassProxyOnLocal = bp;
82 ((WebProxy) result).Address = new Uri (address);
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");
90 address = Environment.GetEnvironmentVariable ("HTTP_PROXY");
92 if (address != null) {
94 Uri uri = new Uri (address);
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";
101 } else if (IPAddress.IPv6Any.Equals (ip)) {
102 UriBuilder builder = new UriBuilder (uri);
103 builder.Host = "[::1]";
107 ((WebProxy) result).Address = uri;
108 } catch (UriFormatException) { }
115 if (name == "bypasslist") {
116 if (!(result is WebProxy))
119 FillByPassList (child, (WebProxy) result);
123 if (name == "module") {
124 HandlersUtil.ThrowException ("WARNING: module not implemented yet", child);
127 HandlersUtil.ThrowException ("Unexpected element", child);
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)
146 if (ntype != XmlNodeType.Element)
147 HandlersUtil.ThrowException ("Only elements allowed", child);
149 string name = child.Name;
151 string address = HandlersUtil.ExtractAttributeValue ("address", child);
152 if (!bypass.Contains (address)) {
153 bypass.Add (address);
158 if (name == "remove") {
159 string address = HandlersUtil.ExtractAttributeValue ("address", child);
160 bypass.Remove (address);
164 if (name == "clear") {
165 if (node.Attributes != null && node.Attributes.Count != 0)
166 HandlersUtil.ThrowException ("Unrecognized attribute", node);
172 HandlersUtil.ThrowException ("Unexpected element", child);
175 proxy.BypassList = (string []) bypass.ToArray (typeof (string));