Jim Rogers

Lives in Baton Rouge, LA, with two dogs, one cat, and one lovely wife. I'm a lead developer for GCR Incorporated.

Katrin and Jim

Month List

Redirecting to posts in BlogEngine

by Jim Feb 15, 2010 9:38 PM

Since I’ve moved my blog from Blogger to BlogEngine, the URLs have of course changed. The old posts were .html files, so my asp.net application doesn’t process them, the and resulting 404 error falls to Brinkster (my host) to handle.

I can configure my Brinkster account to load BlogEngine’s Error404.aspx page in these cases, and the following code will successfully redirect those requests to the new BlogEngine post.

Blogger and BlogEngine both follow the convention of naming a post with the title, replacing spaces with commas.

// Jim: check for brinkster 404 redirect query string
// The query string is not name=value, it's this:
// 404;http://www.jimandkatrin.com:80/CodeBlog/2009/12/some-thoughts-on-code-reviews.html

string reg = @"^404;http://www.jimandkatrin.com:80/CodeBlog/[0-9]{4}/[0-9]{2}/(.*)\.html$";
string qs = Server.UrlDecode(Request.QueryString.ToString()); // raw query string
if (!string.IsNullOrEmpty(qs) &&
  System.Text.RegularExpressions.Regex.IsMatch(qs, reg,
    System.Text.RegularExpressions.RegexOptions.IgnoreCase))
{
  // Get the match, and construct a new URL out of it, in the BlogEngine format:
  System.Text.RegularExpressions.Match match =
  System.Text.RegularExpressions.Regex.Match(qs, reg);
  string url = string.Format("~/post/{0}.aspx", match.Groups[1]);

  Response.Redirect(url);
  Response.End();
  return;
}

// Jim: Set response code so that redirects from Brinkster 404 will actually 
// return the response code 404
Response.StatusCode = 404;
Response.Status = "404 Not Found";

The status code at the end causes the page to actually return a 404 status code, rather than a 200 OK, when Brinkster redirects to this page.

Parsing Email Dates

by Jim Nov 11, 2007 12:38 PM

I'm using Peter Huber's Pop3MimeClient code to check emails, and needed a way to parse the date string from the email header into a .NET DateTime object. The date format is defined in the email standard RFC822.

This is the code I wound up with:

// Date string in the header looks like this:
// Sun, 23 Sep 2007 09:24:59 -0500 (CDT)

// Remove the parenthetical reference to the time zone; this is redundant since
// we have the offset and time zone can't be represented in the format string
string sentString = Headers["Date"];
sentString = System.Text.RegularExpressions.Regex.Replace(sentString,
  "\\([a-zA-Z]*\\)", "");

return DateTime.ParseExact(sentString.Trim(), new string[] {
  "ddd, dd MMM yyyy HH:mm:ss zzzzz",
  "ddd, d MMM yyyy HH:mm:ss zzzzz"},
  System.Globalization.CultureInfo.InvariantCulture,
  System.Globalization.DateTimeStyles.AllowWhiteSpaces);

I used a regex to remove the (CDT) from the end of the string because there doesn't appear to be a wildcard character available for the DateTime format specifier. The two format strings are necessary because "dd" doesn't accept days less than ten.

Tags:

Code

Javascript Query String Parse

by Jim Jan 30, 2007 2:38 PM

This shouldn't be so hard to google. To parse an html page's query string with Javascript, using a regular expression:

// Get the value of one of the arguments in the query string
function getFieldValue(name)
{
    var match = (new RegExp('[?&;]' + name + '=([^&;#]*)')).exec(location.search);
    return match ? unescape(match[1]) : "";
}

Returns an empty string if the name doesn't match.

Parsing bookmarks and images

by Jim Dec 13, 2005 9:54 AM

I uploaded my Firefox bookmarks.html file to the website, so that I could access it from anywhere; I have about a million of them, and it's useful to bring them up at work. The file isn't very user-friendly in its raw form.

So I decided to parse the file, extract the icons from it (they're in there) and add some script to expand and collapse the folders. I could get a little practice with regular expressions along the way.

// Rip the icon data out and replace with images
// I match to the end of the anchor tag so I can use the length
// to put the image at the end of the tag.
Regex regex = new Regex("ICON=\"data:image/x-icon;base64,(.*?)\".*?\\>");
i = 0;
icon = 1;
Match match = regex.Match(file, i);
while (match != null && match.Length != 0)
{
  Debug.WriteLine(match.Value);
  iconname = ExtractIcon(match.Groups[1].Value, dest, icon);
  file = file.Insert(match.Index + match.Length, string.Format(
    "<img src=\"{0}\" border=\"0\" width=16 height=16>&nbsp;", iconname));
  icon++;
  i = match.Index + match.Length;
  match = null;
  if (i < file.Length)
  {
    match = regex.Match(file, i);
  }
}

The image is in the .ico format, so it can be streamed right into a file with that extension:

private static string ExtractIcon(string data, string folder, int index)
{
  int i;
  string filename;
  // Open a binary file for writing
  filename = string.Format("bookmark_icon_{0}.ico", index);
  BinaryWriter bw = new BinaryWriter(
    File.Open(Path.Combine(folder, filename), FileMode.Create));
  bw.Write(Convert.FromBase64String(data));
  bw.Flush();
  bw.Close();
  return filename;
}

Piece of cake. The javascript for showing and hiding the DIVs is pretty straightforward; a little more regex does the trick. Full code for console application here.

In Firefox, image tags can contain data in base64 format. This isn't supported in IE6, but I've tested it in IE7 beta, and that does support it. Unfortunately, I won't have that at work until it's officially released.

Tags: ,