Custom HTTP processing and application of HttpHandler articles

Source: Internet
Author: User
Tags filter bool config http request httpcontext iis implement microsoft iis


In developing applications based on Microsoft IIS, developers can also use development tools such as Visual C + + to develop ISAPI applications for more powerful features, in addition to the ability to write ASP programs. You can write two ISAPI extensions: an ISAPI Server extention and an ISAPI Filter, but the authoring of ISAPI extension applications usually has a higher requirement for developers and is more difficult to develop and deploy. While developing asp.net applications, we can still write ISAPI applications to augment the capabilities of IIS, but ASP.net offers us another option-using HTTP Handler and HTTP Module. This is accomplished by using the IHttpHandler and IHttpModule interfaces. HTTP handler provides functionality similar to the ISAPI Server extention, and HttpModule implements functionality similar to ISAPI filter, and is much simpler to develop and deploy than ISAPI. Applying HttpHandler and HttpModule enables applications to interact with the lower-level request and response services of an IIS Web server. This paper first introduces the concept and basic usage of HttpHandler and HttpModule, and introduces a case of applying httpmodule to implement the privilege system.
The basic model of HTTP processing pipelines
In order to study the HttpModule and IHttpHandler, one must first understand the ASP.net treatment pipeline. In a asp.net application, the system uses a set of related classes to process client requests (request) in a certain order, ASP. NET application is referred to as an HTTP processing pipeline. HttpModule and IHttpHandler are the two processing links on the pipeline. Classes in the HTTP processing pipeline are defined in the System.Web namespace, and are mainly of the following types: · The HttpWorkerRequest abstract class defines the basic method of ASP.net page processing requests; HttpRuntime provides a set of services for processing applications; HttpContext saves all relevant contextual information for processing a request; HttpApplicationFactory an application that provides the relevant directory; · HttpApplication defines common methods, properties, and events for all asp.net applications. This class is also the base class for applications defined in the user's Global.asax file; Modules the event before and after the request is processed; Handlerfactories provides handlers; in the application Handlers process requests and responses. The model for the HTTP processing pipeline is as follows:

Figure 1:http Processing pipeline
On the Windows platform, HTTP Pipline requires the support of IIS. In order to run the ASP.net application, IIS requires the following two files: Aspnet_isapi. DLLs and aspnet_wp. EXE Aspnet_isapi. DLL is an ISAPI extention he will send to IIS please forward aspnet_wp. EXE processing · Aspnet_wp. EXE using httpruntime to handle requests for specific processing can be shown as follows:

The HTTP processing pipeline on Figure 2:iis

The realization of HttpHandler
HttpHandler implements functionality similar to the ISAPI extention, which handles requests (request) information and sends a response (Response). The implementation of the HttpHandler function is achieved by implementing the IHttpHandler interface. In fact, when we write the ASP.net page, the ASP.net page inherits the base class--system.web.ui.page--also implements the HttpHandler interface, is also a httphandler, look at its definition to know (C #):
public class Page:templatecontrol, IHttpHandler

The interface IHttpHandler is defined as follows:
Interface IHttpHandler
{
void ProcessRequest (HttpContext ctx);
BOOL isreuseable {get;}
}

The ProcessRequest in the interface is where you add your own code and handle it accordingly. The Isreuseable property indicates whether the implementation class of the HttpHandler requires caching. The following example shows the basic use of HttpHandler: 1. Create a project called MyNamespace, add a class, name MyHandler, code as follows:
Example 1:
Namespace MyNamespace
{
public class Myhandler:ihttphandler
{
public void ProcessRequest (HttpContext ctx)
{
HttpResponse Response
Response.Write ("This are my Handler");}
public bool IsReusable
{
get {return true;}
}
}
}

2, compile the above code, generate MyNameSpace.Dll files, 3, create a new WebApplication project, or open a WebApplication project, Add the file MyNameSpace.Dll to the project's reference, or copy it to the project's Bin directory, 4, modify the web.config, and add the following:
<configuration>
<system.web>
<add verb= "*" Path= "*.aspx"
Type= "Mynamespace.myhandr, MyNamespace"/>
</system.web>
</configuration>

Description of options in the configuration file: · The verb can be "get" or "post" to indicate that a request for Get or post is processed. "*" indicates that all requests are processed. · Path indicates that the appropriate file is processed, and "*.aspx" indicates that requests to all ASPX pages are processed. You can indicate that the path, such as "/test/*.aspx", indicates that only the ASPX file under the test directory is processed. · In the type attribute, the string before the comma indicates the class name of the HttpHandler implementation class, followed by a string that indicates the name of the DLL file. Now, to request any ASPX page in the project, the page is always displayed with the following line of words:
This is my handler


Because our custom handler intercepts all requests to the ASPX page and processes those requests in its own way. To enable our ASPX page to run smoothly, we need to modify the Web.config file:
<configuration>
<system.web>
<add verb= "*" Path= "*.foo"
Type= "Mynamespace.myhandr,hander"/>
</system.web>
</configuration>


In order to allow requests for files with suffix. Foo to be run by our handler intercept, we need some extra work. Open the admin console for IIS, click the site, select Properties, and jump out of the site's Properties dialog box. Select the Home directory option. As shown in Figure 3:

Figure 3:web Site Properties dialog box
Select the configuration, eject the Application Configuration dialog box, and add ". Foo" to the application map, as shown in Figure 4:

Figure 4: Adding Application Mappings
Well, now that we can add an. foo file to the project, when you send the request to the file, the browser displays:
This is my handler


Access to other ASPX files is not affected.
Realize Handler Factory
Another option to implement the HttpHandler function is to implement a handler Factory, which is implemented by implementing the IHttpHandlerFactory interface. The IHttpHandlerFactory interface is defined as follows:
Interface IHttpHandlerFactory
{
IHttpHandler GetHandler (HttpContext CTX,
String RequestType,
String URL,
String pathtranslated);
void Releasehandler (IHttpHandler handler);
}


The GetHandler method is invoked at the start of the request, and Releasehandler is invoked when the request is finished and all handler are no longer needed. The process of using httphandlerfactory is generally as follows: first define the class that actually handles the HttpHandler, which is called in the Handlerfactory for actual processing:
public class Basichandler:ihttphandler {...}


Then, define your own handlerfactory:
public class Basichandlerfactory:ihttphandlerfactory
{
Public IHttpHandler GetHandler (HttpContext CTX,
String RequestType,
String URL,
String pathtranslated)
{
return new Basichandler ();
}
public void Releasehandler (IHttpHandler handler) {}
}


Finally, register the factory in the Web.config file:
<configuration>
<system.web>
<add verb= "POST" path= "*.foo"
Type= "mynamespace.basichandlerfactory, myassembly"/>
</system.web>
</configuration>

Asynchronous handler
Asynchronous processing of HTTP requests can be achieved by implementing IHttpAsyncHandler. The IHttpAsyncHandler interface inherits the IHttpHandler, also implements the ProcessRequest method and the IsReusable attribute, and needs to implement BeginProcessRequest and EndProcessRequest method. BeginProcessRequest initiates an asynchronous call to handle a single HTTP request, while EndProcessRequest executes the cleanup code at the end of the process. IHttpAsyncHandler implementations and registrations are similar to IHttpHandler, and readers can refer to the MSDN documentation. Now, do you have a certain understanding of the concept and application of HTTP handler? In the next article, we will focus on the application of HTTP module and give an example of implementing a privilege system using HttpModule.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.