HTTP Request Pipeline Training

26
The HTTP Request Pipeline Intertech www.Intertech.com

description

http://www.Intertech.com This slide deck is from an Intertech presentation on the HTTP Request Pipeline delivered at VSLive.

Transcript of HTTP Request Pipeline Training

Page 1: HTTP Request Pipeline Training

The HTTP Request Pipeline

Intertech

www.Intertech.com

Page 2: HTTP Request Pipeline Training

Overview

IIS Configuration

Process Architecture

ASP.NET Request Pipeline Overview

HTTP Application

Configuration of HTTP Handlers

Implementing an HTTP Handler

Implementing an HTTP Module

Page 3: HTTP Request Pipeline Training

IIS Configuration

The ASP.NET Framework uses a COM ISAPI application

This ISAPI extension communicates with a Managed process via named pipes

The ASP.NET Framework runs within its own process – ASPNET_WP.EXE

Within this architecture, IIS delegates requests to the worker process

Page 4: HTTP Request Pipeline Training

Delegation of Requests

Requests are mapped to specific ISAPI applications within IIS application configuration

Each extension can be mapped to a different application

Classic ASP was configured this way as well

Page 5: HTTP Request Pipeline Training

From COM to Managed Code

Once IIS has delegated the request to ASPNET_ISAPI.DLL, it gets passed to managed code via named pipes

Requests for any file type can be mapped to the ASP.NET Framework

38 file types mapped to this ISAPI application when the Framework is installed

Extend functionality of Framework with your own custom types

Map old extensions to Framework to migrate users of old site to your .NET application

Page 6: HTTP Request Pipeline Training

File System

IIS – INETINFO.EXE

.ASPX extension mapped to

ASPNET_ISAPI.DLL

Browse

r

ASP.NET Worker Process

ASPNET_WP.EXE

ASPX

Assembly(Code Behind)

Your Compiled Code

System.Web.UI.Page : IHTTPHandler

Nam

ed

Pip

es

System.Web.

UI.Page

Page Rendering

<HTML>

.

.

</HTML>

Page 7: HTTP Request Pipeline Training

HTTP Handlers

The previous picture describes the Page Handler specifically.

Once a request is mapped to the ASP.NET Framework from IIS, it can be directed to a specific handler within the framework

There are many handlers that install by default with the ASP.NET Framework– Web Services

– Remoting

– Trace Handler

– Image Service

– Many others

Page 8: HTTP Request Pipeline Training

The HTTP Application Lifetime

Instances of handlers are actually created by the HTTPApplication type

The HTTPApplication uses a consistent lifecycle across all handlers

This lifecycle is therefore extensible across different types of handlers

The Page object’s lifecycle is only a small part of this process

The Page object exposes its own consistent lifecycle you’re familiar with from ASP.NET page processing

– This lifecycle is defined by the System.Web.UI.Control base class that the Page and all controls inherit from

Let’s examine the entire pipeline…

Page 9: HTTP Request Pipeline Training

Reque

st

Pip

elin

eHTTP ApplicationSecurity Events

Lifetime Events

Request Events

HTTP Modules

ASynch Events

Context

Established

Conte

xt

HTTP HandlerImplements

IHTTPHandler

Page Handler

Web Service Handler

Remoting Handler

Custom Handlers

Cleanup Events

Response

Stream Sent

To IIS

HTTPContext

HTTP Headers

Request

Response

Session

Server variables

Cache

Application variables

Security Events

Authenticate

Authorize

Lifetime Events

Application Begin

Application End

Session Start

Session End

Request Events

Begin Request

Cache Resolution

State Acquisition

Pre Handler

HTTP Modules

Custom types to

respond to

application events

ASynch Events

Error

Pre-send Headers

Pre-send Content

Page Events

Init

Load

Control Events

Pre Render

Unload

Cleanup Events

Post Handler

Release State

Update Cache

End Request

Page 10: HTTP Request Pipeline Training

Handlers in the Framework

Handlers are configured in the Machine.Config

– This is where specific extensions are mapped to different implementations of the IHTTPHandler Interface

Page Handler : ASPX pages

– .aspx

Web Service Handler – Web Services

– .asmx

HTTP Remoting Handler – Remoting

– .rem and .soap

HTTP Forbidden Handler – returns 404s for protected file types

– Projects, code files, config files, webinfo, resource files, binaries, user controls

Page 11: HTTP Request Pipeline Training

