Files
mono/mcs/class/System.Web/System.Web.Configuration/HttpHandlersSectionHandler.cs
T
Gonzalo Paniagua Javier 00cd44e83d 2002-10-08 Gonzalo Paniagua Javier <[email protected]>
* System.Web/HttpApplication.cs: use handlers from configuration.
	* System.Web/HttpContext.cs: get handlers from ConfigurationSettings.

	* System.Web.Caching/Cache.cs: little fixes.

	* System.Web.Configuration/HttpHandlerTypeMapper.cs: removed.
	* System.Web.Configuration/HandlerFactoryConfiguration.cs: readded. I
	removed it by mistake.
	* System.Web.Configuration/HandlerItem.cs: only load the type if
	we gotta validate it. Implemented initial IsMatch.
	* System.Web.Configuration/HttpConfigurationContext.cs: New file.
	* System.Web.Configuration/HttpHandlersSectionHandler.cs: validate is
	optional (default true).  Use HttpHandlerTypeMapper.

	* System.Web.UI/SimpleHandlerFactory.cs: new handler for .ashx files.

svn path=/trunk/mcs/; revision=8053
2002-10-08 04:29:52 +00:00

115 lines
3.2 KiB
C#

//
// System.Web.Configuration.HttpHandlersSectionHandler
//
// Authors:
// Gonzalo Paniagua Javier ([email protected])
//
// (C) 2002 Ximian, Inc (http://www.ximian.com)
//
using System.Collections;
using System.Configuration;
using System.Xml;
namespace System.Web.Configuration
{
class HttpHandlersSectionHandler : IConfigurationSectionHandler
{
public virtual object Create (object parent, object configContext, XmlNode section)
{
HandlerFactoryConfiguration mapper;
if (parent is HandlerFactoryConfiguration)
mapper = new HandlerFactoryConfiguration ((HandlerFactoryConfiguration) parent);
else
mapper = new HandlerFactoryConfiguration ();
if (section.Attributes != null && section.Attributes.Count != 0)
ThrowException ("Unrecognized attribute", section);
XmlNodeList httpHandlers = section.ChildNodes;
foreach (XmlNode child in httpHandlers) {
XmlNodeType ntype = child.NodeType;
if (ntype == XmlNodeType.Whitespace || ntype == XmlNodeType.Comment)
continue;
if (ntype != XmlNodeType.Element)
ThrowException ("Only elements allowed", child);
if (ntype != XmlNodeType.Element)
ThrowException ("Unexpected node type", child);
string name = child.Name;
if (name == "clear") {
if (child.Attributes.Count != 0)
ThrowException ("Unrecognized attribute", child);
mapper.Clear ();
continue;
}
string verb = ExtractAttributeValue ("verb", child, false);
string path = ExtractAttributeValue ("path", child, false);
string validateStr = ExtractAttributeValue ("validate", child, true);
bool validate;
if (validateStr == null) {
validate = true;
} else {
validate = validateStr == "true";
if (!validate && validateStr != "false")
ThrowException ("Invalid value for validate attribute.", child);
}
if (name == "add") {
string type = ExtractAttributeValue ("type", child, false);
if (child.Attributes.Count != 0)
ThrowException ("Unrecognized attribute", child);
HandlerItem item = new HandlerItem (verb, path, type, validate);
mapper.Add (item);
continue;
}
if (name == "remove") {
if (child.Attributes.Count != 0)
ThrowException ("Unrecognized attribute", child);
if (validate && mapper.Remove (verb, path) == null)
ThrowException ("There's no mapping to remove", child);
continue;
}
ThrowException ("Unexpected element", child);
}
return mapper;
}
static string ExtractAttributeValue (string attKey, XmlNode node, bool optional)
{
XmlNode att = node.Attributes.RemoveNamedItem (attKey);
if (att == null) {
if (optional)
return null;
ThrowException ("Required attribute not found: " + attKey, node);
}
string value = att.Value;
if (value == String.Empty) {
string opt = optional ? "Optional" : "Required";
ThrowException (opt + " attribute is empty: " + attKey, node);
}
return value;
}
static void ThrowException (string msg, XmlNode node)
{
if (node != null && node.Name != String.Empty)
msg = msg + " (node name: " + node.Name + ") ";
throw new ConfigurationException (msg, node);
}
}
}