Mono bug: No overload of JavaScriptSerializer.Deserialize() takes two arguments

Mono does not support the non-generic edition of this method. Use Deserialize(String input) instead.

Advertisements

Sub domains on IIS express

In the beginning I didn’t expect this would be difficult. And perhaps it is easier to set up if you are on a domain. However, in this scenario I’m not, and I’m not sure how much easier it would be if I was.

Anyway, FiddlerCore to the rescue. The solution is not perfect. It would be perfect if IIS Express could handle this for me, but it can’t. So perfect or not, I needed three steps:

  1. Setup FiddlerCore to capture all trafic for the sub domain, e.g. traffic for test.ocase.com
  2. Setup my application to handle sub domains and handle special cases when sub domains are redirected from Fiddler Core
  3. Manipulate the hosts file on the pc to listen to the sub domain, e.g. test.ocase.com

1. Setup FiddlerCore to capture all traffic for the sub domain test.ocase.com

First, I created a new HTTP Module. In the Init section, I placed this code:


if (!Fiddler.FiddlerApplication.IsStarted())
{
Fiddler.FiddlerCoreStartupFlags oFCSF = Fiddler.FiddlerCoreStartupFlags.Default;
Fiddler.FiddlerApplication.BeforeRequest += new Fiddler.SessionStateHandler(FiddlerApplication_BeforeRequest);

Fiddler.FiddlerApplication.Startup(m_Proxy.ListenToHttpPort, oFCSF);
}

Also, I inherited from IDisposable, and in the Dispose() method, I shut down Fiddler:


Boolean disposed;

public void Dispose()
{
if (!disposed)
{
if (Fiddler.FiddlerApplication.IsStarted())
Fiddler.FiddlerApplication.Shutdown();
disposed = true;
}
}

Finally, in the FiddlerApplication_BeforeRequest method, I placed this:

void FiddlerApplication_BeforeRequest(Fiddler.Session oSession)
{
if (oSession.host.ToLower().Contains("test.ocase.com"))
{
oSession.oRequest.headers.Add("proxyhost", oSession.host);
oSession.host = "http://ocase.com:8080";
}
}

This is where the magic happens. Here, I add a new HTTP header to the request with the key “proxyhost”. Whenever my sub domain is requested (contained in the oSession.host property), I handle the request.

I have one small obstacle, however. The HTTP 1.1 protocol specifies that the HTTP request should have a host header. The host header will contain the url including the sub domain. This means that when my browser requests test.ocase.com, it specifies this both in the URL and in the host header. So when fiddler redirects my request to “http://ocase.com:8080” (by specifying this in the oSession.host property), it will specify the url “http://ocase.com:8080” AND the host header “http://ocase.com:8080”. So, basically my sub domain is lost in the redirection process! To prevent this, I create a new HTTP header “proxyhost”, and here I set the value of the original host: “http://test.ocase.com” which includes the sub domain. I need to handle this in my application.

2. Setup my application to handle sub domains including sub domains with proxyhost
Basically, when I check out the sub domain in my application, I will first check to see if proxyhost is available. If it is, then I will use the sub domain specified in proxyhost. This will be the case when the sub domain is redirected through FiddlerCore. If it isn’t, no proxy is being used, thus no proxyhost is availble in the HTTP headers, and I’ll use the sub domain from the URL.

3. Manipulate the hosts file
The hosts file can be found here: %systemroot%\system32\drivers\etc\hosts. For example it could look like this:


# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host

# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost

127.0.0.1 ocase.com
127.0.0.1 test.ocase.com
127.0.0.1 test1.ocase.com
127.0.0.1 test2.ocase.com
127.0.0.1 ocase.ocase.com
127.0.0.1 test.localhost

I have added some sub domains for testing purposes, and I redirect all of them to the ip address of my localhost. This ensures that they are ultimately handled by my app. If I only change the host file and start my app, IIS Express will return an HTTP 400 Bad Request. But with FiddlerCore, the requests are captured at a lower level, and I can parse them correctly.

As with any other HTTP Module I can enable and disbale it in the web.config of my application. The examples posted in this blog post are really simple, but I ended up making it more generic obviously, and I also added more functionality such as logging and url redirection which I needed at a later stage in the development.

FiddlerCore – Fiddler.dll in your projects

I first heard of FiddlerCore on the Hearding Code pod cast recently. Now, you probably know Fiddler – at least for me, this is one of my favorite (free) web debugging tools on Windows. Baiscally, Fiddler captures all http traffic and lets you play around with it, step into the requests, tamper them etc. Cool stuff!

FiddlerCore is a (managed/.NET) dll that you can download and install along with a sample project from here: http://www.fiddler2.com/Fiddler/Core/. Actually, according to The Fiddler Father, Eric Lawrence, on the PodCast, this is not just the dll’s at the bottom of Fiddler, but a special compilation of the Fiddler dll’s that he maintains just for guys like us – thank you, Eric!

So far, my uses of this has been:

  • As a simple proxy redirecting http traffic to other ports
  • As a handler of sub domains on IIS Espress
  • As an http logging module
