aha! regex'ed my solution

2007-12-10 @ 00:12#

been buged by a problem this weekend and finally came up with a reasonable solution. my problem was how to properly split up the HTTP Auth Digest string.

i *was* using a simple Split method based on the command (","). this works fine as long as none of the items themselves have one or more commas embedded in them. for example, if the uri has a comma, then the whole thing falls apart [sigh]. so i needed to avoid this problem. after quite a bit of research, i can see that this is a common problem (with lots of *wrong*) solutions. i *did* come up with a solution that works in this one case, but it's not perfect.

instead of a simple Split(), i now use a regular expression that counts on the fact that the digest string really has a delimiter of, not just a comma, but a comma+space. so my regex looks like this:

// csv elements of the string
//string[] elems = authHeader.Split(new char[] { ',' }); // broken
string[] elems = Regex.Split(authHeader, "(, )", RegexOptions.IgnoreCase); // the solution

again, this is not perfect, but it works for this one case. i tested it with IE, FF, and Safari. i'll continue to look for a better solution, but this will work for now.

update

yeah well...

this solution causes MISE to fail to authenticate. the reason? MSIE does not send the digest string with comma+space as the delimiter - just comma. but all is not completely lost. because MSIE also does not send the query string as part of the digest header, just the complete scheme/host/path. so that means commas in the uri will not be a problem for MSIE. so my code now looks like this:

// csv elements of the string
string[] elems = new string[0];
if (authHeader.IndexOf(", ") != -1)
{
    elems = Regex.Split(authHeader, "(, )", RegexOptions.IgnoreCase);
}
else
{
    elems = authHeader.Split(new char[] { ',' });
}

again, this works in this case. i'll keep looking for a better solution, tho.

code