Showing posts with label Web. Show all posts
Showing posts with label Web. Show all posts

Saturday, July 23, 2011

Exploring IIS7

While working on URLRewriteModule to route all HTTP requests to HTTPS. The very first solution that came to my mind was to develop an 'HttpModule' to handle all the redirection stuff. For this I wrote an custom HttpModule in C#.


//Inside HttpModule
public void OnBeginRequest(Object sender, EventArgs e)
{
   HttpApplication HttpApp = (HttpApplication)sender;
   string HttpUrl = HttpApp.Request.Url.ToString();
   if (!IsSecureRequest(HttpApp.Request)))
   {
     HttpUrl = HttpUrl.Replace("http:", "https:");
     HttpApp.Response.Redirect(HttpUrl.ToString(), true); 
     HttpApp.Response.End();
   }
}

private static bool IsSecureRequest(HttpRequest request)
{
   bool HTTPSServerVar;
   bool RequestIsSecure;

   HTTPSServerVar = String.Compare(request.ServerVariables["HTTPS"], "on", true) == 0;
   RequestIsSecure = request.IsSecureConnection;

   return (HTTPSServerVar | RequestIsSecure);
}

//Web.Config changes

<system.webServer>
<modules>
<add name="urlrewriter" type="HTTP_TO_HTTPS.redir" />
</modules>
</system.webServer>


After testing the solution I was about to deploy it to staging server. So as to avoid pitfalls I rang up our solution architect to get inputs on, performance and security related stuff on my custom HttpModule. His feedback almost made me to scrap my custom HttpModule, the reason being Microsoft had come up with a URLRewrite module for IIS which could be downloaded and installed on an IIS machine. Moreover this module delivers the same functionality which I was trying to implement using custom HttpModule. While the best thing with Microsoft URLRewrite module is that, you could achieve HTTP to HTTPS redirection just by adding rule entries to your applications web.config file. Here's how to get this done.

<rule name="HTTP to HTTPS redirect" stopProcessing="true">
  <match url="(.*)" />
  <conditions>
    <add input="{HTTPS}" pattern="off" ignoreCase="true" />
  </conditions>
  <action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}/{R:1}" />
</rule>


Exploring IIS 6/7 Request Pipeline:
Out of curiosity I decided to dig into the internals on how IIS6/7 hands HTTPS requests. Sometime back I heard that IIS is handling HTTP requests at the kernel level. After going through MSDN I was amazed to find that, with IIS 7 the way HTTPS requests were handled where completely revamped when compared with IIS 6. Let me first show you how SSL requests were handled in IIS 6.

 IIS 6 - HTTPS Request\Response pipeline:
1. Encrypted request received from client by the OS. (Kernel Mode)
2. A kernel mode driver HTTP.SYS accepts the encrypted request. (Kernel Mode)
3. HTTP.SYS passes over the encrypted request to HTTPFilter to decrypt. (User Mode)
4. HTTPFilter passes the decrypted request back to HTTP.SYS. (Kernel Mode)
5. HTTP.SYS forwards the decrypted request to IIS->W3WP.exe (User Mode)
6. HTTP.SYS receives the processed response comes back from IIS->W3WP.exe (Kernel Mode)
7. HTTP.SYS passes on the response to HTTPFilter for encrypting (User Mode)
8. HTTP.SYS receives the encrypted response from HTTPFilter (Kernel Mode)
9. HTTP.SYS sends out the encrypted response back to client (Kernel Mode)


In the above HTTPS request processing you could see that, a context switch is happening between the kernel mode(HTTP.sys) and user mode(HTTPFilter  i.e.   HTTP SSL windows service) for decrypting and encrypting requests. Which is  very expensive and could lead to poor performance under high loads. So as get this resolved, IIS 7 had taken a new approach while handling SSL requests.


 IIS 7 - SSL Request\Response Pipeline:
1. Encrypted request received from client by the OS. (Kernel Mode)
2. A kernel mode driver HTTP.SYS accepts the encrypted request. (Kernel Mode)
3. HTTP.SYS decrypts the request using SChannel. (Kernel Mode)
4. HTTP.SYS forwards the decrypted request to IIS->W3WP.exe (User Mode)
5. HTTP.SYS receives the processed response comes back from IIS->W3WP.exe (Kernel Mode)
6. HTTP.SYS encrypts the response using SChannel. (Kernel Mode)
7. HTTP.SYS sends out the encrypted response back to client (Kernel Mode)


Here you could see that the context switching for request\response encryption is now taken care of in the Kernel Mode itself using SChannel (Secure Channel). This makes IIS 7 more robust when it comes to serving pages under heavy load.


Saturday, June 25, 2011

Open Source Video Players for Web

Longtime back a requirement came up, where I had to integrate video playback functionality in a webpage. The requirements specifically mentioned that it should be platform agnostic. Initially I thought of using Silverlight to get this done; but the downside of using Silverlight was that, it wasn't supporting Linux platform which breaks platform agnostic requirement. 


After some search I ended up with a handful of options which included Flash player and HTML5 Video Playback as well. Fortunately the video formats which I had to handle were all MPEG-4, which was supported by most popular web video players including Silverlight, Flash and HTML5. Finally I decided to go for an hybrid solution which works based on the following logic. 

If the browser supports HTML5 then the "Video" tag was used to playback the video, If not use Adobe Flash player, else use the Silverlight. If the browser didn't support any of those, the user is alerted to upgrade to a latest browser version or to install any of the supported players.

Here are some of the video players I came to stumble upon while working on this requirement.

Open Video Player - A fully open source video player and the best player I had ever seen and the best part is that its freeware. The player comes with a  pretty good number of options for you to choose from, like HTML 5 Video, Silverlight, Flash and iOS (Apple platform).

Flow Player - An Open source video player based on Adobe Flash. A free version is available under the GPL license. The free version even allows commercial use at the time of writing. The only downside is that, it displays their branding logo at the beginning of video playback, which isn't that obstructive to the viewer. If you are concerned about the branding logo at start of every clip, then there's a commercial version as well which is free from all these. Moreover the commercial version allows you to put your own branding logo in there.

JW Player - An open Source video player based on Adobe Flash. Display's branding text on to the left bottom side at the beginning of the video, after a while it disappears. The Pro version has even lot many options but will cost you some bucks. The restricting factor is the licensing terms on using the player on commercial sites.

Thursday, June 23, 2011

Avoid Caching Issues While Development

Browser caching can become a nightmare when developing web applications, as most of the changes what we make in the code may not get reflected at the browser end, which might end up missing the set time frame for that specific task; the reason being caching of contents at the browser level or at the proxy level. 

The problem with browser caching can be sorted out through use of a new feature built-in to modern browsers known "InPrivate Browsing" (in IE) and "Incognito window" (in Chrome). The feature presents you with a session (a new window) which is completely free from all sorts of saved cookie info and content cache. The beauty of this mode is that, what ever browsing you do here wont even leave a trace of those activities in the browser history nor in the cookie container or in the browser cache. 

If you had done Ribbon UI development in SharePoint 2010 before, then definitely you must have faced the dilemma of caching, as what ever changes you made to the Ribbon bar won't get reflected at the browser end as the XML required to render the Ribbon UI is cached. Myself being faced with the dilemma of caching with other development tasks and now in SharePoint, I had decided to go for the aforesaid "Isolated Mode" (as I wish to call it) as its going to eliminate the issues associated with web browser caching.

So next time don't forget to utilize this pretty cool feature packaged with almost all browsers available out there in the net.