![]() |
Web Development »
ASP.NET »
Utilities
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, Architect, Dev, Design
|
||||||||
|
Advanced 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 do any customization of the above based on configurations only.
By using only configuration, create and customize handlers that do a combination of the following:
In your website, add reference to (or put in the bin directory) the assembly ServerComponents.dll
In web.config, add the 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 the 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 this path check this link.
In web.config add the appropriate configuration sections to configure how the handler will behave in different conditions like the following example:
<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 doesn’t affect the actual processing)
MusicFiles: If 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 URL contains test/google then request the query after test/google to the real google website, replace 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 URL contains test/news then get the requested news ASPX page from an FTP, cache it on a local folder for later requests, and then make a server transfer that let 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 which handler should be executed. If more than one handler regex match 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 Redirect Steps are categorized by type as the following:
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 read 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 make an HTTP request for this URL + the relative path, and read the content of the returned response.GetFromFTP: takes an FTP URL with username and password, and read 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 only normal string matches. And you can repeat this steps for as many replaces 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 cache with the seconds attribute which will expire the cache after this duration.Download: stream the content to the user response as a file downloadWrite: stream the content to the user response as response textServerTransfer: make 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 result in an unexplained browser error.Redirect: make 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 have found 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: Deeksha Shenoy |
Copyright 2008 by Bishoy Labib Everything else Copyright © CodeProject, 1999-2009 Web18 | Advertise on the Code Project |