using System; using System.Web ; using System.Web.SessionState; using System.Collections.Specialized; namespace DHZUR { public class DHZUrlHandlerAspx : IHttpHandler { public void ProcessRequest(HttpContext ctx) { HttpResponse Response=ctx.Response; Response.Write("This is my handler"); } public bool IsReusable { get { return true; } } } }
using System; using System.Web ; using System.Web.SessionState; using System.Collections.Specialized; using System.Text.RegularExpressions; using System.Text; using System.Web.UI ; namespace DHZUR { public class DHZUrlHandlerAspxFactory:IHttpHandlerFactory { public DHZUrlHandlerAspxFactory(){} protected virtual HttpHandler[] GetHttpHandlers(HttpContext context) { return DHZUR.DHZUrlReWriteHandlerConfig.Instance().HttpHandlers; }
/// <summary> /// Implementation of IHttpHandlerFactory. By default, it will load an array of HttpHanlder (Dottext.UrlManager.HttpHandler) from /// the blog.config. This can be changed, by overrideing the GetHttpHandlers(HttpContext context) method. /// </summary> /// <param name="context">Current HttpContext</param> /// <param name="requestType">Request Type (Passed along to other IHttpHandlerFactory's)</param> /// <param name="url">The current requested url. (Passed along to other IHttpHandlerFactory's)</param> /// <param name="path">The physical path of the current request. Is not gaurenteed to exist (Passed along to other IHttpHandlerFactory's)</param> /// <returns> /// Returns an Instance of IHttpHandler either by loading an instance of IHttpHandler or by returning an other /// IHttpHandlerFactory.GetHanlder(HttpContext context, string requestType, string url, string path) method /// </returns> public virtual IHttpHandler GetHandler(HttpContext context, string requestType, string url, string path) { //Get the Handlers to process. By defualt, we grab them from the blog.config //Dottext.Framework.Logger.LogManager.Log("url",context.Request.Path); HttpHandler[] items = GetHttpHandlers(context); //Dottext.Framework.Logger.LogManager.Log("path",Dottext.Framework.Util.Globals.RemoveAppFromPath(context.Request.Path,context.Request.ApplicationPath)); //Do we have any? string pc=context.Request.FilePath; if(items != null) { int count = items.Length;
for(int i = 0; i<count; i++) { //We should use our own cached Regex. This should limit the number of Regex's created //and allows us to take advantage of RegexOptons.Compiled System.Text.RegularExpressions.Regex regex=new System.Text.RegularExpressions.Regex(items[i].Pattern ,RegexOptions.IgnoreCase|RegexOptions.Compiled); string res=DHZUrlHandlerAspxFactory.RemoveAppFromPath(context.Request.Path,context.Request.ApplicationPath); if(regex.IsMatch(res)) { //throw new Exception(); switch(items[i].HandlerType) { case HandlerType.Page://默认是Page
return ProccessHandlerTypePage(items[i],context,requestType,url); case HandlerType.Factory: //HandlerConfiguration.SetControls(context,items[i].BlogControls); return (IHttpHandler)items[i].Instance(); case HandlerType.Direct: //Pass a long the request to a custom IHttpHandlerFactory return ((IHttpHandlerFactory)items[i].Instance()).GetHandler(context,requestType,url,path); default: throw new Exception("Invalid HandlerType: Unknown"); } } } } //If we do not find the page, just let ASP.NET take over return DHZUrlReWritePageHandlerFactory.GetHandler(context,requestType,url, path); }
public virtual void ReleaseHandler(IHttpHandler handler) {
} }
}
///
using System; using System.Web ; using System.Web.SessionState; using System.Collections.Specialized; namespace DHZUR { public class DHZUrlHandlerForbidden:IHttpHandler { #region IHttpHandler 成员
public bool IsReusable { get { // TODO: 添加 DHZUrlHandlerForbidden.IsReusable getter 实现 return true; } }
#endregion
} }
using System; using System.Web ; using System.Web.SessionState; using System.Collections.Specialized; namespace DHZUR { public class DHZUrlHandlerForbiddenFactory:IHttpHandlerFactory { public IHttpHandler GetHandler(HttpContext ctx, string requestType, string url, string pathTranslated) {
} public void ReleaseHandler(IHttpHandler handler) { } }
}
//
using System; using System.Web; using System.Globalization; using System.Text.RegularExpressions; using System.Reflection; using System.Configuration; using System.Xml; using System.Xml.Serialization; using System.Xml.XPath; namespace DHZUR {
/// <summary> /// Summary description for RedirectHandler. /// </summary> public class DHZUrlRedirectHandler :IHttpHandler { public DHZUrlRedirectHandler() { // // TODO: Add constructor logic here // } #region IHttpHandler Members
public bool IsReusable { get { // TODO: Add RedirectHandler.IsReusable getter implementation return false; } }
#endregion } } ///
using System; using System.Text.RegularExpressions; using System.Web; using System.Reflection; using System.Configuration; using System.Xml; using System.Xml.Serialization; using System.Xml.XPath; namespace DHZUR { public class DHZUrlReWriteHandlerConfig:IConfigurationSectionHandler { private HttpHandler[] _httpHandlers; [XmlArray("HttpHandlers")] public HttpHandler[] HttpHandlers { get {return this._httpHandlers;} set {this._httpHandlers = value;} } public DHZUrlReWriteHandlerConfig() { } public static DHZUrlReWriteHandlerConfig Instance() { return ((DHZUrlReWriteHandlerConfig)ConfigurationSettings.GetConfig("DHZUrlReWriteHandlerConfig")); } #region IConfigurationSectionHandler 成员
public object Create(object parent, object configContext, System.Xml.XmlNode section) { // TODO: 添加 DHZUrlReWriteRuleConfig.Create 实现 XmlSerializer ser = new XmlSerializer(typeof(DHZUrlReWriteHandlerConfig));
// Return the Deserialized object from the Web.config XML return ser.Deserialize(new XmlNodeReader(section)); }
#endregion } public enum HandlerType { Direct, Factory, Page }; public class HttpHandler { public HttpHandler() { // // TODO: Add constructor logic here // } private HandlerType _handlerType = HandlerType.Page; [XmlAttribute("handlerType")] public HandlerType HandlerType { get {return this._handlerType;} set {this._handlerType = value;} } private string _pattern; [XmlAttribute("pattern")] public string Pattern { get {return this._pattern;} set {this._pattern = value;} } private string _type; [XmlAttribute("type")] public string Type { get {return this._type;} set {this._type = value;} } private readonly object HandlerLock = new object(); private ConstructorInfo constructor = null; public object Instance() { if(constructor == null) { lock(HandlerLock) { if(constructor == null) { System.Type t = System.Type.GetType("DHZUR.HttpHandler,WebApplication1"); constructor = t.GetConstructor(new Type[0]); } } } return constructor.Invoke(null); }
}
}
using System; using System.Web ; using System.Web.SessionState; using System.Collections.Specialized;
namespace DHZUR { public class DHZUrlReWriteModule:System.Web.IHttpModule { #region IHttpModule 成员
using System; using System.Web ; using System.Web.SessionState; using System.Web.UI ; using System.Collections.Specialized; using System.Text.RegularExpressions; using System.Text; namespace DHZUR { public class DHZUrlReWritePageHandlerFactory { public DHZUrlReWritePageHandlerFactory(){}
public void ReleaseHandler(IHttpHandler handler) {
}
} }
//
using System; using System.Text.RegularExpressions; using System.Web; using System.Reflection; using System.Configuration; using System.Xml; using System.Xml.Serialization; using System.Xml.XPath; namespace DHZUR { public class DHZUrlReWriteRuleConfig:IConfigurationSectionHandler { private ReWriteRule[] _reWriteRules; [XmlArray("ReWriteRules")] public ReWriteRule[] ReWriteRules { get {return this._reWriteRules;} set {this._reWriteRules = value;} } public DHZUrlReWriteRuleConfig() { } public static DHZUrlReWriteRuleConfig Instance() { return ((DHZUrlReWriteRuleConfig)ConfigurationSettings.GetConfig("DHZUrlReWriteRuleConfig")); } #region IConfigurationSectionHandler 成员
public object Create(object parent, object configContext, System.Xml.XmlNode section) { // TODO: 添加 DHZUrlReWriteRuleConfig.Create 实现 XmlSerializer ser = new XmlSerializer(typeof(DHZUrlReWriteRuleConfig));
// Return the Deserialized object from the Web.config XML return ser.Deserialize(new XmlNodeReader(section)); }
#endregion } public class ReWriteRule { public ReWriteRule() { // // TODO: Add constructor logic here // } private string _lookFor; [XmlAttribute("LookFor")] public string LookFor { get {return this._lookFor;} set {this._lookFor = value;} }
private string _sendTo; [XmlAttribute("SendTo")] public string SendTo { get {return this._sendTo;} set {this._sendTo = value;} } // private string _type; // [XmlAttribute("type")] // public string Type // { // get {return this._type;} // set {this._type = value;} // } private readonly object HandlerLock = new object(); private ConstructorInfo constructor = null; public object Instance() { if(constructor == null) { lock(HandlerLock) { if(constructor == null) { System.Type t = System.Type.GetType("DHZUR.ReWriteRule,WebApplication1"); constructor = t.GetConstructor(new Type[0]); } } } return constructor.Invoke(null); } }
}
///
using System; using System.Collections.Specialized; using System.Globalization; using System.IO; using System.Security; using System.Web;
// namespace DotNetGuy.Utility.Web - Renamed for consistency namespace DHZUR { /// <summary> /// Replacement static file handler class. Overcomes the "24-hour cache" bug /// in ASP.NET's default static file handler implementation. /// </summary> public class DHZUrlStaticFileHandler : IHttpHandler { // Static constructor
/// <summary> /// Static constructor that fills the string dictionary with /// the known MIME types. /// </summary> static DHZUrlStaticFileHandler() { _mimeMap = new StringDictionary();
// TODO: we send the file in one big chunk; if the file is potentially // large, this code should be re-written to chunk the transfer in // decent sized pieces (say, 64k at a time)
/// <summary> /// Verifies that the file is okay to serve. /// </summary> /// <param name="fi">Information about the file</param> /// <param name="filename">The filename</param> /// <returns>Returns true if the file is okay; false otherwise</returns> private bool _FileIsOkayToServe(FileInfo fi, string filename) { // Don't serve hidden files or directories
if ((fi.Attributes & FileAttributes.Hidden) != 0) return false;
if ((fi.Attributes & FileAttributes.Directory) != 0) return false;
// We specifically exclude filenames that end in periods because the // ASP.NET static file handler does, but I'm not sure why (there's // probably some security reason for it...)
if (filename[filename.Length - 1] == '.') return false;
return true; }
/// <summary> /// Returns a populated FileInfo structure for a file, throwing HTTP exceptions /// in response to missing files or security problems. /// </summary> /// <param name="filename">The filename</param> /// <returns>A populated FileInfo structure</returns> private FileInfo _GetFileInfo(string filename) { if (!File.Exists(filename)) throw new HttpException(404, "Resource not found");
try { return new FileInfo(filename); } catch (IOException) { throw new HttpException(404, "Resource not found"); } catch (SecurityException) { throw new HttpException(401, "Access denied"); } }
/// <summary> /// Gets the MIME type for the given filename /// </summary> /// <param name="filename">The filename</param> /// <returns>Returns the mime type for the file; if unknown, return /// application/octet-stream (default binary data format in MIME)</returns> private static string _GetMimeMapping(string filename) { string result = null; int idx = filename.LastIndexOf('.');
if (idx > 0 && idx > filename.LastIndexOf('//')) result = _mimeMap[filename.Substring(idx+1).ToLower(CultureInfo.InvariantCulture)];
if (result == null) return "application/octet-stream"; else return result; }
这些代码是从.Text Blog Engeer中修改过来的仅备自用的,using System;using System.Web ;using System.Web.SessionState;using System.Collections.Specialized;namespace DHZUR{ public class DHZUrlHandlerAspx : IHttpHandler { p