Http Request Processing Flow

Source: Internet
Author: User
Tags hosting

Introduction

I have consulted many of the net books and found that most authors are standing on a higher level to explain the ASP. They are patient and meticulous in telling you how to drag and drop controls, set control properties, and write codebehind code to implement a specific function.

This practice, in effect, answers the question of " How to Do" , but does not answer the question "Why do you do this "?

Although I highly appreciate Mr. Hua's book, "An explanation of the net development of the temple sacrifice," I decided to skip to the following chapters as I looked at the role and the user (Member). Because I found that he also followed the flow of this part of the explanation to stay on the "How to Do" level. I believe it is impossible for a bull like Mr. Siddhartha to understand the underlying workings simply because the book is already thick.

When you press "How to do" the content to develop the program, you are still a programmer for your users, but for Microsoft developers who implement the MembershipProvider and RoleProvider abstract classes, you have become one of their users.

Note: I have no objection to some authors explaining only "How to do it", nor are you only learning how to do it, and there is also the benefit of being able to develop it quickly. I'm just suggesting a little bit more knowledge of the bottom and a better understanding of some issues.

Hopefully, this series of articles will give you a better understanding of how ASP works and how to do it.

An overview of the HTTP request processing process

Think "Why do I enter www.tracefact.net in the address bar to see Zhang Ziyang's personal space?" "It's like thinking," Why is the apple falling to the ground or not going up the sky? ”。 For ordinary visitors, it is just as natural that the sun rises to the west in the east of the day, and for many programmers, it is only the responsibility of the system administrator or network Manager to think that this has nothing to do with it. After all, IIS is a component of Windows and not a part of ASP. In fact, the IIS and. Net framework have done a lot of behind-the-scenes work from the moment you flick the carriage return to the page rendered within one-tenth seconds of your eyes.

You may find it irrelevant to understand how these behind-the-scenes work works, and as programmers you just have to ensure that the programs you develop can run efficiently. However, in the development process, you find that you often need to use classes such as HttpContext. At this time, have you ever thought about the composition of these classes and how the entities of the class were created? You may simply answer: HttpContext represents a context for the current request. But you know how IIS, the framework, and ASP. How do you work together to process each HTTP request, how to differentiate between different requests, IIS, the framework, and how does the data flow between ASP.

To answer these questions, you first need to understand how IIS handles page requests, which is the basis for understanding the form validation pattern and the Windows validation pattern.

When the HTTP request just arrived at the server

When the server receives an HTTP request, IIS first needs to decide how to handle the request (Note: the server handles an. htm page and an. aspx page must be different). What does IIS do with it? --According to the file's suffix name.

The server Gets the requested page (Note: It can also be a file, such as Jimmy.jpg), the next step is to look for applications on the server that can handle such suffixes if IIS cannot find an application that can handle such files, and the file is not protected by the server side (Note: A protected example is The file in App_Code, an unprotected example is your JS script), then IIS will return the file directly to the client.

Applications that can handle a variety of suffix names are commonly referred to as ISAPI applications (Note:Internet Server application programe Interface, Internet Servers application interface). Although this ISAPI sounds very stylish, but also a "application", but carefully look at its full name to understand: it is actually just an interface, acting as a proxy, its main work is to map the requested page (file) and the prefix name corresponding to the actual handler.

Let's take a closer look at the ISAPI and see what it looks like, follow these steps:

    1. Open IIS.
    2. Select Random site, right mouse button, "Properties".
    3. Select the Home Directory tab.
    4. Select Configure.

You should see the following screen:

Figure 1. Application Configuration

It is clear that all IIS can handle, or that the file type that the ISAPI provides the proxy service, and its corresponding actual spooler, are clearly listed here.

We find the application handler for the. aspx and then click "Edit" and the following screen appears:

Figure 2. editing handlers for. aspx files

As you can see here, all the. aspx files are actually handled by the Aspnet_isapi.dll program, and IIS submits a request for the. aspx page to aspnet_ After Isapi.dll, it was no longer concerned about how the request was subsequently handled. Now we should know thatASP. NET is just one component of the server (IIS), which is an ISAPI extension.

Here are two points to note:

    • When you modify limit to, you can restrict pages (files) to access only in a certain way
    • "Confirming the existence of a file" is a key option for implementing URL mapping, which I'll talk about later.
Understanding the hosting environment (Hosting)

