Note: This is an unedited contribution. If this article is inappropriate,
needs attention or copies someone else's work without reference then please
Report This Article
Introduction
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.
Features
By using only configuration, create and customize handlers that do a combination of the following:
- Authorize request using custom class.
- Get content from File, HTTP, FTP, or a custom class.
- Send the content to user via file download, or in the HTTP response.
- Cache the content in files or memory.
- Replace text in the delivered content.
- Redirect or Server Transfer the user to a new URL.
Use Cases
- If you want to authorize a request with custom code before user downloads a file.
- If you have a web farm with multiple servers that have content files uploaded to one of the servers and you want users to request these files from any server. So each server checks for the file in a certain directory and if not found he tries to gets it from other servers, either using folder sharing or by downloading the file using FTP or HTTP. Example of such scenario is replicating user uploaded images or replicating news content files saved in XML, HTML or ASPX formats and even replacing text inside them to manipulate and correct URLs.
- If you want to emulate another HTTP server by requesting it and then writing the content from the server in the user response.
- If you want to redirect the users from a web server to another using the same path. Example of such scenario is when you have a new domain and you want to redirect users that enter the old domain to the new one while maintaining the same path after the domain.
Using the Code
-
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.
Reference
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>
Steps are executed in the following order:
CustomAuthorize
- Check if cache exists (and if exists we continue from 6)
- All Get steps (could be more than one) are executed in order until one is successful.
- All Replace steps are executed in order.
CacheInFile and/or CachInMemory
- Download or Write or
ServerTransfer 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 download
Write: stream the content to the user response as response text
ServerTransfer: 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
A handy steps shortcut
For handy shortcuts on the available steps and their parameters check and bookmark this: http://bishoylabib.blogspot.com/2008/10/servercomponents.html.
Future
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.