* ToolTask.cs (ProcessOuputTool): Move logging of tool
[mono.git] / mcs / class / System.Web / System.Web.Configuration / CustomErrorsConfigHandler.cs
1 //
2 // System.Web.Configuration.CustomErrorsConfigHandler
3 //
4 // Authors:
5 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //
7 // (C) 2003 Novell, Inc. (http://www.novell.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;
32 using System.Collections;
33 using System.Configuration;
34 using System.IO;
35 using System.Xml;
36
37 namespace System.Web.Configuration
38 {
39         enum CustomErrorMode
40         {
41                 RemoteOnly,
42                 On,
43                 Off
44         }
45         
46         class CustomErrorsConfig
47         {
48                 string defaultRedirect;
49                 CustomErrorMode mode;
50                 Hashtable redirects;
51                 string configFilePath;
52
53                 public CustomErrorsConfig (object parent, object context)
54                 {
55                         if (parent != null) {
56                                 CustomErrorsConfig p = (CustomErrorsConfig) parent;
57                                 mode = p.mode;
58                                 defaultRedirect = p.defaultRedirect;
59                                 if (p.redirects != null && p.redirects.Count > 0) {
60                                         redirects = new Hashtable ();
61                                         foreach (DictionaryEntry entry in p.redirects)
62                                                 redirects [entry.Key] = entry.Value;
63                                 }
64                         }
65
66                         configFilePath = Path.GetDirectoryName ((string) context);
67                 }
68
69                 public string DefaultRedirect {
70                         get { return defaultRedirect; }
71                         set { defaultRedirect = value; }
72                 }
73
74                 public CustomErrorMode Mode {
75                         get { return mode; }
76                         set { mode = value; }
77                 }
78
79                 public string ConfigFilePath {
80                         get { return configFilePath; }
81                 }
82                 
83                 public string this [int statusCode] {
84                         get {
85                                 if (redirects == null)
86                                         return null;
87                                         
88                                 return (string) redirects [statusCode];
89                         }
90
91                         set {
92                                 if (redirects == null)
93                                         redirects = new Hashtable ();
94
95                                 // Overrides any previous setting for statusCode even in the same file
96                                 redirects [statusCode] = value;
97                         }
98                 }
99         }
100         
101         class CustomErrorsConfigHandler : IConfigurationSectionHandler
102         {
103                 public object Create (object parent, object context, XmlNode section)
104                 {
105                         CustomErrorsConfig config = new CustomErrorsConfig (parent, context);
106                         
107                         string defaultRedirect = AttValue ("defaultRedirect", section);
108                         if (defaultRedirect != null)
109                                 config.DefaultRedirect = defaultRedirect;
110
111                         string mode = AttValue ("mode", section);
112                         if (mode != null) {
113                                 switch (mode) {
114                                 case "On":
115                                         config.Mode = CustomErrorMode.On;
116                                         break;
117                                 case "Off":
118                                         config.Mode = CustomErrorMode.Off;
119                                         break;
120                                 case "RemoteOnly":
121                                         config.Mode = CustomErrorMode.RemoteOnly;
122                                         break;
123                                 default:
124                                         ThrowException ("Invalid value for 'mode': " + mode, section);
125                                         break;
126                                 }
127                         }
128                         
129                         if (section.Attributes != null && section.Attributes.Count != 0)
130                                 ThrowException ("Unrecognized attribute", section);
131
132                         if (!section.HasChildNodes)
133                                 return config;
134
135                         XmlNodeList children = section.ChildNodes;
136                         foreach (XmlNode child in children) {
137                                 XmlNodeType ntype = child.NodeType;
138                                 if (ntype == XmlNodeType.Whitespace || ntype == XmlNodeType.Comment)
139                                         continue;
140
141                                 if (ntype != XmlNodeType.Element)
142                                         ThrowException ("Only elements allowed", child);
143
144                                 if (child.Name != "error")
145                                         ThrowException ("Unrecognized node: " + child.Name, child);
146
147                                 string statusCode = AttValue ("statusCode", child, false, false);
148                                 string redirect = AttValue ("redirect", child, false, false);
149                                 int code = 0;
150                                 try {
151                                         code = Int32.Parse (statusCode);
152                                 } catch {
153                                         ThrowException ("Unable to parse 'statusCode': " + statusCode, child);
154                                 }
155
156                                 if (code < 100 || code >= 1000)
157                                         ThrowException ("Invalid value for 'statusCode': " + code, child);
158
159                                 config [code] = redirect;
160                         }
161
162                         return config;
163                 }
164
165                 // To save some typing...
166                 static string AttValue (string name, XmlNode node, bool optional, bool allowEmpty)
167                 {
168                         return HandlersUtil.ExtractAttributeValue (name, node, optional, allowEmpty);
169                 }
170
171                 static string AttValue (string name, XmlNode node)
172                 {
173                         return HandlersUtil.ExtractAttributeValue (name, node, true);
174                 }
175
176                 static void ThrowException (string message, XmlNode node)
177                 {
178                         HandlersUtil.ThrowException (message, node);
179                 }
180                 //
181         }
182 }
183