In essence, ASP. NET is mainly composed of a series of classes, the main purpose of which is to translate the HTTP request to the client's response. The HttpRuntime class is a primary entry for ASP. It has a method called ProcessRequest, which takes a HttpWorkerRequest class as a parameter. The HttpRuntime class contains almost all the information about a single HTTP request: The requested file, server-side variables, QueryString, Http header information, and so on. ASP. NET uses this information to load, run the correct file, and convert the request to the output stream, in general, the HTML page.

Note: two like, can also be a picture.

When the content of the Web. config file changes or the. aspx file changes, in order to be able to unload applications running in the same process (Note: uninstallation is also for reloading), HTTP requests are placed in isolated application domains.

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

For IIS, it relies on one called HTTP. SYS's built-in driver to listen for HTTP requests from outside. When the operating system starts, IIS first registers its own virtual path in HTTP. sys.

Note: This is actually the equivalent of telling HTTP. sys which URLs are accessible and inaccessible. A simple example: Why do you get 404 errors when you access files that don't exist? is determined in this step.

If the request is an accessible url,http. SYS will hand this request to the IIS worker process.

Note: IIS6.0 called w3wp.exe,iis5.0 in the name of aspnet_wp.exe.

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

Note: Optional Performance parameters refer to settings such as the recycle mechanism, time-out settings, and so on.

The next thing to do is the ISAPI described in the previous section.

Note: This part of the content correlation is strong, in order to let everyone good understanding, I finally decided to put the ISAPI in front, maybe the full series will be adjusted when finished.

In addition to mapping files and their corresponding handlers, the ISAPI needs to do some other work:

    1. Gets the current HTTQ request information from HTTP. SYS and saves the information to the HttpWorkerRequest class.
    2. Load httpruntime in a mutually isolated application domain appdomain.
    3. Call the ProcessRequest method of httpruntime.

Then the code that the programmer typically writes is done, and then IIS receives the returned data stream and returns it back to HTTP. SYS, and finally, HTTP. SYS then returns this data to the client browser.

OK, now you see Zhang Ziyang's Space homepage.

Figure 3. The hosting environment for ASP.

Understanding Pipelines (Pipeline)

In the previous two chapters, we discussed the things that IIS and the Framework did in the ephemeral one-tenth seconds of sending an HTTP request to seeing the browser output in a relatively low level. But we have overlooked one detail: How the code written by the programmer is cohesive in this process, and we'll look at this in this chapter.

When an HTTP request enters the ASP. NET runtime, its pipeline consists of a managed module (note:managed Modules) and a handler (Note:handlers), and the pipeline handles the HTTP request.

Figure 4. Understanding Http Pipelines

Let's take a look at how the data in this picture is flowing.

1. httpruntime the HTTP request to Httpapplication,httpapplication, which represents the Web application created by the programmer. HttpApplication creates HttpContext objects for this HTTP request, which contain many other objects about the request, mainly HttpRequest, HttpResponse, HttpSessionState, and so on. These objects can be accessed through the page class or the context class in the program. 、

2. The next HTTP request passes through a series of module, which has full control over the HTTP request. These module can do some things before doing some actual work .

3. After the HTTP request passes through all the module, it is HttpHandler processed. In this step, you perform some actual operations, usually the business logic that is done by the. aspx page. You may feel that you have not experienced this process in creating an. aspx page, but you must know that the. aspx page inherits from the page class, and we look at the signature of the page class:

public class Page:templatecontrol, ihttphandler{
Code omitted
}

As you can see, the page class implements the IHttpHandler interface, and HttpHandler is the lowest level of HTTP request processing.

After the 4.HttpHandler is processed, the HTTP request returns to module once again, when module can do something that has already been completed.

Note : Notice the words I marked in red, and then recall: Is there a large number of Inserting, Inserted, and other paired events in ASP. In fact, here is why ASP. NET can divide an insert operation into two parts, and then separate the behind-the-scenes principle of event interception.

If we focus only on HTTP requests, HttpHandler and HttpModule, and do not consider HttpContext and HttpApplication, then Figure 4 can be simplified as follows:

Figure 5. The flow direction of HTTP requests in HttpHandler and HttpModule

Summarize

In this article, I start by outlining the topics that this series of articles will cover for you. Then, I put forward a problem with some programmers: learning and using ASP at a relatively high level.

Then, I took a visit to my personal Space home page example, led to this article mainly about the three content:

    1. The work that IIS does when the HTTP request just arrives on IIS.
    2. The hosting environment for the HTTP request.
    3. HTTP pipeline.

I hope this article will bring you help.

Http Request Processing Flow

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.