HTTP request processing process

Source: Internet
Author: User
Introduction

I have read a lot of ASP. net books and found that most authors have explained ASP. NET at a relatively high level. They patiently and meticulously tell you how to drag and drop controls step by step, set control properties, and write codebehindCodeTo implement a specific function.

This is actually an answer"How to do it"But did not answer"Why can this be done".

Although I highly recommend Mr. Shi jianghua's "ASP of temple sacrifices. NET development details, but when I looked at its role and user (member) explanations, I decided to skip and read the subsequent chapters directly. I found that he also followed the big stream, and the explanation of this part stays at the "how to do it" level. I believe that a cool man like Mr. Shi cannot not understand the underlying operating principles, just because the book is already very thick.

When you develop based on the content described in "how to do ",ProgramYou are still a programmer for your users, but for Microsoft developers who have implemented the membershipprovider and roleprovider abstract classes, you have become one of their users.

Note:I have no objection to the fact that some authors only explain "How to do it" or that you only learn "How to do it". This also has the advantage of rapid development. I only recommend that you have a better understanding of some problems.

We hope thatArticleSo that you can better understand the operating principles and practices of ASP. NET.

HTTP Request Processing Process Overview

"Why do I enter www.tracefact.net in the address bar to see Zhang Ziyang's personal space ?", It is similar to thinking: "Why did Apple go to the ground instead of the sky ?". For ordinary visitors, this is taken for granted as if the sun rises in the east and falls in the west every day. For many programmers, it is the responsibility of system administrators or network administrators to consider this as irrelevant. After all, IIS is a component of windows and is not an integral part of ASP. NET. In fact, IIS and. NET Framework have already done a lot of behind-the-scenes work within a tenth of a second.

You may feel that it is irrelevant to understand how these behind-the-scenes jobs work. As a programmer, you only need to ensure that the developed programs can run efficiently. However, during development, you often need to use classes such as httpcontext. At this time, have you ever thought about the composition of these classes and how they are created? You may simply answer: httpcontext represents a context of the current request. But you know IIS, framework, Asp. net is how to work together to process each HTTP request, how to distinguish different requests, IIS, framework, Asp. NET data flow between the three?

To answer these questions, you must first understand how IIS processes page requests. This is also the basis for understanding form and Windows authentication modes.

When an HTTP request arrives at the server

When the server receives an HTTP request, IIS first needs to determine how to process the request (Note:Is it true that the server processes A. htm page and A. ASPX page ). So what does IIS do? -According to the file suffix.

The server obtains the requested page (Note:It can also be an extension of a file, such as jimmy.jpg). Then, the server will find an application that can process such extensions. If IIS cannot find an application that can process such files, this file is not protected by the server (Note:A protected example is the file in app_code, and an unprotected example is your JS script). Then IIS will directly return the file to the client.

Applications that can process various extensions, usually called ISAPI applications (Note:Internet server application programe interface, Internet server application interface ). Although this ISAPI sounds very elegant, it is also an "application", but you can see it by looking at its full name:It is actually only an interface that acts as a proxy. Its main task is to map the requested page (File) and the actual processing program corresponding to the suffix.

Let's take a closer look at the ISAPI and see what it looks like. Please follow the steps below:

    1. Open IIS.
    2. Select a site at will, right-click the site, and select "properties ".
    3. Select the "home directory" tab.
    4. Select "configuration ".

You should see the following picture:

Figure 1. Application configuration

Clearly, we can see that all the file types that can be processed by IIS or that is called the proxy service provided by ISAPI and their corresponding actual background processing programs are clearly listed here.

Find the. aspx application handler and click "edit". The following figure is displayed:

Figure 2. Edit the. aspx file Handler

 

All the way here, we can see that all. aspx files are actually processed by the aspnet_isapi.dll program. after the request for the ASPX page is submitted to aspnet_isapi.dll, it no longer cares about how the request is processed subsequently. Now we should know:ASP. NET is only an integral part of Server (IIS). It is an ISAPI extension.

Note the following two points:

    • After you change "limit to", you can restrict access to pages (Files) in a specific way.
    • "Check whether a file exists" is a key option for URL address ing. I will detail it later.
Understanding the host environment (hosting)

