Merge pull request #3769 from evincarofautumn/fix-verify-before-allocs
[mono.git] / mcs / class / System / System.Net.Configuration / ConnectionManagementHandler.cs
1 //
2 // System.Net.Configuration.ConnectionManagementHandler
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 #pragma warning disable 618
40
41 namespace System.Net.Configuration
42 {
43         class ConnectionManagementData
44         {
45                 Hashtable data; // key -> address, value -> maxconnections
46                 const int defaultMaxConnections = 2;
47                 
48                 public ConnectionManagementData (object parent)
49                 {
50                         data = new Hashtable (CaseInsensitiveHashCodeProvider.DefaultInvariant,
51                                               CaseInsensitiveComparer.DefaultInvariant);
52                         if (parent != null && parent is ConnectionManagementData) {
53                                 ConnectionManagementData p = (ConnectionManagementData) parent;
54                                 foreach (string k in p.data.Keys)
55                                         data [k] = p.data [k];  
56                         }
57                 }
58
59                 public void Add (string address, string nconns)
60                 {
61                         if (nconns == null || nconns == "")
62                                 nconns = "2";
63                         // Adding duplicates works fine under MS, so...
64                         data [address] = UInt32.Parse (nconns);
65                 }
66
67                 public void Add (string address, int nconns)
68                 {
69                         data [address] = (uint) nconns;
70                 }
71
72                 public void Remove (string address)
73                 {
74                         // Removing non-existent address is fine.
75                         data.Remove (address);
76                 }
77
78                 public void Clear ()
79                 {
80                         data.Clear ();
81                 }
82
83                 public uint GetMaxConnections (string hostOrIP)
84                 {
85                         object o = data [hostOrIP];
86                         if (o == null)
87                                 o = data ["*"];
88
89                         if (o == null)
90                                 return defaultMaxConnections;
91
92                         return (uint) o;
93                 }
94
95                 public Hashtable Data {
96                         get { return data; }
97                 }
98         }
99
100         class ConnectionManagementHandler : IConfigurationSectionHandler
101         {
102                 public virtual object Create (object parent, object configContext, XmlNode section)
103                 {
104                         ConnectionManagementData cmd = new ConnectionManagementData (parent);
105 #if (XML_DEP)                   
106                         if (section.Attributes != null && section.Attributes.Count != 0)
107                                 HandlersUtil.ThrowException ("Unrecognized attribute", section);
108
109                         XmlNodeList httpHandlers = section.ChildNodes;
110                         foreach (XmlNode child in httpHandlers) {
111                                 XmlNodeType ntype = child.NodeType;
112                                 if (ntype == XmlNodeType.Whitespace || ntype == XmlNodeType.Comment)
113                                         continue;
114
115                                 if (ntype != XmlNodeType.Element)
116                                         HandlersUtil.ThrowException ("Only elements allowed", child);
117                                 
118                                 string name = child.Name;
119                                 if (name == "clear") {
120                                         if (child.Attributes != null && child.Attributes.Count != 0)
121                                                 HandlersUtil.ThrowException ("Unrecognized attribute", child);
122
123                                         cmd.Clear ();
124                                         continue;
125                                 }
126
127                                 //LAMESPEC: the MS doc says that <remove name="..."/> but they throw an exception
128                                 // if you use that. "address" is correct.
129
130                                 string address = HandlersUtil.ExtractAttributeValue ("address", child);
131                                 if (name == "add") {
132                                         string maxcnc = HandlersUtil.ExtractAttributeValue ("maxconnection", child, true);
133                                         if (child.Attributes != null && child.Attributes.Count != 0)
134                                                 HandlersUtil.ThrowException ("Unrecognized attribute", child);
135
136                                         cmd.Add (address, maxcnc);
137                                         continue;
138                                 }
139
140                                 if (name == "remove") {
141                                         if (child.Attributes != null && child.Attributes.Count != 0)
142                                                 HandlersUtil.ThrowException ("Unrecognized attribute", child);
143
144                                         cmd.Remove (address);
145                                         continue;
146                                 }
147
148                                 HandlersUtil.ThrowException ("Unexpected element", child);
149                         }
150 #endif                  
151
152                         return cmd;
153                 }
154         }
155
156         internal class HandlersUtil
157         {
158                 private HandlersUtil ()
159                 {
160                 }
161 #if (XML_DEP)
162                 static internal string ExtractAttributeValue (string attKey, XmlNode node)
163                 {
164                         return ExtractAttributeValue (attKey, node, false);
165                 }
166                         
167                 static internal string ExtractAttributeValue (string attKey, XmlNode node, bool optional)
168                 {
169                         if (node.Attributes == null) {
170                                 if (optional)
171                                         return null;
172
173                                 ThrowException ("Required attribute not found: " + attKey, node);
174                         }
175
176                         XmlNode att = node.Attributes.RemoveNamedItem (attKey);
177                         if (att == null) {
178                                 if (optional)
179                                         return null;
180                                 ThrowException ("Required attribute not found: " + attKey, node);
181                         }
182
183                         string value = att.Value;
184                         if (value == String.Empty) {
185                                 string opt = optional ? "Optional" : "Required";
186                                 ThrowException (opt + " attribute is empty: " + attKey, node);
187                         }
188
189                         return value;
190                 }
191
192                 static internal void ThrowException (string msg, XmlNode node)
193                 {
194                         if (node != null && node.Name != String.Empty)
195                                 msg = msg + " (node name: " + node.Name + ") ";
196                         throw new ConfigurationException (msg, node);
197                 }
198 #endif
199         }
200 }
201