Archive for December, 2008

09 Dec 2008

Creating an extension module for .NET URL Rewriter and Reverse Proxy

4 Comments Uncategorized

Wow that is a long title. Recently I have been looking for quick posts that I can put out each day to keep my blog relevant and also so I don’t feel like I am slacking off too much. Today I want to post about a little known feature in my .NET URL Rewriter and Reverse Proxy (aka. Managed Fusion URL Rewriter) that I have developed in my spare time, mostly out of necessity for this blog and other projects I have worked on.  Here is a quick run through of what it does.

Managed Fusion URL Rewriter is a powerful URL manipulation engine based on the Apache mod_rewrite extension. It is designed, from the ground up to bring all the features of Apache mod_rewrite to IIS 6.0 and IIS 7.0. Managed Fusion Url Rewriter works with ASP.NET on Microsoft’s Internet Information Server (IIS) 6.0 and Mono XPS Server and is fully supported, for all languages, in IIS 7.0, including ASP.NET and PHP. Managed Fusion Url Rewriter gives you the freedom to go beyond the standard URL schemes and develop your own scheme.

But one feature that I added that is not part of the official Apache mod_rewrite documentation is the ability to add custom modules to extend the use of the URL rewriter in non-traditional ways.  One great example of this was born out of wanting to clean up the SEO mess I created in the early days of this blog.  I had to support the following different types of URL patterns:

  1. http://www.coderjournal.com/?p=23
  2. http://www.coderjournal.com/2008/03/14/some-post.html
  3. http://www.coderjournal.com/2008/03/14/some-post

to transform them in to the URL pattern that I finally settled on today:

  • http://www.coderjournal.com/2008/03/some-post

In the above list #2 and #3 were pretty easy to transform using the following rules:

RewriteRule ^(/[0-9]{4}/.*).html$    $1/ [NC,R=301]
RewriteRule ^(/[0-9]{4}/[0-9]{1,2}/)[0-9]{1,2}/(.*)$    $1$2 [R=301]

Because they contained all of the elements that make up my current URL.  As you can imagine problems arose when I had to support links that used #1′s syntax.  It contains zero elements that I can use to create my current URL.  Being a programmer who beleives that each part of a system should handle gracefully the domain it was designed to support, in this case a URL rewriter should be able to handle any senario that has to do with URL rewriting.  I added in support that allowed developers to naturally extend the URL rewriter to accomplish any type of URL rewriting task they could think of.

Setting Up the URL Rewriter Rules

In my case I needed to handle the following SQL query everytime I saw a URL that matched #1.

select concat('http://www.coderjournal.com/',year(post_date),'/',month(post_date),'/',post_name,'/') from wp_posts where ID = $1;

What this query does is query the WordPress database table that contains all the posts by the post ID and have it return the actual absolute path to the post, that should be displayed in the URL.  To do this I created a new directive for the mod_rewrite syntax called RewriteModule.  I also had to extend the RewriteRule and RewriteCond directives to support these new module extensions.  The RewriteModule, RewriteRule, and RewriteCond are defined by the following syntax:

RewriteModule <Reference Name> <Namespace>,<Assembly>
RewriteRule[([<Left Module>],[<Right Module>])] <Pattern> <Substitution>
RewriteCond[([<Left Module>],[<Right Module>])] <Test String> <Condition Pattern>

The parts in light blue parts above are optional to creating the rule.  In my case for this blog the rewriter directives looked like the following:

RewriteModule PostQueryString CoderJournal.Rewriter.Rules.PostQueryStringRuleAction, CoderJournal.Rewriter.Rules
RewriteRule(,PostQueryString)   ^/\?p=([0-9]+)$    "select guid from wp_posts where ID = $1;" [R=301]

I have highlighted in red the important parts of the syntax that indicate the custom module processor that should be used on the RewriteRule directive and how it relates back to the class defined in the RewriteModule

Creating the Module

I have to warn you that I am not going to demonstrate and show all the properties and methods on the interface that are important for creating a custom module, but I am going to show you the actual meat of the module that is involved in the lookup of the URL from the database.

public Uri Execute(int logLevel, string logCategory, HttpContext context, 
                   Pattern pattern, Uri url, string[] conditionValues, 
                   IDictionary<string, string> flags)
{
	string inputUrl = url.GetComponents(UriComponents.PathAndQuery, UriFormat.UriEscaped);
	string sqlCommand = pattern.Replace(inputUrl, Text, conditionValues);
	string substituedUrl = String.Empty;

	using (MySqlConnection connection = new MySqlConnection(Properties.Settings.Default.DatabaseConnection)) {
		using (MySqlCommand command = connection.CreateCommand()) {
			command.CommandText = sqlCommand;
			command.CommandType = CommandType.Text;

			try {
				connection.Open();
				substituedUrl = command.ExecuteScalar() as string;
			} finally {
				connection.Close();
			}
		}
	}

	return new Uri(url, substituedUrl);
}

It may not be clear right away what is going on, but on line 6, I am replacing the defined value in the regular expression (^/\?p=([0-9]+)$) with the SQL query (from above) to produce a query that will be run against the database. So if the following URL came in to my server:

It would produce a SQL query that looked like this:

select concat('http://www.coderjournal.com/',year(post_date),'/',month(post_date),'/',post_name,'/') from wp_posts where ID = 372;

