Skip to content

Instantly share code, notes, and snippets.

@DavidAnson
Created May 2, 2014 04:12
Show Gist options
  • Save DavidAnson/4422305569c4c9f02340 to your computer and use it in GitHub Desktop.
Save DavidAnson/4422305569c4c9f02340 to your computer and use it in GitHub Desktop.
An IHttpModule that rewrites all URLs to /app.js (making the original URL available to the Node.js application).
// Copyright (c) 2014 by David Anson, http://dlaa.me/
// Released under the MIT license, http://opensource.org/licenses/MIT
using System;
using System.Text.RegularExpressions;
using System.Web;
/// <summary>
/// An IHttpModule that rewrites all URLs to /app.js (making the original URL available to the Node.js application).
/// </summary>
/// <remarks>
/// For use with Node.js applications running under iisnode on IIS as an alternative to installing the URL Rewrite module.
/// </remarks>
public class IisNodeUrlRewriter : IHttpModule
{
/// <summary>
/// Unique lookup key for the HttpContext.Items dictionary.
/// </summary>
private const string ItemsKey_PathAndQuery = "__IisNodeUrlRewriter_PathAndQuery__";
/// <summary>
/// Regex matching paths that should not be rewritten.
/// </summary>
/// <remarks>
/// Specifically: /iisnode(/...) /app.js/debug(/...) and /app.js.debug(/...)
/// </remarks>
private Regex _noRewritePaths = new Regex(@"^/(iisnode|app\.js[/\.]debug)(/.*)?$", RegexOptions.IgnoreCase);
/// <summary>
/// Initializes a module and prepares it to handle requests.
/// </summary>
/// <param name="context">An HttpApplication that provides access to the methods, properties, and events common to all application objects within an ASP.NET application.</param>
public void Init(HttpApplication context)
{
context.BeginRequest += HandleBeginRequest;
context.PreRequestHandlerExecute += HandlePreRequestHandlerExecute;
}
/// <summary>
/// Occurs as the first event in the HTTP pipeline chain of execution when ASP.NET responds to a request.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">An System.EventArgs that contains no event data.</param>
private void HandleBeginRequest(object sender, EventArgs e)
{
// Get context
var application = (HttpApplication)sender;
var context = application.Context;
// Rewrite all paths *except* those to the default log directory or those used for debugging
if (!_noRewritePaths.IsMatch(context.Request.Path))
{
// Save original path
context.Items[ItemsKey_PathAndQuery] = context.Request.Url.PathAndQuery;
// Rewrite path
context.RewritePath("/app.js");
}
}
/// <summary>
/// Occurs just before ASP.NET starts executing an event handler (for example, a page or an XML Web service).
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">An System.EventArgs that contains no event data.</param>
private void HandlePreRequestHandlerExecute(object sender, EventArgs e)
{
// Get context
var application = (HttpApplication)sender;
var context = application.Context;
// Restore original path (if present)
var originalPath = context.Items[ItemsKey_PathAndQuery] as string;
if (null != originalPath)
{
context.RewritePath(originalPath);
}
}
/// <summary>
/// Disposes of the resources (other than memory) used by the module that implements IHttpModule.
/// </summary>
public void Dispose()
{
// Nothing to do here; implementation of this method is required by IHttpModule
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment