2004-09-11 Sebastien Pouliot <sebastien@ximian.com>
[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 #endif
36
37 namespace System.Net.Configuration
38 {
39         class DefaultProxyHandler : IConfigurationSectionHandler
40         {
41 #if (XML_DEP)
42                 public virtual object Create (object parent, object configContext, XmlNode section)
43                 {
44                         IWebProxy result = parent as IWebProxy;
45                         
46                         if (section.Attributes != null && section.Attributes.Count != 0)
47                                 HandlersUtil.ThrowException ("Unrecognized attribute", section);
48
49                         XmlNodeList nodes = section.ChildNodes;
50                         foreach (XmlNode child in nodes) {
51                                 XmlNodeType ntype = child.NodeType;
52                                 if (ntype == XmlNodeType.Whitespace || ntype == XmlNodeType.Comment)
53                                         continue;
54
55                                 if (ntype != XmlNodeType.Element)
56                                         HandlersUtil.ThrowException ("Only elements allowed", child);
57                                 
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);
65                                         }
66
67                                         result = new WebProxy ();
68                                         bool bp = (bypass != null && String.Compare (bypass, "true", true) == 0);
69                                         if (bp == false) {
70                                                 if (bypass != null && String.Compare (bypass, "false", true) != 0)
71                                                         HandlersUtil.ThrowException ("Invalid boolean value", child);
72                                         }
73
74                                         if (!(result is WebProxy))
75                                                 continue;
76
77                                         ((WebProxy) result).BypassProxyOnLocal = bp;
78                                         if (address == null)
79                                                 continue;
80
81                                         try {
82                                                 ((WebProxy) result).Address = new Uri (address);
83                                         } catch (Exception) {
84                                                 HandlersUtil.ThrowException ("invalid uri", child);
85                                         }
86                                         continue;
87                                 }
88
89                                 if (name == "bypasslist") {
90                                         if (!(result is WebProxy))
91                                                 continue;
92
93                                         FillByPassList (child, (WebProxy) result);
94                                         continue;
95                                 }
96
97                                 if (name == "module") {
98                                         HandlersUtil.ThrowException ("WARNING: module not implemented yet", child);
99                                 }
100
101                                 HandlersUtil.ThrowException ("Unexpected element", child);
102                         }
103
104                         return result;
105                 }
106
107                 static void FillByPassList (XmlNode node, WebProxy proxy)
108                 {
109                         ArrayList bypass = new ArrayList (proxy.BypassArrayList);
110                         if (node.Attributes != null && node.Attributes.Count != 0)
111                                 HandlersUtil.ThrowException ("Unrecognized attribute", node);
112
113                         XmlNodeList nodes = node.ChildNodes;
114                         foreach (XmlNode child in nodes) {
115                                 XmlNodeType ntype = child.NodeType;
116                                 if (ntype == XmlNodeType.Whitespace || ntype == XmlNodeType.Comment)
117                                         continue;
118
119                                 if (ntype != XmlNodeType.Element)
120                                         HandlersUtil.ThrowException ("Only elements allowed", child);
121                                 
122                                 string name = child.Name;
123                                 if (name == "add") {
124                                         string address = HandlersUtil.ExtractAttributeValue ("address", child);
125                                         if (!bypass.Contains (address)) {
126                                                 bypass.Add (address);
127                                         }
128                                         continue;
129                                 }
130
131                                 if (name == "remove") {
132                                         string address = HandlersUtil.ExtractAttributeValue ("address", child);
133                                         bypass.Remove (address);
134                                         continue;
135                                 }
136
137                                 if (name == "clear") {
138                                         if (node.Attributes != null && node.Attributes.Count != 0)
139                                                 HandlersUtil.ThrowException ("Unrecognized attribute", node);
140                                         
141                                         bypass.Clear ();
142                                         continue;
143                                 }
144
145                                 HandlersUtil.ThrowException ("Unexpected element", child);
146                         }
147
148                         proxy.BypassList = (string []) bypass.ToArray (typeof (string));
149                 }
150 #endif
151         }
152 }
153