ASP. NET 3.5 core programming learning notes (41): HTTP processing program and HTTP Module

Source: Internet
Author: User

The HTTP handler is a component that truly processes requests. It implements the ihttphandler interface. The processrequest method of this interface is the "central console" that controls request processing ". For example, the page class implements the ihttphandler interface. Its processrequest method is used to load and save the view status, and to trigger some common events (such as init, load, and prerender ).

ASP. NET maps each incoming HTTP request to the corresponding HTTP handler. The HTTP handler factory creates instance objects for these handlers (for example, the pagehandlerfactory class can parse the source code of the requested aspx resource, and return class instances representing the corresponding page after compilation ).

The HTTP module is a class that implements the ihttpmodule interface and is used to process running events. The module can process two types of public events: events triggered by httpapplication (including asynchronous events) and events triggered by other HTTP modules. For example, sessionstatemodule is one of ASP. Net built-in modules used to provide session status services for applications. It can trigger end and start events, while other modules can process them through signatures such as session_end and session_start.

The HTTP handler and the HTTP module correspond to the ISAPI extension and ISAPI filter respectively. Although the functions are the same, the programming model of the processing program and module is much simpler.

ISAPI stands for the Internet server application programming interface, which is used for the Protocol for IIS to interact with external components. The ISAPI model is based on a Microsoft Win32 unmanaged dynamic link library and exposes two functions. This model has been significantly extended in IIS 7.0, based on HTTP processing programs and modules, and largely corresponds to the ASP. NET extensible model.

Overview of IIS scalable APIs

Basically, a Web server is a type of server application that can be connected through multiple Internet protocols (such as HTTP, FTP, nntp, and SMTP ).

Web servers generally provide application programming interfaces (APIS) to improve and customize server functions. The first extended API in history is through the common getway Interface (CGI ). A new process is required for the CGI application to process each request. It is not suitable for high-swallowed web sites and is no longer used in modern applications. IIS supports CGI applications, but this function should be avoided if backward compatibility is required. The new version of Web server provides a more efficient alternative model to extend the functions of this module. In IIS, this alternative model adopts the form of ISAPI.

ISAPI Model

IIS loads the ISAPI component (Win32 DLL) to its own process. Then, IIS calls the public entry point on the DLL to process the request. The ISAPI component is retained until IIS is disabled, and request processing does not affect the activity of the Web server. The disadvantage of this model is that any errors that occur when a component is loaded to the Web server will affect the entire server and all installed applications. However, some measures have been taken to solve this problem since IIS 4.0. Before IIS 6.0 appears, we can set the protection level for newly installed applications, which contains three options: low, medium, and high.

.

Applications Running in IIS 6.0 can be grouped by application pool. We can choose whether to use the existing pool to create a new one. This allows applications in the same pool to share the same running time, that is, w3wp.exe.

The ISAPI model has a serious defect in the programming model. ISAPI components must be developed using C or C ++, and multithreading should also be considered. Errors and runtime faults will affect the entire application. Therefore, writing such programs should be extremely cautious.

Starting from IIS 6.0, all functions built on IIS must follow the ISAPI model specifications. ASP and ASP. NET are no exception. Today, the entire ASP. NET platform is closely integrated with IIS, but the two are not one. The Core Component aspnet_isapi.dll is a bridge between IIS and the runtime environment of ASP. NET. When An ASPX resource request is passed in, IIS will give control to aspnet_isapi.dll, and the component will then send the request to the ASP. NET pipeline in the Common Language Runtime (CLR) instance.

Structure of ISAPI Components

The ISAPI extension is called through the URL Ending with the DLL, as shown below:

Http://www.contoso.com/apps/hello.dll

The dll must expose two functions: getextensionversion and httpextensionproc. The getextensionversion function is used to set the version and name of the ISAPI Server Extension. When the extension is loaded, the getextensionversion function is called first. However, it is called only once to initialize internal variables. If everything is normal, this function returns true. If an error occurs, the function returns false. The web server terminates the DLL loading and sends a message to the system log.

The httpextensionproc function is the core of the ISAPI component. This function can receive basic HTTP information (such as query strings and headers) of the request, perform the desired operation, and prepare to send the response to the browser.

The ISAPI programming model consists of two components: ISAPI extension and ISAPI filter.

ISAPI extension

ISAPI extension is the IIS In-process version of the CGI application. IIS extension works in the same way as ASP or ASP. NET pages. It obtains information related to HTTP requests and generates valid and HTTP responses.

The ISAPI extension consists of compiled code. If you want to modify it, you need to re-compile and re-Load it each time. If the DLL is loaded into the memory of the web server, the web server must stop running. If the DLL is loaded into the context of a separate process, the process must stop running. If the DLL is used externally, the execution speed of ISAPI extension is not as fast as that in the process, but it does not affect the stability of IIS.

ISAPI filter

ISAPI filter is a component that can capture and process specific server events. When loading, the filter indicates the event that it can process. If an event is thrown, the filter processes the event or sends it to another filter.

The filter can only run in inprocess mode. It can be shared by the entire web server or specific websites.

The ISAPI filter provides features such as custom authentication mode, compression, encryption, logging, and request analysis. ISAPI filters have great development potential and are flexible in processing incoming and outgoing data streams.

IIS 7.0 Innovation

1. Unified Runtime Environment

IIS 7.0 reflects the unification of ASP. NET and IIS platforms in a program. HTTP processing programs, HTTP modules, running pipelines, and configuration files form an element of the public environment. The entire IIS MPs queue is componentized to accommodate multiple independent and configurable components. A new section is added to the web. config file of the ASP. NET application to configure the IIS environment. Therefore, the ASP. Net Runtime Library is extended to interact with the peripheral web server environment, and to replace the components.

2. Managed ISAPI extensions and filters

If you want to control incoming requests in versions earlier than IIS 7.0, you can write C or C ++ DLL using MFC or the Active Template Library (ATL. HTTP processing programs and modules are unique to ASP. NET, but they can only process ASP. NET-specific resources. Requests are sent to ASP. NET only after being verified by IIS.

In IIS 7.0, we can write HTTP processing programs and modules to filter any requests and use. Net code to add new features to all resources that can be processed by Web servers. Specifically, we write HTTP processing programs and modules as usual, but you can register them for any type.

  

Related Article

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.