Notice that the ID, 372, shows up in both the URL and the query, that is because this is the part I am most interested in, in the URL, because it is the only part of the URL that I need to query the database to find the actual path of the post.

Now that we have the query we can execute it on the database, using lines 9 through 21, and create the resulting URL on line 23. The resulting URL is then passed back through the URL rewriter, and processed using the flags defined. In my case [R=301], actually indicates that I want to do a 301 Permanent Redirect on the URL, which tells the browser and search engines, a like, that they need to update their URL for this page.

You can test out the above conditions by using the following URL’s that all redirect back to this page:

  1. http://www.coderjournal.com/?p=372
  2. http://www.coderjournal.com/2008/12/9/creating-extension-module-net-url-rewriter-reverse-proxy.html
  3. http://www.coderjournal.com/2008/12/9/creating-extension-module-net-url-rewriter-reverse-proxy/

The code as always is available on my SVN server at Google Code.

I hope this comes in handy to some of you developers that have to support legacy URL’s in your own product or a project that you are working on. As always if you have any questions or need anything clarified please feel free to contact me or leave a comment below.

08 Dec 2008

Creating a Progressive Queue in IE6 JavaScript

No Comments Uncategorized

Today IE6 really kicked my butt, I had the following code working in FireFox, IE7, etc, all except IE6:

ShowLoadingPanel();
// execute some code here
HideLoadingPanel();

The above code is really simple, it shows a full screen loading panel when I am executing some code in between the Show and Hide functions for the loading panel. It worked in every browser that I tried except for IE6. This seemed to occur because the execution of the JavaScript was happening in the same thread as the UI rendering of the screen, which would block all screen updates to the browser until the JavaScript was executed. By the time all the JavaScript is done rendering the UI is in it’s final visual state, so it looks like the loading graphic was never run. Note I don’t know for sure that this is what is really happening, but it definitely seems like the most likely possibility. But if this is true it really explains the lack of support for JavaScript Animation, which heavily relies on a progressive incremental rendering of the UI.

On my way back home from work after dealing with this for a while, a light bulb clicked on in my head and it occurred to me. That I might be able to use the setTimeout function to force the execution in to a different thread, which would free up the current thread to render the loading screen.

ShowLoadingPanel();
setTimeout(function() {
    // execute some code here
    HideLoadingPanel();
}, 500);

Basically what the above is doing is creating a slight delay in the execution of the code. Which releases the current thread to render the loading screen to the browser. After that is done the rest of the code, in the setTimeout, is executed in the background and locking up a different thread. The important thing to remember is to keep the method that hides the loading panel inside the setTimeout so that the hide panel method is executed in a different process and after the code that you want to execute.

So that is how you create a progressive queue in IE6 for JavaScript. Until next time I hope you found this useful.

07 Dec 2008

Tweaking Vista Services

No Comments Uncategorized

Recently I have been trying to squeak more speed out of my laptop by shutting down unnecessary services that seem to come with most every software and device driver.  To do this I started with a clean install of Windows Vista and installed the driver only option for my hardware and then got rid of many of the useless services that come with software like iTunes, VMware, and other stuff I find useful.

However with all that tweaking it still doesn’t create an optimal installation of Windows Vista, because Windows by default still runs a ton of unnessisary services.  For example, Microsoft thought it was nessisary to enabled Tablet PC Pen Input by default which I venture to say is useless to a vast majority of Windows Vista users.

Coincidentally while checking out the new HD feature available on YouTube I stubled across an interesting video describing a website called Speedy Vista.

Windows Vista has around 130 services. According many reports, Vista is very computer intensive, so it may take a couple tweaks to lighten it up a bit to suit your needs. The names are rather vague like ‘ReadyBoost’ and ‘SuperFetch’. How will you know which are safe to disable? Hopefully we can help. We have a full list of all Vista services and recommended settings for them. This site contains registry files for easily resetting your services settings back to factory in case you mess it up or just wish to have a good way to go back to factory.

The website offers a cheat sheet of services you can safely shut off, and if you are unsure what the service does, and don’t want to make a mistake, it has a description of what the services does.  But my favorite part of this website is that it provides a batch file and or a registry file that will shut off the unnessisary services automatically.

Windows Vista ‘Safe’ Settings reg bat – Use at your own risk. I’ve tried to weed out services that are obviously unnecessary to give you a good starting point to tweaking for your own needs/preference. Please e-mail me any problems, etc.

Windows Vista Tweaked Settings reg bat- Use at your own risk. Works for me for internet, Windows Update, DVD playing, most other things I want to do. May cause some software, etc. to stop working, but I would like to hope not. Please send me feedback on this file as well as safe if you have any issues running them. It assumes that you have your own Firewall software and Spyware software.

Windows Vista Minimal Settings reg bat – Use at your own risk. Works for me for internet, Windows Update, DVD playing, most other things I want to do. May cause some software, etc. to stop working, but I would like to hope not. Please send me feedback on this file as well as safe if you have any issues running them. It assumes that you have your own Firewall software and Spyware software.

Disclaimer: Use this site at your own risk. I am not responsible for damage to your computer, or anything else.

If none of these above options work for you, you can even use their wizard to turn on and off the services that you don’t want to use.  This allows you to create your own custom registry file to your own specifications.

I just wanted to post this interesting website since I haven’t posted in the past month, because of my book writing.  Which I am happy to say that I am starting my last chapter and I should be done by the end of the year so I can resume my normal posting schedule.