![]() |
Web Development »
ASP.NET »
Samples
Intermediate
License: The Code Project Open License (CPOL)
ServerComponents HTTP HandlersBy Bishoy LabibBy using only configuration, create and customize HTTP handlers that do a lot of tasks. |
C#.NET 2.0, ASP.NET, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
I’m currently involved in multiple consumer focused web portals that are deployed on multiple servers with load balancing. These environments contain a unique set of challenges which includes:
I’ve done a search to find any readymade components that solve some of the above problems, but found nothing. So I decided to create a component that handles HTTP requests in an ASP.NET website and dynamically does any customization of the above based on configurations only.
By using only configuration, create and customize handlers that do a combination of the following:
httpHandlers configuration to tell ASP.NET that the ServerComponent.Handlers.Handler class is handling requests with specific criteria.
<system.web>
<httpHandlers>
<add path="*" verb="GET" validate="false"
type="ServerComponents.Handlers.Handler"/>
</httpHandlers>
</system.web>
Note: in path="*", make sure you change this to the correct path criteria you want. You can add multiple entries to serve multiple options. Also note that this path allows you only to select certain extensions or folders; for more explanation of how to write to this path, check this link.
<configSections>
<section name="serverComponents.handlerConfiguration"
type="ServerComponents.Handlers.HandlerConfiguration"/>
</configSections>
<serverComponents.handlerConfiguration>
<handlers>
<addHandler name="MusicFiles" regex="test/music"
root="http://localhost:12697/TestingWebsite/test/music/">
<steps>
<addStep name="GetFromFile" type="GetFromFile"
path="E:\Audio\English\"></addStep>
<addStep name="Download" type="Download"></addStep>
</steps>
</addHandler>
<addHandler name="GoogleSearch" regex="test/google"
root="http://localhost:12697/TestingWebsite/test/google/">
<steps>
<addStep name="GetFromHTTP" type="GetFromHTTP"
path="http://www.google.com/"></addStep>
<addStep name="Replace1" type="Replace" find="google"
replace="microsoft" isCaseSensitive="false"></addStep>
<addStep name="Replace2" type="Replace" find="mail"
replace="e-mail" isCaseSensitive="false"></addStep>
<addStep name="CacheInMemory" type="CacheInMemory" seconds="300">
</addStep>
<addStep name="Write" type="Write"></addStep>
</steps>
</addHandler>
<addHandler name="NewsCMS" regex="test/news"
root="http://localhost:12697/TestingWebsite/test/news/">
<steps>
<addStep name="GetFromFTP" type="GetFromFTP"
path="ftp://10.0.0.10/news/" username="guest" password=""></addStep>
<addStep name="CacheInFile" type="CacheInFile"
path="G:\Projects\TestingWebsite\newscache\"></addStep>
<addStep name="ServerTransfer" type="ServerTransfer"
path="~/newscache/"></addStep>
</steps>
</addHandler>
</handlers>
</serverComponents.handlerConfiguration>
In the above example, there are three types of handlers (note that all name attributes are just for identifying configuration elements and don’t affect the actual processing):
MusicFiles: If the URL contains test/music, then get the requested file from a folder that is not accessible from normal website browsing and let the user download it.GoogleSearch: If the URL contains test/google, then request the query after test/google to the real Google website, replace a couple of words, cache the result in memory, then return the result to the user as if he is browsing Google from our website.NewsCMS: If the URL contains test/news, then get the requested news ASPX page from an FTP folder, cache it on a local folder for later requests, and then make a server transfer that lets ASP.NET execute the page.This is the handler element:
<addHandler name="MyHandler" regex="test/news"
root="http://localhost:12697/TestingWebsite/test/news/">
It has the following attributes:
name: which is just for configuration element identification purposes.regex: this is a Regular Expression that is matched against the request URL to figure out which handler should be executed. If more than one handler regex matches the URL, the first match will be executed.root: this is the root of the handler URL; everything in the URL after this root is considered the relative path of the requested file, and is appended to any handler step that has a path attribute.Here is the full set of steps that can be added inside a handler:
<addHandler name="MyHandler" regex="test/news"
root="http://localhost:12697/TestingWebsite/test/news/">
<steps>
<addStep name="CustomAuthorize" type="CustomAuthorize"
path="MyNamespace.MyClass"></addStep>
<addStep name="GetFromFile" type="GetFromFile"
path="E:\Audio\English\"></addStep>
<addStep name="GetFromHTTP" type="GetFromHTTP"
path="http://www.google.com/"></addStep>
<addStep name="GetFromFTP" type="GetFromFTP"
path="ftp://10.0.0.10/news/" username="guest" password=""></addStep>
<addStep name="GetFromClass" type="GetFromClass"
path="MyNamespace.MyClass"></addStep>
<addStep name="Replace" type="Replace"
find=http://localhost/OldUrl/
replace=http://localhost:12697/TestingWebsite/
isCaseSensitive="false"></addStep>
<addStep name="CacheInFile" type="CacheInFile"
path="G:\Projects\TestingWebsite\newscache\"></addStep>
<addStep name="CacheInMemory"
type="CacheInMemory" seconds="300"></addStep>
<addStep name="Download" type="Download"></addStep>
<addStep name="Write" type="Write"></addStep>
<addStep name="ServerTransfer" type="ServerTransfer"
path="~/newscache/"></addStep>
<addStep name="Redirect" type="Redirect"
path="http://www.google.com/"></addStep>
</steps>
</addHandler>
CustomAuthorizeCacheInFile and/or CachInMemoryServerTransfer or RedirectSteps are categorized by type as follows:
CustomAuthorize: takes a class name in the path attribute. The class should implement the ServerComponents.Handlers.ICustomAuthorizeHandler interface which takes the HttpContext, and should return true if authorized.GetFromFile: takes a folder path and reads the file with relative path inside this folder. The relative path is retrieved from the user request URL after the handler root.GetFromHTTP: takes a URL in the path attribute and makes an HTTP request for this URL + the relative path, and reads the content of the returned response.GetFromFTP: takes an FTP URL with username and password, and reads a file from the FTP with the URL + the relative path.GetFromClass: takes a class name in the path attribute; the class should implement the ServerComponents.Handlers.ICustomRequestHandler interface which takes the HttpContext and should return a stream that contains the content.Replace: takes the parameters find, replace, isCaseSensitive, and uses Regular Expressions to match and replace. So, the find parameter accepts Regular Expressions, not just normal string matches. And, you can repeat this step for as many replaces as you want.CacheInFile: takes a folder path in which to save cached copies of the content retrieved from any GET method. The cached copy is saved after the replace operations, and is replaced permanently until the file is manually deleted.CacheInMemory: takes the duration of the cache with the seconds attribute which will expire the cache after this duration.Download: streams the content to the user response as a file download.Write: streams the content to the user response as a response text.ServerTransfer: makes a server transfer to the path + relative path. Important note: take into consideration when using ServerTransfer that you do the transfer to a URL that is outside the httpHandlers path criteria because this will cause an infinite loop that results in an unexplained browser error.Redirect: makes a redirect to the path + relative path.For handy shortcuts on the available steps and their parameters, check and bookmark this: http://bishoylabib.blogspot.com/2008/10/servercomponents.html.
Until now, I only used this in testing. Honestly, I didn’t test these steps: CustomAuthorize, GetFromClass, and GetFromFTP.
I plan to use this component in multiple production situations in the next couple of days. So I’ll be adding bug fixes and may be more needed features.
If you find a bug or have a suggestion for a required feature, please tell me.
| You must Sign In to use this message board. | ||||||||
|
||||||||
|
||||||||
|
||||||||
|
||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 5 Oct 2008 Editor: Smitha Vijayan |
Copyright 2008 by Bishoy Labib Everything else Copyright © CodeProject, 1999-2009 Web19 | Advertise on the Code Project |