Handler Uses

Secure file types that are not secured under .NET Forms based authentication– Map .doc and .pdf to framework

Provide link updates from old technology– Map CFM or ASP to Framework

– Redirect to new ASPX page

Add copyright to images before returning them– Load requested image as Bitmap object

– Use GDI+ to paint a ‘watermark’ on your images

Retrieve documents from database – URLs appear to be file locations to user

Page 12: HTTP Request Pipeline Training

IHttpHandler Interface

The IHttpHandler interface defines two methods you must implement

IsReusable – boolean indicating if instances of the type can be reused across requests

ProcessRequest – the hand off from the HttpApplication to your own handling of the request

public bool IsReusable

public void ProcessRequest(HttpContext context)

Page 13: HTTP Request Pipeline Training

Implementing IHttpHandler

Notice the ProcessRequest method accepts an instance of HttpContext as a parameter– Use this to send content down the output stream

public bool IsReusable{

get { return true; }}

public void ProcessRequest(HttpContext context){

context.Response.Write("Here is your custom handler output.");

}

Page 14: HTTP Request Pipeline Training

Configuring the Handler

An entry must be made in the Web.Config to map your custom extension to the handler

<httpHandlers><add path="*.dom" type="MyHandler" verb="*"/>

</httpHandlers>

You must also map the extension to the Framework from within IIS

Page 15: HTTP Request Pipeline Training

Demo

Custom Http Handler

Page 16: HTTP Request Pipeline Training

Extending the Request Pipeline

The easiest way to extend the ASP.NET request processing pipeline is to create a class that inherits from HttpApplication and add it to your project– This is done for you by default by Visual Studio.NET in

the global.asax file

You can trap all standard events in the lifetime of the request and add your custom logic

Page 17: HTTP Request Pipeline Training

Reuse of HttpApplication Types

The global.asax is deployed on a per IIS application basis

This limits your implementation to a single web site

To reuse functionality across different applications, create type that inherits from HttpApplication in stand-alone assembly

Add reference from a web project

Have global.asax inherit from your custom type instead of directly from HttpApplication

Assembly can then be reused across applications

Page 18: HTTP Request Pipeline Training

Demo

Inheriting From HttpApplication

Page 19: HTTP Request Pipeline Training

Creating Custom HttpModules

HttpApplication hooks implemented with HttpModules under the hood

Session management – In or out of process

Authentication– For Forms Authentication

Output Caching – Check cache before request comes in

– Return from cache instead of creating instance of page object when found

Page 20: HTTP Request Pipeline Training

Custom HttpModules

You can extend the pre and post handler pipeline with your own HttpModules

Create implementation of IHttpModule interface

Register into pipeline using Web.Config

Interface exposes only two methods– Init & Dispose

Use Init to sink events in the HttpApplication lifecycle

Page 21: HTTP Request Pipeline Training

Implementing IHttpModule

Init method accepts instance of HttpApplication as argument

All of ASP.NET intrinsics are exposed

Use application instance to sink any events in lifetime of request

Squirrel away local reference to instance

In event traps, examine the path, the user, the Http headers, etc

Page 22: HTTP Request Pipeline Training

Sample Implementation

public class UserDir : HttpApplication{private HttpApplication app;public void Init(HttpApplication application){app = application;app.BeginRequest += new EventHandler(this.CustomEventTrap);}

private void CustomEventTrap(object sender, EventArgs e){ //Custom Logic using ‘app’ Implemented Here }

public void Dispose() {}}

Page 23: HTTP Request Pipeline Training

HttpModule Configuration

<configuration><system.web>…<httpModules><add name=“ModuleName"

type=“Namespace.ClassName,AssemblyName"/></httpModules>…</system.web></configuration>

Page 24: HTTP Request Pipeline Training

Demo

HttpModule Implementation

Page 25: HTTP Request Pipeline Training

Summary

ASP.NET Framework is complete application server platform– Not just for Web Forms

.NET Framework ships with several different applications built into ASP.NET Framework– Web Forms

– Web Services

– Remoting

ASP.NET Framework is completely extensible– Build your own handlers

– Inherit from HttpApplication

– Extend the pipeline with Http Modules

Page 26: HTTP Request Pipeline Training

Help | About Dominic Selly

Instructor for Intertech Training – Now Offering Live Instructor Lead Virtual Training!

www.IntertechTraining.com– Full course outlines and descriptions

Questions? Comments?

[email protected]