In essence, ASP. NET is mainly composed of a series of classes whose main purpose is to convert HTTP requests into responses to clients. The httpruntime class is a main entry of ASP. NET. It has a method called processrequest. This method uses an httpworkerrequest class as a parameter. The httpruntime class contains almost all information about a single HTTP request: the requested file, server variables, querystring, and HTTP header information. ASP. NET uses this information to load and run the correct file and convert the request to the output stream. Generally, it is an HTML page.

Note:In other words, it can also be an image.

When the content of the Web. config file changes or the. aspx file changes, in order to uninstall the application running in the same process (Note:To reload), HTTP requests are divided into isolated application domains.

Note:You may have heard of the application domain before, but you don't know what's going on. The application domain is appdomain.

For IIS, it depends on a built-in driver called HTTP. sys to listen to HTTP requests from outside. When the operating system is started, IIS first registers its own virtual path in HTTP. sys.

Note:Actually, it is equivalent to telling HTTP. sys which URLs are accessible and which are inaccessible. For example, why do you encounter a 404 error when accessing a non-existent file? It is determined in this step.

If the request is an accessible URL, HTTP. sys will send the request to the IIS worker process.

Note:Iis6.0 is called w3wp.exe, and iis5.0 is called aspnet_wp.exe.

Each worker process has an identity and a series of optional performance parameters.

Note:Optional performance parameters, such as the recycle mechanism settings and timeout settings.

The next step is the ISAPI described in the previous chapter.

Note:This part of the content is highly correlated. In order to make everyone better understand it, I decided to put the ISAPI in front of it. It may be adjusted when the full series is completed.

In addition to the corresponding handler of the ing file, ISAPI also needs to do some other work:

    1. Obtain the current httq request information from HTTP. sys and save the information to the httpworkerrequest class.
    2. Load httpruntime in the appdomain of the mutually isolated application domain.
    3. Call the processrequest method of httpruntime.

The next step is the work that programmers usually write. Then, IIS receives the returned data stream and returns it to HTTP. sys, and finally, HTTP. sys then returns the data to the client browser.

OK. Now you can see Zhang Ziyang's space homepage.

Figure 3. ASP. NET host environment

Understanding Pipelines)

In the previous two chapters, we discussed at a relatively low level what IIS and framework did in a second from sending an HTTP request to seeing the browser output. However, we ignore the details of how the Code Compiled by the programmer is connected in this process. Let's take a look at this issue in this chapter.

When an HTTP request enters ASP. net runtime, its pipeline consists of a hosting module (Note: managed modules) and a processing program (Note: handlers), and the pipeline is used to process this HTTP request.

Figure 4. Understand the HTTP Pipeline

Let's take a look at how the data in this figure flows by number.

1. httpruntime transfers an HTTP request to httpapplication. httpapplication represents a web application created by a programmer. Httpapplication creates an httpcontext object for this HTTP request. These objects contain many other objects related to this request, including httprequest, httpresponse, and httpsessionstate. These objects can be accessed through the page class or context class in the program. ,

2. Next, the HTTP request uses a series of modules that have full control over the HTTP request.These modules can do things before performing a specific task..

3. After the HTTP request passes through all modules, it will be processed by httphandler. In this step, perform some actual operations, usually the business logic completed on the. ASPX page. You may not understand this process when creating the. ASPX page. However, you must know that the. ASPX page inherits from the page class. Let's take a look at the page class signature:

Public   Class Page: templatecontrol, ihttphandler {
// Code omitted
}

As you can see, the page class implements the ihttphandler interface, and httphandler is also the lowest layer for HTTP request processing.

4. After httphandler completes processing, the HTTP request returns to the module again,At this time, the module can do some things after some work has been completed.

Note:Pay attention to the words marked in red, and think back: Is there many paired events such as inserting and inserted in ASP. NET? In fact, ASP. NET can divide an insert operation into two parts, and then intercept the background principles of the event separately.

If we focus only on HTTP requests, httphandler, and httpmodule without considering httpcontext and httpapplication, figure 4 can be simplified as follows:

Figure 5. http request flow direction in httphandler and httpmodule

Summary

In this article, I will first outline the topics that will be discussed in this series of articles. Then, I raised a question for some programmers: learning and using ASP. NET at a relatively high level.

Later, I took an example to access my personal space home page and introduced three things described in this article:

    1. What does IIS do when an HTTP request arrives at iis.
    2. The host environment of the HTTP request.
    3. HTTP pipeline.

I hope this article will help you.

 

Address: http://www.tracefact.net/Asp-Net/Http-Request-Processing-Flow.aspx

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.