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;
37 namespace System.Net.Configuration
39 class DefaultProxyHandler : IConfigurationSectionHandler
42 public virtual object Create (object parent, object configContext, XmlNode section)
44 IWebProxy result = parent as IWebProxy;
46 if (section.Attributes != null && section.Attributes.Count != 0)
47 HandlersUtil.ThrowException ("Unrecognized attribute", section);
49 XmlNodeList nodes = section.ChildNodes;
50 foreach (XmlNode child in nodes) {
51 XmlNodeType ntype = child.NodeType;
52 if (ntype == XmlNodeType.Whitespace || ntype == XmlNodeType.Comment)
55 if (ntype != XmlNodeType.Element)
56 HandlersUtil.ThrowException ("Only elements allowed", child);
58 string name = child.Name;
59 if (name == "proxy") {
60 string deflt = HandlersUtil.ExtractAttributeValue ("usesystemdefault", child, true);
61 string bypass = HandlersUtil.ExtractAttributeValue ("bypassonlocal", child, true);
62 string address = HandlersUtil.ExtractAttributeValue ("proxyaddress", child, true);
63 if (child.Attributes != null && child.Attributes.Count != 0) {
64 HandlersUtil.ThrowException ("Unrecognized attribute", child);
67 result = new WebProxy ();
68 bool bp = (bypass != null && String.Compare (bypass, "true", true) == 0);
70 if (bypass != null && String.Compare (bypass, "false", true) != 0)
71 HandlersUtil.ThrowException ("Invalid boolean value", child);
74 if (!(result is WebProxy))
77 ((WebProxy) result).BypassProxyOnLocal = bp;
82 ((WebProxy) result).Address = new Uri (address);
84 HandlersUtil.ThrowException ("invalid uri", child);
89 if (name == "bypasslist") {
90 if (!(result is WebProxy))
93 FillByPassList (child, (WebProxy) result);
97 if (name == "module") {
98 HandlersUtil.ThrowException ("WARNING: module not implemented yet", child);
101 HandlersUtil.ThrowException ("Unexpected element", child);
107 static void FillByPassList (XmlNode node, WebProxy proxy)
109 ArrayList bypass = new ArrayList (proxy.BypassArrayList);
110 if (node.Attributes != null && node.Attributes.Count != 0)
111 HandlersUtil.ThrowException ("Unrecognized attribute", node);
113 XmlNodeList nodes = node.ChildNodes;
114 foreach (XmlNode child in nodes) {
115 XmlNodeType ntype = child.NodeType;
116 if (ntype == XmlNodeType.Whitespace || ntype == XmlNodeType.Comment)
119 if (ntype != XmlNodeType.Element)
120 HandlersUtil.ThrowException ("Only elements allowed", child);
122 string name = child.Name;
124 string address = HandlersUtil.ExtractAttributeValue ("address", child);
125 if (!bypass.Contains (address)) {
126 bypass.Add (address);
131 if (name == "remove") {
132 string address = HandlersUtil.ExtractAttributeValue ("address", child);
133 bypass.Remove (address);
137 if (name == "clear") {
138 if (node.Attributes != null && node.Attributes.Count != 0)
139 HandlersUtil.ThrowException ("Unrecognized attribute", node);
145 HandlersUtil.ThrowException ("Unexpected element", child);
148 proxy.BypassList = (string []) bypass.ToArray (typeof (string));