Less Intrusive HTTP Modules

A powerful tool for ASP.NET applications


April 21, 2008
URL:http://www.drdobbs.com/web-development/less-intrusive-http-modules/207400845


An HTTP module is a .NET class that implements the IHttpModule interface. Once registered with an ASP.NET Web application, such classes hook up any incoming HTTP request and gain full access to the headers and raw data in the packet. All ASP.NET applications inherit a bunch of system HTTP modules as configured in the system's web.config file. An HTTP module can pre-process and post-process a request, and it intercepts and handles system events as well as events raised by other modules.

HTTP modules are extremely powerful tools as through them you get yourself in total control of the request and close to the nerve center of ASP.NET applications. This strength, though, is also a major weakness of HTTP modules. If abused or misused, in fact, HTTP modules can end up taxing any incoming requests and suddenly transform into the perfect bottleneck for your application.

Unlike HTTP handler, HTTP modules are not bound to a specific extension or a family of URLs. In a certain sense, HTTP modules are lower level tools than handlers and are loaded at the very startup of the application. HTTP modules form a pipeline that each Web request passes through. Modules are used to hook up application-level events rather than specific requests. Here's a typical implementation of a HTTP module:

public class SampleModule : IHttpModule
{
    public void Init(HttpApplication app)
    {
        // Register for pipeline events
    }
    public void Dispose() 
    {
        // Nothing to do here
    }
}

The Init module is invoked at the application startup and registers the module with the application events of interest -- BeginRequest, AuthenticateRequest, AcquireRequestState, EndRequest, and the like. The Dispose method is invoked when the application is shutting down and is normally used to dispose of any resources that was acquired earlier. An HTTP module is used to authenticate and authorize a request, to load and save the session state, to deal with the output cache. As you can see, a HTTP module is the ideal tool to implement cross-cutting concerns of an application when not extensions to the ASP.NET platform.

It may happen that your application needs to implement some sort of pre- and post-processing on top of a certain category of requests. The HTTP module is seemingly the perfect to tool; but unfortunately, once registered, it kicks in for each and every request that arrives to your Web server and is dispatched to your ASP.NET application. For example, no JPEG file requests triggers a module because this request is served directly by IIS and never reaches the ASP.NET runtime. If, say, you need to pre-process only ASCX requests than a HTTP module might be optimal if it weren't that it would hook up also ASPX or ASMX requests.

Is there a way to fix this issue? I'm afraid this aspect of HTTP modules is built this way by design. One thing you can certainly do is, however, hook up the BeginRequest event and check whether the current request is something you may be interested in. If this is not the case, then switch off some internal flag that will prevent the rest of HTTP module's code to work for the duration of that request.

It's not a definitive answer, but it helps at least a little bit.

Terms of Service | Privacy Statement | Copyright © 2024 UBM Tech, All rights reserved.