And I’ve only known about it for a month or so… It seems this was really something that I needed!
So far it works like a charm! I don’t know if it is stabile and fast enough to work in a prouction environment. For now I just use it for development purposes only, but for this it is certainly great.
I’ll write a separate blog post with some code on how to handle e.g. sub domains on IIS Express with Fiddler Core.

Convert NewLine \n to \r\n in large text files

I’m currently doing a lot of coding on both Linux and Windows, and this small tool has been helpful to me. It’s not complicated, it just converts all \n line ending’s to \r\n in a textfile. It performs okay, even when the files get above 1 GB it performs within a few minutes.

Specifically, I couldn’t make bcp work with CSV files with \n endings (it seems that bcp automatically adds a \r when \n is specified as the row delimiter, thank you bcp…).

If you have the same need, feel free to download from http://www.box.net/shared/x0tfvdlgy5.

Usage:
n2rn /? to display usage
n2rn filename will convert a file and output it as filename.n2rn
nr2n filename -o outputfilename will convert a file and output it as outputfilename
n2rn filename -o outputfilename -w will convert a file, output it as outputfilename and overwrite any existing outputfilename.

Binary representation and dynamic Flags

Flags are great for representing multiple information in a single digit.

For example, I could define the numbers 1, 2, 4, and 8 to have the semantics:

1: Coffee
2: Sugar
4: Milk
8: Icecream

Here the number 5 would represent Coffe and Milk. The number 8 would represent Coffe and Icecream. Now, the beauty of this is that using base 2 digits Coffee would look like this:

1

If I added sugar, it would now look like this:

11

And if I added Icecream it would now look like this:

1011

I.e. reading from the right to the left: Coffe, Sugar, No milk, but including Icecream.

In other words – it will be very performant to determine from an integer, even if the integer is a bit large, which integer flags it is based on. In order to do this in .NET, however, one has to be able to (1) convert from decimal to base 2 binary, and (2) to convert the single bits to integers.

Two short extension methods are helpful here:


public static class Int32Extensions
{
  public static Boolean[] ToBinary(this Int32 dec)
    {
      List lst = new List();
      while (dec > 0)
      {
        lst.Add(dec % 2 != 0);
        dec /= 2;
      }
    return lst.ToArray();
  }

  public static List GetFlagValues(this Int32 flagRepresentation)
  {
    List lst = new List();
    Boolean[] bs = flagRepresentation.ToBinary();
    for (Int32 i = 0; i < bs.Length; i++)
      if (bs[i]) lst.Add((Int32)Math.Pow(2, i));
    return lst;
  }
}

Now, the implementation can be used like this:


Int32 i = 1000000000;
Boolean[] s = i.ToBinary();
foreach (Int32 f in i.GetFlagValues())
{
  Console.WriteLine(f);
}

In the console this will displayed as:

Obviously, since the value doubles for each additional flag, there is a natural limit to the possible number of flags. However, this limit can be extended by using a datatype like BigInteger in C# 4.0 (remember to reference System.Numerics).

With 10.000 different flags, my pc will calculate each flag within 156 milliseconds. However, please be aware that calculations on such large numbers can be a thread to scalability. 20.000 different flags still perform within half a second (on my pc), but 100.000 flags will perform within 13 seconds – with noticeable impact on other processes on the pc.

Rocking with Python

Python and Ruby have been so hyped in the .NET community for the last couple of months so I decided to attempt to solve this problem using IronPython – originally inspired by a Linq solution posted by Octavio Hernandes.

Find a number consisting of 9 digits in which each of the digits from 1 to 9 appears only once. This number must also satisfy these divisibility requirements:

1.The number should be divisible by 9.
2.If the rightmost digit is removed, the remaining number should be divisible by 8.
3.If the rightmost digit of the new number is removed, the remaining number should be divisible by 7.
4.And so on, until there’s only one digit (which will necessarily be divisible by 1).

I was amazed when I first saw how easy Linq could solve this problem. But … then I found Python. Now – check this out:

import clr
clr.AddReferenceByPartialName("IronPython.Modules")
import IronPython.Modules


def validate(n, denominator):
     if denominator == 1: return True
     if int(n) % denominator != 0: return False
     return validate(n[:len(n)-1], denominator - 1)


for x in IronPython.Modules.PythonIterTools.permutations("123456789"):
     joined = ''.join(x)
     if validate(joined, 9): print(joined)

10 lines – slam dunk! Since this is my first Python code lines ever, I apologize to the Python purists in advance – but hey, I just had to advocate this 🙂 And yes, it does perform.

For a comparison between Linq and C++ solutions please check out my earlier block post on this. Remember to go though the comments where an F# solution is also suggested.

Hard or soft tabs in code?

As the code project states – wars have been fought over lessser topics:

Please note the surprisingly low amount of “No preference”s 😀

Not to mention the 8 % which actually have consistent rules fordifferentiating  hard/soft tabs between files 😀 😀